/*
jQuery Gallery rotation with image feed
Version 0.5

Author: Carl Topham

Copyright 2011 
*/


		
	
	

			
				
		
		var ticks =0;
		
		var mouseover = false;


		function rotateElement(change, chain, chainlength) {
			if (change == null){
   				change = 0;
 			}
			
			if (chain == null){
   				chain = 0;
 			}
			
			if (chainlength == null){
   				chainlength = 1;
 			}
			
			//speed depends on the number of ones to scroll
			
			//always takes 400
			
			var transitionSpeed = 400;
			transitionSpeed = transitionSpeed - (transitionSpeed / (transitionSpeed/chainlength)* 50);
	
			$("#gallery-feed ul li:first").clone().appendTo("#gallery-feed ul");
			
			if( $("#gallery-feed .tab").is(':visible') ) {
				$("#gallery-feed .tab").animate({ 'width': 0}, transitionSpeed);
			}
			
			objHeight = $("#gallery-feed ul li:first").height() + 20; //20 for the padding
			//console.log(objHeight);
			
			$("#gallery-feed ul li:first").animate({ 'margin-top': -objHeight}, transitionSpeed, 'linear', function() {
					$(this).remove();	
					if (change != 0) {
						change = change -1;
					}
					
					
					
					if (change >=1) {
						rotateElement(change, chain, chainlength);	
					}
					
					if (change == 0) {
						setupClicks();
						$("#gallery-feed .tab").animate({ 'width': 8}, transitionSpeed);
						//get the top list item's key which is based on the class
						var myClass = $("#gallery-feed ul li:first a").attr("class");
						$('#gallery-front').trigger('updateGallery', myClass);
					}
					
			});
		}
		

		function setupClicks() {
			//remove all click functions
			
			$('#gallery-feed ul li').unbind('click');
			
			$("#gallery-feed ul li:first").bind('click', function() {
				//get link and goto page
				console.log("Go To page");
			});
			
			$("#gallery-feed ul li").not(":first").bind('click', function() {
				//move me to the top
				index = $(this).index();
				console.log(index);
				rotateElement(index, 1, index+1);
				
			});
			
		};
		
		
		
		function updateGalleryFeed() {
			rotateElement(0, 0, 1);
			
		}
		
		
		function tick() {
			//time has elapsed
			//trigger checks
			ticks ++;
			//console.log('tick!: '+ ticks);
			$('#gallery-feed').trigger('update', [ticks]);
		}
		
		
		function startTimer() {
			setInterval( "tick()", 1000 );
		}
		
		
		
		
		$(document).ready(function () {
		
		
		$("#gallery-front div.billboard").not(":first").hide();
		
		startTimer();
		setupClicks();
		
		
		//pause on mouse over the list
		$('#gallery-feed').mouseenter(function(){
			mouseover = true;
			$('#gallery-feed').trigger('update', ticks);
		}).mouseleave(function(){
			mouseover = false;
		});
		
		//pasue on mouse over the image
		$('#gallery-front div.billboard').mouseenter(function(){
			mouseover = true;
			$('#gallery-feed').trigger('update', ticks);
		}).mouseleave(function(){
			mouseover = false;
		});
				
		
		$('#gallery-feed').bind('update', function(e, tick) {
			try {
				nextTickThreshold;
			} catch (e){
				nextTickThreshold = tick + 4;
			}
			
			if (mouseover ==true) {
				nextTickThreshold = ticks + 5;
			}
	
			if (tick >= nextTickThreshold) {
				
				nextTickThreshold = tick + 5;
				rotateElement(0);
			}
		});
		
		$('#gallery-front').bind('updateGallery', function(e, index) {
			
			$(this).children("div.billboard").each(function() {
				myIndex = $(this).index();
				//console.log(myIndex + " - " + index);
				
				if( $(this).is(':visible') ) {
					outGoing = myIndex;
				}
				
			});
				
				
				$('#gallery-front div.billboard:eq('+outGoing+')').fadeOut(300, function() {
						$('#gallery-front div.billboard:eq('+index+')').fadeIn(300);
				});
				
		
			
	//	});
		

	});


});
	 
