/* PopupDiv è una classe javascript che serve a gestire un div come se fosse un popup */

var PopupDiv = Class.create({
	  
	  /* costruttore della classe PopupDiv con i relativi parametri. */

	  /* idDiv : id del Div che rappresenterà il popup (è obbligatorio) */
	  /* top : coordinata y dell'angolo superiore sinistro del div (se non viene passata di default è 0) */
	  /* left : coordinata x dell'angolo superiore sinistro del div (se non viene passata di default è 0) */
	  /* mobile : mi dice se il popup deve essrer aperto in modalità mobile (Con lo scroller integrato) 
	   	lo croll è realizzato tramite il componente iscroll che deve essere importato*/
	
	  initialize: function(idDiv, top, left, mobile,dimension) {
		this.mouseX = 0;
		this.mouseY = 0;
		this.mobile = mobile || false ;
		this.height=(typeof dimension == 'undefined' || typeof dimension.height == 'undefined') ? null : dimension.height;
		this.width=(typeof dimension == 'undefined' || typeof dimension.width == 'undefined') ? null : dimension.width;
		this.sroller=null;
	    if (!this.ctrl(top,left)) {
	    	this.top = '0px';
		    this.left = '0px';
	    } else {	    
	    	this.top = top;
	    	this.left = left;
	    }
	    if (idDiv != null && idDiv != undefined) {
			this.idDiv  = idDiv;
			this.objDiv = $(idDiv);
			this.objDiv.setStyle({top:this.top,left:this.left});
            /* questa parte serve per nascondere il div quando si clicca con il mouse fuori dal div stesso */
			Event.observe(document, 'mouseup',this.handlerMouseUp.bind(this));
			//Nel caso di mobileattivato inizilizzo lo scroller
			if(this.mobile){
				this.initMobileScroller();
			}
			
			//Imposto le dimensioni
			if(this.height!=null){
				this.objDiv.style.height=this.height;
			}
			if(this.width!=null){
				this.objDiv.style.width=this.width;
			}
			
	    }
	  },
	  
	  initMobileScroller:function () {
		  this.objDiv.style.position = 'absolute';
		  var scroller='<div id=\"'+this.objDiv.identify()+'_scroller\" style=\"position:relative\"><\div>';
		  this.objDiv.update(scroller);
		  this.sroller= new iScroll(this.objDiv.identify());
	  },
	  
	  handlerMouseUp :function (e) {
			e = e || event;
			var target = e.target || e.srcElement;
			var box =this.objDiv;
			do {
				if (box == target) {				
					return;
				}
				target = target.parentNode;
			} while (target);
			box.style.display = 'none';
		},
		
		/*Aggiorna il contenuto */
	  updateContent :function (paramMoveTo,url,parameters) {
		  var popup =this;
		  new Ajax.Request(url,{
			  parameters:parameters,
			  onSuccess: function(transport) {
				  	if(popup.mobile){
				  		$(popup.objDiv.identify()+'_scroller').update(transport.responseText);
				  		popup.sroller.refresh();
				  	}else{
				  		$(popup.objDiv.identify()+'_scroller').update(transport.responseText)
				  	}
				  	popup.moveTo(paramMoveTo);
				  	popup.objDiv.show();
		    	}
			});			
	  },
		
	  /* funzione di controllo usata internamente : controlla che x e y siano valorizzate ritornando true in caso positivo */
	  ctrl: function(top,left) {
		  if (top != undefined && top != null && left != undefined && left != null) {
			  return true;
		  }
		  return false;
	  },
	  
	  getIdDiv: function() {
		  return this.idDiv;
	  },
	  
	  getLeft: function() {
		  return this.left;
	  },
	  
	  getTop: function() {
		  return this.top;
	  },
	  
	  /* sposta il div (facendo riferimento al suo angolo superiore sinistro) in una nuova posizione nelle seguenti modalità : */
	  
	  /* 1 - se o è uguale a {top:100px, left:200px} imposta le coordinate y e x assolute della finestra del browser */
	  /* 2 - se o è uguale a {position:'CENTER'} imposta le coordinate y e x pari a quelle del centro della finestra del browser */
	  /* 3 - se o è uguale a {position:'CENTER', element: refToBaseElement} il div viene posizionato nel centro del */
	  /*     componente grafico refToBaseElement che gli passiamo come riferimento , questa volta non viene usato come ancoraggio */
	  /*     l'angolo superiore sinistro ma il cursore triangolare */
	  
	  moveTo: function(o) { /* ex : {top:'100px', left:'200px'} , {position:'CENTER'} or {position:'CENTER', element: riferimento_ad_un_bottone} */
		  if (o != undefined && o != null) {
			  o.top = (typeof o.top == 'undefined') ? null : o.top;
			  o.left = (typeof o.left == 'undefined') ? null : o.left;
			  o.position = (typeof o.position == 'undefined') ? null : o.position;
			  o.element = (typeof o.element == 'undefined') ? null : o.element;
			  
			  if (o.position != null && o.position == 'CENTER') {
				  if (o.element == null){
					  this.moveTo(this.calculateTopLeftForCenter(this.objDiv));
				  }else {
					  this.moveTo(this.calculateTopLeftForCenter(this.objDiv,o.element));
				  }
			  } else if (o.position != null && o.position == 'CENTER_0N') {
				  if (o.element == null){
					  this.moveTo(this.calculateTopLeftForCenter(this.objDiv));
				  }else {
					  this.moveTo(this.calculateTopLeftForCenterOn(this.objDiv,o.element));
				  }
			  } else {		  		  
				  if (this.ctrl(o.top,o.left)) {
					  this.top = o.top;
					  this.left = o.left;
					  if (this.objDiv != null && this.objDiv != undefined) {
						  this.objDiv.setStyle({top:this.top,left:this.left})
					  }
				  }
			  }
		  }
	  },
	  
	  /* mostra il div */
	  show: function() {
		  if (this.objDiv != null && this.objDiv != undefined) {				  
			  this.objDiv.show();
		  } 
	  },
	  
	  /* nasconde il div */
	  
	  hide: function() {
		  if (this.objDiv != null && this.objDiv != undefined) {				  
			  this.objDiv.hide();
		  } 
	  },
	  /* serve per calcolare le coordinate del div da posizionare al centro dell'oggetto di riferimento (tendenzialmente quello cliccato) */
	  /* obj : riferimento all'oggetto grafico div oppure qualsiasi altro (di tipo grafico) */
	  /* refToBaseElement : riferimento all'oggetto grafico rispetto al quale posizionare il div centralmente */

	calculateTopLeftForCenter: function (obj,refToBaseElement) {	
	  	if (obj != undefined && obj != null) {
	  		var ws = new WindowSize();
	  		if (refToBaseElement == undefined && refToBaseElement == null) {
	  			var x = (ws.width() - obj.getWidth())/2; 
	  			var y = (ws.height() - obj.getHeight())/2;			
	  		} else {
	  			var position = Position.cumulativeOffset(refToBaseElement);
	  			var refToBaseElement_height = refToBaseElement.getHeight();
	  			var x = position[0] - (obj.getWidth() - refToBaseElement.getWidth())/2; 
	  			var y = position[1] + refToBaseElement_height/2;
	  		}
	  		return {top:y+'px', left:x+'px'};
	  	}
	  	return {top:0,left:0};
	},
	calculateTopLeftForCenterOn: function (obj,refToBaseElement) {	
  		var ws = new WindowSize();
		var position = Position.cumulativeOffset(refToBaseElement);
		var refToBaseElement_height = refToBaseElement.getHeight();
		var x = (ws.width() - obj.getWidth())/2;  
		var y = position[1] - refToBaseElement_height;
		return {top:y+'px', left:x+'px'};
	}
});

