/* centra i pop up sullo schermo */
function centerPopX(popW){ if(!popW) popW=500; return ((screen.availWidth - popW)/2);  }
function centerPopY(popH){ if(!popH) popH=270; return ((screen.availHeight - popH)/2); }

function goBack()
{
	history.back();
	return false;
}

function imgPopPreview(imgId)
{
	winBig = window.open(PATH2ROOT + 'dynamic/imgview.php?id=' + imgId + '&' + SID,'_blank','width=100,height=100,screenX=50,left=50,screenY=50,top=50,resizable=1');
	winBig.focus();
}


function resizeImageProportional(w,h,mw,mh)
{
	// trova il lato più grande
	whR = w - h;

	if(whR >= 0)
	{
		if(mw && (w - mw > 0)) { h = h*mw/w; w = mw; }
		if(mh && (h - mh > 0)) { w = w*mh/h; h = mh; }

	}
	else
	{
		if(mh && (h - mh > 0)) { w = w*mh/h; h = mh; }
		if(mw && (w - mw > 0)) { h = h*mw/w; w = mw; }
	}

	// prepara l'array con i due valori da restituire
	var dim = new Array();
	dim[0] = Math.round(w);  // larghezza
	dim[1] = Math.round(h);  // altezza
	return dim;
}



function TMdd1(ogg,e)
{

	// trova l'elemento da cui è partito l'evento
	if(e.srcElement) tt = e.srcElement; //ie
	else tt = e.target; // dom

	// non effettua le modifiche se è stato cliccato il link, ma solo la riga con la freccietta
	if(tt.nodeName != 'A')
	{
		// scambia classi a voci con sottomenu
		topOggClassi = ogg.parentNode.className.split(' ');

		classNotFound = true;
		var i = 0;
		do
		{
			if(topOggClassi[i] == 'subOp')
			{
				topOggClassi[i] = 'subCl';
				classNotFound = false;
			}
			else if(topOggClassi[i] == 'subCl')
			{
				topOggClassi[i] = 'subOp';
				classNotFound = false;
			}
		}while(classNotFound && ++i < topOggClassi.length)
		ogg.parentNode.className = topOggClassi.join(' ');
	}
}

/*
	Tree Menu Drop Down
	argomenti:
	var topOgg = oggetto che riceve l'onclick
	var e = oggetto event

	Da inserire nell'onclick della riga con sottomenu
*/

function TMdd(topOgg,e,oggId)
{

	// trova l'elemento da cui è partito l'evento
	if(e.srcElement) tt = e.srcElement; //ie
	else tt = e.target; // dom

	// non effettua le modifiche se è stato cliccato il link, ma solo la riga con la freccietta
	if(tt.nodeName == 'A' || tt.nodeName == 'DIV' || (tt.nodeName == '#text' && tt.parentNode.nodeName == 'DIV') || (tt.nodeName == 'IMG' && tt.parentNode.nodeName == 'DIV'))
	{
		//closeAllSiblings(topOgg);

		// scambia classi a voci con sottomenu
		topOggClassi = topOgg.className.split(' ');

		classNotFound = true;
		var i = 0;
		do
		{
			if(topOggClassi[i] == 'TMOp')
			{
				topOggClassi[i] = 'TMCl';
				classNotFound = false;
				if(tt.nodeName == 'IMG')
				{
					re = /minus\.gif/g;
					tt.src = tt.src.replace(re,'plus.gif');
				}
				//updateTMMenuCookie(oggId,'C')
			}
			else if(topOggClassi[i] == 'TMCl')
			{
				topOggClassi[i] = 'TMOp';
				classNotFound = false;
				if(tt.nodeName == 'IMG')
				{
					re = /plus\.gif/g;
					tt.src = tt.src.replace(re,'minus.gif');
				}
				//updateTMMenuCookie(oggId,'O')
			}
		}while(classNotFound && ++i < topOggClassi.length)
		topOgg.className = topOggClassi.join(' ');


		/* cerca il submenu relativo alla riga cliccata e lo apre/chiude */
		currSibling = topOgg.nextSibling;
		siblingNotFound = true;
		while(siblingNotFound && currSibling != null)
		{
			if(currSibling.className != null)
			{
				siblingNotFound = false;
			}
			else
				currSibling = currSibling.nextSibling;
		}
		if(currSibling != null)
		{
			if(currSibling.style.display != 'none') currSibling.style.display = 'none';
			else currSibling.style.display = 'block';
		}
	}
}

function closeAllSiblings(currOgg)
{
	pn = currOgg.parentNode;

	currChild = null;
	do
	{
		if(currChild==null) currChild = pn.firstChild
		else currChild = currChild.nextSibling;

		if(currChild!=null)
		{
			if(currChild.className=='TMrootBox1')
			{
				if(currChild!=currOgg.nextSibling)
				{
					currChild.style.display = 'none';
					a = currChild.previousSibling;
					cn = a.className;
					cn.replace(/TMOp/,'TMCl');
					a.className = cn;
				}
			}
		}

	} while(currChild != null)
}

