willow.ready(function($) {	
	willow.myTimeout = "";
	willow.currentNewsStory = 0;
	
	var menuOptions = {
		direction:"down",
		showL3s:false
	};
	
	willow.getMenu("107044|107077|107104|107194|135958|107212|107274",function(data){
		var linkgroupID = new Array(13071, 13066, 13067, 13068, 14395, 13069, 13070);
		$('#L1').menu(data.menu,menuOptions);
		for(var i = 0; i < data.menu.length; i++){
			willow.addMenuLinks(data.menu[i].id, linkgroupID[i]);
		}
	});
	
	$("#photos").cycle({before: willow.cycleBefore});
	
	willow.buildNewsPager($(".pager"));
	
	willow.openStatement($("#statementMission span"), $("#statementMissionFull"), $("#statementMission span"), $("#missionButton"));
	willow.openStatement($("#statementDiversity span"), $("#statementDiversityFull"), $("#statementDiversity span"), $("#diversityButton"));
	
	willow.closeStatement($("#missionButton"), $("#statementMissionFull"), $("#statementMission span"), $("#missionButton"));
	willow.closeStatement($("#diversityButton"), $("#statementDiversityFull"), $("#statementDiversity span"), $("#diversityButton"));
	
	$("#L1_107044").attr("hoffset","1");
	
	$('#search').pdSearch({showButton:true, showButtonClass:'searchSubmit',showButtonText:''});
});

willow.addMenuLinks = function(menuID,linkgroupID){
	willow.getLinks(linkgroupID,function(data){
		var links = data.link;
		if(links.length === 0){return;}
		var lstring = "<ul class='menuSM-Links'>";
		for(var i = 0; i < links.length; i++){
			lstring += "<li><a href='"+links[i].url+"'" + willow.checkTarget(links[i].target) + "><span>"+links[i].title+"</span></a></li>";
		}
		lstring +="</ul>";
		$("#L20_"+menuID+" .menufirst").html(lstring);
	});
}

willow.checkTarget = function(target){
	if(target == 1 || target == true || target == "true"){
		return " target='_blank'";
	}
	return "";
};

willow.cycleBefore = function(){
	if($(this).hasClass("char")){
		$("#wordOverlay").css("background-position","-297px 0px");
	}	
	else if($(this).hasClass("edu")){
		$("#wordOverlay").css("background-position","-297px -50px");
	}	
	else if($(this).hasClass("lead")){
		$("#wordOverlay").css("background-position","-297px -100px");
	}
}

willow.buildNewsPager = function($obj){

	var $stories = $("#stories li"),pString = "";
	
	for(var i = 0; i < $stories.length; i++){
		if(i > 0){
			$stories.eq(i).css("display","none");
		}
		pString += "<li><a href='#' id='pager_"+i+"'>"+(i+1)+"</a></li>";
	}
	
	$obj.append(pString);
	
	$("#pager_0").addClass("on_pager");
	
	$(".pager li a").click(function(e){
		e.preventDefault();
		clearTimeout(willow.myTimeout);
		var $obj = $(this),index = parseInt($obj.html()) - 1;
		
		$(".pager li a").removeClass("on_pager");
		$obj.addClass("on_pager");
		
		for(var i = 0; i < $stories.length; i++){
			if($stories.eq(i).is(":visible")){
				$stories.eq(i).fadeOut(250,function(){
					$stories.eq(index).fadeIn();
					willow.currentNewsStory = index;
					willow.myTimeout = setTimeout(function(){
						willow.myTimeoutFunction($stories);
					},8000);
				});
				return;
			}
		}
	});
	
	willow.myTimeout = setTimeout(function(){
		willow.myTimeoutFunction($stories);
	},8000);
}

willow.myTimeoutFunction = function($stories){
	var nextStory = willow.currentNewsStory + 1;
	var maxStories = 5;
	
	if($stories.length < 5)
	{
	    maxStories = $stories.length;
	}
	if(nextStory >= maxStories)
	{
	    nextStory = 0;		
	}
	$(".pager li a:eq("+nextStory+")").click();
	willow.currentNewsStory = nextStory;
}

willow.footerStatements = function($obj, $long){
	$obj.toggle(
		function(){
			$long.slideDown();
			$obj.css("display","none");
		},
		function(){
			$long.slideUp();
		}
	)
}
willow.openStatement = function($obj, $long, $hide, $btn){
	$obj.click(function(){
		$hide.css("display","none");
		$long.slideDown();
		$btn.css("cursor","pointer");
	});
}
willow.closeStatement = function($obj, $long, $show, $btn){
	$obj.click(function(){
		$long.slideUp();
		$show.css("display","block");
		$btn.css("cursor","default");
	});
}

//the following functions are used on the homepage
function getFLASH(obj){
	if (document.getElementById)
		return document.getElementById(obj)
	else if (document.all)
		return document.all[obj]
}

function getWinSize() {
	var myWidth = 0, myHeight = 0;
	
	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	} else if(document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	} else if(document.body && (document.body.clientWidth || document.body.clientHeight)) {
		//IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	}
	return {w:myWidth,h:myHeight}
}

function adjustFlashSize(id, wWidth, wHeight) {
  var winSizes = getWinSize();
 	//alert("resize"); 
  var flashObj = getFLASH(id);
  //alert(winSizes.w + "," + winSizes.h);
  //alert(wWidth +", "+wHeight);
  if(winSizes.w < wWidth) {
	 flashObj.style.width = wWidth + "px";
  } else {
	 flashObj.style.width = "100%";
  }
  if(winSizes.h < wHeight) {
	 flashObj.style.height = wHeight + "px";
  } else {
	 flashObj.style.height = "100%";
  }
  //alert(flashObj.style.width +", "+flashObj.style.height);
}
function initFlash(){
	if(hasFlashNEEDED){
		getFLASH("non_flash_content").style.display = "none";
		getFLASH("flashcontent").style.display = "";
		UFO.create(FOx, "flashc");
	}else{
		getFLASH("non_flash_content").style.display = "";
	}
}
