willow.ready(function($) {

	$(".photos").cycle({
		random:true,
		startingSlide:willow.GetRandomNumberBetween(0,$(".photos img").length-1),
		timeout:6000	
	});	
	
	//menu
	
	willow.getMenu("119620|119636|119651|119704|119820|136636",function(data){
		$('#nav').menu(data.menu,{direction:"down",showL3s:false});
	});
	
	
	
	willow.buildNewsPager($(".pager"));
	
	
	willow.fixText();
	
	//emergency bulletin options
	var bulletinOptions = {
		emButtonPosition: "right",
		emButton: false,
		data: [
			{type:"announcement",id:3946}
		]
	};
	//emergency bulletin
	$('#Form1').bulletin(bulletinOptions);
	
	//emergency bulletin	
	// willow.getNews("10598",function(data){
		// $('#Form1').bulletin(data,{
			// emButton: false,
			// builtBulletinComplete:function(){
				// $("#em-bar-text #text").fsplit(100);
			// }
		// });
	// },{"backlink":window.location});
	
	//search
    $('#search').pdSearch();
    
    $(window).resize(function(){
    	willow.fixBG();
    });
    willow.fixBG();
});	

willow.fixBG = function(){
	var fHeight = $("#Form1").height();
	if(fHeight <= 1000){
		$("body").addClass("small-screen");
	}else{
		$("body").removeClass("small-screen");
	}
}

willow.newsTimeout = "";

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+"'><span>"+(i+1)+"</span></a></li>";
	}
	
	$obj.append(pString);
	
	$("#pager_0").addClass("on");
	
	$(".pager li a").click(function(e){
		e.preventDefault();
		
		var $obj = $(this),index = parseInt($obj.children("span").html()) - 1;
		
		$(".pager li a").removeClass("on");
		$obj.addClass("on");
		
		for(var i = 0; i < $stories.length; i++){
			if($stories.eq(i).is(":visible")){
				$stories.eq(i).fadeOut(250,function(){
					$stories.eq(index).fadeIn();
				});
				return;
			}
		}
	});
	
	//this does the auto rotation
	willow.newsSpeed = 6000;
	willow.newsTimeout = setTimeout(function(){
		willow.moveNextNews(willow.newsSpeed);
	},willow.newsSpeed);
	
}

willow.moveNextNews = function(speed){
	var $pager = $(".pager li");
	
	for(var i = 0; i < $pager.length; i++){
		if($pager.eq(i).children("a").hasClass("on")){
			if($pager.eq(i).next().length === 0){
				//back to the start
				$pager.eq(0).children("a").click();
			}else{
				$pager.eq(i).next().children("a").click();
			}
			break;
		}
	}
	willow.newsTimeout = setTimeout(function(){
		willow.moveNextNews(speed);
	},speed);
}

willow.fixText = function(){
	$(".stories li .headline a").fsplit(75);
	
	$(".stories li .brief").fsplit(200);
	
	$(".items li .etitle a").fsplit(75);
}

