//Popup Class
var PopUp = Class.create();

PopUp.prototype = {
	
	initialize: function(width, height) {
		this.width = width;
		this.height = height;
		this.isIE = this.checkIfIE();
		
		var body 			= document.getElementsByTagName('body')[0];
		
		var popbox				= document.createElement('div');

		popbox.id				= 'popup_container';
		popbox.className		= 'popup';
		popbox.innerHTML		= 	'<div class="title_bar" id="popup_title_bar">' + 
									'<div class="title"><?=$title?></div>' + 
									'<div class="buttons"><a class="close" href="javascript: popup.hidePopUp();"><span>Close Window</span></a></div>' +
									'</div>' +
									'<div class="body" id="popup_body"></div>' 
									'<div class="bottom"></div>';
		body.appendChild(popbox);
		
		this.popbox = $(popbox.id);
		this.popbox.hide();

	},
	
	
	showPopUp: function(url, form, onClose) {
		this.onClose = onClose;
		
		var params = '';
		if (form) {
			params = form.serialize();
		}
		if ( this.isIE ) this.hideAllDropDowns();
		
		var options = {
			parameters: params,
			onComplete: function() {
				this.displayPop();
			}.bind(this)
		};
		new Ajax.Updater('popup_body', url, options);
	},
	
	displayPop: function() {
		
		
		if (this.width) {
			this.popbox.style.width = this.width;	
		} else {	
			this.popbox.style.width = '300px';	
		}
		
		this.centerPopUp();

		this.popbox.show();
		new Draggable(this.popbox.id, {handle:'popup_title_bar', starteffect: false, endeffect: false });

	},
	
	hidePopUp: function(refresh_page) {
		if (this.onClose) {
			eval(this.onClose+'()');
		}		
		
		this.popbox.hide();

		this.hideAllDropDowns(true);
		if (refresh_page) window.location.reload();
	},
	
	hideAllDropDowns: function(reverse){
		
		var dropdowns = document.getElementsByTagName('select');
		
		for (i=0; i<dropdowns.length; i++) {
			
			dropdowns[i].style.display = ( reverse ? '' : 'none');
		}
		
	},
	
	
	centerPopUp: function() {
	
		var winW;
		var winH;
		var topOffset;

		
		if (self.innerWidth)
		{
			winW = self.innerWidth;
			winH = self.innerHeight;
			topOffset = self.scrollY;
		}
		else if (document.documentElement && document.documentElement.clientWidth)
		{
			winW = document.documentElement.clientWidth;
			winH = document.documentElement.clientHeight;
			topOffset = document.documentElement.scrollTop;
		}
		else if (document.body)
		{
			winW = document.body.clientWidth;
			winH = document.body.clientHeight;
			topOffset = document.body.scrollTop;
		}
		
		var left = parseInt((winW - 300 )/2);
		var top = parseInt((winH-300)/2);

		if (this.leftShift) {
			left = left - this.leftShift;
   		} 


		this.popbox.style.left = left + 'px';
		top = top + topOffset;

		this.popbox.style.top = top + 'px';

	
		
	},

	checkIfIE: function() {
	
		var browserDetection = navigator.userAgent.toLowerCase();
		var found = browserDetection.indexOf('msie') + 1;
	
		return found ? true : false;
	
	},
	
	setContent: function(code) {
		$('popup_body').innerHTML = code;
	},
	
	setContentFromID: function(id) {
		$('popup_body').innerHTML =  $(id).innerHTML;
	}
	
};
	
	
function showPopUp(url, width) {

	if ( this.isIE ) hideAllDropDowns();
	
	var popup_id = 'popup_box_container';
	var popup = document.getElementById(popup_id);
	
	document.getElementById('popup_box_container').style.display = 'none';	

if (width) {
		document.getElementById(popup_id).style.width = width;	
	} else {	
		document.getElementById(popup_id).style.width = '300px';	
	
	}
	
	new Ajax.Updater('popup_box', url, {onComplete:handlerFunc, onFailure:errFunc, asynchronous:true, evalScripts:true});

}

//used in multiple other areas; adding here
function popup_report_options(id){
	if (!popup) popup = new PopUp('300px');
	popup.showPopUp('/reports/select_options/PL-D1' +  '?simple=1&listings=' + id);
}

function popup_url(url){
	if (!popup) popup = new PopUp('300px');
	popup.showPopUp(url);
}

var popup = null;