/*
 * Run this when the Dom is ready
 */
$(function(){
	
	// Enable PNG alpha stuff
	$('body').supersleight({shim: '/images/blank.gif'});
	
	// Call out setupImageSwap if it exists
	if($('#animation-wrapper').length > 0){
		/* Get the screens */
		$.get('/ajax-screens/',function(data){
			/* Insert them */
			$('#animation-wrapper').prepend(data);
			/* Run the image swap */
			setupImageSwap('#animation-wrapper');
		});
		
	}
	
	// Run the logo animator if it exists
	if($('ul.logos').length > 0){
		animateLogos('ul.logos','-145');
	}
	
	// Run the other animations only if they are there! 
	if($('.slider-nav a').length > 0){
		bannerTitleAnimation();
		titleTimer = setTimeout("bannerTitleTimer()",9000);	
	}

});

// Set a global variable here
var moving = false;
var titleMoving = false;
var timer = 0;
var $i 	= 1;
var $i2	= 2;
var titleTimer = 0;

/**
 * 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 	= $(parent).find('>:first');
	var lastItem 	= $(parent).find('>:last');
	
	// Okay great now we have those...
	
	// First the move left (right arrow)
	$('.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
			$(lastItem).fadeOut('normal',
							 function(){
								 
								 // Okay animation complete set moving to false
								 moving = false;
								 
								 // Reset margin and detach
								 $(lastItem).detach();
								 
								 // Now insert it after the last item
								 $(lastItem).insertBefore(firstItem).show();
								 
								 // Recall ourselves
								 setupImageSwap(parent);
								 
							});
		} else {
			// DO NOTHING WE'RE STILL ANIMATING
		}
		
	});
	
	// Now the move right (left arrow)
	$('.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
			$(firstItem).hide().detach();
			
			// Copy it before the first item
			$(firstItem).insertAfter(lastItem);
			
			// Right, now animate it
			$(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("$('.arrow-forward').click()", 5000);
}

/**
Animates the brand logos at the top of the page using jQuery
*/
function animateLogos(parent,distance){
	// Find the first and last child descenders of the parent
	var firstitem 	= $(parent).find('>:first');
	var lastitem 	= $(parent).find('>:last');
	
	// Scroll first item out of view after a delay
	$(firstitem).css({'marginLeft':"0px"})				// Set margin to 0
				.delay('3000')							// Delay for 2seconds
				.animate({"marginLeft": distance+'px'},	// Animate Margin to desired distance
							2000, 
							function(){					// callback and loop
								// Reset margin, and detach the item
								$(firstitem).css({'marginLeft':""}).detach();
								// Insert it after the last item in the list
								$(firstitem).insertAfter(lastitem);
								// Call Self to loop
								animateLogos(parent,distance);
							});
}

/**
 * Timer for the banner title
 */
function bannerTitleTimer(){
	/* Fire the click */
	$('#jsHomepageLink'+$i2).click();
}

/**
 * Actually animates the banner title thing
 */
function bannerTitleAnimation(){
	
	/* Add click event to the a's */
	$('.slider-nav a').click(function(e){
		
		/* Stop link from doing anything */
		e.preventDefault();
		
		/* Are we animating now? */
		if(titleMoving == false){
			
			/* Set Moving to true */
			titleMoving = true;
			
			/* Get the id */
			var id = $(this).attr('id');
			
			var item = $(this);
			
			/* take out the text so we have the id */
			$i2 = id.replace("jsHomepageLink","");
			
			switch($i2){
			
				/* For item number 1, fade out 4 and make sure 1 is visible */
				case"1":
					$('#jsHomepageImage1').fadeIn('normal');
					$('#jsHomepageImage2, #jsHomepageImage3, #jsHomepageImage4').fadeOut('normal',function(){
						/*Turn off selected state of all buttons */
						$('.slider-nav a').removeClass('selected');
						
						/* Set this item selected */
						$(item).addClass('selected');
						
						titleMoving = false;
					});
				break;
				
				/* For item number 2, fade out 1 and make sure 2 is visible */
				case"2":
					$('#jsHomepageImage2').fadeIn('normal');
					$('#jsHomepageImage1, #jsHomepageImage3, #jsHomepageImage4').fadeOut('normal',function(){
						/*Turn off selected state of all buttons */
						$('.slider-nav a').removeClass('selected');
						
						/* Set this item selected */
						$(item).addClass('selected');
						
						titleMoving = false;
					});
				break;
				
				/* For item number 3, fade out 2 and make sure 3 is visible 
				case"3":
					$('#jsHomepageImage3').fadeIn('normal');
					$('#jsHomepageImage1, #jsHomepageImage2, #jsHomepageImage4').fadeOut('normal',function(){*/
						/*Turn off selected state of all buttons 
						$('.slider-nav a').removeClass('selected');*/
						
						/* Set this item selected 
						$(item).addClass('selected');
						
						titleMoving = false;
					});
				break;*/
				
				/* For item number 4, fade out 3 and ensure 4 is visible 
				case"4":
					$('#jsHomepageImage4').fadeIn('normal');
					$('#jsHomepageImage1, #jsHomepageImage2, #jsHomepageImage3').fadeOut('normal',function(){*/
						/*Turn off selected state of all buttons
						$('.slider-nav a').removeClass('selected'); */
						
						/* Set this item selected
						$(item).addClass('selected'); 
						
						titleMoving = false;
					});
				break;*/
			}
			
			/* Increment the counter if not 4*/
			if($i2 == 2){
				/* Make it 1 */
				$i2 = 1;
			}else{
				/* increment */
				$i2++;
			}
		}
		
		clearTimeout(titleTimer);
		titleTimer = setTimeout("bannerTitleTimer()",9000);	
	});
}
