PopupWin = Class.create();

PopupWin.prototype = {

	initialize: function(options) {
		this.id				= "";
		this.frameid		= "";
		this.isPositioned 	= false;
		this.dialog			= null;
		this.framedialog	= null;
		this.document		= null;
		this.backgroundDiv	= null;
		this.displayFlag	= false;
		
		if (options) {
			this.setOptions(options);
		}
		
	},
   
	setOptions : function(options) {
      this.options = {
	  	 id						: null,
		 frameid				: null,
         left					: -1,
		 top					: -1,
		 width					: 300,
		 height					: 200,
		 backgoundLeft			: null,
		 backgroundTop			: null,
		 backgroundWidth		: null,
		 backgroundHeight		: null,
		 backgroundColor		: "#000",
		 babkgroundOpacity		: (50/100),
		 babkgroundMozOpacity	: (50/100),
		 babkgroundKhtmlOpacity	: (50/100),
		 backgroundFilter		: 'alpha(opacity=50)',
		 messageDiv				: 'message',
		 processURL				: null,
		 runScript				: null,
		 btnAction				: null,
		 clickClose				: false
      };


	      
		for(var x in options) {
			this.options[x] = options[x];
			if (typeof this.options[x] != 'function') continue;
		}
		
		this.id				= this.options.id;
		this.dialog			= $(this.id);
		
		if (this.options.frameid) {
			this.frameid		= this.options.frameid;
			this.framedialog	= $(this.frameid);
			
			if (owlimData.browser.msie) this.document = this.framedialog.contentWindow.document;
			else this.document		= this.framedialog.contentDocument;			
		} else {
			this.document		= document;
		}
		
		if (this.options.btnAction) {
			var actions = this.options.btnAction;
			
			for (var i =0; i < actions.length ; i++) {
				
				var action = actions[i];
				var obj = this.document.getElementById(action.id);
				
				obj.popupParent = this;
				obj.action = action.action;
				
				if (action.event == "onclick") {
					obj.onclick = this.doAction.bindAsEventListener(obj);
				} else if (action.event == "onkeyup") {
					obj.onkeyup = this.doAction.bindAsEventListener(obj);
				} else if (action.event =="onkeypress") {
					obj.onkeypress = this.doAction.bindAsEventListener(obj);
				} else if (action.event =="onkeydown") {
					obj.onkeydown = this.doAction.bindAsEventListener(obj);
				}
			}
		}
   },
   
   doAction : function(e) {
		//this.popupParent.dialog_close();
		eval(this.action);
   },
   
   getElement : function(id) {
		return this.document.getElementById(id);
   },
   
	setPosition : function(w_left,w_top)
	{
		if (!arguments.length) {
			this.setToCenterPosition();
		}
		
		if (arguments.length > 0) 
			this.options.left = w_left;

		if (arguments.length > 1) 
			this.options.top = w_top;
		
		this.isPositioned = true;
	},
	
	setToCenterPosition: function() {
		this.options.left 	= (document.body.clientWidth - this.options.width)/2;
		this.options.top	= (document.body.clientHeight-(this.options.height ? this.options.height : this.dialog.style.height))/2;
	},
	
	setSize : function(w_width,w_height)
	{
		
		if (w_width != null && w_width != '') {
			this.options.width = w_width;
		}
		
		if (w_height != null && w_height != '') {
			this.options.height = w_height;
		}
	},
	
	getSize : function() {
		var width = 0;
		var height = 0;
		
		if (this.dialog) {
			width = this.options.width;
			height = this.options.height;
		}
		
		return {width:width, height:height};
		
	},
	
	setBackground: function(){
	
		if (!this.backgroundDiv) {
			
			if ($("backgroundDiv")) {
				this.backgroundDiv = $("backgroundDiv");
			}
			else {
				this.backgroundDiv = document.createElement("div");
				this.backgroundDiv.id = "backgroundDiv";
				document.body.appendChild(this.backgroundDiv);
			}
		}

	
		if (this.options.backgroundWidth) {
			this.backgroundDiv.style.width = this.options.backgroundWidth+"px";
		} else {
			if (owlimData.browser.firefox) {
				this.backgroundDiv.style.width = document.documentElement.scrollWidth+"px";
			} else {
				this.backgroundDiv.style.width = document.body.clientWidth+"px";
			}
		}
		
		if (this.options.backgroundHeight) {
			this.backgroundDiv.style.height = this.options.backgroundHeight+"px";
		}
		else {
			
			if (owlimData.browser.firefox) {
				this.backgroundDiv.style.height = document.documentElement.scrollHeight+"px";
			} else {
				this.backgroundDiv.style.height = document.body.clientHeight+"px";
			}
		}
		
		if (this.options.backgoundLeft) {
			this.backgroundDiv.style.left = this.options.backgoundLeft+"px";
		} else {
			this.backgroundDiv.style.left = "0px";
		}
		
		if (this.options.backgoundTop) {
			this.backgroundDiv.style.top = this.options.backgoundTop+"px";
		} else {
			this.backgroundDiv.style.top = "0px";
		}
		
		this.backgroundDiv.style.backgroundColor	= this.options.backgroundColor;
		this.backgroundDiv.style.opacity			= this.options.babkgroundOpacity;
		this.backgroundDiv.style.MozOpacity			= this.options.babkgroundMozOpacity;
		this.backgroundDiv.style.KhtmlOpacity		= this.options.babkgroundKhtmlOpacity;
		this.backgroundDiv.style.filter				= this.options.backgroundFilter;
		this.backgroundDiv.style.position = 'absolute';
		this.backgroundDiv.style.zIndex = 1000;
		
		if (this.options.clickClose) {
			this.backgroundDiv.onclick = this.dialog_close.bindAsEventListener(this);
		} else {
			this.backgroundDiv.onclick = null;
		}
	},
	
	setPopup: function(){
		
		if (this.displayFlag) {
			this.setBackground();
			this.dialog.style.left = this.options.left+"px";
			this.dialog.style.top = this.options.top+"px";
			this.dialog.style.width = this.options.width +"px"
			this.dialog.style.position = 'absolute';
			this.dialog.style.zIndex = 1001;
			this.backgroundDiv.style.display = '';
			this.dialog.style.display = '';
			if (this.options.runScript) {
				setTimeout("eval("+this.options.runScript+")", 100);
			}
		} else {
			this.dialog.style.display = 'none';
			this.backgroundDiv.style.display = 'none';
		}
	},
	
	dialog_open : function()
	{
		this.displayFlag = true;
		this.setPopup();
	},
	
	dialog_close : function()
	{
		this.displayFlag = false;
		this.setPopup();
	},

	in_alert : function(message)
	{
		this.msg.innerHTML = message;
		this.msg.style.display = '';
		window.setTimeout(this.in_alert_hidden, 2000);
		in_alertcount++;
	},
	
	in_alert_hidden : function()
	{
		in_alertcount--;
		if(!in_alertcount)
			document.getElementById('message').style.display = 'none';
	}
}
