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

function TDarkenBox(){

	
	this.zindex = 50;
	this.opacity = 60;
	this.opaque = (this.opacity / 100);
	this.bgcolor = '#000000';
	this.default_page="vuoto.html";

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

	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='absolute';			
		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='absolute';			
		tnode.style.top='0px';
		tnode.style.left='0px';
		tnode.style.width=this.box_width+'px';
		tnode.style.height=this.box_height+'px';
		tnode.style.backgroundColor='#ffffff';
		tnode.style.zIndex='99';
		tnode.style.border='1px solid #000000';
		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.box.innerHTML = "<iframe name='"+this.frame_id+"' id='"+this.frame_id+"' src='"+this.url+"' width='100%' height='100%' frameborder='0' scrolling='no'></iframe>";

		window.onresize = function(e){ setTimeout(function(){ self.doClose(); } ,10); };
		window.onscroll = function(e){ setTimeout(function(){ self.doClose(); } ,10); };


	}


	this.doDarken = function (vis) {  						  
		if (vis) {    						
			this.dark.style.display='';
		} 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(){


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

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

		} else if( document.body.offsetWidth ) {      
			this.pageWidth = document.body.offsetWidth+'px';
			this.pageHeight = 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;
		var h = this.box_height;
		
		var ps = this.getPageSize();

		var xc = document.body.scrollLeft+(parseInt(ps[0])/2)-(w/2);
		var yc = document.body.scrollTop+(parseInt(ps[1])/2)-(h/2);		
				
		this.box.style.left = xc;
		this.box.style.top = yc;
		

		this.box.style.display = (this.visible)?(""):("none");
		if (!this.visible){
			var el = document.getElementById(this.frame_id);
			if (el && this.url!=""){
				this.url="";
				el.setAttribute("src",this.default_page);
			}
		}

	} 


	this.doOpen = function(url){

		this.url = url || "";

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

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

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

	


}



