/*
Developed by Kyle Somogyi
*/
function slide_horizontal(_so)
{
	var me = this;

	me.so = _so;
	me.mc = $(me.so.mainElement);
	me.imc = $(me.so.mainElement).children().not('.buttonPrevious, .buttonNext');
	me.ia = [];
	me.ci = 1;
	me.imc_w = 0;
	me.deferClick = false;

	var tE = me.imc.children(),
		j = tE.length;
	while(j--)
	{
		me.ia.push($(tE[j]).outerWidth());
		me.imc_w += $(tE[j]).outerWidth();

		$(tE[j]).attr('shiftrIndex', j);
	}

	// Manually set styles
	me.mc.css(
	{
		'overflow': 'hidden',
		'position': 'relative'
	});
	me.imc.css(
	{
		'position': 'absolute',
		'top': '0px',
		'left': '0px',
		'width': me.imc_w+'px'
	});
	me.imc.children().css(
	{
		'float': 'left'
	});
}
slide_horizontal.prototype.next = function()
{
	var me = this,
		imc = me.imc,
		ix=0,
		fi=0;
	
	if(me.deferClick != true)
	{
		while(ix<me.so.jumpAmount)
		{
			fi+=me.ia[me.ci-1]
			ix++;
			if(me.ci >= imc.children().length)
				me.ci = 1;
			else
				me.ci++;
		}
		imc.stop(true, true).animate(
		{
			'left': '-='+fi+'px'
		}, function()
		{
			var tE = me.imc.children(),
				j = tE.length,
				tA = [];
			while(j--)
			{
				if(j < me.so.jumpAmount)
				{
					tA.push(tE[j]);
					$(tE[j]).remove();
				}
			}
			$(tA.reverse()).appendTo(imc);

			imc.css('left', '0px');
		});
	}
};
slide_horizontal.prototype.prev = function()
{
	var me = this,
		imc = me.imc,
		ix=0,
		fi=0;
	
	if(me.deferClick != true)
	{
		while(ix<me.so.jumpAmount)
		{
			fi+=me.ia[me.ci-1]
			ix++;
			if(me.ci <= 1)
				me.ci = imc.children().length;
			else
				me.ci--;
		}

		var tE = me.imc.children(),
			j = tE.length,
			tA = [];
		while(j--)
		{
			if(j >= (tE.length - me.so.jumpAmount))
			{
				tA.push(tE[j]);
				$(tE[j]).remove();
			}
		}
		$(tA.reverse()).prependTo(imc);
		
		imc.css('left', '-'+fi+'px');
		imc.stop(true, false).animate(
		{
			'left': '0px'
		});
	}
};
slide_horizontal.prototype.scrollTo = function(sIndex)
{
	var me = this,
		fin = false,
		tE = me.imc.children(),
		mSI = parseInt($(tE[0]).attr('shiftrIndex'))+1,
		tSI = parseInt(sIndex)+1,
		dir = (mSI < tSI) ? 'next' : 'prev';
	
	if($(tE[0]).attr('shiftrIndex') != sIndex)
	{
		switch(dir)
		{
			case 'next':
				me.scrollToNext(tSI - mSI);
				break;
			case 'prev':
				me.scrollToPrev(mSI - tSI);
				break;
		}
	}
};
slide_horizontal.prototype.scrollToPrev = function(jA)
{
	var me = this,
		imc = me.imc,
		ix=0,
		fi=0;
	while(ix<jA)
	{
		fi+=me.ia[me.ci]
		ix++;
		if(me.ci <= 1)
			me.ci = imc.children().length;
		else
			me.ci--;
	}

	var tE = me.imc.children(),
		j = tE.length,
		tA = [];
	while(j--)
	{
		if(j >= (tE.length - jA))
		{
			tA.push(tE[j]);
			$(tE[j]).remove();
		}
	}
	$(tA.reverse()).prependTo(imc);
	
	imc.css('left', '-'+fi+'px');
	imc.stop(true, true).animate(
	{
		'left': '0px'
	});
};
slide_horizontal.prototype.scrollToNext = function(jA)
{
	var me = this,
		imc = me.imc,
		ix=0,
		fi=0;
	while(ix<jA)
	{
		fi+=me.ia[me.ci]
		ix++;
		if(me.ci >= imc.children().length)
			me.ci = 1;
		else
			me.ci++;
	}
	imc.stop(true, true).animate(
	{
		'left': '-='+fi+'px'
	}, function()
	{
		var tE = me.imc.children(),
			j = tE.length,
			tA = [];
		while(j--)
		{
			if(j < jA)
			{
				tA.push(tE[j]);
				$(tE[j]).remove();
			}
		}
		$(tA.reverse()).appendTo(imc);

		imc.css('left', '0px');
	});
};
