var newsTimer = 20000;

var actualOpenImage = -1

var isMAC = (navigator.userAgent.toLowerCase().indexOf("mac") != -1);
var isIE = document.all;
var isNS6 = (!document.all && document.getElementById ? true : false);
var isNS7 = (navigator.userAgent.toLowerCase().indexOf("netscape/7") != -1);

var myImage = new Array()
var myThumb = new Array()

var bigImageW = new Array()
var bigImageH = new Array()
var bigImageD = new Array()


if(isMAC && isIE){
	myImage[0] = document.createElement('IMG');
	myImage[0].name = 'myVoidImage_0';
	myThumb[0] = document.createElement('IMG');
	myThumb[0].name = 'myVoidImage2_0';
}else{
	myImage[0] = new Image();
	myThumb[0] = new Image();
}

myImage[0].onload = setImageDimensions
myImage[0].src = ""
myImage[0].delta = 0.0
myImage[0].w = 0
myImage[0].h = 0

myThumb[0].src = ""
myImage[0].Index = 0
myThumb[0].Index = 0

bigImageW[0] = 0
bigImageH[0] = 0
bigImageD[0] = 0.0


//var nNews = 0
var cNews = -1
//var loopNews = true
var stopNews = false
var newsTimeout;
//var waitForResponse = false
//var objNews = new Object()
//objNews.error = false
//objNews.Testo = ""
//objNews.responseText = ""
//objNews.responseXml = ""

function newsStart(){
	if(newsQty<1){
		return
	}
	$("newsPallini").attr("src","/gui/img/PalliniMobili.gif");
	if(stopNews){
		$("newsPallini").attr("src","/gui/img/PalliniFissi.gif");
		//newsTimeout = setTimeout("newsStart()",newsTimer)
		return
	}
	$("#boxAnticipazioni").fadeOut(1000,setNews);
}

function setNews(){
	if(cNews<0){
		cNews = 0
		showNewsText()
		if(newsQty>1){
			newsTimeout = setTimeout("newsStart()",newsTimer)
		}
		$("newsPallini").attr("src","/gui/img/PalliniFissi.gif");
		return
	}
	cNews += 1
	if(cNews >= newsQty){
		cNews = 0
	}
	showNewsText()

	if(newsQty>1){
		newsTimeout = setTimeout("newsStart()",newsTimer)
	}
	$("newsPallini").attr("src","/gui/img/PalliniFissi.gif");
	
}


function showNewsText(){
	findObj("boxAnticipazioni").innerHTML = newsList[cNews].Data.replace("\u0027","'")
	findObj("newsCounter").innerHTML = (cNews+1) + "/" + newsQty
	$("#boxAnticipazioni").fadeIn(1500);
}

function manualNews(v){
	clearTimeout(newsTimeout)
	if(v=='pa'){		
		if(!stopNews){
			stopNews = true
		}else{
			stopNews = false
			newsStart()
			return
		}
	}else if(v=='p'){
		if(cNews == 0){
			cNews = (newsQty - 2)
		}else{
			cNews -= 2
		}
		$("#boxAnticipazioni").fadeOut(1000,setNews);
	}else if(v=='s'){
		$("#boxAnticipazioni").fadeOut(1000,setNews);
	}
}




function getXmlNodes(xml, xpath){
	if ( document.all )		// IE
		return xml.selectNodes( xpath ) ;
	else					// Gecko
	{
		var aNodeArray = new Array();

		var xPathResult = xml.evaluate( xpath, xml, 
				xml.createNSResolver(xml.documentElement), XPathResult.ORDERED_NODE_ITERATOR_TYPE, null) ;
		
		if ( xPathResult ) 
		{
			var oNode = xPathResult.iterateNext() ;
			while( oNode )
 			{
 				aNodeArray[aNodeArray.length] = oNode ;
 				oNode = xPathResult.iterateNext();
 			}
		} 
		return aNodeArray ;
	}
}



function openFoto(image){
	if(image!=""){
		myWin = window.open(image,"FotoHome","width=150,height=150,left=50,top=50,resizable,scrollbars")
		myWin.focus()
	}
}