/* Attenzione!!! La seguente classe è incompleta */

/* PopupDivLogin è una classe javascript che serve a gestire un div come se fosse un popup che contiene i campi da compilare */
/* per permettere un login dell'utente e in seguito una eventuale azione passata (se il login ha successo) */

var PopupDivLogin = Class.create(PopupDiv, {
	
    /* idDiv : id del Div che rappresenterà il popup (è obbligatorio) */
    /* top : coordinata y dell'angolo superiore sinistro del div (se non viene passata di default è 0) */
    /* left : coordinata x dell'angolo superiore sinistro del div (se non viene passata di default è 0) */
	/* forward : url/action che verrà eseguito dopo il login */
	
	initialize: function($super, idDiv, top, left, forward) {
		$super(idDiv, top, left);
		this.username = null;
		this.msg = null;
		this.reloadPage = null;
		this.forward = forward;
	},
	
	getUsername: function() {
		return this.username;
	},
	
	getMsg: function() {
		return this.msg;
	},
	
	getReloadPage: function() {
		return this.reloadPage;
	},
	
	getForward: function() {
		return this.referer;
	},
	
	setForward: function(forward) {
		return this.forward = forward;
	},
	  
	  /* serve per calcolare le coordinate del div da posizionare al centro dell'oggetto di riferimento (tendenzialmente quello cliccato) */
	  /* obj : riferimento all'oggetto grafico div oppure qualsiasi altro (di tipo grafico) */
	  /* refToBaseElement : riferimento all'oggetto grafico rispetto al quale posizionare il div centralmente */

	calculateTopLeftForCenter: function (obj,refToBaseElement) {	
	  	if (obj != undefined && obj != null) {
	  		var ws = new WindowSize();
	  		if (refToBaseElement == undefined && refToBaseElement == null) {
	  			var x = (ws.width() - obj.getWidth())/2; 
	  			var y = (ws.height() - obj.getHeight())/2;			
	  		} else {
	  			var position = Position.cumulativeOffset(refToBaseElement);
	  			var refToBaseElement_height = refToBaseElement.getHeight();
	  			var x = position[0] - (obj.getWidth() - refToBaseElement.getWidth())/2; 
	  			var y = position[1] + refToBaseElement_height/2;
	  			var obj_height = obj.getHeight();
	  			var y_refToBaseElement = refToBaseElement.viewportOffset().top;
	  			if (( y_refToBaseElement + obj_height) > ws.height()) { // se esce dalla window ribalta il popupDiv
	  				y = (refToBaseElement_height - obj_height) + (position[1] + refToBaseElement_height/2);
	  				$('sezioneTopFreccia').setStyle({display:'none'});
	  				$('sezioneBottom').setStyle({display:'none'});
	  				$('sezioneTop').setStyle({display:'block'});
	  				$('sezioneBottomFreccia').setStyle({display:'block'});				
	  			} else {
	  				$('sezioneTopFreccia').setStyle({display:'block'});
	  				$('sezioneBottom').setStyle({display:'block'});
	  				$('sezioneTop').setStyle({display:'none'});
	  				$('sezioneBottomFreccia').setStyle({display:'none'});
	  			}
	  		}
	  		return {top:y+'px', left:x+'px'};
	  	}
	  	return {top:0,left:0};
	}
});

