// JavaScript Document
var BalloonObj = {
	Show: function(id){
		this.Fade(id,true);
	},
	Hide: function(id){
		this.Fade(id,false);
	},
	Fade: function(id,show){
		if(PageLoaded){
			var start = show ? 1 : 100;
			var end = show ? 100 : 1;
			var br = new NS_browser();
			
			var o = $('Balloon'+id);
			this.idBalloon = id;
			if(br.type=="ie"){// && br.version<=7){
				o.style.visibility = show ? "visible" : "hidden";
			}else{
				NS_fade(o,start,end,0.5,false);
			}
			o = start = end = br = null;
		}
	},
	setPos: function(id){
		var dim = NS_positionObj('Punto'+id);
		
		var x = id;
		while(x>10){
			x-=10;
		}

		marginLeft = -510+(95*(x-1));
		var marginTop = 60;
		$('Balloon'+id).style.marginLeft = (marginLeft)+"px";// (dim[0]-marginLeft)+"px";
		$('Balloon'+id).style.top = (dim[1]-marginTop)+"px";	
	}
};

var PuntoObj = {
	ajax: new NS_Ajax(),
	startW: 300,
	endW: 800,
	startH: 170,
	Open: function(id){
		this.setPos();
		this.Overlayer(true,id);
		NS_fade("Scheda",0,100,0.2,{obj: this, callback: "subOpen"});
	},
	setPos: function(){
		var o = $('Scheda');
		var dim = NS_dimWindow();
		var y = dim["scroll_top"];
		y = y<190 ? 190 : y;
		o.style.top = (y+15)+"px";

		o.style.width = this.startW+"px";
		this.setWidthCenterBorder(this.startW);
		o.style.marginLeft = "-"+Math.round(this.startW/2)+"px";
		
		$('Scheda').style.display = "block";
		$('Scheda').style.overflow = "hidden";
		$('Scheda').style.height = this.startH+"px";
		this.removeCont();
		$('Scheda_Cont').style.visibility = "hidden";
		$('Scheda_Cont').style.width = "auto";
		$('Scheda_Cont').style.height = "64px";
		//$('Scheda_Cont').style.border="1px solid";
		$('Scheda_center').style.backgroundImage = "url(_ext/img/loader1.gif)";
	},
	setWidthCenterBorder: function(w){
		$('Scheda_br_tc').style.width = $('Scheda_br_bc').style.width = (w-70)+"px";
	},
	Overlayer: function(show,id){
		var start = show ? 0 : 75;
		var end = show ? 75 : 0;
		var dim = NS_dimWindow();
		this.setDimOverlayer();
		this.idPunto = id;
		NS_fade("Overlayer",start,end,0.2,false);	
	},
	setDimOverlayer: function(){
		var hFooter = $('Footer').clientHeight;
		var hSite = $('Site').clientHeight;
		var hScheda = NS_dimObj('Scheda').H + NS_positionObj('Scheda')[1] + 100;		
		if(hScheda > hSite){
			var h = hScheda;
		}else{
			var h = hSite;
		}
		h += hFooter;
		$('Overlayer').style.height = h+"px";
	},
	subOpen: function(){//caricamento dati da ajax
		this.ajax.Reset();
		this.ajax.Send("punto="+this.idPunto,"ajax_response.php","post",{obj:this, callback: "subOpenB"});
	},
	subOpenB: function(){
		//alert(this.ajax.text);
		var xml = this.ajax.analizzaXML(true);
		var colore = xml.dati[0].getAttribute("colore");
		var numero = xml.dati[0].getAttribute("numero");
		var br = new NS_browser();
		var o = $('Scheda_Cont');
		
		this.imgPunto = new Image();
		this.imgPunto.src = "_ext/scripts/icona.php?s="+colore+"&p="+numero+"&w=50";
		this.imgPunto.onload = function(){
			$('ImagePunto').src = this.src;
		};
		
		var img = document.createElement("img"); //crea lo spazio per l'immagine del punto
		img.src = "_ext/img/trasp.gif";// "_ext/scripts/icona.php?s="+colore+"&p="+numero+"&w=50";
		img.style.width = "50px";
		img.style.height = "65px";
		img.style.verticalAlign="middle";
		img.style.marginRight = "20px";
		img.id = "ImagePunto";
		o.appendChild(img);
		
		for(var i=0;i<xml.dati[0].childNodes.length;i++){
			switch(xml.dati[0].childNodes[i].nodeName){
				case "TITOLO":
					var titolo = document.createElement("span");
					titolo.innerHTML = xml.dati[0].childNodes[i].childNodes[0].nodeValue;
					titolo.style.fontSize="24px";
					o.appendChild(titolo);
					break;
				case "TESTO":
					var testo = document.createElement("div");
					testo.style.width = "95%";
					testo.style.textAlign = "justify";
					testo.style.marginBottom = "15px";
					testo.innerHTML = xml.dati[0].childNodes[i].childNodes[0].nodeValue;
					o.appendChild(testo);
					break;
				case "ALLEGATI_G":
					var n = parseInt(xml.dati[0].childNodes[i].getAttribute("n"));
					if(n>0){
						var div = document.createElement("div");
						div.className = "AllegatiSpace";
						div.innerHTML = "Documenti allegati:";
						var ul = document.createElement("ul");
						for(var j=0;j < n;j++){
							var li = document.createElement("li");
							var a = document.createElement("a");
							a.setAttribute("target","_blank");
							a.setAttribute("rel","");
							a.setAttribute("href",xml.dati[0].childNodes[i].childNodes[j].childNodes[0].childNodes[0].nodeValue);
							a.innerHTML = xml.dati[0].childNodes[i].childNodes[j].childNodes[1].childNodes[0].nodeValue;
							
							li.appendChild(a);
							ul.appendChild(li);
						}
						div.appendChild(ul);
						o.appendChild(div);
					}
					break;
				case "FOTO_G":
					var n = parseInt(xml.dati[0].childNodes[i].getAttribute("n"));
					if(n>0){
						var div = document.createElement("div");
						var w_thumb = xml.dati[0].childNodes[i].getAttribute("w");
						var h_thumb = xml.dati[0].childNodes[i].getAttribute("h");
						div.className = "FotoSpace";
						
						for(var j=0;j < n;j++){
							var title = xml.dati[0].childNodes[i].childNodes[j].childNodes[2].childNodes[0].nodeValue;
							var divFoto = document.createElement("div");
							if(br.type=="ie"){
								divFoto.style.styleFloat = "left";
							}else{
								divFoto.style.cssFloat = "left";
							}
							divFoto.style.marginLeft = "8px";
							divFoto.style.marginTop = "4px";
							divFoto.style.width = w_thumb+"px";
							divFoto.style.height = Math.round(1.8*h_thumb)+"px";
							divFoto.style.background = "url(_ext/img/loader2.gif) no-repeat center";
							
							var img = document.createElement("img");
							img.alt = " ";
							img.title = title;
							img.src = xml.dati[0].childNodes[i].childNodes[j].childNodes[0].childNodes[0].nodeValue;
							
							var a = document.createElement("a");
							
							var href = xml.dati[0].childNodes[i].childNodes[j].childNodes[1].childNodes[0].nodeValue;
							
							if(br.type=="ie" && br.version<=7){
								a.style.marginLeft = "8px";
								a.style.marginTop = "4px";
								a.className = "lightview";
								a.href = href;
								a.rel="gallery[punto]";
								a.id = "image_"+j;
							}else{
								a.setAttribute("href",href);
								a.setAttribute("class","lightview");
								a.setAttribute("rel","gallery[punto]");
								a.setAttribute("title",title);
								a.setAttribute("style","margin-left: 8px; margin-top: 4px;");
								a.setAttribute("id","image_"+j);
							}
							
							a.appendChild(img);
							divFoto.appendChild(a);
							div.appendChild(divFoto);
						}
						var divClear = document.createElement("div");
						divClear.setAttribute("style","clear: left;");
						
						div.appendChild(divClear);

						o.appendChild(div);
						for(var j=0;j < n;j++){
							var tmp = $('image_'+j);
							Lightview.updateViews(tmp);
						}
					}
					break;
				case "VIDEO_G":
					var n = parseInt(xml.dati[0].childNodes[i].getAttribute("n"));
					if(n>0){
						var img_tit = document.createElement("img");
						img_tit.src = "_ext/img/punto/video_tit.gif";
						img_tit.alt = "Video";
						var div = document.createElement("div");
						div.className = "VideoSpace";
						var ul = document.createElement("ul");
						for(var j=0;j < n;j++){
							var li = document.createElement("li");
							var a = document.createElement("a");
							//alert(xml.dati[0].childNodes[i].childNodes[j].childNodes[1].childNodes[0].nodeValue);
							
							if(br.type=="ie" && br.version<=7){
								a.href = xml.dati[0].childNodes[i].childNodes[j].childNodes[1].childNodes[0].nodeValue+"&autoplay=1";
								a.className = "lightview";
								a.rel = "set[punto][flash]";
								a.id = "video_"+j;
							}else{
								a.setAttribute("href",xml.dati[0].childNodes[i].childNodes[j].childNodes[1].childNodes[0].nodeValue+"&autoplay=1");
								a.setAttribute("class","lightview");
								a.setAttribute("rel","set[punto][flash]");
								a.setAttribute("id","video_"+j);
							}
							a.innerHTML = xml.dati[0].childNodes[i].childNodes[j].childNodes[0].childNodes[0].nodeValue;	
							
							
							li.appendChild(a);
							ul.appendChild(li);
						}
						
						div.appendChild(img_tit);
						div.appendChild(ul);
						o.appendChild(div);
						
						for(var j=0;j < n;j++){
							var tmp = $('video_'+j);
							Lightview.updateViews(tmp);
						}
					}
					break;
			}
		}
		xml = br = div = o = null;
		this.subOpenC();
	},
	subOpenC: function(){ //apertura finestra
		$('Scheda_Cont').style.height = "auto";
		this.res1 = new NS_resizeObj('Scheda');
		this.res1.setMarginCenter_width = true;
		//this.res1.callback = {obj: this, callback: "subOpenD"};
		this.res2 = new NS_resizeObj('Scheda_br_tc');
		this.res3 = new NS_resizeObj('Scheda_br_bc');
		//this.res1.Mode = this.res2.Mode = this.res3.Mode = "exponent";
		this.res1.Tempo = this.res2.Tempo = this.res3.Tempo = 0.3;
		
		this.res1.Width("auto",this.endW);
		this.res2.Width("auto",this.endW-70);
		this.res3.Width("auto",this.endW-70);
		this.subOpenD();
	},
	subOpenD: function(){
		res1=res2=res3 = null;
		var resCont = new NS_resizeObj("Scheda");
		resCont.callback = {obj:this, callback: "subOpenE"};
		//resCont.MS = 10;
		resCont.Tempo = .3;
		
		resCont.Height("auto","auto");
		this.subOpenE();
	},
	subOpenE: function(){
		$('Scheda_center').style.backgroundImage = "none";
		var br = new NS_browser();
		if(br.type=="ie" && br.version>=8){
			$('Scheda_Cont').style.visibility="visible";
			this.subOpenF();
		}else{
			NS_fade('Scheda_Cont',0,100,1.5,{obj: this, callback: "subOpenF"});
		}
	},
	subOpenF: function(){
		var br = new NS_browser();
		if(br.type!="ie" || (br.type=="ie" && br.version>6)){
			$('Scheda').style.overflow = "visible";
		}
		this.setDimOverlayer();
	},
	Close: function(){
		this.Overlayer(false,0);
		NS_fade("Scheda",100,0,0.2,{obj: this, callback: "subClose"});
	},
	subClose: function(){
		$('Scheda').style.display = "none";	
		this.removeCont();
	},
	removeCont: function(){
		$('Scheda_Cont').innerHTML="";
		$('Overlayer').style.height = "0px";
		try{
			var old = $('Scheda_Cont_dynamic');
			$('Scheda_Cont').removeChild(old);
		}catch(e){}	
	}
};