//********************************************************
//********************************************************
//**
//** CLASS :: TDarkenBox
//**
//********************************************************
//********************************************************

function TDarkenBox(){

	
	this.zindex = 50;
	this.opacity = 80;
	this.opaque = (this.opacity / 100);
	this.bgcolor = '#000000';
	this.default_page="vuoto.php";
	this.imgpath="";

	this.box_width = 400;
	this.box_height = 400;

	this.box_width_c = this.box_width;
	this.box_height_c = this.box_height;

	this.scrolling = 'no';

	var rand_no = Math.random();
	rand_no = rand_no * 10000;
	rand_no = Math.ceil(rand_no);


	this.box_id = "Box91826329383G62872H_"+rand_no;
	this.darken_id = "DarkenBox8172723937363826729F56_"+rand_no;
	this.frame_id = "iFrame1621829829HY8278239_"+rand_no;

	this.box = null;
	this.dark = null;	

	this.visible = false;
	this.pageWidth = 0;
	this.pageHeight = 0;

	this.url = "";


	this.doInit = function(){


		var self = this;
		
		
			 
		var tbody = document.getElementsByTagName("body")[0];		
		//var tbody = document.getElementsByTagName("form")[0];		
		var tnode = null;

		// DARKEN
		
		tnode = document.createElement('div');		
		tnode.style.position='fixed';			
		tnode.style.top='0px';
		tnode.style.left='0px';
		tnode.style.overflow='hidden';
		tnode.style.display='none';
		tnode.id=this.darken_id;		
		tnode.onclick = function(e){ setTimeout(function(){ self.doClose(); },10); }
		// Append
		tbody.appendChild(tnode);
		this.dark=document.getElementById(this.darken_id);
		

		this.dark.style.opacity=this.opaque;
		this.dark.style.MozOpacity=this.opaque;
		this.dark.style.filter='alpha(opacity='+this.opacity+')';
		this.dark.style.zIndex=this.zindex;
		this.dark.style.backgroundColor=this.bgcolor;

		// BOX

		tnode = document.createElement('div');		
		tnode.style.position='fixed';			
		tnode.style.top='0px';
		tnode.style.left='0px';
		tnode.style.width=this.box_width_c+'px';
		tnode.style.height=this.box_height_c+'px';
		tnode.style.backgroundColor='#ffffff';
		tnode.style.zIndex='99';
		tnode.style.border='4px solid #DDDDDD';
		tnode.style.overflow='hidden';
		tnode.style.display='none';
		tnode.id=this.box_id;
		// Append
		tbody.appendChild(tnode);
		this.box=document.getElementById(this.box_id);
		this.setUrl(this.default_page);
		
		//window.onresize = function(e){ setTimeout(function(){ self.doClose(); } ,10); };
		//window.onscroll = function(e){ setTimeout(function(){ self.doClose(); } ,10); };


	}

	this.setOpacity = function (op) {
		opaque = (op / 100);
		this.dark.style.opacity=opaque;
		this.dark.style.MozOpacity=opaque;
		this.dark.style.filter='alpha(opacity='+op+')';
	}

	
	var t_op = 0;
	var tself=this;
	this.fadeToOpacity = function (){
		if (this.t_op<this.opacity)
		{
			this.t_op+=40;
			if (this.t_op>this.opacity) this.t_op=this.opacity;
			this.setOpacity(this.t_op);
			window.setTimeout(function(){ tself.fadeToOpacity(); },50);
		}else{
			window.setTimeout(function(){ tself.fadeToSizeW(); },100);
		}
	}


	this.setSize = function (w,h) {
		this.box_width_c=w;
		this.box_height_c=h;
		this.doUpdate();
	}

	this.fadeToSizeW = function (){
		if (parseInt(this.box_width_c)<this.box_width)
		{
			this.box_width_c=parseInt(this.box_width_c)+100;
			if (this.box_width_c>this.box_width) this.box_width_c=this.box_width;
			this.setSize(this.box_width_c,this.box_height_c);
			window.setTimeout(function(){ tself.fadeToSizeW(); },50);
		}else{
			window.setTimeout(function(){ tself.fadeToSizeH(); },100);
		}
	}

	this.fadeToSizeH = function (){
		if (parseInt(this.box_height_c)<this.box_height)
		{
			this.box_height_c=parseInt(this.box_height_c)+100;
			if (this.box_height_c>this.box_height) this.box_height_c=this.box_height;
			this.setSize(this.box_width_c,this.box_height_c);
			window.setTimeout(function(){ tself.fadeToSizeH(); },50);
		}else{
			
		}
	}


	this.doDarken = function (vis) {  						  
		if (vis) { 
			this.t_op=0;
			this.setOpacity(this.t_op);
			this.setSize("40","40");
			this.fadeToOpacity();
			this.dark.style.display='';
			this.setUrl(this.url);
		} else {     
			this.dark.style.display='none';
		}
	}


	this.getPageSize = function(){
		var de = document.documentElement;
		var w = window.innerWidth || self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
		var h = window.innerHeight || self.innerHeight || (de&&de.clientHeight) || document.body.clientHeight;
		arrayPageSize = [w,h];
		return arrayPageSize;
	}


	this.doUpdate = function(){

		this.box.style.width=this.box_width_c+'px';
		this.box.style.height=this.box_height_c+'px';


		if( document.body && ( document.body.scrollWidth || document.body.scrollHeight ) ) {        
			
			var w = Math.min(parseInt(document.body.offsetWidth),parseInt(document.body.scrollWidth));
			var h = Math.max(parseInt(document.body.offsetHeight),parseInt(document.body.scrollHeight));

			this.pageWidth = w+'px';
			this.pageHeight = h+'px';

		} else if( document.body.offsetWidth ) {      
			this.pageWidth = parseInt(document.body.offsetWidth)+'px';
			this.pageHeight = parseInt(document.body.offsetHeight)+'px';
		} else {       
		   this.pageWidth='100%';
		   this.pageHeight='100%';
		}       

		this.dark.style.width= this.pageWidth;
		this.dark.style.height= this.pageHeight;
		
		
		var w = this.box_width_c;
		var h = this.box_height_c;
		
		var ps = this.getPageSize();

		//var scroll_top = window.pageYOffset || document.documentElement.scrollTop;
		//var scroll_left =  window.pageXOffset || document.documentElement.scrollLeft;

		//this.dark.style.left = parseInt(scroll_left)+"px";
		//this.dark.style.top = parseInt(scroll_top)+"px";

		//var xc = parseInt(scroll_left)+(parseInt(ps[0])/2)-(w/2);
		//var yc = parseInt(scroll_top)+(parseInt(ps[1])/2)-(h/2);

		var xc = (parseInt(ps[0])/2)-(w/2);
		var yc = (parseInt(ps[1])/2)-(h/2);
		
		this.box.style.left = parseInt(xc)+"px";
		this.box.style.top = parseInt(yc)+"px";
		
		this.box.style.display = (this.visible)?(""):("none");
		if (!this.visible)
		{
			this.setUrl(this.default_page);
		}

	}

	this.setUrl = function(url){

		this.box.innerHTML = "<div id='div__"+this.frame_id+"__wait' style='height:100%;'><table cellspacing='0' cellpadding='0' border='0' width='100%' height='100%'><tr><td align='center' valign='middle'><img src='"+this.imgpath+"/ajax-loader.gif' border='0' alt=''></td></tr></table></div><div id='div__"+this.frame_id+"' style='display:none; height:"+this.box_height+"px;'><iframe name='"+this.frame_id+"' id='"+this.frame_id+"' src='"+url+"' width='100%' height='100%' frameborder='0' scrolling='"+this.scrolling+"'></iframe></div>";

		var el = document.getElementById(this.frame_id);
		if (el && url!=""){
			el.setAttribute("src",url);
		}
	}


	this.doOpen = function(url){

		this.url = url || "";

		this.doDarken(true);
		this.visible=true;
		this.doUpdate(); 

	}

	this.doClose = function(){
		this.visible=false;
		this.setUrl(this.default_page);
		this.doUpdate(); 
		this.doDarken(false);
	}

	this.doShow = function(show){
		if (show==1){
			this.toggleBox("div__"+this.frame_id+"__wait",0);
			this.toggleBox("div__"+this.frame_id+"",1);
		}else{
			this.toggleBox("div__"+this.frame_id+"__wait",1);
			this.toggleBox("div__"+this.frame_id+"",0);
		}
	}

	this.toggleBox = function(szDivID, iState) // 1 visible, 0 hidden
	{
		if(document.getElementById)	  //gecko(NN6) + IE 5+
		{
			var obj = document.getElementById(szDivID);
			if (obj!=null){
				// obj.style.display = iState ? "" : "none";
				obj.style.visibility = iState ? "visible" : "hidden";
				obj.style.display = iState ? "" : "none";
			}		
		}	
	}


}