/* LOGIN HEAD */
var PopupDivHeadLogin = Class.create(PopupDiv, {
	
    /* idDiv : id del Div che rappresenterà il popup (è obbligatorio) */
    /* top : coordinata y dell'angolo superiore sinistro del div (se non viene passata di default è 0) */
    /* left : coordinata x dell'angolo superiore sinistro del div (se non viene passata di default è 0) */
	/* forward : url/action che verrà eseguito dopo il login */
	
	initialize: function($super, idDiv, top, left, forward) {
		$super(idDiv, top, left);
		this.username = null;
		this.msg = null;
		this.reloadPage = null;
		this.forward = forward;
	},
	
	getUsername: function() {
		return this.username;
	},
	
	getMsg: function() {
		return this.msg;
	},
	
	getReloadPage: function() {
		return this.reloadPage;
	},
	
	getForward: function() {
		return this.referer;
	},
	
	setForward: function(forward) {
		return this.forward = forward;
	},
	handlerMouseUp :function (e) {
		e = e || event;
		var target = e.target || e.srcElement;
		var box = this.objDiv;
		do {
			if (box == target) {				
				return;
			}
			target = target.parentNode;
		} while (target);
		
		if($(box.identify()).visible()){
			//Rimuovo l'eventuale classe inserita per le customizzazioni del configuratore
			$(box.identify()).removeClassName('configurator');
			box.style.display = 'none';
			$('forgorPassword').style.display = 'none';
			$('formLoginHead').style.display = 'block';
			$('sezioneBottomHeadFreccia').removeClassName('bottomConfiguratorArrow');
			resetLoginField(box);
		}
	},
	  /* serve per calcolare le coordinate del div da posizionare al centro dell'oggetto di riferimento (tendenzialmente quello cliccato) */
	  /* obj : riferimento all'oggetto grafico div oppure qualsiasi altro (di tipo grafico) */
	  /* refToBaseElement : riferimento all'oggetto grafico rispetto al quale posizionare il div centralmente */

	calculateTopLeftForCenter: function (obj,refToBaseElement) {	
	  	if (obj != undefined && obj != null) {
	  		var ws = new WindowSize();
	  		if (refToBaseElement == undefined && refToBaseElement == null) {
	  			var x = (ws.width() - obj.getWidth())/2; 
	  			var y = (ws.height() - obj.getHeight())/2;			
	  		} else {
	  			var position = Position.cumulativeOffset(refToBaseElement);
	  			var refToBaseElement_height = refToBaseElement.getHeight();
	  			var x = position[0] - (obj.getWidth() - refToBaseElement.getWidth())/2; 
	  			//var x = parseInt(refToBaseElement.offsetLeft) - (obj.getWidth() - refToBaseElement.getWidth())/2; 
	  			var y = position[1] + refToBaseElement_height/2;
//	  			var y = parseInt(refToBaseElement.offsetTop) + refToBaseElement_height/2;
	  			var obj_height = obj.getHeight();
	  			var y_refToBaseElement = refToBaseElement.viewportOffset().top;
	  			if (( y_refToBaseElement + obj_height) > ws.height()) { // se esce dalla window ribalta il popupDiv
	  				y = (refToBaseElement_height - obj_height) + (position[1] + refToBaseElement_height/2);
	  				$('sezioneTopFrecciaHead').setStyle({display:'none'});
	  				$('sezioneBottomHead').setStyle({display:'none'});
	  				$('sezioneTopHead').setStyle({display:'block'});
	  				$('sezioneBottomHeadFreccia').setStyle({display:'block'});				
	  			} else {
	  				$('sezioneTopFrecciaHead').setStyle({display:'block'});
	  				$('sezioneBottomHead').setStyle({display:'block'});
	  				$('sezioneTopHead').setStyle({display:'none'});
	  				$('sezioneBottomHeadFreccia').setStyle({display:'none'});
	  			}
	  		}
	  		y = y + 11;
	  		x = x - 700;
	  		return {top:y+'px', left:x+'px'};
	  	}
	  	return {top:0,left:0};
	}
});

