$(document).ready(function(){

	// Remove ID/ from a tags to keep all pages at root
	RemoveIDfromHREFs();

	// Make <hr> into thin gray line
	$("hr").wrap("<div class='hr'></div>")
	
	// Handle the hover click
	$("a.hover").click(ShowHoverContent);
	
	// Add quotes to the blockquote
	$("blockquote").prepend("<span>\"</span>");
	$("blockquote").append("<span>\"</span>");

	// Style column links with arrows
	$(".block li a").append("&nbsp;<span class='suffix'>>>></span>");

	//Bios
	var pBiosArray = $("body.profile #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 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();
	}
	
	// Border around maps
	$("body.customers img[src$='map.jpg']").addClass("border");
	
	// Submit validation
	$("form").submit(function(){
		var shallsubmit=true;
		$("input[class='required']").each(function(){
			if($(this).val()=="") {
				alert($(this).attr("title") + " is required.");
				$(this).focus();
				shallsubmit=false;
				return false;
			}
		})
		// compare email
		if ($("input[name='Email']").val()!=null && $("input[name='EmailConfirm']").val()!=null) {
			if ($("input[name='Email']").val() != $("input[name='EmailConfirm']").val()) {
				alert("Your email addresses do not match. Please correct and retry.");
				shallsubmit=false;
			}
		}
		if (!shallsubmit) {return false;}
	});
	
	// If flash is there, load flash
	if($("a.hover").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 AddToCart(cookieName,cookieValue) {
	var currCookie = $.cookie(cookieName);
	alert($.cookie(cookieName));
	if (currCookie == null) {
		$.cookie(cookieName, cookieValue, { expires: 10 });
	} else {
		$.cookie(cookieName, currCookie + "," + cookieValue, { expires: 10 });
	}
}

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.seaburyapg.com/";
		$(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] && 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>");
	});
	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] && 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 ShowFullProfile() {
	$("div.adr").show();
	$("h4#readfull").hide();
	$("#main p").slideDown("normal");
}

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 OpenLinkOnClick(selectedHREF) {
	//var baseHREF=$("base").attr("href");
	//window.location.href=baseHREF + selectedHREF;
	window.location.href=selectedHREF;
}

function SetupEventForBlocksToSections() {
	$("a.more").click(function(){
		MoreButtonClickHandler();
	});
}

function ShowHoverContent() {
	//LoadFlash();
	$("#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;
}

$.fn.unwrap = function(expr) {
	return this.each(function(){
		$(this).parents(expr).eq(0).after(this).remove();
	});
};



