/**
 *	costruise un slidebar che fa ruotare i div in esso contenuti
 */


function HorizzontalSlider(name,sint,waittime,dir,sname,superpose)
{
	
	

	
	this.idname = name;
	this.sname = sname;
	this.sint = sint;
	this.waittime = waittime;
	this.dir = dir;
	this.superpose = superpose;
	this.timeoutID = 0;
	this.strid = 0;

	this.protectWaitStart = false;
	
	
	
	
	this.initPosition = function()
	{

		this.painlist = new Object();
		this.painlistattr = new Object();
		this.slideb = new getObj(name);
		this.numpains = 0;
		this.numslide = 0;
		this.firstVisiblePain = null;
		this.lastPain = null;
		var pixelbefore = 0;
		var prevpainobj = null;
		var painc = 0;

		chlist = this.slideb.obj.childNodes;
		// conta quanti div sono stati posizionati
		for (ch in chlist)
		{

			if((chlist[ch].tagName == "DIV" || chlist[ch].tagName == "div") &&
				chlist[ch].id &&
				chlist[ch].id.indexOf(this.sname) >=0)
			{
				this.numpains++;
				var o = this.painlist[chlist[ch].id] = chlist[ch];

				this.lastPain = o;
				
				this.painlistattr[chlist[ch].id] = new Object();
				var oa = this.painlistattr[chlist[ch].id];
				
				if(painc==0)
					this.firstVisiblePain = o;
					
				// posiziona i div uno dopo l''altro
				
				oa.prevpainobj = prevpainobj;
				oa.name = chlist[ch].id;
				oa.pos = painc;

				
					
				o.style.top = ( - (o.clientHeight*painc)) + "px";
				
				if(this.dir == "right")
				{
					o.style.left = ( - o.clientWidth ) + "px";
					oa.curroffset = -o.clientWidth;
				}
				else
				{

					if(painc==0)
					{
						o.style.left =  "0px";
						oa.curroffset = 0;
					}
					else
					{
						o.style.left =  ( o.clientWidth  ) + "px";
						oa.curroffset = o.clientWidth;
					}
					
					
				}
						
				
				
				
				// collega l'oggetto precedente a questo
				if(prevpainobj)
				{
					prevoa = this.painlistattr[prevpainobj.id];
					prevoa.nextpainobj = o;
				}
					
				
				prevpainobj = o;
				painc++;
			}
		}
		
		if(prevpainobj)
		{
			prevoa = this.painlistattr[prevpainobj.id];
			prevoa.nextpainobj = null;
		}
	}

	this.enablePlay = function()
	{
		this._play = true;
	}

	this.enablePlay();
	this.initPosition();

	/**
	 * fa la rotazione del capo/coda lista
	 */
	this.rotate = function ()
	{
		
		var lastoa = this.painlistattr[this.lastPain.id];
		// faccio puntare lastPain al primo che ora ? ultimo
		lastoa.nextpainobj = this.firstVisiblePain;
		
		var lastold = this.lastPain;
		this.lastPain = this.firstVisiblePain;
		
		lastoa = this.painlistattr[this.lastPain.id];
		this.firstVisiblePain = lastoa.nextpainobj;
		lastoa.nextpainobj = null;
		lastoa.prexpainobj = lastold;

		firstoa = this.painlistattr[this.firstVisiblePain.id];
		firstoa.prevpainobj = null;
		
	};


	
	this.setAllText = function(text)
	{
		var o = this.firstVisiblePain;
		var no = o;
		var bfirst = true;
		var i = 0;
		
		while(i < this.numpains)
		{
		
			writeLayer(text,this.painlistattr[no.id].name);
			
			no = this.painlistattr[no.id].nextpainobj;		
			
			bfirst = false;	
			i++;
		}
	}
	
	// ottiene l'offset di riposizionamento
	this.getPixelOffset = function (obj)
	{
		var o = obj; 
		
		var firstoa = this.painlistattr[o.id];
		
				
		if(this.dir == "right")
			return ( - (o.clientWidth ));
		else
		{
			return ( o.clientWidth ) ;
		}
	};
	
	
	
	this.repositionLast = function ()
	{
		this.lastPain.style.left = this.getPixelOffset(this.lastPain) + "px";		
		this.painlistattr[this.lastPain.id].curroffset = this.getPixelOffset(this.lastPain);
	}
	
	this.repositionFirst = function ()
	{
		this.firstVisiblePain.style.left = this.getPixelOffset(this.firstVisiblePain) + "px";
		this.painlistattr[this.firstVisiblePain.id].curroffset = this.getPixelOffset(this.firstVisiblePain);
	}
	
	
	this.stopPlay = function()
	{
		this._play = false;
	}
	


	this.startSlide = function(obj,slideoffset)
	{



		var _w = this.firstVisiblePain.clientWidth;
		
		var _obj = obj;
		
		// se == 0 allora posso 
		//	+ 	riposizionare il primo
		//	+	ruotare la lista e il primo diventa l'ultimo
		// 	+ 	scrollare il primo fino a quando non ? posizionato correttamente
		if(slideoffset == _w)
		{
			


			this.rotate();
			
			this.repositionLast();
			
			var _slideoffset = 0;

			this.strid++;

			var glob_slideoffset_name = "_sliderHoriz_slideoffset_" + this.idname + "_" + this.strid;
			
			eval( glob_slideoffset_name + " = _slideoffset;");
			
			var varname = "_sliderHoriz_" + this.idname + "_" + this.strid;
			
			eval( varname + " = _obj;");
			
			

			if(this._play)
			{
				this.timeoutID =  setTimeout( varname + ".startSlide(" + varname + "," + glob_slideoffset_name + ")",this.waittime);
			}
			else
			{
				clearTimeout(this.timeoutID);
				return true;
			
			}
		}
		else
		{
		
		
			var _w = this.firstVisiblePain.clientWidth;
			
			var _slideoffset = slideoffset;
			
			this.strid++;
					
			this.firstVisiblePain.style.left =  this.painlistattr[this.firstVisiblePain.id].curroffset-- + "px";
			
			_no = this.painlistattr[this.firstVisiblePain.id].nextpainobj;
			
			_no.style.left = this.painlistattr[_no.id].curroffset-- + "px";

			_slideoffset++;
			
			var glob_slideoffset_name = "_sliderHoriz_slideoffset_" + this.idname + "_" + this.strid;
			
			eval( glob_slideoffset_name + " = _slideoffset;");
			
			var varname = "_sliderHoriz_" + this.idname + "_" + this.strid;
			
			eval( varname + " = _obj;");

			if(this._play)
			{
				
				this.timeoutID = setTimeout( varname + ".startSlide(" + varname + "," + glob_slideoffset_name + ")",this.sint);
			}
			else
			{
			
				clearTimeout(this.timeoutID);
				return true;
			}

			
		}
	
	}


	this.enablePlayWait = function(slideoffset)
	{

		this.enablePlay();
		this.startSlide(this,slideoffset);
		this.protectWaitStart = false;
		
	}

	this.startSlideWait = function(slideoffset,timew)
	{

		if(!this.protectWaitStart)
		{
			this.protectWaitStart = true;

			this.initPosition();

			var _slideoffset = slideoffset;

			var _obj = this;

			this.strid++;

			var glob_slideoffset_name = "_sliderHoriz_slideoffset_sw__" + this.idname ;
			
			eval( glob_slideoffset_name + " = _slideoffset;");
			
			var varname = "_sliderHoriz_sw__" + this.idname ;
			
			eval( varname + " = _obj;");


			setTimeout( varname + ".enablePlayWait(" + glob_slideoffset_name + ")",timew);
		}

	}

}

