//------------------------------------
//
//	HOME.JS
//
//------------------------------------

//BEGIN jQuery

$(function(){

	////////////////////////
	// SAME HEIGHTS
	
	$('.content').sameHeights('.column');


	////////////////////////
	// PROMO SLIDER
	
	$('#promo_slider').slider();
	

	////////////////////////
	// PERFECT COFFEE
	
	function perfect(){
	
		// Objects
		var $s = $('#perfect_coffee .steps');	// The container
		var $nav;								// The nav (eventually)
		var $img = $s.find('.img');				// The image placeholder
		var $step = $('#perfect_coffee .step');	// The badge
		
		// Variables
		var total = $s.find('div').length;		// The total number of steps
		var c = 1;								// The current step
		var nav = '';							// The nav html (eventually)
		var mooseBan = false;					// Our pet moose
		var hover = false;						// Are we auto?
		var timer;								// For auto
		
		// For auto function
		$('#perfect_coffee').hover(function(){
			
			hover = hover == true ? false : true;
			
			if( !hover ){
				auto();
			}else{
				clearTimeout(timer);
			}
			
		});
		
		// Create the nav HTML
		for( i = 1; i <= total; i++ ){
		
			nav += '<li>' + i + '</li>';
		
		}
		
		// Build the nav
		var $nav = $('<ul/>',{
			'html':	nav
		}).appendTo($s);
		
		// Position the new nav
		$nav.css({
			'margin-left':	Math.round( 0 - $nav.outerWidth() / 2 )
		}).children('li').click(function(e){
		
			// Get the clicked li
			var $t = $(this);
		
			// Only do something if it's not active and we have a good moose
			if( !$t.hasClass('on') ){
			
				// Go to the new step!
				goTo( parseFloat( $t.text() ) );
			
			}
			
			// Stop bad things
			e.preventDefault();
			
		});
		
		// Image click
		$img.click(function(e){
			if( !mooseBan ){
			
				goTo(c+1);
			
			}
			
			e.preventDefault();
			
		});
		
		// Auto slide
		function auto(){
		
			clearTimeout(timer);
			
			if( !hover && !mooseBan ){
			
				timer = setTimeout(function(){
				
					$img.click();
					
				},5000);
							
			}
		
		}
		
		goTo(1);
		
		// Go to
		function goTo(n){

			// Can we go to?
			if( !mooseBan ){
				
				// Naughty moose!
				mooseBan = true;
				
				// Make sure we don't go too far!
				if( n > total ){
					n = 1;
				}
		
				// The new step is…
				var $new = $s.find('div:eq(' + ( n - 1 ) + ')');
				
				// Clear out the old
				$img.find('img:visible').fadeOut(200);
				
				// Add a listener
				$step.bind({
					'webkitAnimationEnd':	function(){
						$s.removeClass('animate');
						$step.unbind();
					}
				});
				
				// Animate the badge (in modern browsers)
				$s.addClass('animate');
				
				// Add in the image
				$('<img/>',{
					'src':	$new.data('image')
				}).prependTo($img).delay(200).fadeIn(200);
				
				// Hide the current text
				$new.siblings('div').hide();
				
				// Show the new text
				$new.show();
				
				// Set the nav
				$nav.find('li:eq(' + ( n - 1 ) + ')').addClass('on').siblings('li').removeClass('on');
				
				// Set the badge
				setTimeout(function(){
				
					$step.text('#' + n );
					
					if( !hover ){
						auto();
					}

				},300);
				
				// Set new current
				c = n;
				
				// Let the moose back in
				mooseBan = false;
			
			}
			
		}
		
	}
	
	perfect();

});
