//author:yohsii
//simple horizontal scroll solution.
//dependencies: jquery core and effects/animate; can edit 1 line of code to remove jquery dependency but it wont look as smooth if the panels move without animation.
//TODO - make left scroll more tolerant to border settings/offset
// params - instance name, container id, panel type (i.e. div), wrap(bool), speed(ms), poll delay(ms)
//just bind startl/startr to onmouseover and stops to onmouseout
function HScroll(iname,container,paneltype,wrap,speed,delay)
{
	this.t;
	this.d=delay;
	this.l=false;
	this.iname=iname;
	this.container=container;
	this.paneltype=paneltype;
	this.wrap=wrap;
	this.speed=speed;
	this.loff;
	this.e=document.getElementById(this.container);
	this.ee=this.e.getElementsByTagName(this.paneltype);
	HScroll.prototype.refresh=function()
	{
		this.ee=this.e.getElementsByTagName(this.paneltype);
	}
	HScroll.prototype.changespeed=function(speed)
	{
		this.speed=speed;
	}
	HScroll.prototype.startr=function()
	{
		this.sr();
		this.t=setInterval(this.iname+".sr()",this.d);
	}
	HScroll.prototype.startl=function()
	{
		this.sl();
		this.t=setInterval(this.iname+".sl()",this.d);
	}
	HScroll.prototype.stops=function()
	{
		clearInterval(this.t);	
	}
	HScroll.prototype.sl=function()
	{
		if(this.l) return false;
		this.l=true;
		var ind=-1;
		for(var i=0;i<this.ee.length;i++)
		{
			if(this.ee[i].offsetLeft<=this.e.offsetLeft+this.e.scrollLeft+1
				  &&
				this.ee[i].offsetLeft+this.ee[i].offsetWidth>=this.e.offsetLeft+this.e.scrollLeft+1  
				  ){ind=i;break;}
		}
		var offset=0;
		if(ind>0)
		{
			offset=this.ee[ind-1].offsetLeft-(this.e.offsetLeft+1);
		}
		if(this.wrap&&this.loff==0)
		{
			offset=this.ee[this.ee.length-1].offsetLeft;
		}
		var that=this;
		$("#"+that.container).animate({scrollLeft:offset},that.speed,function(){that.l=false});
		this.loff=offset;
	}
	HScroll.prototype.sr=function()
	{
		if(this.l) return false;
		this.l=true;
		var ind=-1;
		for(var i=0;i<this.ee.length;i++)
		{
			if(this.ee[i].offsetLeft==this.e.offsetLeft+this.e.scrollLeft+1){ind=i;break;}
		}
		var offset=0;
		if(ind<this.ee.length-1&&ind>-1)
		{
			offset=(this.ee[ind+1].offsetLeft-this.ee[ind].offsetLeft)+this.e.scrollLeft;
		}
		if(offset==0&&!this.wrap){this.l=false;return false;}
		var that=this;
		$("#"+that.container).animate({scrollLeft:offset},that.speed,function(){that.l=false});
	}
}