/*
 * iFocus_Dialog
 * Version: 0.5
 * Date: 01 January, 2009 
 * http://www.iFocus.hk/
 * Copyright (c) 2009 iFocus Ltd.
 */
 
var focusDialog = null;
function iFocus_Dialog(args) {		
	this.args = args;
	this.viewpointW = 0;
	this.viewpointH = 0;
	this.scrollTop = 0;
	this.scrollLeft = 0;
	this.screenWidth = 0;
	this.screenHeight = 0;
	this.bodyW = 0;
	this.bodyH = 0;
	this.dialogH = 300;
	this.dialogW = 500;
	this.dialogTop = 0;
	this.dialogLeft = 0;
	this.maskID = 'iFocus_Dialog_Mask';	
	this.mask;
	this.containerID = 'iFocus_Dialog_Container';	
	this.container;
	this.updateStatus = null;
	this.noContent = "You have not input any content.";
	
	focusDialog = this;	
	this.init();
	this.openDialog();
}

iFocus_Dialog.prototype.init = function(){	
	this.setScreenSize();
	if (this.args){	
		if (this.args.width){
			this.dialogW = this.args.width;
		}
		if (this.args.height){
			this.dialogH = this.args.height;
		}
	}	
	this.mask = $(this.maskID);
	if (this.mask){
		document.body.removeChild(this.mask);
		//this.createMask();
	}
	this.container = $(this.containerID);
	if (this.container){
		document.body.removeChild(this.container);
		//this.createContainer();
	}
	this.createMask();
	this.createContainer();
}
iFocus_Dialog.prototype.createMask =function(event){	
	this.mask = document.createElement('div');
	document.body.appendChild(this.mask);
	var obj = this.mask;
	obj.id  = this.maskID;		
	if (this.bodyH<this.viewpointH){
		obj.style.height = this.viewpointH+"px";
	} else {
		obj.style.height = this.bodyH+"px";
	}
	if (this.bodyW<this.viewpointW){		
		obj.style.width = "100%";
	} else {
		obj.style.width = this.bodyW+"px";
	}
	obj.style.backgroundColor = "#000000";
	obj.style.display = "none";
	obj.style.position = "absolute";
	obj.style.top = "0px";
	obj.style.left = "0px";
	obj.style.opacity = .50;
	obj.style.zIndex = 10000;
	obj.style.filter = 'alpha(opacity=50)';
	obj.alpha = 50;
}
iFocus_Dialog.prototype.resetMask =function(){	
	if (this.bodyH<this.viewpointH){
		this.mask.style.height = this.viewpointH+"px";
	} else {
		this.mask.style.height = this.bodyH+"px";
	}
	if (this.bodyW<this.viewpointW){		
		this.mask.style.width = "100%";
	} else {
		this.mask.style.width = this.bodyW+"px";
	}
}
iFocus_Dialog.prototype.createContainer =function(event){	
	this.container = document.createElement('div');
	document.body.appendChild(this.container);
	var obj = this.container;
	obj.id  = this.containerID;		
	this.setDialogPos();
	
	//alert(Element.getStyle(obj,"border"));
	obj.style.height = this.dialogH+"px";
	obj.style.width = this.dialogW+"px";
	obj.style.display = "none";
	obj.style.position = "absolute";
	obj.style.top = this.dialogTop+"px";
	obj.style.left = this.dialogLeft+"px";	
	obj.style.zIndex = this.mask.style.zIndex+1;
	
	var closeBtn = document.createElement('div');
	closeBtn.className = "closeBtn";
	closeBtn.iFocus_Dialog = this;
	closeBtn.onclick = function(event){
		this.iFocus_Dialog.closeDialog();
	}
	
	var content = document.createElement('div');
	content.className = "iFocus_Dialog_Content";
	if (!this.args){
		content.innerHTML = this.noContent;
	} else {
		this.setContent(content);
	}	
	this.container.appendChild(closeBtn);
	this.container.appendChild(content);	
}
iFocus_Dialog.prototype.setDialogPos =function(){	
	if (this.viewpointW < this.dialogW){
		this.dialogLeft=0;
	}else {
		this.dialogLeft = (this.viewpointW/2)-(this.dialogW/2);
	}
	if (this.viewpointH < this.dialogH){
		//alert(this.viewpointH +" "+ this.dialogH);
		this.dialogTop=0;
	}else {
		this.dialogTop = (this.viewpointH/2)-(this.dialogH/2);
	}
}
iFocus_Dialog.prototype.setContent =function(content){	
	if (this.args.contentType == "iframe"){
		this.buildIFrameContent(content);
	} else if (this.args.contentType == "ajax"){
		this.buildAjaxContent(content);
	} else if (!this.args.content){
		content.innerHTML = this.noContent;		
	} else {
		content.innerHTML = this.args.content;		
	}
}
iFocus_Dialog.prototype.buildIFrameContent =function(content){	
	var iframe = "<iframe src="+this.args.content+" frameborder='0' scrolling='no' width='100%' height='"+(this.dialogH-10)+"px'></iframe>";
	content.innerHTML=iframe;
	
}
iFocus_Dialog.prototype.buildAjaxContent =function(content){	
	var url = this.args.content;
	var pars = "";
	//alert(pars);
	var myAjax = new Ajax.Request(url, { 
		method:'post',
		parameters:pars,
		onSuccess: function(transport){				
			content.innerHTML=transport.responseText;
		}, 
		onFailure: function(){
			content.innerHTML=this.noContent;
		} 
	});	
}
iFocus_Dialog.prototype.resetContainerPos =function(){	
	this.setDialogPos();
	if (this.viewpointH > this.dialogH){
		this.container.style.top = this.dialogTop+this.scrollTop+"px";
	} else {
		this.container.style.top = this.dialogTop+"px";
	}
	if (this.viewpointW > this.dialogW){
		this.container.style.left = this.dialogLeft+this.scrollLeft+"px";
	} else {
		this.container.style.left = this.dialogLeft+"px";
	}	
}

