/* ***********************************************   
** Javascript con funzioni generiche utilizzato **
** nel portale "Lords of Knowledge"             **
**                                              **
** (c) 2003 by Ohmnibus <mail@ohmnibus.net>     **
**                                              **
** Ogni utilizzo del presente file senza previa **
** autorizzazzione dell'autore è da             **
** considerare come atto di pirateria           **
** informatica. Per favore mettere a conoscenza **
** l'autore di eventuali abusi.                 **
************************************************ */   

var sHideGif = "grafica/i_hidmax.gif";
var sShowGif = "grafica/i_hidmin.gif";
var iSlideSpeed = 15;
var oExpanded = undefined;

function doSubmit(oForm, sAction) {
	oForm.Action.value=sAction;
	oForm.submit();
}

/* Shift selected option from a select to another */
function shiftOpt(oSrc, oDest) {
	var iToShift;
	var oToShift;
	if (oSrc.selectedIndex!=-1) {
		iToShift = oSrc.selectedIndex;
		oToShift = oSrc.options[iToShift];
		if (oToShift.value != "") {
			oSrc.options[iToShift] = null;
			oDest.options[oDest.length] = oToShift;
		}
	}
}

/* Fill textbox with select's options values */
function fillLinkList(oSrc, oDest) {
	var sTmp;
	var iCnt;
	sTmp = "";
	for (iCnt=0; iCnt<oSrc.length; iCnt++) {
		if (oSrc.options[iCnt].value != "") {
			sTmp=sTmp + oSrc.options[iCnt].value + ";";
		}
	}
	oDest.value=sTmp;
}

/* Check if is possible to get an element ref from his id */
function canGetElement() {
	return !(!document.getElementById|document.all|document.layers);
}

/* Get an element reference from his id */
function getElement(sElementId) {
	oElement = undefined;
	if (document.getElementById) {
		oElement = document.getElementById(sElementId);
	} else {
		if (document.layers) {
			oElement = document.layers[sElementId];
		} else {
			oElement = document.all.sElementId;
		}
	}
	return oElement;
}

function hideshow(which, img, alt) {
	if (canGetElement()) {
		oWhich = getElement(which);
		oImage = getElement(img);

		window.focus();
		if (oWhich.style.display == "none") {
			//oWhich.style.display = "";
			oImage.src = sShowGif;
			if (alt == undefined) {
				oImage.alt = "Nascondi sottomen&ugrave;";
			} else {
				oImage.alt = "Nascondi " + alt;
			}
			setCookie(which, "show", "/");
			doSlide(oWhich);
		} else {
			//oWhich.style.display = "none";
			oImage.src = sHideGif;
			if (alt == undefined) {
				oImage.alt = "Mostra sottomen&ugrave;";
			} else {
				oImage.alt = "Mostra " + alt;
			}
			setCookie(which, "hide", "/");
			doSlide(oWhich);
		}
	}
}

/* Expand a div and contract other */
function expand(which) {
	if (canGetElement()) {
		oWhich = getElement(which);
		window.focus();
		if (oExpanded != oWhich) {
			if (oExpanded != undefined) {
				doSlide(oExpanded);
			}
			doSlide(oWhich);
			oExpanded = oWhich;
		} else {
			doSlide(oWhich);
			oExpanded = undefined;
		}
	}
}

function setCookie(name, value, path, domain, expires, secure){
	//expire = " expires=Wed, 1 Jan 2020 00:00:00 GMT;";
	document.cookie = name + "=" + escape(value) +
		((expires) ? ";expires=" + expires.toGMTString() : "") +
		((path) ? ";path=" + path : "") + 
		((domain) ? ";domain=" + domain : "") +
		((secure) ? ";secure" : "");	
}

function getCookie(name){
	var dc = document.cookie;
	var prefix = name + "=";
	var begin = dc.indexOf("; " + prefix);
	if (begin == -1) {
		begin = dc.indexOf(prefix);
		if (begin != 0)
			return null;
	} else {
		begin += 2;
	}
	var end = document.cookie.indexOf(";", begin);
	if (end == -1) {
		end = dc.length;
	}
	return unescape(dc.substring(begin + prefix.length, end));
}

function doSlide(obj){
	if(obj.style.display == "none"){
		obj.style.visibility = "hidden";
		obj.style.display = "block";
		iHeight = obj.offsetHeight;
		obj.style.overflow = "hidden";
		obj.style.height="1px";
		obj.style.visibility = "visible";
		pxPerLoop = iHeight/iSlideSpeed;
		slide(obj,1,iHeight,pxPerLoop);
	} else {
		obj.style.overflow = "hidden";
		iHeight = obj.offsetHeight;
		pxPerLoop = -(iHeight/iSlideSpeed);
		slide(obj,iHeight,1,pxPerLoop);
	}
}

function slide(obj,offset,full,stepSize){
	var bOpening;
	bOpening = stepSize > 0;
	if((bOpening && offset < full) || (!bOpening && offset > full)){
		obj.style.height = offset+"px";
		offset=offset+stepSize;
		setTimeout((function(){slide(obj,offset,full,stepSize);}),1);
	} else {
		if (bOpening) {
			obj.style.height = "auto";
		} else {
			obj.style.display = "none";
			obj.style.height = "auto";
		}
	}
}

//var originalLightBoxContent;

function openLightBox(lightBox, darkBackground) {
	var lightObj;
	var darkObj;
	var pageSize;
	if (canGetElement()) {
		pageSize = getPageSize();
		lightObj = getElement(lightBox);
		darkObj = getElement(darkBackground);
		if (lightObj != undefined && darkObj != undefined) {
			darkObj.style.height = pageSize[1] + "px";
			lightObj.style.display='block';
			darkObj.style.display='block';
			//originalLightBoxContent = lightObj.innerHTML;
		}
	}
}

function closeLightBox(lightBox, darkBackground) {
	var lightObj;
	var darkObj;
	if (canGetElement()) {
		lightObj = getElement(lightBox);
		darkObj = getElement(darkBackground);
		if (lightObj != undefined && darkObj != undefined) {
			lightObj.style.display='none';
			darkObj.style.display='none';
			//lightObj.innerHTML = originalLightBoxContent;
			lightObj.innerHTML = "";
		}
	}
}

function getPageSize() {	
	var xScroll, yScroll, windowWidth, windowHeight;
	var htmlElement;
	if (window.innerHeight && window.scrollMaxY) {
		xScroll = document.scrollWidth;
		yScroll = self.innerHeight + self.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else {
		htmlElement = document.getElementsByTagName("html").item(0);
		xScroll = htmlElement.offsetWidth;
		yScroll = htmlElement.offsetHeight;
		xScroll = (xScroll < document.body.offsetWidth) ? document.body.offsetWidth : xScroll;
		yScroll = (yScroll < document.body.offsetHeight) ? document.body.offsetHeight : yScroll;
	}
	if (self.innerHeight) {
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) {
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) {
		htmlElement = document.getElementsByTagName("html").item(0);
		windowWidth = htmlElement.clientWidth;
		windowHeight = htmlElement.clientHeight;
		windowWidth = (windowWidth == 0) ? document.body.clientWidth : windowWidth;
		windowHeight = (windowHeight == 0) ? document.body.clientHeight : windowHeight;
	}
	var pageHeight = (yScroll < windowHeight) ? windowHeight : yScroll;
	var pageWidth = (xScroll < windowWidth) ? windowWidth : xScroll;
	return new Array(pageWidth, pageHeight, windowWidth, windowHeight);
};
