//------------------------------------
//
//	SITE.JS
//	Requires:	jquery 1.6.x
//
//------------------------------------

////////////////////////////
// BEGIN JQUERY

$(function(){
	
	//////////////////////////
	// TARGET BLANK
	
	$('a[href*="http://"]:not([href*="'+location.hostname+'"]):not([class*="internal"]), .external').live('click',function() {
	    window.open($(this).attr('href'));
	    return false;
	});
	
	////////////////////////
	// CONTENT TABS
	
	function setContent(){
	
		var mooseBan = false;
		
		var leftPos = 200;
		
		// On load check for URL
		var loadHash = window.location.hash ? window.location.hash.replace('#/','#') : false;
		
		// Set content and tab + centrally align text
		if(loadHash){
			
			var href = loadHash.replace('#', '/');
			
			$(loadHash).show().css({
				left: leftPos
			}).addClass('active').children('div').css({
				marginTop: (($('.content').height() - $(loadHash).children('div').height()) / 2)
			});
			
		}
		
		$('a[href="' + href + '"]').parent('li').addClass('on');
		
		// And on tab click
		$('#primary li:not(.shop) a').click(function(e){
			
			var $a = $(this);
			
			if( !$a.parent('li').hasClass('on') && !mooseBan ){
		
				$a.parent('li').addClass('on').siblings('.on').removeClass('on');
				
				var hash = $a.attr('href').replace('/', '#');

				swap(hash);
				
				window.location.hash = hash.replace('#','#/');
			
			}
			
			e.preventDefault();
		
		});
		
		function swap(id){
		
			mooseBan = true;
		
			// New section
			var $m = $('#sections');
			var $new = $(id);
			var $old = $m.children(' section.active');

			// Variables
			var contentH = $('.content').height();
			
			
			if($old.length) {
				
				$old.animate({left: -1000}, 750, 'easeInExpo', function(){

					$new.show().animate({left: leftPos}, 750, 'easeOutExpo', function(){

						mooseBan = false;

					}).height(contentH).addClass('active').children('div').css({
						marginTop: ((contentH - $new.children('div').height()) / 2)
					});

				}).removeClass('active');

				
			}else {
				
				$new.show().animate({left: leftPos}, 750, 'easeOutExpo', function(){

					mooseBan = false;

				}).height(contentH).addClass('active').children('div').css({
					marginTop: ((contentH - $new.children('div').height()) / 2)
				});
				
			}
			
			
		}
		
	}
	
	setContent();
	
});

//////////////////////////
// EASING

jQuery.extend( jQuery.easing,{
	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;
	},
	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;
	},
	easeInBack: function (x, t, b, c, d, s) {
		if (s == undefined) s = 1.70158;
		return c*(t/=d)*t*((s+1)*t - s) + b;
	},
	easeOutBack: function (x, t, b, c, d, s) {
		if (s == undefined) s = 1.70158;
		return c*((t=t/d-1)*t*((s+1)*t + s) + 1) + b;
	},
	easeOutBounce: function (x, t, b, c, d) {
		if ((t/=d) < (1/2.75)) {
			return c*(7.5625*t*t) + b;
		} else if (t < (2/2.75)) {
			return c*(7.5625*(t-=(1.5/2.75))*t + .75) + b;
		} else if (t < (2.5/2.75)) {
			return c*(7.5625*(t-=(2.25/2.75))*t + .9375) + b;
		} else {
			return c*(7.5625*(t-=(2.625/2.75))*t + .984375) + b;
		}
	},	
	easeOutQuad: function (x, t, b, c, d) {
		return -c *(t/=d)*(t-2) + b;
	},
	easeInOutQuad: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return c/2*t*t + b;
		return -c/2 * ((--t)*(t-2) - 1) + b;
	}
});
