willow.ready(function($) {
	// Initalizes variables for tagline fade
	willow.wordsImg = $("#words img");
	willow.index = -1;
	willow.last = false;
	
	// Menu data
  	var menuOptions = {direction:"right",showL3s:false};
	willow.getMenu("123071|126077|123072|123074|123073|123075|123076|123077",function(data){
		$('#L1').menu(data.menu,menuOptions);
	});

	// Emergency Bulletin	
	var emOptions = {emButtonPosition: "right",emButton: false};
	willow.getNews("12365",function(data){$('#Form1').bulletin(data,emOptions);},{"backlink":window.location}); 
	
	// Cycle the photos
	$("#photos").cycle({before:willow.taglineFadeIn});
	
	// Podium search
	$('#search').pdSearch({showButton:true, showButtonClass:'searchButton', showButtonText:''}); 
	$(".searchButton").css("margin","0");
	
	// Manual cycle through news stories
	willow.navStories($(".fStory"));
	
	// Cuts off news title at 70 characters
	$(".mainNewsTitle").fsplit(70);
	$(".fsNewsDetail").fsplit(130);
	
	// Start with the tagline hidden by having z-index of -1 in the CSS, after the fade out has happened so the proper style variables are set reset the z-index
	willow.wordsImg.fadeOut(function(){
		$("#words").css("visibility","visible");
		$("#discover").fadeOut(function(){
			$("#discover").css("visibility","visible");
		});
	});
});

// Function for fading in the tagline
willow.taglineFadeIn = function(){
	if(!willow.last){ // If statemy to track if the "Discover You" has been faded in
		if(willow.wordsImg.length > willow.index){ // If the number of times there has been a fadein is less then the max, fade the next word in
			willow.wordsImg.eq(willow.index).fadeIn(1000, function(){
				willow.index++; // Increase fade index
			});
		}
		else if(willow.wordsImg.length = willow.index){ // If the number of images that have been faded in equals the max number, fade the words out and fade in the "Discover You"
			willow.wordsImg.fadeOut(function(){
				$("#words").css("visibility","hidden");
				$("#discover").fadeIn(1000, function(){
					willow.last = true; // Set variable ending the tagline fade cycle.
				});
			});
		}
	}
}

willow.navStories = function($obj){
	var stories = $obj;
	var firstStory = 0, currentStory = 0, lastStory = $obj.length-1;
	
	$obj.eq(firstStory).css("display","block");
	
	// Previous story click event
	$("#prevFeaturedStory a").click(function() {
		currentStory -= 1;
		// If on first story go to last story else go to previous story
		if(currentStory < firstStory){
			currentStory = lastStory;
			$obj.eq(firstStory).fadeOut(function(){
				$obj.eq(currentStory).fadeIn();
			});			
		}
		else{
			$obj.eq(currentStory+1).fadeOut(function(){
				$obj.eq(currentStory).fadeIn();
			});	
		}
	});
	
	// Next story click event
	$("#nextFeaturedStory a").click(function() {
		currentStory += 1;
		// If on last story go to first story else go to next story
		if(currentStory > lastStory){
			currentStory = firstStory;
			$obj.eq(lastStory).fadeOut(function(){
				$obj.eq(currentStory).fadeIn();
			});			
		}
		else{
			$obj.eq(currentStory-1).fadeOut(function(){
				$obj.eq(currentStory).fadeIn();
			});	
		}
	});
}


