willow.ready(function($){
	willow.currentNewsStory = 0;
	willow.currentEventStory = 0;
	willow.storyHolder = 0;
	
	//Bulletin
	willow.getNews(12263,function(data){$("body").bulletin(data,{emButton:false});},{"backlink":window.location});
	//$('body').bulletin({sid:465,gid:12263,emButton:false});

	//pagers
	willow.buildNewsPager($(".pager"));
	willow.buildEventsPager($(".event_pager"));
	
	$object = $("#valuesearch");
	willow.setSearch($object,{pt:'Montrose School'});
	
	$(".title a").fsplit(40);
	$(".news_events_text").fsplit(100);
	$("#quote h2").fsplit(185);

});	




//willow.myTimeout = "";
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 li a").click(function(e){
		e.preventDefault();
		//clearTimeout(willow.myTimeout);
		var $obj = $(this),index = parseInt($obj.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();
					/*news and events rotate*/
					/*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 < 6)
	{
	    maxStories = $stories.length;
	}
	if(nextStory >= maxStories)
	{
	    nextStory = 0;		
	}
	$(".pager li a:eq("+nextStory+")").click();
	willow.currentNewsStory = nextStory;
}

//events
//willow.myEventTimeout = "";
willow.buildEventsPager = function($obj){

	var $eventstories = $("#event_stories li"),pString = "";
	
	for(var i = 0; i < $eventstories.length; i++){
		if(i > 0){
			$eventstories.eq(i).css("display","none");
		}
		pString += "<li><a href='#' id='eventpager_"+i+"'>"+(i+1)+"</a></li>";
	}
	
	$obj.append(pString);
	
	$("#eventpager_0").addClass("on");
	
	$(".event_pager li a").click(function(e){
		e.preventDefault();
		//clearTimeout(willow.myEventTimeout);
		var $obj = $(this),index = parseInt($obj.html()) - 1;
		
		$(".event_pager li a").removeClass("on");
		$obj.addClass("on");
		
		for(var i = 0; i < $eventstories.length; i++){
			if($eventstories.eq(i).is(":visible")){
				$eventstories.eq(i).fadeOut(250,function(){
					$eventstories.eq(index).fadeIn();
					willow.currentEventStory = index;
					/*willow.myEventTimeout = setTimeout(function(){
						willow.myEventsTimeoutFunction($eventstories);
					},8000);*/
				});
				return;
			}
		}
	});
	
	/*willow.myEventTimeout = setTimeout(function(){
		willow.myEventsTimeoutFunction($eventstories);
	},8000);*/
}

willow.myEventsTimeoutFunction = function($eventstories){
	var nextEventStory = willow.currentEventStory + 1;
	var maxEventStories = 5;
	if($eventstories.length < 6)
	{
	    maxEventStories = $eventstories.length;
	}
	if(nextEventStory >= maxEventStories)
	{
	    nextEventStory = 0;		
	}
	$(".event_pager li a:eq("+nextEventStory+")").click();
	willow.currentEventStory = nextEventStory;
}


