// JavaScript Document

function ControllaValori()
{
	var form = document.form_contatto;
	var nome = form.nome.value;
	var cognome = form.cognome.value;
	var citta = form.citta.value;
	var tel = form.tel.value;
	var email = form.email.value;
	var msg = "";
	
	if (nome == "")
		msg += " nome";
	
	if (cognome == "") 
	{
		if (msg != "") msg += ",";
		msg += " cognome";
	}
	
	if (citta == "")
	{
		if (msg != "") msg += ",";
		msg += " citt&agrave;";
	}
	
	if (tel == "")
	{
		if (msg != "") msg += ",";
		msg += " telefono";
	}
	
	if (email == "")
	{
		if (msg != "") msg += ",";
		msg += " email";
	}
	
	if (msg != "")
	{
		alert("Per favore inserire:" + msg + ". Grazie.");
		return false;
	}
}

/**	 FUNZIONI PER IL DEBUG  **/

function log(testo) {
	if (debug.innerText != "")
		debug.innerText = debug.innerText + ";    ";
	debug.innerText = debug.innerText + testo;
}

function addEvent(el, eve, f)
{
	if (el.addEventListener) {
		return el.addEventListener (eve,f,false);
	} else if (el.attachEvent) {
		return el.attachEvent ('on'+eve,f);
	} else {
		if (eve == 'mouseover')
			return el.onmouseover = f;
		else if (eve == 'mouseout')
			return el.onmouseout = f;
	}
}

/**  FUNZIONI PER LA GESTIONE DEGLI EVENTI DEL MAUSE SUL MENU'  **/

function creaEventiMouse(targetId)
{
	var target = document.getElementById(targetId);
	addEvent(target, 'mouseover', entra);
	addEvent(target, 'mouseout', esci);
}

function entra() 
{
	subMenu = document.getElementById('menu-prodotti');
	subMenu.style.display = 'block';
}

function esci() 
{
	subMenu = document.getElementById('menu-prodotti');
	subMenu.style.display = 'none';
}

/**  FUNZIONI PER IL FADE DELL'IMMAGINE  **/

function opacity(id, opacStart, opacEnd, millisec) {
	debug = document.getElementById('debug');
	log("call opacity");
	log(document.getElementById(id).src);
	//speed for each frame
	var speed = Math.round(millisec / 100);
	var timer = 0;

	//determine the direction for the blending, if start and end are the same nothing happens
	if(opacStart > opacEnd) {
		for(i = opacStart; i >= opacEnd; i--) {
			setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
			timer++;
		}
	} else if(opacStart < opacEnd) {
		for(i = opacStart; i <= opacEnd; i++)
			{
			setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
			timer++;
		}
	}
}

//change the opacity for different browsers
function changeOpac(opacity, id) {
	var object = document.getElementById(id).style; 
	object.opacity = (opacity / 100);
	object.MozOpacity = (opacity / 100);
	object.KhtmlOpacity = (opacity / 100);
	object.filter = "alpha(opacity=" + opacity + ")";
}

function shiftOpacity(id, millisec) {
	//if an element is invisible, make it visible, else make it ivisible
	if(document.getElementById(id).style.opacity == 0) {
		opacity(id, 0, 100, millisec);
	} else {
		opacity(id, 100, 0, millisec);
	}
}

function blendimage(divid, imageid, imagefile, millisec) {
	var speed = Math.round(millisec / 100);
	var timer = 0;
	
	//set the current image as background
	//document.getElementById(divid).style.backgroundImage = "url(" + document.getElementById(imageid).src + ")";
	
	//make image transparent
	//changeOpac(0, imageid);
	
	//make new image
	document.getElementById(imageid).src = imagefile;

	//fade in image
	for(i = 0; i <= 100; i++) {
		setTimeout("changeOpac(" + i + ",'" + imageid + "')",(timer * speed));
		timer++;
	}
}

function blendimage_originale(divid, imageid, imagefile, millisec) {
	var speed = Math.round(millisec / 100);
	var timer = 0;
	
	//set the current image as background
	document.getElementById(divid).style.backgroundImage = "url(" + document.getElementById(imageid).src + ")";
	
	//make image transparent
	changeOpac(0, imageid);
	
	//make new image
	document.getElementById(imageid).src = imagefile;

	//fade in image
	for(i = 0; i <= 100; i++) {
		setTimeout("changeOpac(" + i + ",'" + imageid + "')",(timer * speed));
		timer++;
	}
}

function currentOpac(id, opacEnd, millisec) {
	//standard opacity is 100
	var currentOpac = 100;
	
	//if the element has an opacity set, get it
	if(document.getElementById(id).style.opacity < 100) {
		currentOpac = document.getElementById(id).style.opacity * 100;
	}

	//call for the function that changes the opacity
	opacity(id, currentOpac, opacEnd, millisec)
}
