$(document).ready(function(){

	// Fix strong inside p tag
	$("p>strong").addClass("noleftmargin");

	// Handle the hover click
	$("a.hover").click(ShowHoverContent);

	// Style column links with arrows
	$(".block li a").append("&nbsp;<span class='suffix'>>>></span>");

	//Bios
	var pBiosArray = $("body.bios #main p[className!='admin']");
	if (pBiosArray.length > 2) {
		AddShowFullProfileFeature(pBiosArray);
	}
	$('h4#readfull a').click(function(){
		$('address').show();
		$('#therest').show();
		$('h4#readfull').hide();
		return false;
	});
	
	// Subtabs like home page
	$("ul.subtabs a").click(function(){
		$(this).parent().siblings().removeClass('selected');
		$(this).parent().addClass('selected');
		return false;
	 });
	$("ul.subtabs li a").click(function(){
	var tabNo = $(this).parent().attr('id').charAt(6);
	$("div[id^='tabpanel']").hide();
	$('#tabpanel' + tabNo).show();
	return false;
	});
	
	// Light up entries in news area on hover
	$("body.press div.item").hover(function() {
		$(this).addClass('entryhover');},
		function() {$(this).removeClass('entryhover');
	});

	// Go to the item in question
	$("body.press div.item").click(function() {
		OpenLinkOnClick($(this).find("a").attr("href"));
	});
	
	// Light up entries in customers section on hover
	$("body.customers div.item").hover(function() {
		$(this).addClass('entryhover');},
		function() {$(this).removeClass('entryhover');
	});

	// Go to the item in question
	$("body.customers div.item").click(function() {
		OpenLinkOnClick($(this).find("a").attr("href"));
	});
	
	// Light up entries in search results on hover
	$("body.searchresults tr:not(:first)").hover(function() {
		$(this).addClass('entryhover');},
		function() {$(this).removeClass('entryhover');
	});

	// Go to the item in question
	$("body.searchresults tr:not(:first)").click(function() {
		OpenLinkOnClick($(this).find("a").attr("href"));
	})
	// Massage the jobs section
	$(".jobsblock h4 a").each(function(){
		var jobsText = $(this).html();
		var commaLoc=jobsText.indexOf(", ");
		if (commaLoc!=-1) {
			var prefText=jobsText.substr(0,commaLoc);
			var postText=jobsText.substr(commaLoc+1,jobsText.length);
			$(this).html(prefText + "<em>" + postText + "</em>");
		}
	});
	// Create collapsing blocks
	if (window.location.href.indexOf("#")!=-1) {
		SetupSectionsForCompressedH3();
	} else {
		SetupBlocksForCompressedH3();
	}
	
	// Remove ID/ from a tags to keep all pages at root
	RemoveIDfromHREFs();
	
	// If flash is there, load flash
	if($("#flashcontent").get(0)!=null) {
		LoadFlash();
	}

});

function AddShowFullProfileFeature(pBiosArray) {
	$("div.adr").hide();
	pBiosArray.each(function(index){
		if (index==1) {
			$(this).after("<h4 id='readfull'><a href='' onclick='ShowFullProfile();return false;'>show full profile...</a></h4>");
		} else if (index > 1) {
			$(this).hide();
		}
	});
}

function LoadFlash() {
	var so = new SWFObject("Files/SAA_Flash8.swf/$file/SAA_Flash8.swf", "mymovie", "707", "350", "8", "#ffffff");
	so.write("flashcontent");
}	

function RemoveIDfromHREFs() {
	$("a[href*='ID']").each(function(){
		var link=$(this).attr("href");
		var prefix="http://www.seacappartners.com/";
		prefix="http://" + window.location.hostname + "/";
		$(this).attr("href",prefix + link.substr(3,link.length));
	});
}

function SetupBlocksForCompressedH3() {
	var sectionCount = 0;
	$("#main h3.compressed").each(function(){
		sectionCount++;
		var summary = $(this);
		var nextTag = $(this).next();
		var firstSentence = "";
		while (nextTag[0]) {
			if (!nextTag.hasClass("admin") && !nextTag.hasClass("compressed")) {
				if (firstSentence == "") {
					var spanSentence = nextTag.find("span").text();
					var periodLoc = nextTag.text().indexOf(".");
					if (spanSentence != "") {
						firstSentence = spanSentence;
					} else if (periodLoc == -1) {
						firstSentence = nextTag.text();
					} else {
						firstSentence = nextTag.text().substr(0,periodLoc + 1);
					}
					var link="<a href='" + window.location.pathname + "#section" + sectionCount + "' class='more'><img src='Files/btn_more_36x10.gif/$file/btn_more_36x10.gif' width='36' height='10' alt='more' border='0'/></a>";	
					$(this).after("<p class='intro'>" + firstSentence + "&nbsp;" + link + "<p>");
					summary = summary.add($(this).next());
				}
				nextTag.hide();
				summary = summary.add(nextTag);
			}
			nextTag = nextTag.next();
		}
		summary.wrapAll("<div class='abstractblock'></div>");
		nextTag = "";
	});
	SetupEventForBlocksToSections();
}