function closeImage(i){
	stopImage = true
	document.getElementById("imageBig_"+i).style.width = myThumb[i].width
	document.getElementById("imageBig_"+i).style.height = myThumb[i].height
	document.getElementById("imageBig_"+i).src = myThumb[i].src
	actualOpenImage = -1
}

function openImage(i){
	if(bigImageW[i]==0){return}
	if(actualOpenImage!=-1){
		closeImage(actualOpenImage)
	}
	startW = myThumb[i].width
	startH = myThumb[i].height
	document.getElementById("imageBig_"+i).style.width = startW
	document.getElementById("imageBig_"+i).style.height = startH
	document.getElementById("imageBig_"+i).src = myImage[i].src
	myImage[i].w = startW
	myImage[i].h = startH
	moveImage(i)
}
var stopImage = false

function moveImage(i){
	if(stopImage){
		stopImage = false
		return
	}
	actualOpenImage = i
	pixelW = 4
	timer = 25
	
	el = document.getElementById("imageBig_"+i)
	pixelH = parseInt(pixelW * bigImageD[i])
	
	//document.getElementById("testH").value = pixelH
	//document.getElementById("testW").value = pixelW
	//document.getElementById("delta").value = bigImageD[i]
	//alert("1")
	myImage[i].w = myImage[i].w + pixelW
	myImage[i].h = myImage[i].h + pixelH
	
	if(myImage[i].w < bigImageW[i]){
		el.style.width = myImage[i].w
		el.style.height = myImage[i].h
		setTimeout("moveImage("+i+")",timer)
	}
}

function setImageDimensions(myImage){
	bigImageW[this.Index] = this.width
	bigImageH[this.Index] = this.height
	bigImageD[this.Index] = this.height / this.width
}





/*

// JavaScript Document
var netNews = new Object();
netNews.READY_STATE_UNINITIALIZED = 0;
netNews.READY_STATE_LOADING = 1;
netNews.READY_STATE_LOADED = 2;
netNews.READY_STATE_INTERACTIVE = 3;
netNews.READY_STATE_COMPLETE = 4;
netNews.responseText = ""
netNews.responseXml = ""
netNews.debug = false
netNews.error = false
netNews.returnObject = null

netNews.XMLHTTPRequestWrapper = function(onload, onerror) {
	this.req = null;
	this.onload = (onload) ? onload : this.defaultLoad;
	this.onerror = (onerror) ? onerror : this.defaultError;
}

netNews.XMLHTTPRequestWrapper.prototype.loadData = function(url, method, params, retObj, contentType) {
	netNews.error = false
	netNews.returnObject = retObj
	netNews.returnObject.responseText = ""
	netNews.returnObject.responseXml = ""
	
	if (!method) {
		method="GET";
	}
	if (!contentType && method == "POST") {
		contentType = 'application/x-www-form-urlencoded';
	}
	if (window.XMLHttpRequest) {
		this.req = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		this.req = new ActiveXObject("Microsoft.XMLHTTP");
	}
	if (this.req) {
		try {
			var loader = this;
			this.req.onreadystatechange = function() {
				netNews.XMLHTTPRequestWrapper.onReadyState.call(loader);
			}
			this.req.open(method, url, true);
			if (contentType) {
				this.req.setRequestHeader('Content-Type', contentType);
				this.req.setRequestHeader("Cache-Control", "no-store, no-cache, must-revalidate");
				this.req.setRequestHeader("Cache-Control", "post-check=0, pre-check=0");
				this.req.setRequestHeader("Pragma", "no-cache");
			}
			this.req.send(params);
		}
		catch (err) {
			this.onerror.call(this);
		}
	}
}

netNews.XMLHTTPRequestWrapper.onReadyState = function() {
	var req = this.req;
	if (req.readyState == netNews.READY_STATE_COMPLETE) {
		if (req.status == 200 || req.status == 0) {
			//netNewsresponseText = this.req.responseText
			//netNewsresponseXml = this.req.responseXML
			netNews.returnObject.responseText = this.req.responseText
			netNews.returnObject.responseXml = this.req.responseXML
			this.onload.call(this);
		}else {
			if(netNews.debug){
				this.onerror.call(this);
			}
			netNews.error = true
			netNews.returnObject.error = true
		}
	}
}

netNews.XMLHTTPRequestWrapper.prototype.defaultError = function() {
	alert("error fetching data!"
	+"\n\nreadyState:" + this.req.readyState
	+"\nstatus: " + this.req.status
	+"\nheaders: " + this.req.getAllResponseHeaders());
}

netNews.XMLHTTPRequestWrapper.prototype.defaultLoad = function() {
	alert("Server Procedure Correctly Executed!"
	+"\n\nresponseText:" + this.req.responseText
	+"\nresponseXML: " + this.req.responseXML);
}

function newsExecuteCallback(page, variables, method, receivedData, returnObject) {
	if(method && method.toUpperCase() == "POST"){
		new netNews.XMLHTTPRequestWrapper(receivedData).loadData(page, "POST", variables, returnObject); 
	}else{
		if(variables!=""){
			if(page.indexOf("?")!=-1){
				page += "&" + variables
			}else{
				page += "?" + variables
			}
		}
		new netNews.XMLHTTPRequestWrapper(receivedData).loadData(page, "GET", null, returnObject); 
	}
}

*/




