willow.fixPodiumNav();

switch(pdGlobal.currentPages[0].id){
	case 134993: bannerPhotoID = 43941; break; //About KES
	case 134994: bannerPhotoID = 43942; break; //Admissions
	case 134995: bannerPhotoID = 43943; break; //School Programmes
	case 134996: bannerPhotoID = 43951; break; //School Life
	case 134997: bannerPhotoID = 43945; break; //Alumni
	case 136205: bannerPhotoID = 43946; break; //Giving
	case 134998: bannerPhotoID = 43944; break; //In The Community
	case 148000: bannerPhotoID = 43952; break; //My KES
	default: bannerPhotoID = 43952; break; //My KES group is the default group
}

willow.ready(function($) {
	// Podium Search 
	$('#search').pdSearch({showButton:true, showButtonClass:'searchButton', showButtonText:''}); 
	$(".searchButton").css("margin","0");
	
	// Menu data 
	var menuOptions = {direction:"down",showL3s:false,currentL1:pdGlobal.currentPages[0].id};
	willow.getMenu("134993|134994|134995|134996|134997|136205|134998|148000",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();
		}
	});
	
	// Emergency Bulletin 
	var emOptions = {emButtonPosition: "right",emButton: false};
	willow.getNews("12729",function(data){$('#Form1').bulletin(data,emOptions);},{"backlink":window.location});
		
	// Shift the usernav and schoolnav flyouts to the left or right 
	$("#GL_un_-1").mouseover(function(){
		var p = $("TABLE.u2g").parent();
		var l = p.css('left');
		var nl = parseInt(l) - parseInt($('#Form1').offset().left);
		nl = nl - 14;
		p.css('left',nl);
	});
	$("#GL_c5i0_sn_-1").mouseover(function(){
		var p = $("TABLE.s3g").parent();
		var l = p.css('left');
		var nl = parseInt(l) - parseInt($('#Form1').offset().left);
		nl = nl - 14;
		p.css('left',nl);
	});
		
	// Pagebuilder homepage specifics
	if(pdGlobal.currentPages[pdGlobal.currentPages.length-1].id === 134992){
		$("#lOutTbl").addClass("homepage");
		$("#lInTblRCell").css("padding","0");
		$("#banner").addClass("homepage");
		$("#footer").addClass("homepage");
		
		// Flash
        var hasflash = willow.hasFlash();
		if (hasflash == 1){
			flashembed("bannerPhotos", {src: "/flash/kingsedgehill_home2.swf",wmode:"transparent"});
		}
		else{
			$("#bannerPhotos").append("<img id='hpBannerPhotos' src='https://cdn.media34.whipplehill.net/ftpimages/459/podium/Style629/images/nfDefault.jpg' height='385' width='495' border='0' />");
		}
		//willow.getAllPhotos(43940, willow.homepagePhotos);
	}
	else{
		// Banner photos accept on homepage
		willow.getAllPhotos(bannerPhotoID, willow.bannerPhotos);
		
		// Self managed social media in footer, accept on homepage
		willow.smButtons($("#socialMedia"),17778);
	}
	
	willow.pageTitles($('.ptl1'),'L1_pageTitle.swf','800','35','FDB313');
	willow.pageTitles($('.ptl2'),'L2_pageTitle.swf','800','45','000000');
	//willow.pageTitles($('.ptl3'),'L3_pageTitle.swf','800','45','000000');
	
});	

willow.pageTitles = function($obj,swf,thisWidth,thisHeight,thisColor){
	if($obj.length > 0){
		var titleString = $obj.html();
		titleString = titleString.replace('&','!!');
		titleString = titleString.replace("'",'@@');
		titleString = titleString.replace('"','##');
		$obj.flashembed(
		{src:'/flash/'+swf, wmode:'transparent', width:thisWidth, height:thisHeight, allowscriptaccess:'always', id:$obj.attr('class')+'_flash'},
		{Title:titleString,Container:$obj.attr('class'),htmlWidth:$obj.width(),htmlHeight:$obj.height(),TitleColor:thisColor}
		);
	}
}

function flashTitleResize(TextHeight, TextWidth, Container){
	$('.'+Container).height(TextHeight+'px');
	$('.'+Container).width(TextWidth+'px');
	$('.'+Container+' object').attr('wmode','transparent');
}



willow.bannerPhotos = function(data){
	// Check to see if there is an album in the group, if there is not show default photo 
	if(typeof data.photos === "undefined"){
		$('#bannerPhotos').css("background-image","url(https://cdn.media34.whipplehill.net/ftpimages/459/podium/Style629/images/defaultBannerPhoto.jpg)");
	}
	else{
		// Grab a random index from the number of photos 
		var photoNum = willow.GetRandomNumberBetween(0,(data.photos[0].images.length-1));
		
		// Check to see if there are photos added to the album if not show default photo
		if(data.photos[0].images[photoNum].path.length > 22){
			$('#bannerPhotos').css("background-image","url(https://cdn.media34.whipplehill.net"+data.photos[0].images[photoNum].path+")");
		}
		else{
			$('#bannerPhotos').css("background-image","url(https://cdn.media34.whipplehill.net/ftpimages/459/podium/Style629/images/defaultBannerPhoto.jpg)");
		}
	}
}

willow.homepagePhotos = function(data){
	$("#bannerPhotos").append("<div id='hpBannerPhotos'></div>");
	// Check to see if there is an album in the group, if there is not show default photo 
	if(typeof data.photos === "undefined"){
		$('#hpBannerPhotos').css("background","url(/ftpimages/459/podium/Style629/images/homepageNF.jpg) no-repeat");
	}
	else{
		// Check to see if there are photos added to the album if not show default photo
		if(data.photos[0].images[0].path.length < 22){
			$('#hpBannerPhotos').css("background","url(/ftpimages/459/podium/Style629/images/homepageNF.jpg) no-repeat");
		}
		else{
			for(var i = 0; i < data.photos.length; i++){
				for(var j = 0; j < data.photos[i].images.length; j++){
					$('#hpBannerPhotos').append("<img src='"+data.photos[i].images[j].path+"' height='385' width='495' border='0'/>");
				}
			}

			var startIdx = willow.GetRandomNumberBetween(0,($("#hpBannerPhotos img").length));
			$("#hpBannerPhotos").cycle({startingSlide: startIdx,easing:"easeInOutQuad"});
		}
	}
}

// Self managed links function
willow.smButtons = function($obj,id){
	willow.getLinks(id,function(data){
		var links = data.link, lstring = "", numLinks = links.length;
		if(numLinks === 0){return;}
		if(numLinks > 4){
			numLinks = 4;
		}	
		for(var i = 0; i < numLinks; i++){
			lstring += "<a href='"+links[i].url+"'" + willow.checkTarget(links[i].target) + ">"+
			((typeof links[i].image !== "undefined") ? "<img src='"+links[i].image.path+"' height='"+links[i].image.height+"' width='"+links[i].image.width+"'border='0'/>" : links[i].title)+"</a>";
		}		
		$obj.html(lstring);
	});
}

// Checks to see if self managed link has the 'open in new tab' flag set 
willow.checkTarget = function(target){
	if(target == 1 || target == true || target == "true"){
		return " target='_blank'";
	}
	return "";
};