function SetupSectionsForCompressedH3() {
	var sectionCount = 0;
	var topLink="<a href=\'" + window.location.pathname + "#top'><img src='Files/btn_top_65x10.jpg/$file/btn_top_65x10.jpg' width='65' height='10' alt='top' border='0'/></a>";
	$("#main h3.compressed").each(function(){
		sectionCount++;
		var link="<a name='section" + sectionCount + "' title='section" + sectionCount + "'></a>"
		var nextTag = $(this).next();
		$(this).replaceWith("<h3 class='section' " + link + $(this).text() + "</h3>");
		while (nextTag[0]) {
			if (nextTag.hasClass("admin") || nextTag.hasClass("compressed")) {
				var lastPara = nextTag.prev();
				break;
			} else {
				var lastPara = nextTag;
				nextTag = nextTag.next();
			}
		}
		if (lastPara.tagName == "P") {
			lastPara.replaceWith("<p>" + lastPara.text() + topLink + "</p>");
		} else {
			lastPara.after("<p>" + topLink + "</p>");
		}
	});
}

function SetupEventForBlocksToSections() {
	$("a.more").click(function(){
		MoreButtonClickHandler();
	});
}

function MoreButtonClickHandler() {
	var sectionCount = 0;
	var topLink="<a href=\'" + window.location.pathname + "#top'><img src='Files/btn_top_65x10.jpg/$file/btn_top_65x10.jpg' width='65' height='10' alt='top' border='0'/></a>";
	$("p.intro").remove();
	$("div.abstractblock").each(function(){$(this).replaceWith($(this).html())});
	$("#main h3.compressed").each(function(){
		sectionCount++;
		var link="<a name='section" + sectionCount + "' title='section" + sectionCount + "'></a>"
		var nextTag = $(this).next();
		$(this).replaceWith("<h3 class='section' " + link + $(this).text() + "</h3>");
		while (nextTag[0]) {
			nextTag.show();
			if (nextTag.hasClass("admin") || nextTag.hasClass("compressed")) {
				var lastPara = nextTag.prev();
				break;
			} else {
				var lastPara = nextTag;
				nextTag = nextTag.next();
			}
		}
		if (lastPara.tagName == "P") {
			lastPara.replaceWith("<p>" + lastPara.text() + topLink + "</p>");
		} else {
			lastPara.after("<p>" + topLink + "</p>");
		}
	});
}

function OpenLinkOnClick(selectedHREF) {
	//var baseHREF=$("base").attr("href");
	//window.location.href=baseHREF + selectedHREF;
	window.location.href=selectedHREF;
}

function ShowHoverContent() {
	$("#hoveroverlay").removeClass("hidden");
	$("#hovercontent").removeClass("hidden");
	$("#hoveroverlay").click(HideHoverContent);
	SetHoverPosition();
	return false;
}

function HideHoverContent() {
	$("#hoveroverlay").addClass("hidden");
	$("#hovercontent").addClass("hidden");
}

function SetHoverPosition() {
var pageSize = GetPageSize();
$("#hovercontent").css({left: parseInt((pageSize[0] -707) / 2,10) + 'px'});
$("#hovercontent").css({top: parseInt((pageSize[1] - 300) / 2,10) + 'px'});
}

function GetPageSize(){
  var de = document.documentElement;
  var w = window.innerWidth || self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
  var h = window.innerHeight || self.innerHeight || (de&&de.clientHeight) || document.body.clientHeight;
  arrayPageSize = [w,h];
  return arrayPageSize;
}

function ShowFullProfile() {
	$("div.adr").show();
	$("h4#readfull").hide();
	$("#main p").slideDown("normal");
}

$.fn.unwrap = function(expr) {
	return this.each(function(){
		$(this).parents(expr).eq(0).after(this).remove();
	});
};



