//WAIT FOR THE PAGE TO BE READY
$(document).ready(function(){

	xPos = 0;
	current = 1;
	total = $('#slider').children('div').size();
	height = $('#homepage').outerHeight();
	mouse = true;
	
	development = false;
	
	updateHeight();

	var li = $('#navigation li').live('click', function(){
		if($(this).children('a').attr('class')!='on' && mouse == true){
			
			$(this).children('a').addClass('on');
			$(this).siblings('li').children('a').removeClass('on');
			
			newX = (li.index(this) * 800) - ((li.index(this) * 800) * 2);
			
			xPos = newX;
			current = li.index(this) + 1;
			
			slideTo(newX);
		}
		return false;
	});

	$('#next').click(function(){
		if(mouse==true && total>current){
			xPos = xPos - 800;
			current = current + 1;
			slideTo(xPos);
		}
		updateNav();
		debug('Next button');
		return false;
	});
	
	$('#prev').click(function(){
		if(mouse==true && current>1){
			xPos = xPos + 800;
			current = current - 1;
			slideTo(xPos);
		}
		updateNav();
		debug('Prev button');
		return false;
	});
	
	$('a.internal').click(function(){
		newLoc = $(this).attr('href').substr(1);
		$('#navigation li.'+newLoc).click();
		return false;
	});

	function slideTo(newX){
		debug('Start anim');
		mouse = false;
		$('#slider').animate({left:newX},800,'easeInOutExpo',function(){
			mouse = true;
			debug('End anim');
		})
		updateHeight();
	}
	
	function updateHeight(){
		newH = $('#slider div.content:eq('+(current-1)+')').outerHeight();
		$('#content_wrapper').animate({height:newH},400,'easeInOutExpo');
		
		newT = (newH/2) + 253;
		
		$('#next, #prev').animate({top:newT},400,'easeInOutExpo');
		
		if(current == 1){
			$('#prev').addClass('disabled').animate({opacity:0.2}, 200);
			$('#next').removeClass('disabled').animate({opacity:1}, 200);
		}else if(current == total){
			$('#next').addClass('disabled').animate({opacity:0.2}, 200);
			$('#prev').removeClass('disabled').animate({opacity:1}, 200);
		}else{
			$('#next, #prev').removeClass('disabled').animate({opacity:1}, 200);
		}
	}
	
	function updateNav(){
		$('#navigation li a.on').removeClass('on');
		$('#navigation li:eq('+(current - 1)+') a').addClass('on');
	}
	
	function debug(act){
		if(development == true){
			$('p#debug').show().html(act+'<br/>X: '+xPos+'<br/>Current: '+current+'<br/>Mouse: '+mouse+'<br/>Height:'+height);
		}
	}

	//COLORBOX STUFF
	
	$(".photo").colorbox();
	$(".youtube").colorbox({iframe:true, width:650, height:550});
	$(".terms").colorbox({width:"600px", height:"60%", inline:true, href:"#terms"});

// End jQuery goodness
});

jQuery.easing['jswing'] = jQuery.easing['swing'];

jQuery.extend( jQuery.easing,
{
	easeInExpo: function (x, t, b, c, d) {
		return (t==0) ? b : c * Math.pow(2, 10 * (t/d - 1)) + b;
	},
	easeOutExpo: function (x, t, b, c, d) {
		return (t==d) ? b+c : c * (-Math.pow(2, -10 * t/d) + 1) + b;
	},
	easeInOutExpo: function (x, t, b, c, d) {
		if (t==0) return b;
		if (t==d) return b+c;
		if ((t/=d/2) < 1) return c/2 * Math.pow(2, 10 * (t - 1)) + b;
		return c/2 * (-Math.pow(2, -10 * --t) + 2) + b;
	}
});