/**
 * Popup Class for javascript
 */

function Popup(p_iWidth, p_iHeight){

	this.toCenter 			= this.toCenter || true;
	this.blockScreen 		= this.blockScreen || true;
	
	this.iXlocation 		= this.iXlocation || 0;
	this.iYlocation 		= this.iYlocation || 0;
	this.iWidth				= p_iWidth || 200;
	this.iHeight			= p_iHeight || 100;
	
	this.sPopupId			= this.sPopupId || 'JsPopup';
	this.sBlockId			= this.sBlockId || 'JsBlock';
	
	this.oPopup				= null;
	this.oBlock				= null;
	
}

Popup.prototype.createPopup = function(){
	//Get body
	var oBody = document.getElementsByTagName('body').item(0);
	
	//Set divs
	this.oPopup = document.createElement('div');
	this.oPopup.setAttribute('id', this.sPopupId);
	
	//add style
	this.oPopup.style.width = this.iWidth+'px';
	this.oPopup.style.height = this.iHeight+'px';	
	
	//Add divs
	oBody.appendChild(this.oPopup);
}

/**
 * 
 * @param {Number} p_iFromPercent to make the blocker transparent ( default is 0 tot start en end bij 50 )
 * @param {Number} p_iToPercent to make the blocker transparent ( default is 0 tot start en end bij 50 )
 */
Popup.prototype.createBlock = function(p_nFromPercent, p_nToPercent, p_nTimer){
	var iFromPercent 	= p_nFromPercent || 0;
	var iToPercent		= p_nToPercent || 50;
	var iTimer			= p_nTimer || 2;
	
	//Get body
	var oBody = document.getElementsByTagName('body').item(0);
	
	//Set divs
	this.oBlock = document.createElement('div');
	this.oBlock.setAttribute('id', this.sBlockId);
	
	/**
	 * Bug Fix SMSShell
	 * @todo fix for FireFox
	 */
	var oContainer = globalId('container');
	var iHeight = oContainer.scrollHeight;
	var iBodyHeight = document.body.clientHeight;
	if(iBodyHeight > iHeight){
		iHeight = iBodyHeight;
	}
	
	//Start Block Screen settings
	this.oBlock.style.position  = 'absolute';
	this.oBlock.style.width		= '100%';
	this.oBlock.style.height 	= iHeight + 'px';
	this.oBlock.style.top		= '0px';
	this.oBlock.style.left		= '0px';
	this.oBlock.style.background = '#000000';
	this.oBlock.style.zIndex	= '100';	
	
	
	//Add divs
	oBody.appendChild(this.oBlock);
	
	//Show opacity
	oBlock = this.oBlock;
	ShowOpactity = function(){
		if(iFromPercent <= iToPercent) {
			oBlock.style.filter = 'alpha(opacity=' + iFromPercent + ')';
			oBlock.style.opacity = iFromPercent / 100;
			
			iFromPercent++;
			setTimeout('ShowOpactity()', iTimer);
		}
	}
	ShowOpactity();
}

Popup.prototype.show = function(){
	if(this.oPopup == null){
		this.createPopup();
	}
	if (this.oBlock == null && this.blockScreen) {
		this.createBlock();
	}
	this.toPosition();
}

Popup.prototype.toPosition = function(){
	if(this.toCenter){
		this.iYlocation = (document.body.offsetHeight - this.iHeight) / 2;
		this.iXlocation = (document.body.clientWidth - this.iWidth) / 2;
	}
	this.oPopup.style.top = this.iYlocation +'px';
	this.oPopup.style.left = this.iXlocation +'px';
}

Popup.prototype.innerHTML = function(p_sHTML){
	if(this.oPopup == null){
		this.createPopup();
	}
	this.oPopup.innerHTML = p_sHTML;
}

Popup.prototype.close = function(){
	var oPopup = null;
	var oBlock = null;
	var oBody = document.getElementsByTagName('body').item(0);
	
	//Popup
	if (this.oPopup == null){
		if(document.getElementById){
			oPopup = document.getElementById(this.sPopupId);
		}else if(document.all){
			oPopup = document.all[this.sPopupId];
		}else if (document.layers){
			oPopup = document.layers[this.sPopupId];
		}	
	}else{
		oPopup = this.oPopup;
	}
	
	//remove popup
	if (oPopup != null) {
		oBody.removeChild(oPopup);
	}
	
	
	//Block
	if (this.oBlock == null){
		if(document.getElementById){
			oBlock = document.getElementById(this.sBlockId);
		}else if(document.all){
			oBlock = document.all[this.sBlockId];
		}else if (document.layers){
			oBlock = document.layers[this.sBlockId];
		}	
	}else{
		oBlock = this.oBlock;
	}
	
	//remove block
	if (oBlock != null && this.blockScreen) {
		oBody.removeChild(oBlock);
	}
}

