//------------------------------------
//	HOME.JS
//	Author: 	Engage Interactive
//	Requires:	jquery 1.5
//	Version:	0.1
//------------------------------------

////////////////////////////
// BEGIN JQUERY

jQuery(function( $ ){

	// FORM VALUE TITLE SWAP THINGY	
	$('form input[title]').each(function(){
		$(this).attr('value', $(this).attr('title'));
	});
	$('form input[title]').focus(function(){
		if($(this).attr('value') == $(this).attr('title')){
			$(this).attr('value', '');
		}
	});
	$('form input[title]').blur(function(){
		if($(this).attr('value') == $(this).attr('title') || $(this).attr('value') == ''){
			$(this).attr('value', $(this).attr('title'));
		}
	});
	
	//////////////////////////
	// TWITTER

	$('#twitter div').tweet();
	
	
	//////////////////////////
	// Slideshow
	
	function slideShow(){
		
		// Objects
		var $slideshow = $('#promo_panel');
		var $slider = $('#cycle_window');
		var $slide = new Array();
		var $a = $slideshow.find('nav a:not(.link)');
		var $n;
		var $link = $slideshow.find('.link');
		
		// Variables
		var pause = 4;
		var s = 800;
		var e = 'easeInOutExpo';
		var t = $slider.find('img').length;
		var w = $slider.width();
		var h = $slider.height();
		var nav = '';
		var timer = {};
		var c = 1;
		var mooseBan = true;
		var canClick = $slider.find('img').length > 1;

		// Remove nav if you can't click it
		if (!canClick)
			$a.hide();
		
		// Create array of slide objects and the nav
		for( i = 1; i <= t; i++ ){
		
			// Add to the array
			$slide[i] = $slider.find('img:eq(' + ( i - 1 ) + ')');
			
			// Generate HTML
			nav = nav + '<li><a href="#">' + i + '</a></li>';
			
			// Finished the loop
			if( i == t ){
			
				// Create the nav
				$n = $('<ul/>',{
					'html':	nav
				}).appendTo($slideshow.find('nav'));
				
				// Load the first slide
				goTo('up');
				
			}
		}
		
		
		// Go to function
		function goTo(dir){
		
			mooseBan = true;
		
			var $old = $slider.find('img:visible');
			var $new = $slide[c];
			
			oldY = dir == 'up' ? 0 - h : h;
			newY = dir == 'up' ? h : 0 - h;
			
			// Animate
			$old.animate({
				'top':	oldY
			},600,e,function(){
				$(this).hide();
			});
			
			$new.css({
				'display':	'block',
				'top':		newY
			}).animate({
				'top':		0
			},600,e,function(){
			
				mooseBan = false;
			
			});
			
			if( $new.attr('title') != '' ){
				$link.show().attr('href',$new.attr('title'));
			}else{
				$link.hide();
			}
			
			// Set nav on state
			$n.find('li a.on').removeClass('on');
			$n.find('li:eq(' + ( c - 1 ) + ') a').addClass('on');
		}
		
		
		//////////////////////////
		// THE CLEVER BIT
		
		// Automatically slide
		function auto(){

			var p = $slide[c].data('pause');
		
			if( p == undefined ){
				p = pause;
			}
		
			timer = setTimeout(function(){
			
				clearTimeout(timer);
			
				$slideshow.find('a.down').click();
				auto();
			
			},p * 1000);
			
		}

		if (canClick)
			auto();
		
		// Hovering
		$slideshow.bind({
			'mousemove':	function(){
				clearTimeout(timer);
				$slideshow.unbind('mousemove');
			},
			'mouseenter':	function(){
				clearTimeout(timer);
			},
			'mouseleave':	function(){
				auto();
			}
		});
		
		
		// Clicking of links
		$a.live('click',function(e){
		
			if( !mooseBan && !$(this).hasClass('on') && !$(this).hasClass('link') && canClick){
			
				var dir;
			
				if( $(this).hasClass('arrow') ){
					
					if( $(this).hasClass('down') ){
						c++;
						dir = 'up';
					}else{
						c--;
						dir = 'down';
					}

					if( c > t ){
						c = 1;
					}else if( c < 1 ){
						c = t;
					}
					
				}else{
					
					var num = $(this).text();
					var dir = c > num ? 'down' : 'up';
					c = num;
									
				}

				goTo(dir);
			
			}
			
			e.preventDefault();
			
		});
		
	}
	
	slideShow();
	
});
