var options = {
    direction:"down",
	showL3s:false,
	currentL1:pdGlobal.currentPages[0].id
};

willow.detailPages = [204,52416];

$(document).ready(function(){	
	//build all the menus
	willow.getMenu("128018|128019|128020|128021|128022|128023|128024|134133",function(data){
   	    var menu = data.menu,menuString = "";
   	    //dropdown
   	    $('#L1').menu(menu,options);
   	    //right menu
		var $rightmenu = $(".right-menu");
		if($rightmenu.length > 0){
			for(var i = 0; i < menu.length; i++){
				if(pdGlobal.currentPages[0].id == menu[i].id){
					menuString+="<div class='top-nav'><a href='/default.aspx'>Home</a>:&nbsp;<a href='/podium/default.aspx?t="+pdGlobal.currentPages[0].id+"'>"+pdGlobal.currentPages[0].title+"</a></div><ul class='L2'>";
					for(var j = 0; j < menu[i].L2.length; j++){
						menuString+="<li><a"+willow.insertClass(menu[i].L2[j].id)+" href='"+menu[i].L2[j].url+"'>"+menu[i].L2[j].title+"</a>";				
						if(pdGlobal.currentPages[1] != undefined){
							if(menu[i].L2[j].L3.length > 0 && menu[i].L2[j].id == pdGlobal.currentPages[1].id){
								menuString+="<ul class='L3'>";
								for(var k = 0; k < menu[i].L2[j].L3.length; k++){
									menuString+="<li><a"+willow.insertClass(menu[i].L2[j].L3[k].id)+" href='"+menu[i].L2[j].L3[k].url+"'>-&nbsp;"+menu[i].L2[j].L3[k].title+"</a></li>";	
								}
								menuString+="</ul>";
							}
						}
						menuString+="</li>";				
					}
					menuString+="</ul>"
					$rightmenu.append(menuString);
				}
			}
		}
		
		//sitemap
		var $sitemap = $(".sitemap");
		if($sitemap.length > 0){
			$sitemap.sitemap(data);	
		}
   	});
   	
   	willow.getPhotos(29540,function(data){
   		var photos = data.photo.images;
   		var rand = Math.floor(Math.random()*photos.length)
   		$("#banner h1 img").attr("src",photos[rand].path);
   	});
   	
	willow.setSearch($("#search"),{t:130838,pt:'Thacher Search Results'});
	
	//weather code
	$('#weather').getWeather();
	
	//emergency bulletin
	willow.getNews(12563,function(data){
   		$("body").bulletin(data);
   	});
   	
   	//table for 
   	var $table = $(".inv-table");
   	if($table.length > 0){
   		willow.getList(3,function(data){
   			var list = data.list,leftColor = "", rightColor = "",tString = "<table class='maintext' width='100%' cellpadding='0' cellspacing='0' border='0' style='font-size:11px; border-right: solid 1px #9E9A62; border-left: solid 1px #9E9A62; border-bottom: solid 1px #9E9A62;'>";
   			for(var i = 0; i < list.length; i++){
   				if(i%2 === 0){
   					leftColor = "#E2DFB3";
   					rightColor = "#D5D198";
   				}else{
   					leftColor = "#EEEECC";
   					rightColor = "#E8E6BF";
   				}
   				tString += "<tr><td align='center' valign='middle' width='50%' bgcolor='"+leftColor+"' class='maintext' style='padding:3px;'><div style='font-size:11px; padding-left:5px; padding-right:5px;'>"+list[i].brief+"</div></td>"+
							"<td align='left' valign='middle' width='50%' bgcolor='"+rightColor+"' class='maintext' style='padding:3px;'><div style='font-size:11px; padding-left:5px; padding-right:5px;'>"+list[i].title+"</div></td></tr>";
   			}
   			tString += "</table>";
   			$table.html(tString);
   		});
   	}
   	
   	//work the detail page magic
   	if(!willow.checkDetail()){
   		willow.showBackground();
   	}	
	
	//CSP sdb 121310 added this so as to expand the parent iframe which was cutting off the news content, this is a temp fix
	$(document).ready(function() {	
		loc = new String(parent.window.location);//grab the URL
		var findid = loc.indexOf('&');
		if (findid != -1){
			loc = loc.split("&");
			var newsid = loc[1].split("=")[1];// second equal sign's value---the news id	
			if(pdGlobal.currentPages[0].title == "News Detail" && newsid == "664243"){
				$("iframe:nth-child(1)").removeAttr("onLoad").height(5177);
				//alert(window.document.height);
			}
		}
	});
	
	//quote code
	if($(".quote").length > 0){
		var listid = 4;
		switch(pdGlobal.currentPages[0].id){
			case 128018: listid = 22; break;//about - maybe 9, only one missing
			case 128019: listid = 4; break;//admissions
			case 128020: listid = 5273; break;//CSP sdb 042810 was 6 //academics
			case 128021: 
				if(pdGlobal.currentPages.length > 1){
					if(pdGlobal.currentPages[1].id == 128768){
						listid = 8; 
					}else if(pdGlobal.currentPages[1].id == 128773){
						listid = 9; 
					}
				}
				break;//student life
			case 128022: listid = 7; break;//the arts
			case 128023: listid = 10; break;//athletics
			case 128024: listid = 11; break;//outdoor life
		}
		
		willow.getList(listid,function(data){
			var lists = data.list,quote="";
			var rand = Math.floor(Math.random()*lists.length);
			for(var i = 0; i < lists.length; i++){
				if(i == rand){
					quote+="<div class='text'>"+lists[i].full+"</div><div class='author'>-&nbsp;"+lists[i].title+"</div>";
				}
			}
			$(".quote").html(quote);
		});
	}
});

willow.checkDetail = function(){
	for(var i = 0; i < willow.detailPages.length; i++){
   		if(pdGlobal.currentPages[pdGlobal.currentPages.length-1].id === willow.detailPages[i]){	
   			return true;
   		}
   	}
   	return false;
}
			
willow.showBackground = function(){
	if(pdGlobal.currentPages[pdGlobal.currentPages.length-1].type !== "content"){
		$("#lInTbl").addClass("portal-back");
	    $("#menu-fix").css("display","block");
	    $("#L1_128018").hover(function(){
	    	$("#menu-fix").css("display","none");
	    },function(){
	    	$("#menu-fix").css("display","block");
	    });
	    $(".sitelcol").css("display","block");
	}
}

willow.insertClass = function(id){
	var found = false;
	for(var i = 0; i<pdGlobal.currentPages.length; i++){
		if(parseInt(pdGlobal.currentPages[i].id) == parseInt(id)){
			found = true;
		}
	}
	if(found){
		return " class='on'";
	}
	return "";
}

willow.isFrontendPage = function(){
	pages = new Array(128018,128019,128020,128021,128022,128023,128024,128385,130181,698);
	for(var i = 0; i < pages.length; i++){
		if(pages[i] === parseInt(pdGlobal.currentPages[0].id)){
			return true;
		}	
	}
	return false;
}


