willow.fixPodiumNav();

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");

var bcArray = new Array();
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);
	bcArray.push(modVP);
}

switch(pdGlobal.currentPages[0].id){
	case 136951: $(".ptl1, .ptl2, .ptl3").css("color","#591f67"); break; //About Us
	case 136952: $(".ptl1, .ptl2, .ptl3").css("color","#d64709"); break; //Edge
	case 136954: $(".ptl1, .ptl2, .ptl3").css("color","#4d7620"); break; //Admissions
	case 136953: $(".ptl1, .ptl2, .ptl3").css("color","#0056b2"); break; //French American PPK-8
	case 136955: $(".ptl1, .ptl2, .ptl3").css("color","#8f172c"); break; //International 9-12
	case 136956: $(".ptl1, .ptl2, .ptl3").css("color","#176c8c"); break; //Athletics
	case 136957: $(".ptl1, .ptl2, .ptl3").css("color","#cf700b"); break; //Support the School
	default:; break;
}

willow.ready(function($) { 
	$('#search').pdSearch({showButton:true, showButtonClass:'searchBtn', showButtonText:''}); // Podium Search
	$(".searchBtn").css("margin","0");
	
	willow.getMenu("136951|136952|136954|136953|136955|136956|136957",function(data){ // Menu data
		var $sitemap = $(".sitemap");
		if($sitemap.length > 0){
			$sitemap.sitemap(data);
			//uncomment this if you want to expand all by default $("#expand").click();
		}
		// Adding Left Column Menu via jQuery Template
		$("#lc_navTmpl").tmpl(data.menu,{
			len : willow.len
		}).prependTo("#leftColMenu");

		willow.menuActions(); // After menu completely built out add events 
		(pdGlobal.currentPages[pdGlobal.currentPages.length-1].type === "portal") ? willow.openMenu("portal") : willow.openMenu(pdGlobal.currentPages[0].id); // Open current L1 menu
	});
	
	$("#Form1").bulletin({data:[{type:"news",id:12595}]});// EM bulletin
	
	willow.getLinks(14814,function(data){ // Self managed social media link
		var links = data.link;
		if(links.length === 0){return;}
		$("#linksTmpl").tmpl(links,{
			target : willow.checkTarget,
			img : willow.checkImg
		}).appendTo("#socialMedia");
	});
	
	
	
	
	
	if(pdGlobal.currentPages[pdGlobal.currentPages.length-1].id === 138601){ // Homepage only styles
		$("body").addClass("homepage");
		
		willow.getPhotos(24939,function(data){ // All photos from first album only
			// default photo path var so only one template can be used
			var dPath = "https://cdn.media910.whipplehill.net/ftpimages/369/podium/style795/images/defaultQuote.png";
			if(typeof data.photo.images !== "undefined"){
				if(data.photo.images[0].path.length > 60){
					var $p = $("#bannerImgTmpl").tmpl(data.photo.images); // Builds out image HTML
					$("#hpQuote").html($p).cycle({timeout:3000});	// Adds HTML to page and calls cycle plugin
					return;
				}
			}
			$("#bannerImgTmpl").tmpl({path:dPath}).appendTo("#hpQuote");
		});
		
		willow.getNewsMedia(12594,function(data){// Banner Gallery
			var $g = $("#hpGalTmpl").tmpl(data,{
				isSecure : function(){return securePg},
				len : willow.len
				}); // Build out images/thumbs
			$("#hpGallery").html($g); // add to page
			$(".thumbTitle").fsplit(45);
			willow.imgScroll($(".galItem")); // Build out scroll/thumb actions
			brightcove.createExperiences();
		},{numrows:4});
	}
	
	
	
	
	
	if(pdGlobal.loginBoxMode === "Hidden"){ // Hide the top/bottom of the user nav if the login box is hidden
		$("#lNavTop, #lNavSep").addClass("hideEl");
	};
	
	$("#GL_un_-1").mouseover(function(){ // Move usernav flyout left 7 px
		var p = $("TABLE.u2g").parent();
		var l = p.css('left');
		var nl = parseInt(l) - parseInt($('#Form1').offset().left);
		nl = nl - 7;
		p.css('left',nl);
	});
	
	if($(".annualFund").length > 0){$(".annualFund").annualfund();} // Annual Fund
	
	// Hacks the podium button to change the Read More/Close text to french
	// Basically takes the function string of the on click and edits the html text passed into that function 
	$(".cchtxt-14084 div[onclick*='ShowHide2']").attr("onclick",function(index,attr){
		$(this).find(".cchbtnt-14084").html("Lire la suite");
		var string1 = $(this).attr("onclick").toString();
		var string2 = jQuery.trim(string1.substring(string1.indexOf("{",0)+1, string1.indexOf("}",0)));

		if($.browser.mozilla){ // Firefox only, finds the space between the Read and the More, FF uses \xA0 while other browsers don't
			var sp = string2.substring(58,62);
			string2 = string2 = string2.replace("Read"+sp+"More","Lire la suite");
		}else{ // Chrome/Safari/IE
			var sp = String.fromCharCode(160)
			string2 = string2.replace("Read"+sp+"More","Lire la suite");
		}

		string2 = string2.replace("Close","Fermer");

		$(this).attr("onclick","");
		$(this).click(function(){
			eval(string2);
		});
	});
	if(pdGlobal.currentPages[pdGlobal.currentPages.length-1].id === 140820){$("#L1_portal").addClass("on");}
});