var remindNews = ""
var remindWinWid = 400
remindWinWid
function openNewsRemind(id){
	remindNews = id
	stopNews = true
	if (window.innerWidth) {
        W = window.innerWidth;
   	    H = window.innerHeight;
	} else if (document.body.clientWidth) {
		W = document.body.clientWidth;
		H = document.body.clientHeight;
	} else {
		W = body.offsetWidth;
		H = body.offsetHeight
	}
	$("#newsRemindBg").css("top",0)
	$("#newsRemindBg").css("left",0)
	$("#newsRemindBg").css("width",W)
	$("#newsRemindBg").css("height",W)
	$("#newsRemindBg").fadeTo(1500,.7);
	openRemindForm()
}

function openRemindForm(){
	if (window.innerWidth) {
        W = window.innerWidth;
   	    H = window.innerHeight;
	} else if (document.body.clientWidth) {
		W = document.body.clientWidth;
		H = document.body.clientHeight;
	} else {
		W = body.offsetWidth;
		H = body.offsetHeight
	}
	$("#newsRemind").css("top",100)
	$("#newsRemind").css("left", parseInt((W - remindWinWid) / 2))
	$("#newsRemind").fadeIn(500);
}

function closeNewsRemind(){
	$("#newsRemind").fadeOut(1500);
	$("#newsRemindBg").fadeTo(1500,0,removeBgLayer);
	stopNews = false
	setTimeout("setNews()",2500)
}

function removeBgLayer(){
	$("#newsRemindBg").width("1")
	$("#newsRemindBg").height("1")
	$("#newsRemindBg").css("display","none")
}

function sendRemind(){
	mail = findObj("remindMail").value
	sms = findObj("remindSms").value
	gg = findObj("remindGiorni").options[findObj("remindGiorni").selectedIndex].value
	ore = findObj("remindOre").options[findObj("remindOre").selectedIndex].value
	mailValida = true
	smsValida = true
	
	if(mail == ''){
		mailValida = false
	}
	rxMail = new RegExp("^[\\w\.=-]+@[\\w\\.-]+\\.[a-zA-Z]{2,4}$");
	if(mailValida && !rxMail.test(mail)){
		mailValida = false
	}
	
	if(sms == ''){
		smsValida = false
	}
	rxSms = new RegExp(/^[\d]+$/);
	if(smsValida && !rxSms.test(sms) || sms.length<7){
		smsValida = false
	}
	
	if((!smsValida && !mailValida) || (smsValida && mailValida)){
		alert("E' richiesta la mail o l'sms")
		return
	}
	
	//page, variables, method, receivedData
	page = "/inc/RemindNews/Remind.asp"
	variables = "mail=" + mail
	variables += "&sms=" + sms
	variables += "&gg=" + gg
	variables += "&ore=" + ore
	variables += "&remindNews=" + remindNews
	method = "POST"
	//prompt(page+"?"+variables,page+"?"+variables)
	remindExecuteCallback(page, variables, method, remindReceiveData)
		
}



