//Author: Scott Lanning
//Modded by: Joseph Karns
//Date: April 11, 2011

//get rotator data
function buildRotator()
{
	//create headlines array
	var headlinesArray = new Array();

	//Setup Animation Varible
	var isAnimating = false;
	
	//loop thru and populate array
	$("div#sw-content-container10 div.ui-widget.app.headlines div.ui-widget-detail ul.ui-articles li div.ui-article").each(function(i)
	{
		headlinesArray[i] = new Array();
		headlinesArray[i][0] = $("img:first", this).attr("src");
		headlinesArray[i][1] = $("h1:first", this).text();
		headlinesArray[i][2] = $("p:first", this).text();
		headlinesArray[i][3] = $("a:first", this).attr("href");
	});
	if(headlinesArray.length == 0) {
		$("#hp-custom-rotator").hide();
	}else{
		//populate initial state
		$("h1#headline-title").text(headlinesArray[0][1]);
		$("p#headline-teaser").text(headlinesArray[0][2]);
		$("a#readmorelink").attr("href", headlinesArray[0][3]);
	
		//create selector dots
		for(j=0; j<headlinesArray.length; j++)
		{
			//add another div to align selectors right
			$("#headline-selector").append('<div class="selector"></div>');
		}
	
		//load headline images
		loadImages(0,headlinesArray);
	
		//set auto rotate
		rotateTimer(headlinesArray);
	
		//sets first selector dot to selected
		$("div#headline-selector div.selector:first").css("background-position","left bottom");
	
		//sets click function for selector dots
		$("div#headline-selector div.selector").click(function()
		{
			var thisIndex = $(this).index();

			if(isAnimating == true)
			{
				//if animating then wait until animation is done
				setTimeout(function(){
					//stop auto rotate
					killTimer();
					//call click function
					selectorClick(thisIndex,headlinesArray);
				},1000);
			
			}
			else 
			{
				//stop auto rotate
				killTimer();
				//call click function
				selectorClick(thisIndex,headlinesArray);
			}	
		});
		$("#hp-custom-rotator").show();
	}
}
function selectorClick(thisIndex,headlinesArray)
{
	//sets next image to rotate to
	$("div.image").removeClass("next");
	$("div.image").eq(thisIndex).addClass("next").show();
	
	//sets to animating
	isAnimating = true;
	
	//sets selector
	$("div#headline-selector div.selector").css("background-position","left top");
	$("div#headline-selector div.selector").eq(thisIndex).css("background-position","left bottom");
	
	//set headlines content
	$("h1#headline-title").text(headlinesArray[thisIndex][1]);
	$("p#headline-teaser").text(headlinesArray[thisIndex][2]);
	$("a#readmorelink").attr("href", headlinesArray[thisIndex][3]);
	
	
	//sets up next rotating element
	$("div#hp-rotator-image div.image.active").fadeOut(1000, function()
	{	
		
		$("div.image.active").removeClass("active");
		$("div.image.next").addClass("active").removeClass("next");
		$("div.image.active").next().addClass("next").show();
		
		//if image last set first image to next
		if(!$("div.image.next").size())
		{
			$("div.image:first").addClass("next").show();
		}
		
		//sets to not animating
		isAnimating = false;
	});

	rotateTimer(headlinesArray);		
}
//ajax load images
function loadImages(currentFile, headlinesArray)
{
	if(currentFile < headlinesArray.length){
		//BUILD OUT DIV CONTAINERS
		$("div#hp-rotator-image").append("<div class='image loading' id='article" + currentFile + "'></div>");
		
		var currentDiv = "div#hp-rotator-image #article" + currentFile;
		$(currentDiv).prepend("<img src='" + headlinesArray[currentFile][0] + "' />");
		$(currentDiv).removeClass("loading");
		loadImages(currentFile+1,headlinesArray);
	} else {
		$("div.image:first").addClass("active");
		$("div.image:eq(1)").addClass("next");
	}
}
function rotateTimer(headlinesArray){
	//set timer to auto rotate every 8 seconds
	intTimer = setInterval(function(){
		rotateImages(headlinesArray);
	}, 8000);	
}
function killTimer(){
	clearInterval(intTimer);	
}
function rotateImages(headlinesArray)
{
	//rotate if all images loaded
	if(!$("div#hp-rotator-image div.image").hasClass("loading"))
	{
		var isAnimating = true;
		
		//gets active image number
		var headlineIndex = $("div.image.next").attr("id").replace("article","");
		
		//sets active dot
		$("div#headline-selector div.selector").css("background-position","top left");
		$("div#headline-selector div.selector").eq(headlineIndex).css("background-position","bottom left");

		//sets active content
		$("h1#headline-title").text(headlinesArray[headlineIndex][1]);
		$("p#headline-teaser").text(headlinesArray[headlineIndex][2]);
		$("a#readmorelink").attr("href", headlinesArray[headlineIndex][3]);
		
		//sets next image 
		$("div#hp-rotator-image div.image.active").fadeOut(1000, function(){
			
			$("div.image.active").removeClass("active");
			$("div.image.next").addClass("active").removeClass("next");
			$("div.image.active").next().addClass("next").show();
			
			//if last image set first image to next
			if(!$("div.image.next").size())
			{
				$("div.image:first").addClass("next").show();
			}
			isAnimating = false;
		});
	}
}