// 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;
}

// jQuery Template helper function to determine lenth of the object passed
willow.len = function($obj){
	return $obj.length;
}

// Creates menu actions
willow.menuActions = function(){
	var $allL1 = $(".L1_a"),
		$allL2 = $(".L2");
		
	$allL1.click(function(e){ // On click of +/- open/close the menu depending on the state it's in
		var $this = $(this),
			$L2 = $this.siblings(".L2");
			
		if($this.attr("id") === "L1_portal"){return;} // If user clicked login link exit function
		
		e.preventDefault();
		if($this.hasClass("on")){ // Close menu if it was the one clicked on and it's open
			$this.removeClass("on");
			$L2.slideUp(1200);
			return;
		}
		
		$allL1.removeClass("on"); // Close all menus
		$allL2.slideUp(1200);
		
		if(!$this.hasClass("on")){ // Open menu if closed
			$this.addClass("on");
			$L2.slideDown(1200)
		}
	});
	
	$(".hasL3").hover(
		function(){ // On hover of L2 with L3 menu stop animation of current menu and animate it out all the way
			$(this).addClass("openFlyout").children(".L3_container").find(".L3").queue("fx", []).stop().animate({"left": 0});
		},
		function(){
			willow.closeMenus(); // Off hover close all flyouts
		}
	);
	
	var $L2s = $(".L2_a");
	var $L3s = $(".L3_a");

	for(var i = 0; i < $L2s.length; i++){ // Adds on state font color to proper L2 item
		var L2id = $L2s.eq(i).attr("id").split("_")[1];
		if(pdGlobal.currentPages.length > 1){
			if((L2id == pdGlobal.currentPages[1].id)){
				$L2s.eq(i).addClass("on");
			}
		}
	}
	for(var i = 0; i < $L3s.length; i++){ // Adds on state to proper L3 item
		var L3id = $L3s.eq(i).attr("id").split("_")[1];
		if(pdGlobal.currentPages.length > 2){
			if((L3id == pdGlobal.currentPages[2].id)){
				$L3s.eq(i).addClass("onL3");
			}
		}
	}
}

willow.closeMenus = function(){ // Close all menus, stops flyouts mid animation and animates all them back in
	$(".L3").queue("fx", []).stop().animate({"left": "-202"},function(){
		$(this).parents(".hasL3").removeClass("openFlyout");
	}).queue().dequeue();
}

willow.openMenu = function(id){ // Automatically opens proper menu when under an L1
	var $L1s = $(".L1_a");
	for(var i = 0; i < $L1s.length; i++){
		if($L1s.eq(i).attr("id").split("_")[1] == id){
			var $curL1 = $L1s.eq(i);
			$curL1.addClass("on").siblings(".L2").slideDown(1200);
			$curL1.siblings(".expander").addClass("on");
		}
	}
}

