var popup = function() {
	
	var wrapperId = 'wrapper';

	function showAlert(msg, okMsg) {
		
		closeAlert();
		
		if ($(msg)) {
			msg = $(msg).title;
		}
		
		var div_content = '<div id="alert" class="alert">';
		if(parseInt(navigator.userAgent.substring(navigator.userAgent.indexOf("MSIE")+5)) == 6) {
			div_content += '<iframe style="width:500px;height:310px;margin-top: -200px;position:absolute;filter:progid:DXImageTransform.Microsoft.Alpha(opacity=0);" src="javascript:false;" frameborder="0" scrolling="no"></iframe>';
		}
		div_content += '<div class="msg">';
		div_content += msg;
		div_content += '<a href="javascript:popup.closeAlert();">' + okMsg + '</a>';
		div_content += '</div>';
		div_content += '</div>';
		div_content += '<div id="loading_div" style="height:' + getPageHeight() + 'px;">&nbsp;</div>';
		new Insertion.Before($(wrapperId), div_content);
		
		$('alert').style.top = getCenter('alert') + 'px';
	}
	
	function closeAlert() {
		if ($('alert')) $('alert').remove();
		if ($('loading_div')) $('loading_div').remove();
	}

	function showConfirm(msg, location, yesMsg, noMsg) {
		
		closeConfirm();
		
		if ($(msg)) {
			msg = $(msg).title;
		}
		
		var div_content = '<div id="confirm" class="alert">';
		if(parseInt(navigator.userAgent.substring(navigator.userAgent.indexOf("MSIE")+5)) == 6) {
			div_content += '<iframe style="width:500px;height:310px;margin-top: -200px;position:absolute;filter:progid:DXImageTransform.Microsoft.Alpha(opacity=0);" src="javascript:false;" frameborder="0" scrolling="no"></iframe>';
		}
		div_content += '<div class="msg">';
		div_content += msg;
		div_content += '<div class="clear"></div>';
		div_content += '<a href="javascript:popup.closeConfirm();" class="min">' + noMsg + '</a>';
		div_content += '<a href="' + location + '" class="min">' + yesMsg + '</a>';
		div_content += '</div>';
		div_content += '</div>';
		div_content += '<div id="loading_div" style="height:' + getPageHeight() + 'px;">&nbsp;</div>';
		new Insertion.Before($(wrapperId), div_content);
		
		$('confirm').style.top = getCenter('confirm') + 'px';
	}
	
	function closeConfirm() {
		if ($('confirm')) $('confirm').remove();
		if ($('loading_div')) $('loading_div').remove();
	}
		
	function showPopup(html) {
		
		closePopup();
		
		var div_content = '';
		div_content += '<div id="popupId" class="popup">';
		if(parseInt(navigator.userAgent.substring(navigator.userAgent.indexOf("MSIE")+5)) == 6) {
			div_content += '<iframe style="width:500px;height:500px;margin-top:0px;position:absolute;filter:progid:DXImageTransform.Microsoft.Alpha(opacity=0);" src="javascript:false;" frameborder="0" scrolling="no"></iframe>';
		}		
		div_content += '<div class="html">';
		div_content += '<a href="javascript: popup.closePopup();" class="close">X</a>';
		div_content += html;
		div_content += '</div>';
		div_content += '</div>';
		div_content += '<div id="loading_div" style="height:' + getPageHeight() + 'px;">&nbsp;</div>';
		new Insertion.Before($(wrapperId), div_content);
		
		var top_center = getCenter('popupId');
		
		if(parseInt(navigator.userAgent.substring(navigator.userAgent.indexOf("MSIE")+5)) == 6) {
			top_center = top_center - 100;
		}
		
		$('popupId').style.top = top_center + 'px';
	}
	
	function closePopup() {
		if ($('popupId')) $('popupId').remove();
		if ($('loading_div')) $('loading_div').remove();
	}
	
	function request(controller, action, param) {	
		
		loadingStart();
		
		var url = wwwbase.get() + 'ajax/get_popup_html/';
		var pars = 'controller=' + controller + '&action=' + action + '&' + param;
		
		new Ajax.Request( url, { method: 'post', parameters: pars, onComplete: recieve } );	
		
	};
	
	function recieve(request) {		
		eval("var result = " + request.responseText + ";");
		loadingStop();	
		showPopup(result['html']);
	};
	
	function getPageHeight() {
		
		if ($(wrapperId).offsetHeight < getClientHeight()) {
			height = getClientHeight();
		} else {
			height = $(wrapperId).offsetHeight;
		}
		return height;
	}
	
	function getCenter(id) {
		
		var scrollTop = getScrollTop();
		var clientHeight = getClientHeight();
	
		var center = scrollTop + clientHeight/2 - $(id).clientHeight/2 - 50;
		
		if (center < 1) center = 229;
		
		return center;
	}

	function getScrollTop() {
		if (window.pageYOffset)
			return window.pageYOffset;
		if (document.documentElement)
			return document.documentElement.scrollTop;
		if (document.body)
			return document.body.scrollTop;
	}
	
	function getClientHeight() {
		if (window.pageYOffset)
			return window.innerHeight;
		if (document.documentElement)
			return document.documentElement.clientHeight;
		if (document.body)
			return document.body.clientHeight;
	}
	
	function loadingStart() {
		var div_content = '<h2 id="loding_text">Loading ...</h2><div id="loading_div" style="height:' + getPageHeight() + 'px;">&nbsp;</div>';
		new Insertion.Before($(wrapperId), div_content);
		
		$('loding_text').style.top = getCenter('loding_text') + 'px';
	}
	function loadingStop() {
		$('loding_text').remove();
		$('loading_div').remove();
	}
	
	return {
		showAlert: function(msg, okMsg) {			
			showAlert(msg, okMsg);
		},
		closeAlert: function() {			
			closeAlert();
		},
		showConfirm: function(msg, location, yesMsg, noMsg) {			
			showConfirm(msg, location, yesMsg, noMsg);
		},
		closeConfirm: function() {			
			closeConfirm();
		},	
		showPopup: function(controller, action, param) {			
			request(controller, action, param);
		},
		closePopup: function() {			
			closePopup();
		},
		loadingStart: function() {			
			loadingStart();
		},
		loadingStop: function() {			
			loadingStop();
		}
	};
}();
