// JavaScript Document
$(document).ready(function(){
	var slideWidth = 960;
	var curPos = 0;
	var slides = $('.boxContainer');
	var totalSlides = slides.length;
	var autoDelay = 7;
	var intTimer = null;

// Countdown code
$('#countdown').countdown({until:$.countdown.UTCDate(-8, 2012,  4-1, 21), format: 'd', layout:'<h1>{dn}</h1><h3>Days until Kelpfest!</h3>'});

	$('#showBox').css('overflow','hidden');
	
	//swfobject.embedSWF("swf/ColoringWidget.swf", "gby", "960", "320", "10.0.0");

	
	slides.wrapAll('<div id="slideInner"></div>')
	.css({
    	'float' : 'left',
    	'width' : slideWidth
  	});
	
	$('#slideInner').css('width', slideWidth*(totalSlides+2));
	
	/*
		Begin Event Handler Hell 
	*/
	
	$('#showBox').mouseenter(function(){stopAutoPlay()});
	$('#showBox').mouseleave(function(){startAutoPlay()});
	// setup the controls
	$('#showBox').before('<div class="controlBtn" id="leftControl"></div>');
	$('#showBox').before('<div class="controlBtn" id="rightControl"></div>');
	
	var offset =$('#showBox').offset();

	$('.controlBtn').css('top',offset.top+140);
	$('#leftControl').css('left',offset.left-16);
	$('#rightControl').css('left',offset.left+slideWidth-16);

	$(window).resize(function(){
		offset =$('#showBox').offset();
		$('#leftControl').css('left',offset.left-16);
		$('#rightControl').css('left',offset.left+slideWidth-16);
	});

	$('#leftControl').hover(function(){
			$(this).stop(true,true).animate({'left' :offset.left-32},{duration:200,queue:false});
			$(this).css('background-position', '-32px 0px');
	});
	
	$('#leftControl').mouseleave(function(){
			$(this).stop(true,true).animate({'left' :offset.left-16},{duration:200,queue:false});
			$(this).css('background-position', '0px 0px');
	});
	
	$('#rightControl').hover(function(){
			$(this).stop(true,true).animate({'left' :offset.left+slideWidth},{duration:200,queue:false});
			$(this).css('background-position', '-32px 0px');
	});
	
	$('#rightControl').mouseleave(function(){
			$(this).stop(true,true).animate({'left' :offset.left+slideWidth-16},{duration:200,queue:false});
			$(this).css('background-position', '0px 0px');
	});
	
	/*
		End Event Handler Hell 
	*/
	
	slides.eq(slides.length-1).clone().prependTo($('#slideInner'));
	slides.eq(0).clone().appendTo($('#slideInner'));
	
	$('#slideInner').css('marginLeft', -slideWidth);
	
	$('.controlBtn').click(function(){
		
		if($('#slideInner').is(':animated')){
			$('#slideInner').stop(true,true);
		}else{
			if($(this).attr('id')=='rightControl'){
				advanceSlide(true);
				startAutoPlay();
			}else{
				prevSlide(true);
				startAutoPlay();
			}
		}
	});
	
	startAutoPlay();
	
	function advanceSlide(goFast){
		var fSpeed = typeof(goFast)=='boolean' ? 400 : 1200;
		if(++curPos>=slides.length){
				$('#slideInner').animate({'marginLeft' : slideWidth*(-(curPos+1))},{duration:fSpeed,queue:false,complete:function(){
					$('#slideInner').css('marginLeft', -slideWidth);
				}});
				curPos=0;
		}else{
			$('#slideInner').animate({'marginLeft' : slideWidth*(-(curPos+1))},{duration:fSpeed,queue:false});
		}

	}
	
	function prevSlide(goFast){
		
		var fSpeed = (goFast==true) ? 400 : 1200;
		if(--curPos<0){
			$('#slideInner').animate({'marginLeft' : slideWidth*(-(curPos+1))},{duration:fSpeed,queue:false,complete:function(){
					$('#slideInner').css('marginLeft', -slideWidth*slides.length);
				}});
				curPos=slides.length-1;
		}else{
			$('#slideInner').animate({'marginLeft' : slideWidth*(-(curPos+1))},{duration:fSpeed,queue:false});
		}
	}

	function startAutoPlay(){
		if(intTimer!=null){
			clearInterval(intTimer);
		}
		//$('.controlBtn').hide(500);
		intTimer = setInterval(advanceSlide,autoDelay*1000);
	}
	
	function stopAutoPlay(){
		if(intTimer!=null){
			clearInterval(intTimer);
		}
		//$('.controlBtn').show(500);
	}
});
	

