var EM_ID=12529, menuOpen=false, announcementsCycle=true, photosCycle=true;

var options = {
   emButtonPosition: "right",
   emButton: false
};

//cookie stuff to remember L1 toggle
var toggleMenuState = grabCookieVal("toggleMenuState");
		
willow.ready(function($) {
	//menu
	willow.getMenu("133064|133065|133066|133067|133068",function(data){
		var menu = data.menu,menuString = "<div id='L2_container'>",$sitemap = $(".sitemap");
		//sitemap plugin
		if($sitemap.length > 0){
			$sitemap.sitemap(data);
		}
		
		//left menu
		for(var i = 0; i < menu.length; i++){
			menuString+="<ul class='L2' id='L2_"+i+"'>";
			for(var j = 0; j < menu[i].L2.length; j++){
				menuString+="<li><a href='"+menu[i].L2[j].url+"'"+willow.checkTarget(menu[i].L2[j].target)+">"+menu[i].L2[j].title+"</a></li>";							
			}
			menuString+="</ul>";
		}
		menuString += "<div class='clear'><!-- --></div></div>"
		$("#L2_wrapper").append(menuString);
		willow.buildMenuActions();
	});
	
	$("#searchSubmit").click(function(){ submitSearch(); return false; });

	//set to cycling announcements
	// $("#announcementsList").cycle({
	    // pause:1,
	    // fit:1,
	    // containerResize:1,
	    // fx:'scrollUp,scrollLeft,scrollDown,scrollRight',
	    // randomizeEffects:0,
	    // timeout:6000,
	    // speed:1000,
	    // before:function(){ if(announcementsCycle){ $("#announcements").removeClass("hideOnLoad"); announcementsCycle = false; } }
	// });
	$("#hpPhotoList").cycle({
	    fit:1,
	    containerResize:1,
	    speed:3500,
	    timeout:500,
	    pause:1,
	    before:function(){
	    	if(photosCycle){ $("#hpPhoto").removeClass("hideOnLoad"); photosCycle = false; }
	    	// make the images center vertically in the slideshow container
	    	$(".imgCenter", this).css("margin-top","-"+($("img", this).attr("height")/2)+"px");
	    	$(".vertCenter", this).css("height",($(this).parent().height()/2)+"px");
	    }
	});
	$.getJSON("/data/json.asp?type=news&id="+EM_ID+"&backlink="+window.location,function(data){ $('#form1').bulletin(data,options); });
	
	//events popup
	$('#dialog').jqm({overlay: 0});
	/*var p = $("#iCal_popout");
	var position = p.position();
	$("#dialog").css({"margin-left": position.left,"margin-top": position.top - 1});
	*/

});	
		
		//more cookie stuff
		function grabCookieVal(variable){
			var returnVal = false;
			if(document.cookie.indexOf(variable)>-1){ returnVal = document.cookie.split(variable)[1].split("=")[1].split(";")[0]; }
			return returnVal;
		}

		willow.checkTarget = function(target){
			if(target == 1 || target == true || target == "true"){
				return " target='_blank'";
			}
			return "";
		}
		
		function submitSearch(){ window.location = "/podium/default.aspx?t=52392&q="+document.getElementById("search").value+"&pt=Princeton Day School - Search"; }
		function SubmitOnEnter(evt){
			var charCode = (evt.which) ? evt.which : event.keyCode;
			if(charCode == "13" || charCode == "26"){ submitSearch(); return false; }
		}
		
		
willow.buildMenuActions = function(){
	//toggle L1 fix
	var $L2c = $("#L2_container"), $L2w = $("#L2_wrapper");
	$L2c.css({"top":($L2c.height()*-1)});
	$L2w.css({"height":"0"});
				
	//$("#navWrapIn").hide();
	$('.toggleMenu').click(function(e){
		var $obj = $(this);
		e.preventDefault();
		if($obj.hasClass("open")){
			$L2c.animate({top:($L2c.height()*-1)},500);
			$L2w.animate({"height":0},500);
			$obj.removeClass("open");
			$(".toggleMenu").css("background-position","0 0");
			menuOpen=true;
		}else{
			$L2w.animate({"height":$L2c.height()},500);
			$L2c.animate({top:0},50);
			$obj.addClass("open");
			$(".toggleMenu").css("background-position","0 -13px");
			menuOpen=false;
		}
		
		//(re)set cookie value (true/false) on each click
		var expire = new Date(); expire.setDate(expire.getDate()+1);
		document.cookie = "toggleMenuState="+(!menuOpen)+"; expires="+expire+"; path=/";
	});
	//check on load for existing menu cookie value
	if(toggleMenuState=="false"){
		//$(".toggleMenu").click();
	}else{
		$(".toggleMenu").click();
		$(".toggleMenu").addClass("open");
	}
	/*if(toggleMenuState=="true"){
		$(".toggleMenu").click();
		$(".toggleMenu").addClass("open");
	}*/
}	
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		

