var	JavaScriptSchoolID = 123, EM_ID = 11767,
	MenuArray = new Array(
	{pageid : 121086},
    {pageid : 122488},
    {pageid : 121088},
    {pageid : 121087},
    {pageid : 121089}
);

willow.smButtons = function($obj,id){
	willow.getLinks(id,function(data){
		var links = data.link;
		if(links.length === 0){return;}
		var lstring = "<ul class='smbuttons_"+id+"'>";
		lstring += "<li><a id='loginButt' class='hideText' href='/podium/'>Login</a></li>";
		for(var i = 0; i < links.length; i++){
			lstring += "<li><a class='button' href='"+links[i].url+"'" + ((links[i].target) ? " target='_blank'" : "") + "><span>"+
			((typeof links[i].image !== "undefined") ? "<img src='"+links[i].image.path+"' border='0'/>" : links[i].title)+"</span></a></li>";
		}
		lstring += "</ul>";
		$obj.html(lstring);
	});
}

willow.ready(function($) {
	willow.smButtons($("#button_area"),13699);

});

$(document).ready(function(){
	/*
	$button = $(".button span");
	alert($button.width());
	$(".button").css({"width":$button.width() + 14});
	*/	
	$("body").bulletin({sid:JavaScriptSchoolID,gid:EM_ID,emButton:true});
	//KISS it
		//grab cookies
		var thisL1 = grabCookieVal("curL1tID"), rn = grabCookieVal("fwcdsPhoto");
		//if no cookie, set thisL1 to "0"
		thisL1 = (thisL1)?thisL1:0;
		//check for random number cookie value and to see if we're not still on the same L1
	if(!rn || parseInt(thisL1) != pdGlobal.currentPages[0].id){
		//set up new random image (ensuring we don't get the same as the last one)
		do { var newRN = Math.floor(Math.random()*6); }
		while(newRN==rn);
		rn = newRN;
		//write new cookies
		document.cookie = "curL1tID="+pdGlobal.currentPages[0].id+"; path=/";
		document.cookie = "fwcdsPhoto="+rn+"; path=/";
	}
	
	var $slider = $("#slider");
	var speed = 500;
	var timeout = "";
	
	//turn on correct L1
	if(!isNaN(isL1Page())){		
		timeout = setTimeout(function(){
	    	var pos = GetPosition($("#L1_"+isL1Page()));
	    	$slider.animate({"top":pos},speed);
	    },speed);
	}else{
	    $slider.css('display','none');
	}
	
	$("body").addClass("photo"+rn);
	
	if(pdGlobal.currentPages[pdGlobal.currentPageLevel-1].type != "content"){
		$("#lInTblLCell").css("display","block");
		$slider.css('display','none');
		$(".sitepg").css("background-image","none");
	}
	
	$("#L1 li a").hover(function(){
		var $obj = $(this);
		$slider.css('display','block');
		clearTimeout(timeout);
		$slider.queue("fx", []).stop();
		$slider.animate({"top":GetPosition($obj)},speed);
			$slider.queue(function () {
			$(this).dequeue();
		});
	},function(){
		if(!isNaN(isL1Page())){
			timeout = setTimeout(function(){
				var pos = GetPosition($("#L1_"+isL1Page()));
				$slider.animate({"top":pos},speed);
			},speed);
		}else{
			$slider.css('display','none');
		}
	});
	
	//build L2s
	var options = {
		schoolid:123,
		currentL1:pdGlobal.currentPages[0].id,
		pageid:(pdGlobal.currentPages[1])?pdGlobal.currentPages[1].id:0
	};
	$("#L2").podiumlevel2(options);
	
	
	$("#show-left").click(function(){
		var lcol = $("#lInTblLCell");
		if(lcol.css("display") == "none"){
			lcol.show();
		}else{
			lcol.hide();
		}
	});
	
/******************************************/
/******* Custom Buttons - ala KISS ********/
/******************************************/
	//set the button classes (podium overwrites any class tag we put in a text group) and set up the hover state with animations
	if($("#customButtons a").length>0){
		$("#customButtons a")
			.removeAttr("class")
			.attr("class",$("#customButtons a").attr("classes"))
			.hover(
				function(){
					if(!$(this).hasClass("on")){
						$("#customButtons .active").animate({top:0},200).removeClass("active");
						$(this).animate({top:9},200).addClass("active");
					}
				},
				function(){ if(!$(this).hasClass("on")){ $(this).animate({top:0},200).removeClass("active"); } }
			);

		//set on states for the page we're on, if we're on a custom button page
		if(pdGlobal.currentPages[pdGlobal.currentPageLevel-1]){
			switch(pdGlobal.currentPages[pdGlobal.currentPageLevel-1].id){
				case 124701:
				case 124702:
				case 124703:
				case 124704:
				case 124705:
				case 124706:
				case 124707:
				case 127966:
				case 127973:
				case 127976:
					$("#customButtons_"+pdGlobal.currentPages[pdGlobal.currentPageLevel-1].id).addClass("on");
			}
		}
	}
	
	//custom flash dealies - nonflash (and flash replace when ready)
	if(pdGlobal.currentPages[pdGlobal.currentPageLevel-1]){
		switch(pdGlobal.currentPages[pdGlobal.currentPageLevel-1].id){
			case 121091:	//core values page
				$("#coreValues").flash({swf:'/flash/photo-corevalues.swf?pGroup=31656', height:'425', width:'961', hasVersion:9, params:{wmode:'transparent'}}).css("display","block");
				$("#lInTblRCell").css("padding-top","435px");
				break;
			case 125183:	//admissions page??
				$("#admissionsLanding").flash({swf:'/flash/photo-admissions.swf?pGroup=30949', height:'381', width:'961', hasVersion:9, params:{wmode:'transparent'}}).css("display","block");
				$("#lInTblRCell").css("padding-top","382px");
				break;	
		}
	}
	
	//CSP sdb 103009 added this to reduce the content channel to 725px, was running way off the channel
	switch(pdGlobal.currentPages[pdGlobal.currentPageLevel-1].id){
		case 204:	$(".cch4").css("width","725px");break;
		default:break;
	}
	
	
});

var positions = {
	one:0,
	two:23,
	three:46,
	four:69,
	five:92
};
	
function GetPosition($obj){
	if($obj.hasClass("one")){
		return positions.one;
	}else if($obj.hasClass("two")){
		return positions.two;
	}else if($obj.hasClass("three")){
		return positions.three;
	}else if($obj.hasClass("four")){
		return positions.four;
	}else if($obj.hasClass("five")){
		return positions.five;
	}
}

function isL1Page(){
	for(var i in MenuArray){
		if(parseInt(pdGlobal.currentPages[0].id) == MenuArray[i].pageid){
			return MenuArray[i].pageid;
		}
	}
	return NaN;
}

/*Search Functions */
function submitSearch(){
	__pdSiteSearch('', document.getElementById("search").value);
}
function SubmitOnEnter(evt){
	var charCode = (evt.which) ? evt.which : event.keyCode
	if(charCode == "13" || charCode == "26"){
		submitSearch();
		return false;
	}
}


function grabCookieVal(variable){
	var returnVal = false;
	if(document.cookie.indexOf(variable)>0){ returnVal = document.cookie.split(variable)[1].split("=")[1].split(";")[0]; }
	return returnVal;
}