/* LOGOUT */
var PopupDivHeadLogout = Class.create(PopupDiv, {
	
    /* idDiv : id del Div che rappresenterà il popup (è obbligatorio) */
    /* top : coordinata y dell'angolo superiore sinistro del div (se non viene passata di default è 0) */
    /* left : coordinata x dell'angolo superiore sinistro del div (se non viene passata di default è 0) */
	/* forward : url/action che verrà eseguito dopo il login */
	
	initialize: function($super, idDiv, top, left, forward) {
		$super(idDiv, top, left);
		this.username = null;
		this.msg = null;
		this.reloadPage = null;
		this.forward = forward;
	},
	
	getUsername: function() {
		return this.username;
	},
	
	getMsg: function() {
		return this.msg;
	},
	
	getReloadPage: function() {
		return this.reloadPage;
	},
	
	getForward: function() {
		return this.referer;
	},
	
	setForward: function(forward) {
		return this.forward = forward;
	},
	handlerMouseUp :function (e) {
		e = e || event;
		var target = e.target || e.srcElement;
		var box = this.objDiv;
		do {
			if (box == target) {				
				return;
			}
			target = target.parentNode;
		} while (target);
		box.style.display = 'none';
	}
});

/* -- metodi di utilità per la gestione dei div -- */

/* WindowSize classe javascript che restituisce il width e l'height della finestra del browser */

var WindowSize = Class.create({
	
    width: function() {
        var myWidth = 0;
        if (typeof(window.innerWidth) == 'number') {
            //Non-IE
            myWidth = window.innerWidth;
        }
        else if (document.documentElement && document.documentElement.clientWidth) {
            //IE 6+ in 'standards compliant mode'
            myWidth = document.documentElement.clientWidth;
        }
        else if (document.body && document.body.clientWidth) {
            //IE 4 compatible
            myWidth = document.body.clientWidth;
        }
        return myWidth;
    },
    
    height: function() {
        var myHeight = 0;
        if (typeof(window.innerHeight) == 'number') {
            //Non-IE
            myHeight = window.innerHeight;
        }
        else if (document.documentElement && document.documentElement.clientHeight) {
            //IE 6+ in 'standards compliant mode'
            myHeight = document.documentElement.clientHeight;
        }
        else if (document.body && document.body.clientHeight) {
            //IE 4 compatible
            myHeight = document.body.clientHeight;
        }
        return myHeight;
    }
});



