// JavaScript Document
// Home Page Gallery //

/////////////////////////////////////////////////////////////////////////////////////////
// Setup variables

	var galleryCounter = 0
	var currentGallery = 0
	var animateDelay = 4000 //miliseconds
	var canAnimate = true
	
	var containerID1 = "#galleryLeft" // if this exists, run gallery, also animate this
	var containerID1Width = 258
	
	var containerID2 = "#galleryRight" // also animate this
	var containerID2Width = 761
	
	var contentLength = ".left-pane-small-content" // counts these
	
	// set default easing type
	jQuery.easing.def = "easeInOutQuint";

	
/////////////////////////////////////////////////////////////////////////////////////////
// Setup

	function startGallery(){
		// run script if galleryLeft id exists on page
		if ( $(containerID1).length > 0 ) createNav()
	}


////////////////////////////////////////////////////////////////////////////////////////////////////
// Create the navigation

	function createNav(){
	
		// counts the number if items with this class
		galleryCounter = $(contentLength).length
				
		$('#content').prepend('<div id="nav"></div>')
		
		for (var i=0; i<galleryCounter; i++){
		 $('#nav').append('<a id="' + i + '" href="#"></a>')
		 
		// number navigation click action
			$('#'+i ).click(function () { 
				currentGallery = ($(this).attr('id') );
				animateGallery();
				canAnimate = false
			})
		
		}
		
		highlightNav();
	 
	}
	
	
/////////////////////////////////////////////////////////////////////////////////////////
// Animation Timer

	function autoplayTimer(){
		// run the startAnimation based on the animateDelay Speed
		timer = setInterval( startAnimation, animateDelay);
	}



////////////////////////////////////////////////////////////////////////////////////////////////////
// Reel Animation - Only animates once to correct position, repeated using autoplayTimer function

	function startAnimation(){

		if (canAnimate){
			
			if ( currentGallery < galleryCounter -1 ) currentGallery++;
			else currentGallery = 0;// else go back to the start
			
			animateGallery();
		}

	}

//////////////////////////////////////////////////////
// Animate!

	function animateGallery(){
		
		$(containerID1).css("left", (currentGallery * containerID1Width)*-1);
		$(containerID2).css("left", (currentGallery * containerID2Width)*-1);
		highlightNav();
	}


//////////////////////////////////////////////////////
// Highlight the navigation to show which poster number is active

	function highlightNav(){
	
		for ( i=0; i < galleryCounter; i++ ){
			
			if (i == currentGallery){
				
				$('#'+i).removeClass("navOff");
				$('#'+i).addClass("navOn");
				
			} else {
				
				$('#'+i).removeClass("navOn");
				$('#'+i).addClass("navOff");
				
			}
		}
		
	}


////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////
// GO!


	womAdd('startGallery();');
	womOn();
	autoplayTimer();


