$(document).ready(function(){

	// Handle the hover click
	$("a.hover").click(ShowHoverContent);
	
	// Remove ID/ from a tags to keep all pages at root
	//RemoveIDfromHREFs();

	// Style column links with arrows
	$(".block li a").append("&nbsp;<span class='suffix'>>>></span>");
	$(".special li span").remove();
	$(".profileslist li span").remove();

	//Bios
	var pBiosArray = $("body.bios #main p[className!='admin']");
	if (pBiosArray.length > 2) {
		AddShowFullProfileFeature(pBiosArray);
	}
	var pBiosArray = $("body.consultingbios #main p[className!='admin']");
	if (pBiosArray.length > 2) {
		AddShowFullProfileFeature(pBiosArray);
	}
	
	// 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 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();
	}
	
	// Make <hr> into thin gray line
	$("hr").wrap("<div class='hr'></div>")
	
	// Add class to images on right or left
	AddClassToImages();
	
	// If flash is there, load flash
	if($("#flashcontent").get(0)!=null) {
		LoadFlash();
	}
	
	if($("#flashcontentlogin").get(0)!=null) {
		LoadFlashLogin();
	}
	
	if($("#flashcontentconsult").get(0)!=null) {
		LoadFlashConsult();
	}
	
});

function RemoveIDfromHREFs() {
	$("a[href*='ID']").each(function(){
		var link=$(this).attr("href");
		var origText = $(this).text();
		var prefix="http://" + window.location.hostname + "/";
		$(this).attr("href",prefix + link.substr(3,link.length));
		if (origText != "") {
			$(this).text(origText);
		}
	});
}

function AddClassToImages() {
	$("img[align='right']").addClass("right");
	$("img[align='left']").addClass("left");
}

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 LoadFlashConsult() {
	var so = new SWFObject("Files/consulting.swf/$file/consulting.swf", "mymovie", "170", "100", "8", "#ffffff");
	so.write("flashcontentconsult");
}

function LoadFlashLogin() {
	var so = new SWFObject("Files/log-in2.swf/$file/log-in2.swf", "mymovie", "170", "80", "8", "#ffffff");
	so.write("flashcontentlogin");
}

function OpenLinkOnClick(selectedHREF) {
	//var baseHREF=$("base").attr("href");
	//window.location.href=baseHREF + selectedHREF;
	window.location.href=selectedHREF;
}

function SetupEventForBlocksToSections() {
	$("a.more").click(function(){
		MoreButtonClickHandler();
	});
}

function SetupBlocksForCompressedH3() {
	var sectionCount = 0;
	$("#main h3.compressed").each(function(){
		sectionCount++;
		var summary = $(this);
		var nextTag = $(this).next();
		var firstSentence = "";
		while (nextTag[0] && nextTag[0].tagName == "P") {
			if (!nextTag.hasClass("admin")) {nextTag.hide();}
			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());
			}
			if (!nextTag.hasClass("admin")) {summary = summary.add(nextTag);}
			nextTag = nextTag.next();
		}
		summary.wrapAll("<div class='abstractblock'></div>");
	});
	ClearEmptyParas();
	SetupEventForBlocksToSections();
}

function ClearEmptyParas() {
	$("p").each(function(){
		if ($(this).text() == "") {
			$(this).remove();
		}
	});
}

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] && nextTag[0].tagName == "P") {
			if (!nextTag.hasClass("admin")) {var lastPara = nextTag;}
			nextTag = nextTag.next();
			if (!nextTag[0]) {
				lastPara.replaceWith("<p>" + lastPara.text() + topLink + "</p>");
			} else if (nextTag[0].tagName!="P") {
				lastPara.replaceWith("<p>" + lastPara.text() + topLink + "</p>");
			}
		}
	});
}

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 p").show();
	$("div.abstractblock").each(function(){$(this).replaceWith($(this).html())});
	$("#main h3.compressed").each(function(){
		var nextTag = $(this).next();
		while (nextTag[0] && nextTag[0].tagName == "P") {
			if (!nextTag.hasClass("admin")) {var lastPara = nextTag;}
			nextTag = nextTag.next();
			if (!nextTag[0]) {
				lastPara.replaceWith("<p>" + lastPara.text() + topLink + "</p>");
			} else if (nextTag[0].tagName!="P") {
				lastPara.replaceWith("<p>" + lastPara.text() + topLink + "</p>");
			}
		}	
		sectionCount++;
		var link="<a name='section" + sectionCount + "' title='section" + sectionCount + "'></a>"
		$(this).replaceWith("<h3 class='section' " + link + $(this).text() + "</h3>");	
	});
}

function ShowFullProfile() {
	$("div.adr").show();
	$("h4#readfull").hide();
	$("#main p").slideDown("normal");
}

function ShowHoverContent() {
	//LoadFlash();
	$("#hoveroverlay").removeClass("hidden");
	$("#hovercontent").removeClass("hidden");
	$("#hoveroverlay").click(HideHoverContent);
	SetHoverPosition();
	if($("#flashcontentconsult").get(0)!=null) {
		$("#flashcontentconsult").hide();
	}
	return false;
}

function HideHoverContent() {
	$("#hoveroverlay").addClass("hidden");
	$("#hovercontent").addClass("hidden");
	if($("#flashcontentconsult").get(0)!=null) {
		$("#flashcontentconsult").show();
	}
}

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;
}