function updateTMMenuCookie(cId,stato)
{
	var cVal = getCookie('TMId');
	if(cVal!='')
	{
		addValue = true;
		coppie = cVal.split('.');
		for(i in coppie)
		{
			valori = coppie[i].split(':');
			if(valori[0] == cId)
			{
				coppie[i] = cId + ':' + stato;
				addValue = false;
				break;
			}
		}
		if(addValue) coppie[coppie.length] = cId + ':' + stato;
		cVal = coppie.join('.');

	}
	else cVal = cId + ':' + stato;
	cExp = new Date();
	cExp.setFullYear(cExp.getFullYear() + 1);
	setCookie('TMId', cVal, cExp, '/');
}

function setCookie(cookieName, cookieValue, expires, path)
{
	document.cookie =
		escape(cookieName) + '=' + escape(cookieValue)
		+ (expires ? '; expires=' + expires.toGMTString() : '')
		+ (path ? '; path=' + path : '');
}

function getCookie(cookieName)
{
	var cookieValue = '';
	var posName = document.cookie.indexOf(escape(cookieName) + '=');
	if (posName != -1) {
		var posValue = posName + (escape(cookieName) + '=').length;
		var endPos = document.cookie.indexOf(';', posValue);
		if (endPos != -1) cookieValue = unescape(document.cookie.substring(posValue, endPos));
		else cookieValue = unescape(document.cookie.substring(posValue));
	}
	return (cookieValue);
}


var RotationImage = function()
{
	this.imgList=new Array();
	this.imgLoaded=new Array();
	this.linkList=new Array();
	this.rotationTimeout=5000;
	this.lastImgIndex=0;
	this.nextImgIndex=1;
	this.imgUrl=PATH2ROOT+'common/images/rotationImages/';
	this.useImgUrl=false;
	this.imgObj=null;
	this.imgId=null;

	this.rotationInterval=null;
}

RotationImage.prototype.preload = function(pos)
{
	this.imgLoaded[pos] = new Image();
	if(this.useImgUrl) this.imgLoaded[pos].src = this.imgList[pos];
	else this.imgLoaded[pos].src = this.imgUrl+this.imgList[pos];
}

RotationImage.prototype.getNext = function()
{
	if(this.imgList.length>1)
	{

		if((this.imgList.length-1) == this.lastImgIndex)
		{
			this.lastImgIndex = 0;
			this.nextImgIndex = 1;
		}
		else
		{
			this.lastImgIndex++;
			if((this.imgList.length-1) == this.nextImgIndex) this.nextImgIndex = 0;
			else this.nextImgIndex++;
		}

		if(this.imgLoaded[this.nextImgIndex] === null)
		{
			this.preload(this.nextImgIndex);
		}

		if(this.useImgUrl) this.show(this.imgList[this.lastImgIndex]);
		else this.show(this.imgUrl+this.imgList[this.lastImgIndex]);
	}
}

RotationImage.prototype.show = function(url,linkData)
{
	if(this.imgObj==null)
	{
		this.imgObj = document.getElementById(this.imgId);
	}
	if(url)
	{
		if(this.linkList.length > 0)
		{
			linkData = this.linkList[this.lastImgIndex];
			imgLink = this.imgObj.parentNode;
			if(linkData[0]!='')
			{
				if(imgLink.nodeName!='A')
				{
					newImgLink = imgLink.insertBefore(document.createElement('A'),this.imgObj);
					newImgLink.appendChild(this.imgObj);
					imgLink = newImgLink;
				}
				imgLink.href = linkData[0];
				if(linkData[1]!='') imgLink.target = linkData[1];
			}
			else
			{
				if(imgLink.nodeName=='A')
				{
					pNode = imgLink.parentNode;
					pNode.appendChild(this.imgObj);
					pNode.removeChild(imgLink);
				}
			}
		}
		//this.imgObj.src = url;


		if(this.imgObj.filters)
		{
			try{
				this.imgObj.style.filter= 'Fade(overlap=1.0,duration=2.0)';
				this.imgObj.filters(0).Apply();
				this.imgObj.src = url;
				this.imgObj.filters(0).Play();
			}
			catch(e)
			{
				this.imgObj.style.filter= 'BlendTrans(duration=2)';
				this.imgObj.filters(0).Apply();
				this.imgObj.src = url;
				this.imgObj.filters(0).Play();
			}
		}
		else
		{
			for(fd=100,ft=10; fd>=0; fd -= 2, ft+=10)
			{
				window.setTimeout("__rc.rList['" + this.imgId + "'].imgObj.style.opacity = "+(fd/100)+";",ft);
			}
			window.setTimeout("__rc.rList['" + this.imgId + "'].imgObj.src = '" +url+"';",(ft+10));
			for(fd=0,ft+=20; fd<=100; fd += 2, ft+=10)
			{
				window.setTimeout("__rc.rList['" + this.imgId + "'].imgObj.style.opacity = "+(fd/100)+";",ft);
			}
		}

	}
}




