willow.fixPodiumNav();

willow.ready(function($) {
	// Podium Search 
	$('#search').pdSearch({showButton:true, showButtonClass:'searchBtn', showButtonText:''}); 
	$(".searchBtn").css("margin","0");
	
	// Menu data 
	var menuOptions = {showL3s:false, currentL1:pdGlobal.currentPages[0].id, speed: 0};
	willow.getMenu("127960|127961|127962|127963|127964",function(data){
		$('#L1').menu(data.menu,menuOptions);
		
		var $sitemap = $(".sitemap");
		if($sitemap.length > 0){
			$sitemap.sitemap(data);
			//uncomment this if you want to expand all by default $("#expand").click();
		}
	});
	
	if($(".s2g").length < 1){$(".unftr").css("background-image","url(/ftpimages/533/podium/style552/images/leftColFooter.png)");}
	
	// Emergency Bulletin
	willow.getNews("12912",function(data){$('#Form1').bulletin(data,{emButton: false});},{"backlink":window.location}); 
	
	// Adds faded line to right of page title
	$(".ptl1, .ptl2, .ptl3").wrapInner("<span class='ptSpan'></span>");
	$("<span class='ptFade'></span>").appendTo(".ptl1, .ptl2, .ptl3");// Insert some mockup
	for(var i = 0; i < $(".ptSpan").length; i++){
		var $obj = $(".ptSpan").eq(i),
			$p = $obj.parent(),
			w = $p.width() - $obj.width() - 6; // Get width of text to calculate with to the right
		$(".ptFade").eq(i).css("width",w+"px");
	}
	
	// jQuery template self managed links
	willow.getLinks(16183,function(data){
		var links = data.link;
		if(links.length === 0){return;}
		$("#linksTmpl").tmpl(links,{
			target : willow.checkTarget,
			img : willow.checkImg
		}).appendTo("#socialMedia");
	});
	
	// jQuery template self managed links
	willow.getLinks(16181,function(data){
		var links = data.link,
			cnt = -1;
		if(links.length === 0){return;}
		$("#footLinksTmpl").tmpl(links,{
			target : willow.checkTarget,
			count : function(){
					cnt++;
					if(cnt === 0){return false;}
					else{return true;}
				}
		}).appendTo("#footOmni");
	});
	
	// Annual fund
	if($(".annualFund").length > 0){$(".annualFund").annualfund();}
	
	// If on homepage
	if(pdGlobal.currentPages[pdGlobal.currentPages.length-1].id === 129589){
		$("body").addClass("homepage");
		
		willow.getNewsMedia(12920, function(data){
			if(data.news.length === 0){return;}
			var t = 0;
			$("#hpThumbTmpl").tmpl(data.news).appendTo("#hpThumbCenter").click(function(e){
				e.preventDefault(); // Prevents default action on thumb <a> tags
				$thumbs.removeClass("onThumb"); // On click move on class to clicked thumb
				$(this).addClass("onThumb");
				
				for(var i = 0; i < data.news.length; i++){ 
					if(data.news[i].id == $(this).attr("nid")){
						willow.hpMedia(data.news[i]); // Shows proper media depending on what is in the new story
					}
				}
			}).wrap(function(){ // Initial position of all thumbs
				$(this).css("left",((t*128))+"px");
				t = t+1;
			});
			
			var $thumbs = $(".hpThumb");
			willow.hpMedia(data.news[0]); // Displays first news media
			$thumbs.eq(0).addClass("onThumb");
			
			if($thumbs.length > 5){ // If more then 5 thumbs show move arrows and initiate scroll functionality
				willow.imgScroll($thumbs);
				$("#hpThumbPrev, #hpThumbNext").addClass("plus5");
			}
		});
	}
	
	//Hide page photo on news detail page.
	if(pdGlobal.currentPages[pdGlobal.currentPages.length-1].id === 204){
		$("#lNavPhoto").css("display","none");
	}

	if($.browser.msie && $(".cal-filter-drop").length > 0){
		$("#footerContainer").css("position","static");
	}
});


// Checks to see if self managed link has the 'open in new tab' flag set 
willow.checkTarget = function(t){
	if(t === 1 || t === true || t === "true"){
		return " target='_blank'";
	}
	return "";
};

// Checks to see if self managed link has an image
willow.checkImg = function(lk){
	if(typeof lk.data.image !== "undefined"){
		if(lk.data.image.height > 0 && lk.data.image.width > 0){
			return "<img src='"+lk.data.image.path+"' height='"+lk.data.image.height+"' width='"+lk.data.image.width+"' alt='"+lk.data.title+"' border='0'/>"
		}else{
			return "<img src='"+lk.data.image.path+"' alt='"+lk.data.title+"' border='0'/>"
		}
	}
	return lk.data.title;
}

willow.hpMedia = function(story){
	var $hpVid = $("#hpVideo"),
		$hpGal = $("#hpGallery"),
		autoplay = false;
	
	if(story.author === "autoplay"){autoplay = true;} // if author field is set to 'autoplay', it autoplays the video on the HP
	if(story.images.length > 0){$("#hpPhoto").attr("src",story.images[0].zoom.path);} // Adds background photo

	if(parseInt(story.hasVideo) > 0){ // If story has video
		$hpVid.html("").removeClass("hideEl");
		$hpGal.html("").addClass("hideEl");
		$("#hpVideoTmpl").tmpl(story.media[0],{
			ap : autoplay
		}).appendTo("#hpVideo").wrap(function(){
			brightcove.createExperiences();
			return;
		});
	}
	else if(parseInt(story.hasPhoto) > 0){ // If story has image gallery
		$hpVid.html("").addClass("hideEl");
		$hpGal.html("").removeClass("hideEl");
		$("#hpGalTmpl").tmpl(story.media[0]).appendTo("#hpGallery");
	}
	else{ // If no video or image gallery
		$hpVid.html("").addClass("hideEl");
		$hpGal.html("").addClass("hideEl");
	}
}

willow.imgScroll = function($img){
	var endPos = ($img.length-1) * 128;
		
	$img.eq($img.length-1).addClass("lastThumb");
	
	$("#hpThumbNext").click(function(){ 
		if($img.queue()[0] !== "inprogress"){ // Prevents animation from happening if animation from already running
			for(var i = 0; i < $img.length; i++){
				var p = $img.eq(i).position().left;
				$img.eq(i).animate({"left":p-128},function(){ // right to left animation
					($(this).position().left == -128) ? $(this).css("left",(endPos)+"px").addClass("lastThumb") : $(this).removeClass("lastThumb");
				});
			}
		}
	});

	$("#hpThumbPrev").click(function(){
		if($img.queue()[0] !== "inprogress"){ // Prevents animation from happening if animation from already running
			for(var j = 0; j < $img.length; j++){
				if($img.eq(j).hasClass("lastThumb")){
					$img.eq(j).css("left","-128px"); // Puts last thumb in the left most position for left to right animation
					
					for(var i = 0; i < $img.length; i++){ // Loops through thumbs to animate them
						var p = $img.eq(i).position().left;
						$img.eq(i).animate({"left":p+128},function(){
							($(this).position().left == endPos) ? $(this).addClass("lastThumb") : $(this).removeClass("lastThumb"); // Keeps track of last thumb
						});
					}
					
					return; // Breaks out of first loop
				}
			}
		}
	});
}
