jQuery(document).ready(
	function () {
		// Icon prepending
		var pdfLinks = $("#page_content a[href$='.pdf']");
		var jpgLinks = $("#page_content a[href$='.jpg']");
		var exeLinks = $("#page_content a[href$='.exe']");
		var iconPrepend = function (target, filetype) {if (!($(target).parents(".noicon").length)) {$(target).addClass(filetype);}}
		jpgLinks.each(function(){iconPrepend(this, "img");});
		pdfLinks.each(function(){iconPrepend(this, "pdf");});
		exeLinks.each(function(){iconPrepend(this, "exe");});

		// Open PDF's in a popup
		pdfLinks.click(function(e){window.open(this.href, "", "resizable=yes,location=no,menubar=no,scrollbars=no,status=no,toolbar=no,fullscreen=no,dependent=no,width=700,height=700,left=10,top=10");e.preventDefault();});
		
		// Make alternating table rows
		$("#page_content table tr:odd").addClass("alt");
		
		// IE gives script elements dimensions, which can mess up the document flow...
		if (jQuery.browser.msie) {$("script").css({display: "none"});}
		
		// BEGIN FAUX LIGHTBOX
			var flashVideos = $("* [src='/content/media-center/player.swf']");
			// closing function
			endItAll = function () {
				$("#popup").fadeOut("250",
									function() {
										$("#popup").remove();
										$("#sheet").fadeOut("100",
															function() {
																$("#sheet").remove();
																if (flashVideos){flashVideos.css({"visibility":"visible"});}
															});
									});
			}
			// launching function
			$("#page_content a[rel='popup']").click(function (e) {
				if ($("#popup").length != 0) {endItAll;}
				var popup = $('<div id="popup" class="clearfix">');
				var i = new Image();
				var caption = $('<div id="caption">')
					.css({
							bottom: "10px",
							left: 0,
							background: "#fff",
							color: "#000",
							textAlign: "center",
							fontWeight: "bold",
							fontSize: "120%",
							padding: "10px 0 10px"
						 })
					.html($(this).text());
				var closeButton = $('<div id="close">')
					.css({
							position: "absolute",
							width: "auto",
							height: "auto",
							textAlign: "right",
							paddingRight: "20px",
							fontWeight: "bold",
							bottom: 0,
							right: 0,
							margin: "0 12px 12px 0",
							width: "100px",
							height: "30px",
							background: "transparent url(/elements/images/messaging/closeButton.gif) right center no-repeat",
							display: "none",
							zIndex: "9999999"
						});
				var sheet = $('<div id="sheet">')
					.css({
							position: "absolute",
							top: 0,
							left: 0,
							width: $(document).width(),
							height: $(document).height(),
							background: "#000",
							opacity: "0.75",
							display: "none",
							zIndex: "99999"
						 });
				popup.append(i).append(caption).append(closeButton);
				$(i).css({display: "block", margin: "0 auto 5px"}).attr("src", $(this).attr("href"))
					.load(
						  function () {
							var viewportOffset = window.pageYOffset || document.documentElement && document.documentElement.scrollTop || document.body.scrollTop;
							var viewportHeight = $(document).height() - viewportOffset;
							var topOffset = viewportOffset + (viewportHeight/2);
							popup.css({
										  padding: "10px",
										  background: "#000",
										  position: "absolute",
										  top: topOffset,
										  left: "50%",
										  // the margin top should be half the height of the entire popup, but we don't know the height
										  // until it is added to the body, which we haven't done yet. So we need the height of the image
										  // plus the image margin, plus the popup padding, plus the height of the caption, plus the caption
										  // padding. We know everything but the height of the caption (line-height), so we guestimate 15px.
										  marginTop: -((i.height + 60)/2),
										  marginLeft: -((i.width + 10)/2),
										  display: "none",
										  zIndex: "999999",
										  width: i.width
									  });
							$("body").append(sheet);
							$("body").append(popup);
							// hide flash video (actually, just set the visibility, so the page retains it's layout. display: none will remove it from the flow)
							if (flashVideos) {flashVideos.css({"visibility":"hidden"});}
							sheet.fadeIn("100", function() {popup.fadeIn("250");});
						  }
						 );
				popup.hover(
								function () {$(this).css({cursor: "pointer"}); closeButton.fadeIn("200");},
								function () {$(this).css({cursor: "default"}); closeButton.fadeOut("200");}
							);
				sheet.hover(
								function () {$(this).css({cursor: "wait"});},
								function () {$(this).css({cursor: "default"});}
							);
				$("*").click(endItAll);
				e.preventDefault();
			});
		// END FAUX LIGHTBOX
	
	
	$("#primaryNavigation li").hover(
		function() {
			$(this).addClass("subhover");
		}, function(){
			$(this).removeClass("subhover");
		}
	);
});
