$(document).ready(function() {

	// main nav
	function clearTypeIEFix() {
        if ( $.browser.msie )
            this.style.removeAttribute('filter');
    }

	var AllowTopNavDropDownClose = true;

	$(".TopNav a").click(function() { 
		$(".TopNavDropDown .SuperSection").hide();
		$(".TopNavDropDown ." + $(this).attr("class")).fadeIn(500,clearTypeIEFix);
		AllowTopNavDropDownClose = false;
		setTimeout(function() { AllowTopNavDropDownClose = true; }, 500);
	});
	
	$(document).bind("keyup click", function() {
		if( AllowTopNavDropDownClose )
			$(".TopNavDropDown .SuperSection").fadeOut(500,clearTypeIEFix);
	});
	
	//replace the title h2 with an img
	$(".TopNavDropDown h2").each(function() { 
		$(this).replaceWith('<img src="/_/images/megadrop-' + $(this).html().toLowerCase().replace(" ","-") + '.gif" alt="' + $(this).html() + '" title="" />');
	});
	
	//replace the text of link to an img
	$(".TopNavDropDown .Events a").each(function() {
		$(this).html('<img src="/_/images/megadrop-' + $(this).html().toLowerCase().replace(" ","-") + '.jpg" alt="' + $(this).html() + '" title="" />');
	});
						   
	// highlight box
	$(".Highlight span.Title")
		.each(function() {
			var Highlight = $(this).parent();
			var H2 = $(this).detach();
			$(Highlight).wrapInner('<div class="Padding"></div>');
			Highlight.prepend(H2);
		})
		.after('<span class="RightArrow"></span>');
	$(".Highlight")
		.wrapInner('<div class="InnerBG"></div>')
		.append('<div class="BottomPadding"></div>');

	// faded images
	var FadedOpacity = 0.5;
	var FullOpacity = 1;
	var Duration = 250;
	$("img.Faded")
		.fadeTo(0,FadedOpacity)
		.mouseenter(function() {
			$(this).fadeTo(Duration,FullOpacity);					 
		})
		.mouseleave(function() { 
			$(this).fadeTo(Duration,FadedOpacity);					 
		});
	
	//* image captions
	$("img + .ImageCaption").not(".RightSideBar .ImageCaption").each(function() { 
		var Img = $(this).prev();
		var OuterWrapper = document.createElement("div");
		var InnerWrapper = document.createElement("div");
		$(InnerWrapper)
			.css("width", Img.width() + "px")
			.addClass(Img.attr("className"));
		Img
			.removeAttr("class")
			.wrap(InnerWrapper);
		InnerWrapper = Img.parent();
		InnerWrapper
		    .addClass("InnerWrapper")
			.append(this);
		$(OuterWrapper)
			.addClass("ImageCaptionWrapper");
			
		InnerWrapper.wrap(OuterWrapper);
	});
	//*/
	
	// external links
	$("a").not(".MainColumn a,.RightSideBar a").each(function() {
		if( $(this).attr("href") == null || $("img",this).size() > 0 ) return;
		if( $(this).attr("href").substr(0,7) == "http://" || $(this).attr("href").substr(0,8) == "https://" )
			$(this).addClass("External");
	});
	
	//$(".MainColumn a,.RightSideBar a").removeClass("External");
	
	// photo flipper
	$(".PhotoFlipper")
		.each(function() {
			var PhotoFlipper = this;
			var ImageList = new Array();
			var KeyImage = $("img",this).first();
			
			var Index = 0;
			$("li",this).each(function() {
				$(this).css({ "z-index": (Index == 0 ? 3 : 1) });
				ImageList.push(this);
				++Index;
			});

			$(PhotoFlipper)
				.css({
					height: KeyImage.height() + 10 + "px",
					width: KeyImage.width() + 5 + "px"
				})			
				.click(nextImage)
				.mouseenter(function() {
					stopAutoPlay();
				})
				.mouseleave(function() {
					startAutoPlay()
				})
				.append('<li class="Foreground"></li>')
				.append('<li class="Background"></li>');
			$(".Foreground, .Background", PhotoFlipper)
				.css({
					height: KeyImage.height() - 5 + "px",
					width: KeyImage.width() - 5 + "px"
				});
				
			var TOID = null;
			function startAutoPlay() {
				if( TOID ) clearInterval(TOID);
				TOID = setInterval(nextImage,2500);
			}
			
			function stopAutoPlay() {
				if( TOID ) clearInterval(TOID);
			}
			
			startAutoPlay();
				
			Index = 0;
			function nextImage() {
				var NextIndex = (Index + 1) % ImageList.length;
				$(ImageList[Index]).css({ "z-index": 3 });
				$(ImageList[NextIndex])
					.css({ "z-index": 2 })
					.fadeTo(0,1.0);
				$(ImageList[Index]).fadeTo(1000,0,function() { $(this).css({ "z-index" : 1 }); });
				Index = NextIndex;
			}
		})
	
	// Shrinkwrap
	setupShrinkwrap();
	
});

function setupShrinkwrap() {
	var JumpTo = $.deparam.querystring().jumpto;
		
	var First = true;
	$(".Shrinkwrap").each(function() {
		var Wrap = $(this);
		var WrapHeight = Wrap.outerHeight()
		var H2 = $(this).children("h2");
		
		// don't process wraps that have already been processed (allows this function to be called repeatedly)
		if( H2.hasClass("Open") || H2.hasClass("Closed") ) return;
		
		if( ! $(Wrap).hasClass("ClosedByDefault") && ( First || $(Wrap).hasClass("OpenByDefault") || JumpTo && Wrap.attr("id") == JumpTo ) ) {
			H2.addClass("Open");
		} else {
			H2.addClass("Closed");
			if( JumpTo )
				Wrap.css({ height: H2.outerHeight() + "px" });
			else
				Wrap.animate({ height: H2.outerHeight() + "px" }, 1000);
		}
			
		H2.click(function() { 
			if( Wrap.outerHeight() == WrapHeight ) {
				if( $.browser.msie ) 
					$(".PhotoFlipper",Wrap).hide();
				H2.removeClass("Open");
				H2.addClass("Closed");
				Wrap.animate({ height: H2.outerHeight() + "px" });
			} else {
				if( $.browser.msie ) 
					$(".PhotoFlipper",Wrap).show();
				H2.addClass("Open");
				H2.removeClass("Closed");
				Wrap.animate({ height: WrapHeight + "px" });
			}
		});
		
		First = false;
	});
	
	if( JumpTo ) 
		$('html,body').animate({scrollTop: $("#" + JumpTo).offset().top}, 1000);

}
