$().ready(function()
{	
	var time = 600; // length of sliding animation
	var easing = 'easeout'; // type of easing equation
	var end = 33; // positioning variable
	var start = 33;
	var fadeInSpeed = 250; // speed to fade in server types
	var fadeOutSpeed = 250; // speed to fade out server types
	var lastTab = 's-100'; // keep track of last server viewed
	
	var lastTabClicked = 'truflex-s'; // 
	var lastDivShown = 'truflex-s';
	
	var serverSPos = null;
	var serverPos = null;
	
	var TFS = new Object();
	TFS.lastActiveTab = 's-100';
	TFS.end = 33;
	
	var TF = new Object();
	TF.lastActiveTab = 't-600';
	TF.end = 33;
	
	var TFQ = new Object();
	TFQ.lastActiveTab = 'q-650';
	TFQ.end = 33;
	
	// add default overstates
	$('#s-100').addClass('current');
	$('#truflex-s').addClass('active');
	$('#t-600').addClass('current');
	$('#q-650').addClass('current');
	
	// round nav corners -- so sweet!
	$('.nav-inner .first').corner('tl bl round 3px');
	$('.nav-inner .last').corner('tr br round 3px');
	
	// make all footer containers equal height
	$(".box").vjustify(); 
	
	function a(x)
	{
		alert(x);
	}
		
	// --> FUNCTION -- this moves the divs back and forth	
		
	function moveThis(div,end)
	{
		if ( start >= end )
		{
			destination = -(Math.abs(end) - Math.abs(start));
			$('#' + div + ' .plan-holder').animate({left: destination}, time, easing);
		}
		else if ( start <= end )
		{
			destination = Math.abs(end) + Math.abs(start);
			$('#' + div + ' .plan-holder').animate({left: destination}, time, easing);
		}
		else if (start == end)
		{
			return;
		}
		return false;
	}
	
	
	function slideThis(id)
	{
		if (id.match(/s/)) // if this is a truflex-s server
		{
			// get id of linked clicked and set to active	
			TFS.id = id;
			$('#' + TFS.lastActiveTab).removeClass('current'); // remove previous active state
			TFS.end = $('#' + TFS.id).attr('posx');
			TFS.lastActiveTab = TFS.id
			$('#' + TFS.id).addClass('current');
			moveThis('p-truflex-s', TFS.end); // move the div in the correct direction
		}
		else if (id.match(/t/gi))
		{
			TF.id = id;
			$('#' + TF.lastActiveTab).removeClass('current'); // remove previous active state
			TF.end = $('#' + TF.id).attr('posx');
			TF.lastActiveTab = TF.id
			$('#' + TF.id).addClass('current');
			moveThis('p-truflex',TF.end); // move the div in the correct direction
		}
		else if (id.match(/q/gi))
		{
			TFQ.id = id;
			$('#' + TFQ.lastActiveTab).removeClass('current'); // remove previous active state
			TFQ.end = $('#' + TFQ.id).attr('posx');
			TFQ.lastActiveTab = TFQ.id
			$('#' + TFQ.id).addClass('current');
			moveThis('p-quad',TFQ.end); // move the div in the correct direction
		}
	}
	
	
	
	// FUNCTION - slideLeft/slideRight server plans
	$('.nav-inner a').bind('click', function()
	{			
		slideThis($(this).attr('id'));
	});
		
	// FUNCTION - show/hide server types 
	$('#nav a').bind('click', function()
	{
		if ( $(this).attr('class') == 'active' ) { return; } // if tab is active, don't do anything
		
		var activeTab = ( $(this).attr('id') ); // get the id of the link clicked
		if (activeTab.match(/s/gi))
		{
			$('.plan-holder').css('left', serverSPos);
		}

		$('#p-' + lastDivShown).fadeOut(fadeOutSpeed, function() // fade out the old previous target, fade in the new target
		{
			$('#p-' + activeTab).fadeIn(fadeInSpeed);
		});

		// set new lastDivShow value
		lastDivShown = activeTab;

		// turn on matching plan
		
		if ( $(this).attr('class') != 'active' )
		{
			$('#' + lastTabClicked).removeClass();
			$(this).addClass('active');
			lastTabClicked = activeTab;
			return false;
		}
	});
});

jQuery.fn.vjustify=function() {
    var maxHeight=0;
    this.each(function(){
        if (this.offsetHeight>maxHeight) {maxHeight=this.offsetHeight;}
    });
    this.each(function(){
        $(this).height(maxHeight + "px");
        if (this.offsetHeight>maxHeight) {
            $(this).height((maxHeight-(this.offsetHeight-maxHeight))+"px");
        }
    });
};