/*
 * Dymamic Width Carousel
 * v.1.0.0
 *
 * Copyright (c) 2011 Kat McGowan (http://www.squigloo.com.au)
 * Dual licensed under the MIT
 * and GPL (http://www.gnu.org/licenses/gpl.txt) licenses.
 *
 *
 * http://www.squigloo.com.au
 */
 //$(document).ready(function() { 
jQuery(document).ready(function($) {
	var caroWidth = $('#sq-carousel').width();
	var timer;
	var curID = 1;
	var prevID = 5;
	var maxBanners = 5;
	var delay = 9000;
	var speed = 1000;
	var isAnimating = false;
	var timerActive = false;
	var firstTween = true;
	resetPositions();
	setupNav();
	loaded();
	
	$('#link_'+curID).addClass("current"); 
	
	//delay start
	timer = setTimeout(autoTween, delay);
	
	function animBanners(){
		//console.log("TRANSITIONING: Prev: " +prevID + " Cur: " + curID);
		amount = '-=' + caroWidth;
		isAnimating = true;
		//Anim out prev banner
		$('.banner_'+prevID).animate({
			left: amount
		  }, speed, function() {
				// Animation complete.
				updatePosition()
		 });
		 //Anim in next banner
		 $('.banner_'+curID).animate({
			left: amount
		  }, speed, function() {
			  	isAnimating = false;
				// Animation complete.
				if (timerActive){
					clearTimeout(timer);
				}
				timerActive = true;
				timer = setTimeout(autoTween, delay);
		 });
		 //Update nav buttons
		 $('#link_'+prevID).removeClass("current"); 
		 $('#link_'+curID).addClass("current"); 
	}
	//Automatically go to next slide
	function autoTween(){
		//console.log("... auto tweening to : " + curID);
		//timer is done
		timerActive = false;
		//current becomes prev
		prevID = curID;
		curID++;
		if(curID > maxBanners){
			curID = 1;	
		}
		animBanners();
	}
	//Manual go to banner anim
	function jumpToBanner(clickID){
		if (clickID != curID && !isAnimating){
			//console.log("isAnimating: " + isAnimating);
			//console.log("... manual tweening to : " + clickID);
			//kill timer for autotweening
			if (timerActive){
				timerActive = false;
				clearTimeout(timer);
			}
			//update cur ID
			prevID = curID;
			curID = clickID;
			//trace
			animBanners();
		}
		
	}
	//Prev slide completed, move back to RHS
	function updatePosition(){
		isAnimating = false;
		$('.banner_'+prevID).css('left',caroWidth);
	}
	//move all except current 
	function resetPositions(){
		for (i = 1; i <= maxBanners; i++){
			if(i != curID){
				if (i == prevID && isAnimating){
					//leave it, its stil moving
				} else {
					//console.log("Position: " +i);
					$('.banner_'+i).css('left',caroWidth);
				}
			}
		}
	}
	//Set up the carousel nav buttons
	function setupNav(){
		$('.caroLink').click(function() {
			 idName = $(this).attr("id");
			 id = idName.charAt(idName.length-1)
			 jumpToBanner(id);
		});
	}
	//Its loaded so you can make the other banners off the screen visible now
	function loaded(){
		for (i = 1; i <= maxBanners; i++){
			$('.banner_'+i).css('visibility','visible');
		}
	}
	
	$(window).resize(function() {
	  caroWidth = $('#sq-carousel').width();
	  //console.log("Updated width: " + caroWidth); 
	  resetPositions();
	});
	

});

