//Author: Scott Lanning
//Date: February 23, 2011

//get rotator data
function buildRotator()
{
	//create headlines array
	var headlinesArray = new Array();
	
	//loop thru and populate array
	$("div#sw-content-container10 div.ui-widget.app.headlines div.ui-article").each(function(i)
	{
		headlinesArray[i] = new Array();
		headlinesArray[i][0] = $("img:first", this).attr("src");
		headlinesArray[i][1] = $("h1:first", this).html();
		headlinesArray[i][2] = $("p:first", this).text();
	});
	
	//populate initial state
	$("h1#headline-title").html(headlinesArray[0][1]);
	$("div#headline-teaser").html(headlinesArray[0][2]);
	
	//create selector dots
	for(j=0; j<headlinesArray.length; j++)
	{
		$("#headline-selector").append('<div class="selector"></div>');
	}
	
	//create more link if applicable
	if($("#sw-content-container10 a:last").text() == "more")
	{
		$("#sw-content-container10 a:last").addClass("great-city-more-link");
		var moreLink = $("a.great-city-more-link").attr("href");
		$("#headline-selector").append('<a id="more-link" href="' + moreLink +'"><img src="/cms/lib/DC00001581/Centricity/Template/8/more.png" /></a>');
	}
	
	//load headline images
	loadImages(0,headlinesArray);
	
	//set auto rotate
	rotateTimer(headlinesArray);
	
	//sets first selector dot to selected
	$("div.selector:first").css("background-position","bottom left");
	
	//sets click function for selector dots
	$("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);
			},800);
			
		}
		else 
		{
			//stop auto rotate
			killTimer();
			//call click function
			selectorClick(thisIndex,headlinesArray);
		}	
	});
}
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
	$(".selector").css("background-position","top left");
	$(".selector").eq(thisIndex).css("background-position","bottom left");
	
	//set headlines content
	$("h1#headline-title").html(headlinesArray[thisIndex][1]);
	$("div#headline-teaser").html(headlinesArray[thisIndex][2]);
	
	
	//sets up next rotating element
	$("div#hp-rotator-image div.image.active").fadeOut(2000, 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;
	});		
}
//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);
	}, 13000);	
}
function killTimer(){
	clearInterval(intTimer);	
}
function rotateImages(headlinesArray)
{
	//rotate if all images loaded
	if(!$("div#hp-rotator-image div.image").hasClass("loading"))
	{
		isAnimating = true;
		
		//gets active image number
		var headlineIndex = $("div.image.next").attr("id").replace("article","");
		
		//sets active dot
		$(".selector").css("background-position","top left");
		$(".selector").eq(headlineIndex).css("background-position","bottom left");

		//sets active content
		$("h1#headline-title").html(headlinesArray[headlineIndex][1]);
		$("div#headline-teaser").html(headlinesArray[headlineIndex][2]);
		
		//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;
		});
	}
}
