/*Copyright Joseph P. Neathawk Productions 2010 - All Rights Reserved  */

var tabSlider = new function()
{
	var me = this;
	var name = 'tabSlider';	
	var initialized = false;
	
	var current = 0;
	var total = 0;
	var direction = 0;
	var moving = false;
	var moveTimer = new Date().getTime();
	var moveDuration = 1000 * 0.6;
	var element = new Array();
	
	//typically set to a little more than the actual width in order to leave a margin between the tabs
	var width = 900;
	
	var setWidth = function()
	{
		//this function isnt needed, but is here for future proofing
		var widthSetterElement = document.getElementById(name + 'Wrapper');
		if(widthSetterElement)
		{
			var newWidth = Number(widthSetterElement.offsetWidth) + 25;
			if(newWidth > width)
			{
				width = newWidth;
			}
		}
	}
	
	me.initialize = function()
	{	
		if(!initialized)
		{
			setWidth();
			var complete = false;
			var counter = 0;
			while(!complete)
			{
				element[counter] = document.getElementById(name + (counter + 1));				
				if(element[counter])
				{	
					element[counter].style.left = (width * counter) + 'px';
					total = ++counter;
				}
				else
				{
					complete = true;
				}
			}
			initialized = true;
		}
	};
	
	me.previous = function()
	{
		me.jump(current);
	};
	
	me.next = function()
	{
		me.jump(current + 2);
	};
	
	me.jump = function(number)
	{		
		me.initialize();
		if(number > 0 && number <= total)
		{
			if(number < current)
			{
				direction = -1;
			}
			else
			{
				direction = 1;			
			}
			current = number - 1;
			if(current <= 0)
			{	
				current = 0;
			}
			else if(current >= total)
			{	
				current = total - 1;
			}		
			moving = true;
			moveTimer = new Date().getTime();
			me.move();
		}
	};
	
	me.move = function()
	{	
		if(moving)
		{				
			//
			var newTime = new Date().getTime() - moveTimer;
			var x = newTime / moveDuration;
			var y = x * x;
			if(y < 0)
			{
				y = 0;	
			}
			var distance = Number(element[current].style.left.replace("px",""));
			var step = Math.floor(distance - (y * distance));
			
			if(newTime >= moveDuration)
			{
				moving = false;
			}	
			else
			{					
				for(var i = 0; i < total; i++)
				{
					element[i].style.left = ((width * (i - current)) + step) + 'px';
				}
			}	
			setTimeout(name + '.move()',20);
		}
		else
		{			
			moveTimer = new Date().getTime();
			direction = 0;			
			for(var i = 0; i < total; i++)
			{
				element[i].style.left = (width * (i - current)) + 'px';
			}
		}
	};
}