function remindReceiveData(){
	alert(netRemind.responseText)
	if(netRemind.responseText.indexOf(/errore/gi)==-1){
		closeNewsRemind()
		findObj("remindMail").value = ""
		findObj("remindSms").value = ""
		findObj("remindOre").selectedIndex = 0
		findObj("remindGiorni").selectedIndex = 0
	}
}


// JavaScript Document
var netRemind = new Object();
netRemind.READY_STATE_UNINITIALIZED = 0;
netRemind.READY_STATE_LOADING = 1;
netRemind.READY_STATE_LOADED = 2;
netRemind.READY_STATE_INTERACTIVE = 3;
netRemind.READY_STATE_COMPLETE = 4;
netRemind.responseText = ""
netRemind.responseXml = ""
netRemind.debug = false
netRemind.error = false

netRemind.XMLHTTPRequestWrapper = function(onload, onerror) {
	this.req = null;
	this.onload = (onload) ? onload : this.defaultLoad;
	this.onerror = (onerror) ? onerror : this.defaultError;
}

netRemind.XMLHTTPRequestWrapper.prototype.loadData = function(url, method, params, contentType) {
	netRemind.error = false
		
	if (!method) {
		method="GET";
	}
	if (!contentType && method == "POST") {
		contentType = 'application/x-www-form-urlencoded';
	}
	if (window.XMLHttpRequest) {
		this.req = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		this.req = new ActiveXObject("Microsoft.XMLHTTP");
	}
	if (this.req) {
		try {
			var loader = this;
			this.req.onreadystatechange = function() {
				netRemind.XMLHTTPRequestWrapper.onReadyState.call(loader);
			}
			this.req.open(method, url, true);
			if (contentType) {
				this.req.setRequestHeader('Content-Type', contentType);
				this.req.setRequestHeader("Cache-Control", "no-store, no-cache, must-revalidate");
				this.req.setRequestHeader("Cache-Control", "post-check=0, pre-check=0");
				this.req.setRequestHeader("Pragma", "no-cache");
			}
			this.req.send(params);
		}
		catch (err) {
			this.onerror.call(this);
		}
	}
}

netRemind.XMLHTTPRequestWrapper.onReadyState = function() {
	var req = this.req;
	if (req.readyState == netRemind.READY_STATE_COMPLETE) {
		if (req.status == 200 || req.status == 0) {
			netRemind.responseText = this.req.responseText
			netRemind.responseXml = this.req.responseXML
			this.onload.call(this);
		}else {
			if(netRemind.debug){
				this.onerror.call(this);
			}
			netRemind.error = true
		}
	}
}

netRemind.XMLHTTPRequestWrapper.prototype.defaultError = function() {
	alert("error fetching data!"
	+"\n\nreadyState:" + this.req.readyState
	+"\nstatus: " + this.req.status
	+"\nheaders: " + this.req.getAllResponseHeaders());
}

netRemind.XMLHTTPRequestWrapper.prototype.defaultLoad = function() {
	alert("Server Procedure Correctly Executed!"
	+"\n\nresponseText:" + this.req.responseText
	+"\nresponseXML: " + this.req.responseXML);
}

function remindExecuteCallback(page, variables, method, receivedData) {
	if(method && method.toUpperCase() == "POST"){
		new netRemind.XMLHTTPRequestWrapper(receivedData).loadData(page, "POST", variables); 
	}else{
		if(variables!=""){
			if(page.indexOf("?")!=-1){
				page += "&" + variables
			}else{
				page += "?" + variables
			}
		}
		new netRemind.XMLHTTPRequestWrapper(receivedData).loadData(page, "GET", null); 
	}
}


//Scriptacolus
var iWidth = 16
var iHeight = 16
var imgWidth = 0
var imgHeight = 0
var canStart = false
var timer = 15
function ImageBoxLoaded(){
	if(!canStart){
		setTimeout("ImageBoxLoaded()",1500)
		return
	}
	canStart = false
	$("#ImageBox").fadeTo(0,1.0,startIncrement);
}