// Controler for homepage image scroll
willow.imgScroll = function($img){
	var w = 804,
		len = $img.length-1,
		endPos = len * w,
		$popUps = $(".thumbPopup"),
		$allThumbs = $(".singleThumb");
		
	for(var i = 0; i < $img.length; i++){ // Positions images/videos
		$img.eq(i).css("left",((i*w))+"px");
	}
	
	$popUps.eq(0).addClass("showPopup"); // Show popup of first item
	
	$("#galNextArrow").click(function(e){
		var nid = 0;

		e.preventDefault();
		if($img.queue()[0] !== "inprogress"){ // Prevents animation from happening if animation from already running
			for(var i = 0; i < $img.length; i++){
				var $cImg = $img.eq(i);
				var	p = $cImg.position().left;
					
				$cImg.animate({"left":p-w},function(){ // right to left animation
					if($(this).position().left === -w){
						$(this).css("left",(endPos)+"px"); // Sets the position of the image just slided out to the end position
					}
				});
				if(p === 804){
					nid = $cImg.attr("nid");// Finds the ID of the next image to show proper popup
				}else{
					willow.pauseAllVideos(i);
				}
			}
			willow.showPopup(nid, $popUps);
		}
	});

	$("#galBackArrow").click(function(e){
		var nid = 0;

		e.preventDefault();
		if($img.queue()[0] !== "inprogress"){ // Prevents animation from happening if animation from already running
			for(var i = 0; i < $img.length; i++){
				var $cImg = $img.eq(i);
				
				if($cImg.position().left === endPos){
					$cImg.css("left","-"+w+"px"); // Puts last image in the left most position for left to right animation
					nid = $cImg.attr("nid"); // Grabs ID of the image to show for popup
					
					for(var j = 0; j < $img.length; j++){ // Loops through images to animate them
						var p = $img.eq(j).position().left;
						$img.eq(j).animate({"left":p+w});
					}
					willow.showPopup(nid, $popUps);
				}else{
					willow.pauseAllVideos(i);
				}
			}
		}
	});

	$allThumbs.click(function(){ // Adds click actions to thumbs
		if($img.queue()[0] !== "inprogress"){
			var $this = $(this);
			var nid = $this.attr("nid");
				
			if($this.children(".thumbPopup").hasClass("showPopup")){return;}
			willow.showPopup(nid,$popUps);
			willow.animateImg(nid, endPos, w);
			for(var i = 0; i < $img.length; i++){
				var $item = $img.eq(i);
				willow.pauseAllVideos();
			}
		}
	});
}

willow.pauseAllVideos = function(){
	for(var i = 0; i < bcArray.length; i++){
		bcArray[i].pause();
	}
}

// Loops through the thumbs and shows the popup based on the ID passed through
willow.showPopup = function(id, $pops){
	$pops.removeClass("showPopup");
	for(var i = 0; i < $pops.length; i++){
		var $curThm = $pops.eq(i);
		if(parseInt($curThm.attr("nid")) === parseInt(id)){
			$curThm.addClass("showPopup");
		}
	}
}

// Animates the images over the proper number of times depending on the thumb clicked on
willow.animateImg = function(clickedID, ePos, width){
	var $images = $(".galItem");
	
	if($images.queue()[0] !== "inprogress"){
		var $img1, $img2, $img3, $img4;
		for(var i = 0; i < $images.length; i++){
			if($images.eq(i).position().left === 0){
				$img1 = $images.eq(i);
			}else if($images.eq(i).position().left === 804){
				$img2 = $images.eq(i);
			}else if($images.eq(i).position().left === 1608){
				$img3 = $images.eq(i);
			}
			else{$img4 = $images.eq(i);}
		}
		var p1 = $img1.position().left,
			id1 = $img1.attr("nid"),
			p2 = $img2.position().left,
			id2 = $img2.attr("nid");

		$img1.animate({"left":p1-width},function(){ // right to left animation of showing image
			$(this).css("left",(ePos)+"px");
		});
		
		$img2.animate({"left":p2-width},function(){ // right to left animation of image to show
			if(clickedID === id2){
				if(typeof $img3 != "undefined"){$img3.css("left","804px");}
				if(typeof $img4 != "undefined"){$img4.css("left","1608px");}
				return;
			}
			else{
				if(typeof $img3 != "undefined"){$img3.css("left","804px");}
				if(typeof $img4 != "undefined"){$img4.css("left","1608px");}
				willow.animateImg(clickedID, ePos, width);
			}
		});
	}
}