iFocus_Dialog.prototype.setScreenSize =function(){	
	if (this.maskID.indexOf("iFocus")<0)
		return;
	borwserDimensions = document.viewport.getDimensions();
	bodyDimensions = $(document.body).getDimensions();	
	scrollOffsets = document.viewport.getScrollOffsets();
	
	this.bodyW = bodyDimensions.width;
	this.bodyH = bodyDimensions.height;
	this.viewpointW = borwserDimensions.width;
	this.viewpointH = borwserDimensions.height;
	this.scrollTop = scrollOffsets.top;
	this.scrollLeft = scrollOffsets.left;
	if (bodyDimensions.height<borwserDimensions.height){
		this.screenHeight = borwserDimensions.height+scrollOffsets.top;
	} else {
		this.screenHeight = bodyDimensions.height+scrollOffsets.top;
	}
	if (bodyDimensions.width<borwserDimensions.width){		
		this.screenWidth = borwserDimensions.width+scrollOffsets.left;	
	}else {
		this.screenWidth = bodyDimensions.width+scrollOffsets.left;		
	}	
}
iFocus_Dialog.prototype.update =function(){
	this.setScreenSize();		
	this.resetMask();
	this.resetContainerPos();
}
iFocus_Dialog.prototype.hideSelectionBox = function(){
	var selectionList = $$("select");
	for (var i=0;i<selectionList.length;i++){
		$(selectionList[i]).style.visibility = 'hidden';
	}
}
iFocus_Dialog.prototype.showSelectionBox = function(){
	var selectionList = $$("select");
	for (var i=0;i<selectionList.length;i++){
		$(selectionList[i]).style.visibility = 'visible';
	}
}
iFocus_Dialog.prototype.openDialog =function(event){
	$(this.mask).show();
	$(this.container).show();
	this.hideSelectionBox();
	this.updateStatus = setInterval("focusDialog.update()",100);
}
iFocus_Dialog.prototype.closeDialog =function(event){
	$(this.mask).hide();
	$(this.container).hide();
	this.showSelectionBox();
	clearInterval(this.updateStatus);
}