function startIncrement(){
	$("#ImageClose").css("visibility","visible")
	$("#Img_ImageBox").fadeTo(0,0)
	imgWidth = parseInt($("#Img_ImageBox").width())
	imgHeight = parseInt($("#Img_ImageBox").height())
	imgWidth += 5
	imgHeight += 100
	incrementWidth()
}

function incrementWidth(){
	newWidth = $("#ImageBox").width() + iWidth
	if(newWidth > imgWidth){
		newWidth = imgWidth
		newLeft = parseInt($("#ImageBox").css("left").replace(/px/gi,"")) - (iWidth / 2)	
		$("#ImageBox").css("left",newLeft)
		$("#ImageBox").css("width",newWidth)
		incrementHeight()
	}else{
		newLeft = parseInt($("#ImageBox").css("left").replace(/px/gi,"")) - (iWidth / 2)
		$("#ImageBox").css("left",newLeft)
		$("#ImageBox").css("width",newWidth)
		setTimeout("incrementWidth()",timer)
	}
}

function incrementHeight(){
	newHeight = $("#ImageBox").height() + iHeight
	if(newHeight > imgHeight){
		newHeight = imgHeight
		$("#ImageBox").css("height",newHeight)
		//incrementWidth()
		displayImage()
	}else{
		$("#ImageBox").css("height",newHeight)
		setTimeout("incrementHeight()",timer)
	}
}

function displayImage(){
	$("#ImageBoxLoading").css("display","none")
	$("#Img_ImageBox").fadeTo(1500,1)
}


function openImageBox(image){
	
	$("#Img_ImageBox").attr("src",image)
	$("#Img_ImageBox").load(ImageBoxLoaded);
	
	if (window.innerWidth) {
        W = window.innerWidth;
   	    H = window.innerHeight;
	} else if (document.body.clientWidth) {
		W = document.body.clientWidth;
		H = document.body.clientHeight;
	} else {
		W = body.offsetWidth;
		H = body.offsetHeight
	}
	$("#ImageBoxBg").css("top",0)
	$("#ImageBoxBg").css("left",0)
	$("#ImageBoxBg").css("width",W)
	$("#ImageBoxBg").css("height",W)
	$("#ImageBoxBg").fadeTo(1500,.7, openImageTable);
	
}

function openImageTable(){
	if (window.innerWidth) {
        W = window.innerWidth;
   	    H = window.innerHeight;
	} else if (document.body.clientWidth) {
		W = document.body.clientWidth;
		H = document.body.clientHeight;
	} else {
		W = body.offsetWidth;
		H = body.offsetHeight
	}
	
	$("#ImageBox").css("width",100)
	$("#ImageBox").css("height",150)
	$("#ImageBox").css("top",100)
	$("#ImageBox").css("left", parseInt((W - 150) / 2))
	$("#ImageBox").fadeIn(500,1.0);
	//alert(1)
	stopLoad = false
	$("#ImageBox").click(closeImageBox)
	canStart = true
}

function closeImageBox(){
	$("#ImageBox").fadeOut(1500);
	$("#ImageBoxBg").fadeTo(1500, 0, removeBgImageBox)
}

function removeBgImageBox(){
	//alert(22)
	$("#Img_ImageBox").css("display","none")
	//$("#ImageClose").css("visibility","hidden")
	$("#ImageBox").css("display","none")
	$("#ImageBoxBg").css("display","none")
	$("#ImageBoxBg").width("1")
	$("#ImageBoxBg").height("1")
	//$("#ImageBoxBg").src = ""
	//$("#ImageBox").css("left",1)
	//$("#ImageBox").css("top",1)
	
	$("#ImageBox").width("1")
	$("#ImageBox").height("1")
	//$("#ImageBox").fadeTo(0,1.0);		
	
	//$("#ImageBoxLoading").css("display","")
	//$("#Img_ImageBox").load(function(){var a=1;});
	$("#Img_ImageBox").attr("src","/gui/Trasparenza.gif")
	
}



