﻿// da fare:  controllo posizione di Hide, resizeFoto
var OrigineY=115, OrigineX=25, Tema;
var width_Body, height_Body;
var myX, myY, objX, objY, d_x, d_y, m_x,m_y;
var moveFlag=false,overFlag=false,resizeFlag=false,timer=false,speed=50;
var Obj_Up = new Array, Obj_opened=-1;
var space_gif="../themes/space.gif";
function PopWindowInit(){
	document.onmouseup=EndMove;
	document.onmousemove=MoveHandler;
	document.onmousedown=cancel_event;
	document.onselectstart=cancel_event;
	
	if (window.addEventListener){
		window.addEventListener('DOMMouseScroll', wheel, false);
		window.onmousewheel=document.onmousewheel=wheel;
	}
	window.onresize=document.onresize=resizeWindow;
	width_Body=parseInt((document.all)? document.body.clientWidth : window.innerWidth);
	height_Body=parseInt((document.all)? document.body.clientHeight : window.innerHeight);
	setTema("metallicblue");	
};
function wheel(event){
	var delta=0;
	if (!event) event=window.event;
	if (event.wheelDelta) {
		delta=event.wheelDelta/120; 
		if (window.opera) delta=-delta;
	} else if (event.detail) delta=event.detail/3;
	if (delta && overFlag && Oggetto(overFlag).style.visibility=='visible')Oggetto(overFlag+((overFlag.indexOf('Fotografia')>-1 || overFlag.indexOf('Fumetti')>-1)? 'Miniature' : 'Contenuto')).scrollTop+=delta*30;
    if (event.preventDefault)event.preventDefault();
    event.returnValue=false;
}
function resizeWindow(){if (document.all) window.history.go(0);else location.href="index.html";}
function cancel_event(){ return (!moveFlag && !resizeFlag) };
function PopUP(titolo, msg, icon, mylayer, stili){ 
	if (Oggetto(mylayer)) {eval(mylayer+'Object.Show()');} 
	else {
		d_x=(d_x<width_Body/2)? (d_x+=20) : OrigineX;
		d_y=(d_y<height_Body/2)? (d_y+=25) : OrigineY;		
		var obj=new PopWindow (titolo, msg, d_x, d_y, width_Body/2, height_Body/2, (stili? stili : "PopUp"), mylayer, icon);
		return obj
	 }
};
function Oggetto(s){return  document.getElementById? document.getElementById(s) : document.all? document.all[s] :  0;};
function setTema(nome){Tema="../themes/"+nome+"/"};
function imgFailed(imgObj){alert('Impossibile caricare immagine ['+this.src+ ']')};
function ImageLoadArray(pics,tema){
	var imgObjs=new Array(pics.length);
	for (var i=0; i<pics.length; i++) {
		imgObjs[i]=new Image();
		if (document.all) imgObjs[i].onerror=imgFailed;
		else imgObjs[i].onError=imgFailed;
		imgObjs[i].src=tema+pics[i];
	}
	return imgObjs
};
function HTMLBody(titolo, nlay, stili, icon){
var obj=nlay+'Object';
var img=['sidebox-title-left.gif','sidebox-title-bg.gif','sidebox-title-right.gif','sidebox-bar-left.gif','sidebox-bar-bg.gif','sidebox-bar-right.gif','sidebox-bottom-left.gif','sidebox-bottom-bg.gif','sidebox-bottom-right.gif','EnlargeButton.gif','CrossButton.gif','LineButton.gif','ReduceButton.gif'];
	img=ImageLoadArray(img,Tema);
var carica=''+
'<table width="100%" height="100%">'+
	'<tr>'+
		'<td rowspan="3" valign="middle"></td>'+
		'<td colspan="2" valign="middle"></td>'+
		'<td rowspan="3" valign="middle"></td>'+
	'</tr>'+
	'<tr>'+
		'<td align="right" valign="middle" width="5%">'+
			'<p align="center"><img src="../themes/loading.gif" align="middle" border="0" height="13"></p>'+
		'</td>'+
		'<td nowrap="nowrap" width="5%"><p align="center"><font face="Verdana, Helvetica" size="-1"><b> sto caricando ...</b></font></p></td>'+
	'</tr>'+
	'<tr>'+
		'<td colspan="2" valign="middle"></td>'+
	'</tr>'+
'</table>';
var txt=''+
'<table border="0" cellspacing="0" cellpadding="0" width="100%" height="30">'+
	'<tr>'+
		'<td><img src="'+img[0].src+'" alt="" border="0"></td>'+
		'<td width="100%"><img src="'+img[1].src+'" alt="" border="0" width="100%" height="30"></td>'+
		'<td><img src="'+img[2].src+'" alt="" border="0"></td>'+
	'</tr>'+
'</table>'+
'<div id="'+ nlay+'Barra" style="position: absolute; top: 0px; left: 0px; width: 100%; height: 100%; visibility: inherit">'+
	'<table border="0" cellspacing="0" cellpadding="0" '+((stili)? 'class="'+stili+'Header"' : '' )+'>'+
		'<tr>'+
			'<td><img src="'+space_gif+'" alt="" width="4" border="0"></td>'+
			'<td><img src="'+((icon)? icon : space_gif)+'" alt="" border="0"></td>'+					
			'<td nowrap><div  id="'+nlay+'Drag" onmousedown="InitializeMove('+ obj+');" style="width: 100%; height: 25px; margin-top: 5px; margin-left: 5px; visibility: inherit; overflow: hidden;">'+ titolo+'</div></td>'+
			'<td><img src="'+space_gif+'" alt="" width="3" border="0"></td>'+
			'<td><a href="#" onclick="if('+obj+'.from_minimize) '+obj+'.Show(); else '+obj+'.Hide();" title="Riduci"><img id="Minimize'+nlay+'"src="'+img[11].src+'" border="0" alt="Riduci"></a></td>'+
			'<td><img src="'+space_gif+'" alt="" width="3" border="0"></td>'+
			'<td><a href="#" onclick="'+obj+'.Full();" title="Apri finestra"><img id="Reduce'+nlay+'" src="'+img[9].src+'" border="0" alt="Apri a schermo pieno"></a></td>'+
			'<td><img src="'+space_gif+'" alt="" width="3" border="0"></td>'+
			'<td><a href="#" onclick="'+obj+'.Kill();" title="Chiudi finestra"><img id="Close'+nlay+'" src="'+img[10].src+'" border="0" alt="Chiudi finestra"></a></td>'+
		'</tr>'+
	'</table>'+
'</div>'+
'<div id="'+ nlay+'Corpo" style="position: absolute; top: 30px; left: 0px; width: 100%; height: 100%; visibility: visible">'+
	'<table width="100%" height="100%" border="0" bgcolor="white" cellspacing="0" cellpadding="0">'+
		'<tr>'+
			'<td><img src="'+img[3].src+'" alt="" width="4" height="100%" border="0"></td>'+
			'<td bgcolor="white" style="background-image: url('+img[4].src+'); width: 100%; height:100%;"></td>'+
			'<td><img src="'+img[5].src+'" alt="" width="4" height="100%" border="0"></td>'+
		'</tr>'+
	'</table>'+
	'<table width="100%" height="30" border="0" cellspacing="0" cellpadding="0">'+
		'<tr>'+
			'<td><img src="'+img[6].src+'" alt="" border="0"></td>'+
			'<td width="100%"><img src="'+img[7].src+'" alt="" width="100%" height="30" border="0"></td>'+
			(document.all? '<td background="'+img[8].src+'"><div id="'+ nlay+'Resize" onmousedown="resizeFlag='+ obj+';" onmouseup="EndMove();" style=" width: 24px; height: 30px; cursor: nw-resize;"></div></td>' : '<td id="Resize'+nlay+'" onmousedown="resizeFlag='+ obj +';" style="cursor: nw-resize"><img src="'+img[8].src+'" alt="Resizer" border="0"></td>')+
			'</tr>'+
		'</table>'+
	'<div id="'+nlay+'Footer" style="position: relative; top: -30px; left: 0px; height: 25px; margin-top: 5px; margin-left: 5px; visibility: inherit; overflow: hidden;"></div>'+
	'<div id="'+nlay+'Contenuto" style="position: absolute; top: 0px; left: 5px; width: 10px; height: 10px; clip: rect(0px 10px 10px 0px); overflow: auto; visibility: inherit;">'+carica+'</div>'+
'</div>';

return txt
};
function PushOver(MyLayer){
	if(resizeFlag) return;
	if(overFlag && overFlag!=MyLayer && eval(overFlag+'Object.from_minimize')==false){
		with(Oggetto(overFlag).style){
			MozOpacity=("0.70");
			filter = ("Alpha(Opacity=80)");
			zIndex=3;
		}
	}
	if(overFlag!=MyLayer){
		AlphaFocus(MyLayer)
		Obj_Up[++Obj_opened]=MyLayer;
	}
};
function AlphaFocus(i){

	with(Oggetto(i).style){
		MozOpacity=("1.0");
		filter = ("Alpha(Opacity=100)");
		zIndex=100;
	}
	overFlag=i;
};
function InitializeMove(obj){
	moveFlag=obj;
	var myobj=Oggetto(obj.nlay);
	objX=myX-parseInt((document.all) ? myobj.style.left : myobj.offsetLeft);
	objY=myY-parseInt((document.all) ? myobj.style.top : myobj.offsetTop);
};
function MoveHandler(e){
	if ((!document.all && e.which==2) || (document.all && event.button>1)) { alert("event2"); return};
	
	myX=(document.all)? event.x : e.pageX;
	myY=(document.all)? event.y : e.pageY;
	if(resizeFlag){ 
		if ((myX-resizeFlag.Contenitore.x+12)<160 || (myY-resizeFlag.Contenitore.y+15)<120) return;
		resizeFlag.Resize((myX-resizeFlag.Contenitore.x+12), (myY-resizeFlag.Contenitore.y+15));
	}else if(moveFlag) moveFlag.Move((myX-objX), (myY-objY)); 
};
function EndMove(){
	objX=0;objY=0;
	if (resizeFlag){
		if (Oggetto(resizeFlag.nlay+"Miniature")) ResizeFoto(resizeFlag.nlay.substring(3,resizeFlag.nlay.length),resizeFlag.Width-160,resizeFlag.Height);
		if (resizeFlag.nlay.indexOf("DivFoto_")>-1) ResizeFoto(resizeFlag.nlay.substring(3,resizeFlag.nlay.length),resizeFlag.Width,resizeFlag.Height);				
	}
	moveFlag=false;
	resizeFlag=false;
	KillTimer();
};
function KillTimer(t){ if(t) clearTimeout(t); else clearTimeout(timer); return false}	
function makeObj(obj){
    this.obj=obj+"Object";
	this.elemento=Oggetto(obj);
	this.css=this.elemento.style;
	this.Width=parseInt(this.css.width);
	this.Height=parseInt(this.css.height);
	this.x=parseInt(this.css.left);
	this.y=parseInt(this.css.top);
	this.clip=clipIt;
	this.size=sizeIt;
	this.move=moveIt;
	this.show=showIt;
   	eval(this.obj+"=this");
    return this
};
function PopWindow(titolo, msg, x,y,  w,h, stili, MyLayer, icon ){
	this.nlay=!MyLayer? 'div'+Math.round(50000*Math.random()) : MyLayer;
    this.obj=this.nlay+"Object";
    this.Icon=icon;
	this.Msg=msg;
	this.Footer='';
	this.x=x;
	this.y=y;
	this.Width=w;
	this.Height=h;
	this.Stili=stili;
	this.Tema=Tema;
	this.from_minimize=false;
	this.from_fullscreen=false;
	
	var InsideHtml=HTMLBody(titolo, this.nlay, stili, icon);

	var str='<div id="'+ this.nlay+'" onmousedown="PushOver(this.id);" style="position: absolute; top: '+ ((this.y)? this.y : 0)+'px; left: '+((this.x)? this.x : 0)+'px; width:  '+ this.Width+'px; height:  '+ this.Height+'px; z-index: 3; visibility:visible; overflow: hidden"></div>';
	if (document.all){document.body.insertAdjacentHTML('beforeEnd', str )}
	else if (document.getElementById){
		this.elemento=document.createElement('div');
		document.getElementsByTagName('body').item(0).appendChild(this.elemento);
		this.elemento.innerHTML=str
	} 
	this.Contenitore=new makeObj(this.nlay);
	this.Contenitore.elemento.innerHTML=InsideHtml;
	this.DragBar=new makeObj(this.nlay+'Drag');
	this.Corpo=new makeObj(this.nlay+'Corpo'); 
	this.Contenuto=new makeObj(this.nlay+'Contenuto');
	this.PiePagina=new makeObj(this.nlay+'Footer');
	
	this.MinimizeImage=document.images["Minimize"+this.nlay];
	this.ReduceImage=document.images["Reduce"+this.nlay];
	this.Write=writePop;
	this.WriteFooter=writePopFooter;
	
	this.Hide=HidePop;
	this.Full=FullScreenPop;
	this.Show=ShowPop;
	this.Kill=KillPop;	
	this.Resize=ResizePop;
	this.Move=MovePop;
   	eval(this.obj+"=this");
	if (this.Msg) this.Write(this.Msg);
	this.Show();
    return this
};
function sizeIt(w,h){if (w) { this.css.width=w+'px'; this.Width=w;};if (h) {this.css.height=h+'px';this.Height=h}};
function moveIt(x,y){this.css.left=x+"px";this.css.top=y+"px";this.x=x;this.y=y};
function clipIt (w,h){this.css.clip='rect(0px '+ w+'px '+ h+'px 0px)'};
function showIt(s){this.css.visibility=(s)? 'visible' : 'hidden';};
function writePop(s){this.Contenuto.elemento.innerHTML=(this.Stili ? '<div '+ 'class="'+this.Stili+'Body">'+s+'</div>' : s);this.Msg=s};
function writePopFooter(s){this.PiePagina.elemento.innerHTML=(this.Stili ? '<div '+ 'class="'+this.Stili+'Footer">'+s+'</div>' : s);this.Footer=s};
function MovePop(x,y){
	x=(x<1)? 0 : (x>(width_Body-this.Contenitore.Width))? width_Body-this.Contenitore.Width : x;
	y=(y<1)? 0 : (y>(height_Body-this.Contenitore.Height))? height_Body-this.Contenitore.Height : y;
	this.Contenitore.move(x,y);
};
function ResizePop(w, h){
	if (resizeFlag){this.Width=w;this.Height=h;}
	this.Contenitore.size(w,h);
	this.DragBar.size(w-100,25);
	this.Corpo.size(w,h-60);
	this.Contenuto.size(w-10,h-60);
	this.Contenuto.clip(w-10,h-60);
	this.PiePagina.size(w-35);
	if (Oggetto(this.nlay+"Miniature")){
		with(Oggetto(this.nlay+"Miniature").style){
			clip='rect(0px 140px '+ (h-60)+'px 0px)';
			height=h-60;
		}
	}
	if (Oggetto(this.nlay+"ZoomImage")){
		with(Oggetto(this.nlay+"ZoomImage").style){
			width=w-150;
			height=h-60;
			clip='rect(0px '+ (w-150)+'px '+ (h-60)+'px 0px)';
		}
	}
};
function FullScreenPop(){
	var w=width_Body,h=height_Body,x=0,y=0;
	if (parseInt(this.Contenitore.css.width)==width_Body) { 
		this.ReduceImage.src=this.Tema+'EnlargeButton.gif';
		this.ReduceImage.alt="Apri a schermo pieno";
		x=this.x;
		y=this.y;
		w=this.Width;
		h=this.Height;
	} else {	
		if (!this.from_minimize){
			this.x=this.Contenitore.x;
			this.y=this.Contenitore.y;
			this.Width=this.Contenitore.Width;
			this.Height=this.Contenitore.Height;
		}
		this.ReduceImage.src=this.Tema+'ReduceButton.gif';
		this.ReduceImage.alt="Riduci a finestra";
		this.from_fullscreen=true;
		y=y+OrigineY;
		h=h-OrigineY;
	}
	this.Show();
	this.Move(x,y);
	this.Resize(w,h);
	if (Oggetto(this.nlay+"Miniature")) ResizeFoto(this.nlay.substring(3,this.nlay.length), w-160,h);
	if (this.nlay.indexOf("DivFoto_")>-1) ResizeFoto(this.nlay.substring(3,this.nlay.length),w,h);				
};
function HidePop(){
	StopMovie(this.nlay);
	
	if (!this.from_fullscreen){
		this.Width=this.Contenitore.Width;
		this.Height=this.Contenitore.Height;
		this.x=this.Contenitore.x;
		this.y=this.Contenitore.y;
	}
	this.MinimizeImage.src=this.Tema+'ReduceButton.gif';
	this.MinimizeImage.alt="Apri a finestra";
	this.ReduceImage.src=this.Tema+'EnlargeButton.gif';
	this.ReduceImage.alt="Apri a schermo pieno";
	this.from_minimize=true;
	this.from_fullscreen=false;
	this.Resize(160, 60);
	this.Corpo.show(false);
	m_x=(m_x && m_x<width_Body-this.Contenitore.Width)? m_x+this.Contenitore.Width : 5;
	m_y=(!m_y)? height_Body-30 : (m_x==5)? m_y-=30 : m_y;
	this.Contenitore.move(m_x, m_y);
	
	for (var i=Obj_Up.length-1; i>=0; i--){
		if (Obj_Up[i]!=this.nlay && eval(Obj_Up[i]+'Object.from_minimize')==false && Oggetto(Obj_Up[i]).style.visibility=='visible'){
			AlphaFocus(Obj_Up[i]);
			break;
		}
	}
};
function KillPop(){
	StopMovie(this.nlay);
	
	this.Corpo.show(false);
	this.Contenitore.show(false);
	for (var i=Obj_Up.length-1; i>=0; i--){
		if (eval(Obj_Up[i]+'Object.from_minimize')==false && Oggetto(Obj_Up[i]).style.visibility=='visible'){
			AlphaFocus(Obj_Up[i]);
			break;
		}
	}
};
function ShowPop(){
	if (this.nlay.indexOf('Flash')>-1) HideOtherMovie(this.nlay);
	this.MinimizeImage.src=this.Tema+'LineButton.gif';
	this.MinimizeImage.alt="Riduci";
	this.from_minimize=false;	
	this.Contenitore.show(true);
	this.Corpo.show(true);
	if (this.from_minimize) this.from_minimize=false; this.Resize(this.Width,this.Height);
	this.Move(this.x,this.y);
	PushOver(this.nlay)
};
// 
function GPop(url, title, icon, tema){
	var temaTemp = Tema;
	icon= icon? icon : 'page';
	if (tema) setTema(tema);
	var obj=PopUP(title, '', '../themes/'+icon+'.gif',findNome(url));
	callAHAH(url, obj.nlay+'Contenuto', '', '');
	if (tema) setTema(temaTemp);
	return obj;
};
function findNome(s){while (s.indexOf("/")>-1) s=s.substring(s.indexOf("/")+1, s.length);return s.substring(0,s.indexOf('.'));};
function callAHAH(url, pageElement, callMessage, errorMessage) { document.getElementById(pageElement).innerHTML = callMessage; try { req = new XMLHttpRequest(); /* e.g. Firefox */ } catch(e) { try { req = new ActiveXObject("Msxml2.XMLHTTP"); /* some versions IE */ } catch (e) { try { req = new ActiveXObject("Microsoft.XMLHTTP"); /* some versions IE */ } catch (E) { req = false; } } } req.onreadystatechange = function() {responseAHAH(pageElement, errorMessage);}; req.open("GET",url,true); req.send(null); } 
function responseAHAH(pageElement, errorMessage) { if(req.readyState == 4) { if(req.status == 200) { output = req.responseText; document.getElementById(pageElement).innerHTML = output; } else { document.getElementById(pageElement).innerHTML = errorMessage+"\n"+req.responseText; } } }
