/**
 *	Usage
 *	new W.Popup({id: 'slides', exit: obj.method.bind(obj)});
 * 	new W.Popup({width: 800, height: 600, classname: 'iframe', iframe: 'index.html'});
 */
W.Popup = function(obj)
{
	this.init(obj);
};



W.Popup.prototype = {
	
	classname		: '',
	width			: 0,
	height			: 0,
	top				: '50%',
	left			: '50%',
	iframe			: null,
	scrolling		: null,			// scrolling for popup window, or iframe
	id				: 'popup_win',
	req				: null,
	method			: 'GET',
	exitCallback	: null,			// reference to exit callback function when exiting popup
	dim				: true,			// enable or disable dimming of the main page
	popup			: null,			// reference to popup window
	hideFlash		: false,
	
	

	init : function(obj)
	{
	
		if (obj) {
			this.classname		= obj.classname;
			this.width			= obj.width || 0;
			this.height			= obj.height || 0;
			this.iframe			= obj.iframe;
			this.scrolling		= obj.scrolling || this.scrolling;
			this.id				= obj.id || this.id;
			this.req			= obj.req;
			this.method			= obj.method || this.method;
			this.exitCallback	= obj.exit;
			if (obj.dim != undefined) this.dim = obj.dim;
		}
		
		if (navigator.userAgent.toLowerCase().indexOf('safari') != -1) { this.hideFlashFromSafari(); }

		this.overlay();
		this.window();
		//this.center();
		this.populate();
	},
	
	
	
	hideFlashFromSafari : function()
	{
		var embeds = document.getElementsByTagName('embed');
		
		for (var i = embeds.length; --i >= 0; ) {
			embeds[i].style.visibility		= 'hidden';
		}
	},
	
	
	
	showFlash : function()
	{
		var embeds	= document.getElementsByTagName('embed');
		
		for (var i = embeds.length; --i >= 0; ) {
			embeds[i].style.visibility		= 'visible';
		}
		
		
		var objects	= document.getElementsByTagName('object');
		
		for (i = objects.length; --i >= 0; ) {
			objects[i].style.visibility		= 'visible';
		}
	},
	
	
	
	overlay : function()
	{
		var obj					= this;
		
		var win					= W.Dom.getContentSize();
		
		if (!W.$('overlay')) {
			var div					= document.createElement('div');
			div.style.position		= 'absolute';
			div.style.left			= 0;
			div.style.top			= 0;
			div.style.height		= win.y + 'px';
			div.style.width			= win.x + 'px';
			div.id					= 'overlay';
			div.onclick				= function() { obj.exit(); };
			
			if (this.dim) {
				div.style.background	= '#000000';
				div.style.opacity		= '.50';
				div.style.filter		= 'alpha(opacity=50)';
			}
			
			document.getElementsByTagName('body')[0].insertBefore(div, null);
		} else {
			W.$('overlay').style.display = 'block';
		}
	},



	window : function()
	{
		var div;
		
		if (!W.$(this.id)) {
			div		= document.createElement('div');
			div.id		= this.id;
			
	
			if (this.classname) {
				div.className			= this.classname;
			} else {
				div.style.position		= 'absolute';
			}
			
			this.popup	= document.getElementsByTagName('body')[0].insertBefore(div, null);
		} else {
			this.popup = div = W.$(this.id);
			div.style.display = 'block';
		}
		
		var scroll				= W.Dom.getScroll();
		
		
		//***** set the height and width *****//
		if (this.height) {
			div.style.height	= this.height + 'px';
		} else {
			// check if height is set in the style sheet
			if (!isNaN(this.height = parseInt(W.Dom.getStyle(div, 'height')))) { div.style.height = this.height + 'px'; } else { this.height = 0; }
		}
		
		if (this.width) {
			div.style.width	= this.width + 'px';
		} else {
			// check if width is set in the style sheet
			if (!isNaN(this.width = parseInt(W.Dom.getStyle(div, 'width')))) { div.style.width = this.width + 'px'; } else { this.width = 0; }
		}
		
		//***** set the top and left *****//
		var top;
		var left;
		
		if ((top = parseInt(W.Dom.getStyle(div, 'top'))) !== 0) { this.top = top + 'px'; }
		if ((left = parseInt(W.Dom.getStyle(div, 'top'))) !== 0) { this.left = left + 'px'; }
		
		div.style.top	= this.top;
		div.style.left	= this.left;
				
		if (this.top == '50%') div.style.marginTop = -(parseInt(this.height / 2, 10)) + scroll.y + 'px';
		if (this.left == '50%') div.style.marginLeft = -(parseInt(this.width / 2, 10)) + scroll.x + 'px';

	},
	
	
	
	center : function()
	{
		var win		= W.Dom.getWindowSize();
		var scroll	= W.Dom.getScroll();
		var div		= this.popup;
		
		div.style.top		= parseInt((win.y / 2), 10) - parseInt((this.height / 2), 10) + scroll.y + 'px';
		div.style.left		= parseInt((win.x / 2), 10) - parseInt((this.width / 2), 10) + scroll.x + 'px';
		div.style.display	= 'block';
		
	},



	populate : function()
	{
		var obj	= this;
		var iframe;
		
		// if you refresh the page, safari will also show the last iframe - about:blank partially fixes the problem
		if (this.iframe && !W.$('popup_iframe')) {
			iframe				= document.createElement('iframe');
			iframe.id			= 'popup_iframe';
			iframe.src			= this.iframe;
			iframe.frameBorder 	= 0;
			//iframe.onload		= function() { alert(W.$('close')) };
			
			if (this.classname) { iframe.className	= this.classname; }
			if (this.width)		{ iframe.width		= this.width; }
			if (this.height)	{ iframe.height		= this.height; }
			if (this.scrolling) { iframe.scrolling	= this.scrolling; }
			
			this.popup.insertBefore(iframe, null);
		} else {
			iframe 		= W.$('popup_iframe');
			iframe.src	= 'about:blank';
			iframe.src	= this.iframe;
		}
		
		if (iframe.attachEvent) {		// WinIE
			iframe.attachEvent('onload', function() {
					if (iframe.contentWindow.document.getElementById('close')) iframe.contentWindow.document.getElementById('close').onclick = function() { obj.exit(); };
				}
			);
		} else {						// Firefox, Safari
			iframe.onload = function() {
				if (iframe.contentDocument.getElementById('close')) iframe.contentDocument.getElementById('close').onclick = function() { obj.exit(); };
			};
		}
		
		//if (this.src) return this.popup.innerHTML = this.src;
		if (this.req) { new W.Request(this.method, this.req, obj.receive.bind(obj), null); }
	},
	
	
	
	receive : function(req)
	{
		var content				= req.responseText;
		this.popup.innerHTML	= content;
		
		var pattern = /<script[^>]*>([^<]*)<\/script>/gim;			// /<script.*?>(.*?)<\/script>/gim;		JS 1.5 version
		var result;
		
		while((result = pattern.exec(content)) !== null) {
			eval(result[1]);
		}
		
		var close;
		var obj	= this;
		if ((close = W.$('popup_close')) !== null) { close.onclick = function() {obj.exit(); return false; }; }
		
	},



	exit : function()
	{
		if (this.exitCallback) { this.exitCallback(); }
		
		if (navigator.userAgent.toLowerCase().indexOf('safari') != -1) { this.showFlash(); }
		
		if (W.$('popup_iframe')) W.$('popup_iframe').src = 'about:blank';	// IE caches previous iframe content
		//W.$(this.id).innerHTML = '';
		//document.getElementsByTagName('body')[0].removeChild(W.$(this.id));
		//document.getElementsByTagName('body')[0].removeChild(W.$('overlay'));
		W.$('overlay').style.display = 'none';
		W.$(this.id).style.display = 'none';
	}
};