/*
 * Run this when the Dom is ready
 */
jQuery(function(){
	// Call out setupImageSwap
	/**
	 * HAVE DISABLED IMAGE SETUP HERE AS NOTHING TO SETUP
	 * TODO Remove code if not going to be used
	 */
	// setupImageSwap('#animation-wrapper');
	
	homepageBanner();
	clearTimeout(timer);
	timer = setTimeout("bannerSwitch()",5000);
});

// Set a global variable here
var moving = false;
var timer = 0;

var $i = 2;

/**
 * Sets Up our Image Animations
 * 
 * 
 * @param parent	The item containing the items to be moved
 * @param distance	The width of a single item so we know how far to animate
 */
function setupImageSwap(parent){
	
	/* We need to find the first and last items to begin with 
	by using direct descenders and first and last sudo classes
	*/
	
	var firstItem 	= jQuery(parent).find('>:first');
	var lastItem 	= jQuery(parent).find('>:last');
	
	// Okay great now we have those...
	
	// First the move left (right arrow)
	jQuery('.arrow-forward').unbind('click').one('click',function(){
		// First clear the timer
		clearTimeout(timer);
		// ONLY ACTIVATE ONCE WE HAVE DONE MOVING
		if(moving == false){
			
			// We're moving, set it to true
			moving = true;
			
			// Animate the first item
			jQuery(lastItem).fadeOut('normal',
							 function(){
								 
								 // Okay animation complete set moving to false
								 moving = false;
								 
								 // Reset margin and detach
								 jQuery(lastItem).detach();
								 
								 // Now insert it after the last item
								 jQuery(lastItem).insertBefore(firstItem).show();
								 
								 // Recall ourselves
								 setupImageSwap(parent);
								 
							});
		} else {
			// DO NOTHING WE'RE STILL ANIMATING
		}
		
	});
	
	// Now the move right (left arrow)
	jQuery('.arrow-back').unbind('click').one('click',function(){
		// First clear the timer
		clearTimeout(timer);
		// ONLY ACTIVATE ONCE WE HAVE DONE MOVING
		if(moving == false){
			
			// We're moving set it to true
			moving = true;
			
			// We need to grab the last item first by setting it's margin to negative and detaching it
			jQuery(firstItem).hide().detach();
			
			// Copy it before the first item
			jQuery(firstItem).insertAfter(lastItem);
			
			// Right, now animate it
			jQuery(firstItem).fadeIn('normal',
							function(){
				
								// Okay we did all the moving around we need to, set moving to false
								moving = false;
								
								// Recall ourselves
								setupImageSwap(parent);
							});
		} else {
			// DO NOTHING WE'RE STILL ANIMATING
		}
	});
	// First clear the timer
	clearTimeout(timer);
	// Run a timer
	timer = setTimeout("jQuery('.arrow-forward').click()", 5000);
}

////////////////////////////////////////
//Rotating banner on Homepage middle col 
///////////////////////////////////////
//$(function(){
	
//});


function bannerSwitch(){
	/* We need to fire a click event on the correct item */
	$('#jsHomepageLink'+$i).click();
	/* Clear the timer */
	clearTimeout(timer);
	/* Setup a new one  */
	timer = setTimeout("bannerSwitch()",5000);
}

function homepageBanner(){
	
	/* When we click on one of the little linky things */
	$('.slider-nav a').click(function(){
		
		/* If we're not moving */
		if(moving == false){
			
			/* Set moving to true */
			moving = true;
			
			/* Store the id of the item we've clicked */
			var id = $(this).attr('id');
			
			var item = $(this);
			
			// If id = the first image
			if(id == 'jsHomepageLink1'){
				
				// Animate the imageSlider margin left
				$('#imageSlider').animate({marginLeft:'0px'},'normal',function(){
					
					// Turn off the selected state of all other
					$('.slider-nav a').removeClass('selected');
					$(item).addClass('selected');
					
					// Set moving back to false once we're finished
					moving = false;
					
					/* Make the i counter 2 so the next timer fires on it */
					$i = 2;
				});
			}
			else if(id == 'jsHomepageLink2'){
				
				// Animate the imageSlider margin left
				$('#imageSlider').animate({marginLeft:'-252px'},'normal',function(){
					
					// Turn off the selected state of all other
					$('.slider-nav a').removeClass('selected');
					$(item).addClass('selected');
					
					// Set moving back to false once we're finished
					moving = false;
					
					/* Make the i counter 3 so the next timer fires on it */
					$i = 3;
				});
			}
			else if(id == 'jsHomepageLink3'){
				
				// Animate the imageSlider margin left
				$('#imageSlider').animate({marginLeft:'-504px'},'normal',function(){
					
					// Turn off the selected state of all other
					$('.slider-nav a').removeClass('selected');
					$(item).addClass('selected');
					
					// Set moving back to false once we're finished
					moving = false;
					
					/* Make the i counter 4 so the next timer fires on it */
					$i = 4;
				});
			}
			else if(id == 'jsHomepageLink4'){
				
				// Animate the imageSlider margin left
				$('#imageSlider').animate({marginLeft:'-756px'},'normal',function(){
					
					// Turn off the selected state of all other
					$('.slider-nav a').removeClass('selected');
					$(item).addClass('selected');
					
					// Set moving back to false once we're finished
					moving = false;
					
					/* Make the i counter 1 so the timer rolls the images back again */
					$i = 1;
				});
			}
			
		}
		return false;
	});
}
