//-------------------------//
//--image reel animation--//
//-----------------------//
$(document).ready(function() {	
//Set Default State of each SMI reel image
$(".paging").show();
$(".paging a:first").addClass("active");

//Get size of images, how many there are, then determin the size of the image reel.
var timer = null;
var curIndex = 0;
var numItems = $(".image_reel").children().size();
var timerDone = false;

var imageWidth = $(".window").width();
var imageSum = $(".image_reel div").size();
var imageReelWidth = imageWidth * imageSum;

//Adjust the image reel to its new size
$(".image_reel").css({ 'width': imageReelWidth });

//Paging + Slider Function
rotate = function (index) {
	var image_reelPosition = index * imageWidth; //Determines the distance the image reel needs to slide

	$(".paging a").each(function (ind, el) {
		if ($(el).attr("rel") == index + 1)
			$(el).addClass('active');
		else
			$(el).removeClass('active');
	});

	//Slider Animation
	$(".image_reel").animate({
		left: -image_reelPosition}, {
        duration:500,
        easing: 'easeInOutCubic'
      });

	curIndex = index;
};

advance = function () {
	curIndex = curIndex + 1;

	//reset if on last item, or pause
	if (curIndex >= numItems) {
		//curIndex = 0;
		/*$(".image_reel").each(function (ind,el)
												  {
													  $(el).unbind('hover');
												  });*/
		timerDone = true;								  
		pauseAnim();
		rotate(0);
		return;
	}

	rotate(curIndex)
}

startAnim = function () {
	if (timerDone == false)
	{
		if (timer != null)
			clearInterval(timer);
			timer = setInterval(advance, 5000);
	}
}


pauseAnim = function () {
	clearInterval(timer);
	timer=null;
}

//start the timer
startAnim();


//On Hover
$(".image_reel").hover(pauseAnim, startAnim);


//On link Click
$(".paging a").click(function () {						   
	//Reset Timer/kill timer
	timerDone = true;
	pauseAnim(); //pauses the animation once a link is clicked
	//startAnim();
			
	rotate($(this).attr("rel") - 1); //Trigger rotation immediately
	return false; //Prevent browser jump to link anchor
});
});
