// Protocol check to include the secure or non-secure brightcove script
var securePg = false,
	protocol = "http:",
	adminProtocol = "admin";
if(window.location.protocol === "https:"){
	securePg = true;
	protocol = "https:";
	adminProtocol = "s"+adminProtocol;
}
$.getScript(protocol+"//"+adminProtocol+".brightcove.com/js/BrightcoveExperiences_all.js");

willow.fixPodiumNav();

// Brightcove API stuff, needs to be declared at top of javascript
var bcExp, modVP;
function onTemplateLoaded(experienceID) {// called when template loads, this function stores a reference to the player and modules.
	bcExp = brightcove.getExperience(experienceID);
	modVP = bcExp.getModule(APIModules.VIDEO_PLAYER);
}

willow.ready(function($) {
	$("#Form1").bulletin({data:[{type:"announcement",id:4437}]});// EM bulletin
	$('#search').pdSearch({showButtonClass:'searchBtn', showButtonText:''}); // Podium Search 
	if($(".annualFund").length > 0){$(".annualFund").annualfund("",{bar_height:182});}// Annual fund
	
	willow.getMenu("156247|156248|156249|156250|156251",function(data){// Menu data 
		$('#L1').menu(data.menu,{showL3s:false, currentL1:pdGlobal.currentPages[0].id});
		
		var $sitemap = $(".sitemap");
		if($sitemap.length > 0){
			$sitemap.sitemap(data);
			//$("#expand").click(); // uncomment this if you want to expand all by default 
		}
		// Adding Footer sitemap via jQuery Template
		$("#footNavTmpl").tmpl(data.menu).appendTo("#footerNav");
	});
	
	if(pdGlobal.currentPages[pdGlobal.currentPages.length-1].id === 156239){ // HP specific styles
		$("body").addClass("homepage");
		
		willow.getPhotos(48670,function(data){ // Self managed photos
			// default photo path var so only one template can be used
			var dPath = "//cdn.media56.whipplehill.net/ftpimages/163/podium/style646/images/defaultImgHP.jpg";
			if(typeof data.photo.images !== "undefined"){
				if(data.photo.images[0].path.length > 60){
					$("#bannerImgTmpl").tmpl(data.photo.images).appendTo("#hpmPhotos");
					$("#hpmPhotos").cycle();
					return;
				}
			}
			$("#bannerImgTmpl").tmpl({path:dPath}).appendTo("#hpmPhotos");
		});
		
		$.getJSON('/data/json.asp?type=video&id=48669',function(data){ // Load video JSON data, print to page
			var $v = $("#hpmVideosTmpl").tmpl(data,{
				isSecure : function(){return securePg;}
			}); // Build out template HTML
			
			$("#hpmVideos").html($v).wrap(function(){ // Add to page then do a callback for added elements
				brightcove.createExperiences();
				$("#videoList .mediaTitle").click(function(e){ //On click of video title set on state and show proper video
					e.preventDefault();
					$("#videoList .mediaTitle").removeClass("medOn");
					$(this).addClass("medOn");
					modVP.loadVideo($(this).attr("vid"), "referenceId", "full"); // Gets video reference ID from attribue off of a tag and call brightcove loadVideo API function
				});
			});
		});
		
		willow.getNews(15616,function(data){  // Self managed news
			var $n = $("#hpmNewsTmpl").tmpl(data);// Build out template HTML
			$("#hpmNews").html($n).wrap(function(){// Add to page then do a callback for added elements
				var $newsTitles = $("#newsList .mediaTitle");
				$newsTitles.click(function(e){ //On click of video title set on state and show proper video
					e.preventDefault();
					$newsTitles.removeClass("medOn");
					$(this).addClass("medOn");
					for(var i = 0; i < $newsTitles.length; i++){
						if($newsTitles.eq(i).attr("nid") === $(this).attr("nid")){
							$("#currentStory").html($("#newsStoryTmpl").tmpl(data.news[i]));
						}
					}
				});
			});
		});
		
		willow.getLinks(19601,function(data){$("#linksTmpl").tmpl(data.link).appendTo("#hpmSocialMedia");}); // Self managed social media
		
		$(".mediaTab").click(function(e){ // Toggle which media section is showing when click media tabs
			var $mediaSections = $("#hpmContainer").children();
			e.preventDefault();
			modVP.stop(); // Brightcove API call to stop video playback when switching tabs
			for(var i = 0; i < $mediaSections.length; i++){
				var $curSection = $mediaSections.eq(i);
				if($(this).attr("elshow") === $curSection.attr("id")){
					$(".mediaTab").removeClass("on");
					$(this).addClass("on");
					$mediaSections.removeClass("topMedia");
					$curSection.addClass("topMedia");
				}
			}
		});
	}
	
	$("#sitemapBtn").click(function(e){
		e.preventDefault();
		var $this = $(this);
		$this.toggleClass("open");
		$this.hasClass("open") ? $this.html("Close Sitemap") : $this.html("Open Sitemap");
		// Scroll to bottom of window after open sitemap
		$("#footerNav").slideToggle(1000,function(){
			window.scrollTo(0, document.body.scrollHeight);
		});
	});
	
	willow.getPhotos(49686,function(data){ // Self managed left column image
		if(typeof data.photo.images !== "undefined"){
			if(data.photo.images.length > 0){
				$("#leftColImg").attr("src", data.photo.images[0].path);
			}else{
				$("#leftColImg").attr("src","//cdn.media56.whipplehill.net/ftpimages/163/podium/style646/images/leftColDefaultImg.png");
			}
		}
	});
});


