var url = window.location.href;
var sf = "";
if (url.indexOf("server-asp")>-1) sf = "www.aisdp.org/";

window.onload = function() {
	//ajaxInit();
	renderSwfs();
	if (document.getElementById("slideBox")) {
		slideInit("r", 3000, 10, 5, "slideBox", "slide_"); //direction, delay, pixelsToSlide, speed, id_main_box, id_prefix
	}
	if ($("slide")) {
		try {
			slide();
		}
		catch(e) { 
			alert(e.faultCode?e.faultCode:e.message?e.message:e) 
		}
	}
}

curPos = undefined;
origine = undefined;

function slide(curPos, origine) {
	if (document.getElementById("slide")) {
		//var others = document.getElementById("risposte").getElementsByTagName("div");
		var others = document.getElementById("lit").getElementsByTagName("div");
		for (i=0; i<others.length; i++) {
			var other = others[i];
			varPixNext = 132 * i;
			if (origine != "true") {
				if (curPos != "true") {
					document.getElementById(other.id).style.left = ""+varPixNext+"px";
				}
			}
			if (others.length > 1) {
				setTimeout("move('"+other.id+"', '"+others.length+"')", 500);
			}
		}
		curPos = "true";
		setTimeout("slide('"+curPos+"', '"+origine+"')", 2000);
	}
}

function move(div, divs) {
	new Effect.Move(""+ div +"", { x: -132, y: 0, mode: 'relative', duration:1.8 });
	if (document.getElementById(div).style.left.indexOf("-") > -1) {
		//alert(div + " | " + document.getElementById(div).style.left);
		origine = "true";
		pix = 132 * (divs - 1);
		//alert(pix);
		document.getElementById(div).style.left = ""+pix+"px";
	}
}

function renderSwfs() {
	if (document.getElementById("swf_clickus")) {
		var params = {
			wmode: "transparent"
		};
		swfobject.embedSWF("swf/clickus.swf", "swf_clickus", "682", "293", "9.0.0", "swf/expressInstall.swf", {}, params, {});
	}
	/*if (document.getElementById("swf_clickus")) {
		var params = {
			wmode: "transparent"
		};
		var nome = document.getElementById("swf_clickus").innerHTML;
		swfobject.embedSWF("swf/" + nome + ".swf", "swf_clickus", "682", "293", "9.0.0", "swf/expressInstall.swf", {}, params, {});
	}*/
}

function RefreshImage(valImageId) {
	var objImage = document.images[valImageId];
	if (objImage == undefined) {
		return;
	}
	var now = new Date();
	objImage.src = objImage.src.split('?')[0] + '?x=' + now.toUTCString();
}

function resize(Elem, Img) {
	var obj = document.getElementById(Elem);
	var img = document.getElementById(Img);
	obj.style.width = (img.style.width + 4) + "px";
	obj.style.height = (img.style.height + 4) + "px";
	obj.style.left = (getWindowProperty("w")/2 - getElementProperty(Img, "w") /2) + "px";
	obj.style.top = (getWindowProperty("h")/2 - getElementProperty(Img, "h") /2) + "px";
}

function getWindowProperty(which) {
	var value = 0;
	if (document.layers||(document.getElementById&&!document.all)) {
		if (which == "w") {
			value = window.innerWidth;
		} else if (which == "h") {
			value = window.innerHeight;
		}
	} else if (document.all) {
		if (which == "w") {
			value = document.documentElement.clientWidth;
		} else if (which == "h") {
			value = document.documentElement.clientHeight;
		}
	}
	return value;
}

/*if(document.layers||(document.getElementById&&!document.all)){
   browseWidth=window.outerWidth;
   browseHeight=window.outerHeight;
}else if(document.all){
   browseWidth=document.body.clientWidth;
   browseHeight=document.body.clientHeight;
}*/


function getElementProperty(Elem, which) {
	var value = 0;
	if (document.getElementById) {
		var elem = document.getElementById(Elem);
	} else if (document.all) {
		var elem = document.all[Elem];
	}
	if (which == "w") {
		value = elem.offsetWidth;
	} else if (which == "h") {
		value = elem.offsetHeight;
	}
	return value;
}

function showDiv(id_objs, evento) {
	//alert(id_objs);
	var id_obj_array  = id_objs.split(", ");
	for (n=0; n<id_obj_array.length;n++) {
		id_obj = id_obj_array[n];
		if (id_obj != "") {
			obj = document.getElementById(id_obj);
			obj.style.display = (obj.style.display == "block" ? "none" : "block");
		}
	}
	if (evento != "") {
		$("attivita_container").innerHTML = "<b>"+evento+"</b><input readonly='readonly' type='hidden' name='attivita' id='attivita' value='"+evento+"' />";	
	}
	if (obj.style.display == "block") {
		$("form_info").style.display = "block";
		$("form_info").reset();
		$("form_bg_middle").innerHTML = "<div id='ajax_response'></div><div id='nota_bene'><em>*NB: tutti i campi obbligatori, tranne il campo "+String.fromCharCode(34)+"messaggio"+String.fromCharCode(34)+"</em></div>"
	}
	RefreshImage('imgCaptcha');
}

function ajax_form(form_id, action, update_id, redirect) {
	//$(update_id).update("<img id='loader' src='/"+sf+"img/ajax-loader.gif' />");
	//alert(form_id + "," + action + "," + update_id)
	$(form_id).action = action;
	$(form_id).getMyBody=function(){
		var objIn=this.serialize(true);
		var valOut="";
		  for (var s in objIn){
				
valOut+=(valOut==""?"":"&")+s+"="+escape(objIn[s]);
		  }     
		return(valOut);
		};
	
	$(form_id).request({
       	onFailure: function() {alert(form_id + "," + action) },
		encoding:'iso-8859-1',
		postBody:$(form_id).getMyBody(),
        onSuccess: function(t) {
			var resp = t.responseText;
            $(update_id).update(resp);
			var num1 = resp.indexOf("goto[");
			if (redirect || num1 > -1 ) {
				//alert(redirect + "|" + t.responseText.indexOf("goto["))
				var redirect_str = "";
				if (redirect) redirect_str = redirect;
				if (num1 > -1) {
					var num2 = resp.indexOf("]");
					var redirect_str = resp.substr(num1 + 5, num2 - num1 - 5);
				}
				/*if () {
					redir(redirect_str, 3000);
				}*/
			}
			//alert($(update_id).innerHTML);
			if (resp.indexOf('Sono stati riscontrati i seguenti errori:') == -1) {
				$(form_id).style.display = "none";
			}
			if (resp != "") {
				$("nota_bene").style.display = "none";
			}
			if (resp.indexOf('img/ajax-loader.gif') > -1) {
				hideLoader = function() {
					$("loader").style.display = "none";
				}
				showResp = function() {
					$("ok").innerHTML = "Richiesta informazioni inviata con successo";
				}
				setTimeout("hideLoader()", 500);
				setTimeout("showResp()", 600);
			}
        }
    });
	//return false;
}