/* 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); }


/*
	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 == 'DIV' || (tt.nodeName == '#text' && tt.parentNode.nodeName == 'DIV') || (tt.nodeName == 'IMG' && tt.parentNode.nodeName == 'DIV'))
	{
		// 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');
				}
				updateMenuCookie(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)
		{
			//alert(currSibling.className);
			if(currSibling.className != null && currSibling.className == 'TMsub')
			{
				siblingNotFound = false;
			}
			else
				currSibling = currSibling.nextSibling;
		}
		if(currSibling != null)
		{
			if(currSibling.style.display != 'none') currSibling.style.display = 'none';
			else currSibling.style.display = 'block';
		}
	}
}

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);
}
