
/*
	API per la ricerca degli oggetti sulla pagina compatibili NS4
*/

function getObj(name)
{
  if (document.getElementById)
  {
  
  	if(this.obj = document.getElementById(name))
  	{
		this.style = document.getElementById(name).style;
	}
		
  }
  else if (document.all)
  {
	if(this.obj = document.all[name])
		this.style = document.all[name].style;
  }
  else if (document.layers)
  {
	if(this.obj = getObjNN4(document,name))
		this.style = this.obj;
  }
}

function getObjNN4(obj,name)
{
	var x = obj.layers;
	var foundLayer;
	for (var i=0;i<x.length;i++)
	{
		if (x[i].id == name)
		 	foundLayer = x[i];
		else if (x[i].layers.length)
			var tmp = getObjNN4(x[i],name);
		if (tmp) foundLayer = tmp;
	}
	return foundLayer;
}

function findPosX(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}

function findPosY(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}


// calcola il prossimo spostamento secondo una accellerazione decrescente sulla lunghezza width
function computeNextOffsetLeft(width,cur)
{
	// utilizza la parabola y = x^2, 
	// + calcolo la radice quadrata di width
	cur_x = Math.sqrt(cur);
	cur_x--;
	//max_x = Math.sqrt(width);
	if(cur_x >=0)
	{

	   if(cur_x <= 16)
	   {
		   return cur_x--;
	   }
	   return cur_x * cur_x -2;

	}
	return 0;
}

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

function SlideBar(name,sint,waittime)
{
	this.idname = name;
	this.sint = sint;
	this.waittime = waittime;
	this.slideb = new getObj(name);
	this.numpains = 0;
	this.numslide = 0;
	this.painlist = new Object();
	this.painlistattr = new Object();
	
	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("slidepain") >=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.posx = pixelbefore;
			oa.prevpainobj = prevpainobj;
			oa.name = ch;
			
			o.style.left = pixelbefore + "px";

			if(painc == 0)
				o.style.top = "0px";
			else
				o.style.top = ( - painc*o.clientHeight) + "px";
			
			pixelbefore += 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.shiftPainLeft = function(painobj,lpx)
	{
		oa = this.painlistattr[painobj.id];
		oa.posx -= lpx;
		painobj.style.left = oa.posx + "px";
		
	};
	
	this.shiftAndPushRight = 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.repositionFirstPain = function()
	{
		// muove il primo pain alla posizione dell''ultimo
		// calcolando lo spazion occupato dai successivi
		var nexto = this.painlistattr[this.firstVisiblePain.id].nextpainobj;
		var totpx = 0;
		
		
		while(nexto)
		{
			totpx += nexto.clientWidth;
			nexto = this.painlistattr[nexto.id].nextpainobj;
		}
		
	
		var foa = this.painlistattr[this.firstVisiblePain.id];
	
		foa.posx = totpx;
		this.firstVisiblePain.style.left = totpx + "px";
	};

}
// oggetto da spostare a sinistra di lpx








function startSlide(obj,old,cur)
{

	

	
	old = cur;
	cur = computeNextOffsetLeft(obj.firstVisiblePain.clientWidth,cur);

	obj.shiftPainLeft(obj.firstVisiblePain,old-cur);

	if(cur>0)	
	{
		to = obj;
		_old = old;
		_cur = cur;
		setTimeout("startSlide( to , _old , _cur )",obj.sint);
	}
	else
	{
		var delay = 1;
		
		if(obj.numslide && (obj.numslide % obj.numpains) == 0)
		{
			delay = obj.waittime;
			obj.repositionFirstPain();
			obj.numslide = 0;
		}
		else
			obj.numslide++;
			
			
		obj.shiftAndPushRight();
		
		w = obj.firstVisiblePain.clientWidth;
		to = obj;
		setTimeout("startSlide(to,w,w)",delay);
			
	}
}



function writeLayer(text,id)
{
	var x = new getObj(id);
	x = x.obj;

	if (document.getElementById)
	{
		x.innerHTML = '';
		x.innerHTML = text;
	}
	else if (document.all)
	{
		x.innerHTML = text;
	}
	else if (document.layers)
	{
		text2 = '<P CLASS="testclass">' + text + '</P>';
		x.document.open();
		x.document.write(text2);
		x.document.close();
	}
}


function getEventTarget(e)
{
	var targ;
	if (!e) var e = window.event;
	
	if (e.target) targ = e.target;
	else if (e.srcElement) targ = e.srcElement;
	
	return targ;
}


