//**************************
//*       Site v2.0       
//*      devised by      
//* CreativeFreestyle.com
//**************************

//***********************************
// set initial variables 
//***********************************
// set up initial values
var loopTheCarousel = true;
var animateTime = 1000;
var newLeft = 0;
var curPosition = 0;
var curLeft = 0;
var imgWidth = 640;
var carouselAnimating = false;
var curImgCount = 1;
var clickedNavItem;
// dots
var totalDots = 0;
var dotWidth = 14; //including the right margin
var dotSwap = 0;
// timer
var timer;
var carouselPause = 4; // in seconds

$(document).ready(function () {

	//***********************************
	// set variables that deal with loaded content
	//***********************************
	var imgCount = $(".carousel #carouselItems div").size();
	var widthOfContainer = imgCount * imgWidth;

	//***********************************
	// initialise the carousel
	//***********************************
	if ($(".carousel").length > 0) {
		// Set width of containing divs
		$(".carousel #carouselItems").width(widthOfContainer);
		
		//add dots
		$(".carousel").append('<div id="carouselDots"></div>');
		$(".carousel #carouselItems div").each(function () {	
			$("#carouselDots").append('<a href="#" class="carouselDot">Dot</a>');
		});
		$("#carouselDots a:first").addClass("active");
		totalDots = $("#carouselDots a").size();
		//set width of dot container
		$("#carouselDots").css("width", (dotWidth * totalDots) + "px");
	}
	
	//***********************************
	// movement and status change
	//***********************************
	function movePanel() {
		if (!carouselAnimating) {
			curLeft = $(".carousel #carouselItems").position().left;
			
			function dotSwap() {
				$("#carouselDots a").removeClass("active");
				dotSwap = Math.round((-newLeft / imgWidth) + 1);
				if (dotSwap == 0) {
					dotSwap = totalDots;
				} else if (dotSwap == (totalDots + 1)) {
					dotSwap = 1;
				}
				$("#carouselDots a:nth-child(" + dotSwap + ")").addClass("active");
			}
			
			if ($(clickedNavItem).hasClass("prev")) {
				newLeft = curLeft + imgWidth;
				curImgCount -= 1;
				dotSwap();
			} else {
				newLeft = curLeft - imgWidth;
				curImgCount += 1;
				dotSwap();
			}
			
			// Check for left and right extremes
			if ((newLeft - imgCount) > 0 || newLeft <= -(widthOfContainer - imgCount)) { // minus imgCount is to fix IE and its positioning issues
				if (loopTheCarousel == true) {
					if (newLeft > 0) {
						newLeft = -widthOfContainer + imgWidth;
						curImgCount = imgCount;
					} else {
						newLeft = 0;
						curImgCount = 1;
					}
					carouselAnimating = true;
					
					$(".carousel #carouselItems").stop().animate({left: newLeft}, animateTime, function () { 
						carouselAnimating = false;
					});
				}
			} else {
				carouselAnimating = true;
				$(".carousel #carouselItems").stop().animate({left: newLeft}, animateTime, function () { 
					carouselAnimating = false;
				});
			}
		}
	};// END movePanel
	
	//**********************
	// Set the click events
	//**********************
	
	//for the arrows
	$(".carousel .navBar .links a").live("click",function (e) {
		e.preventDefault();
		if (!carouselAnimating) {
			clickedNavItem = $(this);
			movePanel();
		}
		clearInterval(timer); // maybe remove this, or make it optional, so automation doesnt stop
		
	});
	
	// for the dots
	$("#carouselDots a").click(function (e) {
		e.preventDefault();
		if (!carouselAnimating) {
			//set variable for this dot & work out where this dot sits in the list of dots
			var clickedDot = $(this);
			var position = $("#carouselDots a").index(clickedDot);

			newLeft = -(imgWidth * position);
			carouselAnimating = true;
			$(".carousel #carouselItems").animate({left: newLeft}, animateTime, function () { 
				carouselAnimating = false;
			});
			//swaps out active dot
			$("#carouselDots a").removeClass("active");
			$(this).addClass("active");
		}
		clearInterval(timer);
	});		

	//**********************
	// Set the interval
	//**********************
	var timer = setInterval(movePanel, carouselPause * 1000)
	
});// END doc ready