var RotationContainer =
{
	rList:new Array()
}

RotationContainer.addItem = function(ogg)
{
	//console.trace();
	this.rList[ogg.imgId] = ogg;
}

RotationContainer.init = function()
{
	//console.trace();
	window.__rc = this;
	for(i in this.rList)
	{
		this.handleRotation("start",this.rList[i].imgId);
		//myAddEventListener(document.getElementById(this.rList[i].imgId),'mouseover',new Function(" __rc.handleRotation('stop','" + this.rList[i].imgId +"')"));
		//myAddEventListener(document.getElementById(this.rList[i].imgId),'mouseout',new Function(" __rc.handleRotation('continue','" + this.rList[i].imgId +"')"));
		//document.getElementById(this.rList[i].imgId).onmouseover = new Function(" __rc.handleRotation('stop','" + this.rList[i].imgId +"')");
		//document.getElementById(this.rList[i].imgId).onmouseout = new Function(" __rc.handleRotation('continue','" + this.rList[i].imgId +"')");
	}
}

RotationContainer.handleRotation = function(comando,oggId)
{
	//console.trace();
	ogg = this.rList[oggId];
	if(comando=="stop")
	{
		window.clearInterval(ogg.rotationInterval);
	}
	else if(comando=="continue")
	{
		if(ogg.imgList.length > 1)
		{
			//ogg.preload(ogg.nextImgIndex);
			//window.setTimeout(" __rc.rList['" + oggId + "'].getNext()",300);
			ogg.preload(ogg.nextImgIndex);
			ogg.rotationInterval = window.setInterval(" __rc.rList['" + oggId + "'].getNext()",ogg.rotationTimeout);
		}
	}
	else if(comando=="start")
	{
		if(ogg.imgList.length > 1)
		{
			ogg.preload(ogg.nextImgIndex);

			ogg.rotationInterval = window.setInterval(" __rc.rList['" + oggId + "'].getNext()",ogg.rotationTimeout);
		}
	}
}

var rc = RotationContainer;
myAddEventListener(window,"load",new Function('rc.init();'));


function clearRightMouseButton(e)
{
	if(!e) e = window.event;
	switch(e.type)
	{
		case 'mousedown':
			if(e.which == 1) return true;
		case 'contextmenu':
		case 'selectstart':
		case 'select':
		case 'dragstart':
			return clearEvent(e);
			break;
	}
}

function clearEvent(e)
{
	e.cancelBubble = true;
	e.returnValue = false;
	return false;
}

function myAddEventListener(obj,ev,cbFunc)
{
	if(obj!==null)
	{
		if(obj.attachEvent)
		{
			obj.attachEvent('on'+ev,cbFunc);
		}
		else if(obj.addEventListener)
		{
			obj.addEventListener(ev,cbFunc,false);
		}
		else
		{
			obj['on'+ev] = cbFunc;
		}
	}
}


function myRemoveEventListener(obj,ev,cbFunc)
{
	if(obj!==null)
	{
		if(obj.detachEvent)
		{
			obj.detachEvent('on'+ev,cbFunc);
		}
		else if(obj.removeEventListener)
		{
			obj.removeEventListener(ev,cbFunc,false);
		}
		else
		{
			obj['on'+ev] = null;
		}
	}
}



function Debugger()
{
	this.testo = '';
	this.newEl = null;

	this.log = function(a)
	{
		this.append(a);
		/*
		if(arguments.length>1)
		{
			for(argNum = 1; argNum < arguments<length; argNum++)
			{
				this.append(typeof(arguments[argNum]).toString());
			}
		}*/
		this.show();
	}
	this.info = function(a)
	{

	}
	this.warn = function(a)
	{

	}
	this.debug = function(a)
	{

	}
	this.error = function(a)
	{

	}

	this.trace = function()
	{
		this.append('');
	}

	this.append = function(str)
	{
		this.testo += '<br>'+str;
	}

	this.show = function()
	{
		if(this.newEl==null)
		{
			this.newEl = document.createElement('DIV');
			with(this.newEl.style)
			{
				position = 'absolute';
				bottom = '0px';
				clear = 'both';
				backgroundColor = '#EEEEEE';
				padding = '5px';
				width = '100%';
				height = '80px';
				overflow = 'auto';
			}
			document.body.appendChild(this.newEl);
		}
		this.newEl.innerHTML = this.testo;
	}
}


//if(typeof(console)=='undefined') console=new Debugger();