/**
*
*AUTHOR : Pawan Tripathi (pawantri@gmail.com)
*
**/

var popup = {
	win:null, counter:0, template:null, sx:0, sy:0, dir:'', ox:0, oy:0, resizing:false, dragging:false, mxw:0, mxh:0, wins:new Array(), zi:0, maxWins:10, adw:0, adh:0, a:null, b:null, bo:'none', onresize:null,
	initialize : function(e){
		var ua = navigator.userAgent;
		if(document.all && ua.indexOf('Opera') == -1){
			popup.mxw = document.documentElement.clientWidth;
			popup.mxh = document.documentElement.clientHeight;
		}
		else{
			popup.mxw = window.innerWidth;
			popup.mxh = window.innerHeight;
		}
		popup.adw = popup.adh = 0;
		window.onresize = popup.initialize;
	},

	show:function(content, config){
		if(!(this.mxw || this.mxh)) this.initialize();
		
		if(this.counter >= this.maxWins){
			alert("Error : Max popups limit exceeded");
			return;
		}
		else if(config && config.id && document.getElementById(config.id)){
			alert("Error : Duplicate popup id");
			return;
		}

		
		if(content.indexOf(' ') == -1 && content.indexOf('<') == -1 && content.indexOf('>') == -1){
			try{
				$.get(content, function(data){
					popup.win.innerHTML = data;
					if(typeof(config.onload) == "function")
						config.onload();
				});
			}
			catch(e){
				try{
					content = location.href.indexOf('www.') >= 0 ? content : content.replace('www.', '');
					new Ajax.Request(content, {onSuccess:function(obj){
						popup.win.innerHTML = obj.responseText;
						if(typeof(config.onload) == "function")
							config.onload(obj.responseText);
					}});
				}
				catch(e){
					alert('JQuery or Prototype extension is required to load content from another file!'); return;
				}
			}
			content = config && config.preloader?config.preloader:"loading...";
		}
		
		if(config && config.modal){
			var lr1 = document.createElement("div");
			lr1.id = "popup_modal_layer";
			with(lr1.style){
				position = "fixed";
				left = top = "0px";
				width = popup.mxw + "px";
				height = popup.mxh + "px";
			}
			document.body.style.overflow = 'hidden';
			document.body.appendChild(lr1);
		}
		
		this.win = document.createElement('div');
		this.win.innerHTML = content;
		this.win.className = "popup";
		if(config && config.id) this.win.id = config.id;
		if(config && typeof(config.onClose) == "function") this.win.onClose = config.onClose;
		document.body.appendChild(this.win);
		
		if(!(config && config.width)) config.width = this.win.offsetWidth;
		if(!(config && config.height)) config.height = this.win.offsetHeight;
		
		var scrollTop = window.pageYOffset;
		if(!scrollTop) scrollTop = document.documentElement.scrollTop;
		with(this.win.style){
			position = "absolute";
			left = (this.mxw - config.width) / 2 + "px";
			top = ((this.mxh - config.height) / 2 + scrollTop) + "px";
			zIndex = ++this.zi;
		}
		
		if(config && config.width) this.win.style.width = config.width + "px";
		if(config && config.height)	this.win.style.height = config.height + "px";
		
		this.counter++;
		this.win.onmousedown = function(e){
			if(document.all) e = event;
			popup.win = this;
			if((document.all?e.srcElement:e.target).className == "title_bar"){
				if(!this.getAttribute('s')){
					popup.dragging = true;
					popup.ox = parseInt(this.style.left);
					popup.oy = parseInt(this.style.top);
					if(isNaN(popup.ox) || isNaN(popup.oy)){
						popup.ox = this.offsetLeft;
						popup.oy = this.offsetTop;
					}
					popup.sx = e.clientX;
					popup.sy = e.clientY;
				}
	//			if (e.preventDefault) e.preventDefault();
			}
		}
		return this.win;
	},
	
	getWindow : function(obj){
		while(obj.tagName != "body"){
			if(obj.className == "popup") return obj;
			else obj = obj.parentNode;
		}
	},
	
	close : function(obj){
		obj = popup.getWindow(obj);
		try{ obj.onClose(obj) }
		catch(e){}
		var x = obj.parentNode.removeChild(obj);
		popup.counter--;
		var ml = document.getElementById('popup_modal_layer');
		if(ml) document.body.removeChild(ml);
		document.body.style.overflow = "auto";
		return x;
	}
};
document.onmouseup = function(){ popup.dragging=false; popup.resizing = false; };
document.onmousemove = function(e){
	if(document.all) e = event;
	if(popup.dragging){
		popup.win.style.left = (popup.ox + e.clientX - popup.sx) + "px";
		popup.win.style.top = (popup.oy + e.clientY - popup.sy) + "px";
		return false;
	}
}
