/* Copyright (c) 2008 Kean Loong Tan http://www.gimiti.com/kltan
 * Licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
 * Version: 1.1 (March 26, 2008)
 * Requires: jQuery 1.2+
 * Modified by Webshark (http://www.webshark.hu)
 */

(function($) {

	var dialogDisplayed = false;
	var currentPos = true;

	$.fn.createDialog = function(options) {

		// Extend our default options with those provided.
		var opts = $.extend({}, $.fn.createDialog.defaults, options);
			currentPos = opts.center;
			if (!dialogDisplayed) { //display dialog if none is there
				$("body").append('<div id="lhpopup-overlay"></div><div id="lhpopup" style="visibility: hidden;"></div>');
				overlayPos(1);
				dialogDisplayed=true;
			}

			if(opts.progress)
				$("#jDialogProgressBar").show();

			$.ajax({
				type: opts.method,
				data: opts.data,
				url: opts.addr,
				success: function(msg){
					$("#lhpopup").html(msg);
					if (currentPos)
						reposition();
				}
			});
			//only IE6 needs this function
			if($.browser.msie && parseInt($.browser.version) < 7) {
				$(window).scroll(function(){
					if(dialogDisplayed==1) {
						overlayPos();
						if (currentPos)
							reposition();
					}
				});
			}
			$(window).resize(function(){
				if (dialogDisplayed==1) {
					overlayPos();
				}
			});

			$(window).unload( function () {
				if (dialogDisplayed==1)
					$.closeDialog();
			});
/*
			$(window).keydown(function(event){
				if (event.keyCode == 27)
					$.closeDialog();
			});
*/


		//private function
		function overlayPos(init){
			var left = 0;
			var top = 0;
			var overlayWidth = sizeCalc.getScrollWidth();
			var overlayHeight = sizeCalc.getRealHeight();
			var winHeight =  $(window).height();

			if ($.browser.msie && parseInt($.browser.version) < 7) { //if IE6
				$("#lhpopup-overlay").css({
									  top: 0,
									  left: 0,
									  width: overlayWidth,
									  height: overlayHeight,
									  position: "absolute",
									  display: "block",
									  color: opts.bg,
									  zIndex: opts.index
								  });
			}
			else { //other browsers
				$("#lhpopup-overlay").css({
									  top: 0,
									  left: 0,
									  width: overlayWidth,
									  height: overlayHeight,
									  position: "fixed",
									  display: "block",
									  background: opts.bg,
									  zIndex: opts.index
								  }).show();
			}

			if (init==1) {
				$("#lhpopup-overlay").css("opacity", 0);
				$("#lhpopup-overlay").fadeTo(200, opts.opacity);
			}
		}

		//private function
		function reposition(){ //calculate the position
			var left = 0;
			var top = 0;
			var winWidth = sizeCalc.getWindowWidth();
			var winHeight = sizeCalc.getWindowHeight();

			if ($.browser.opera)
				winHeight = sizeCalc.getWindowHeight();

			var dialogHeight = $("#lhpopup").height();
			var dialogWidth = $("#lhpopup").children().width();

			if ($.browser.msie) {
				left = document.body.scrollLeft || document.documentElement.scrollLeft;
				top = document.body.scrollTop || document.documentElement.scrollTop;
			}
			else {
				left = window.pageXOffset;
				top = window.pageYOffset;
			}
			var topOff = top + winHeight/2 - $("#lhpopup").height()/2; //offset for IE6
			var	leftOff = left + winWidth/2 - dialogWidth/2; //offset for IE6
			var topFixed = topOff - top;
			var	leftFixed = leftOff - left;

            var topOff_P = Math.abs(topOff);

			if ($.browser.msie && parseInt($.browser.version) < 7) { // IE6
				//IE 6 fix
				$("select").hide();
				//IE 6 fix
				$("#lhpopup select").show();
				//IE6 doesn't support fixed position
				$("#lhpopup").children().css({
														  top: topOff,
														  left: leftOff,
														  position: "absolute",
                                                          visibility: "visible",
														  zIndex: (opts.index+1)
													  }).show();
			}
			else{	// firefox and IE7
				$("#lhpopup").children().css({   top: topOff_P,
														  left: leftOff,
														  position: 'absolute',
                                                          visibility: "visible",
														  zIndex: (opts.index+1)
													  }).show();
			}
		}
	};

	$.fn.createDialog.defaults = {
		progress: true,
		center: true,
		method: 'GET',
		data: '',
		opacity: 0.80,
		bg: '#FFFFFF',
		index: 2000
	};

	$.closeDialog = function(){
		dialogDisplayed=false;
		if($.browser.msie && parseInt($.browser.version) < 7) //IE6 bug
			$("select").show();
		//fade out and remove DOM nodes
		$("#lhpopup-overlay").fadeTo(200, 0, function(){
			$("#lhpopup, #lhpopup-overlay").remove();
		});

	};

})(jQuery);

sizeCalc = {

    getWindowWidth: function() {
	    return (document.layers||(document.getElementById&&!document.all)) ? window.outerWidth : (document.all ? document.body.clientWidth : 0);
	},

	getWindowHeight: function() {
	    return window.innerHeight ? window.innerHeight :(document.getBoxObjectFor ? Math.min(document.documentElement.clientHeight, document.body.clientHeight) : ((document.documentElement.clientHeight != 0) ? document.documentElement.clientHeight : (document.body ? document.body.clientHeight : 0)));
	},

	getScrollWidth: function() {
	return document.all ? Math.max(Math.max(document.documentElement.offsetWidth, document.documentElement.scrollWidth), document.body.scrollWidth) : (document.body ? document.body.scrollWidth : ((document.documentElement.scrollWidth != 0) ? document.documentElement.scrollWidth : 0));
	},

	getScrollHeight: function(){
		body_height = document.all ? Math.max(Math.max(document.documentElement.offsetHeight, document.documentElement.scrollHeight), Math.max(document.body.offsetHeight, document.body.scrollHeight)) : (document.body ? document.body.scrollHeight : ((document.documentElement.scrollHeight != 0) ? document.documentElement.scrollHeight : 0));
		container_height = document.getElementById(container_id).scrollHeight;
		if (body_height < container_height) return container_height;
		else return body_height;
	},

	getScrollLeft: function() {
		return document.all ? (!document.documentElement.scrollLeft ? document.body.scrollLeft : document.documentElement.scrollLeft) : ((window.pageXOffset != 0) ? window.pageXOffset : 0);
	},

	getScrollTop: function() {
		return document.all ? (!document.documentElement.scrollTop ? document.body.scrollTop : document.documentElement.scrollTop) : ((window.pageYOffset != 0) ? window.pageYOffset : 0);
	},

	getRealHeight: function() {
		return (sizeCalc.getScrollHeight() < sizeCalc.getWindowHeight()) ?  sizeCalc.getWindowHeight() : sizeCalc.getScrollHeight();
	}

}

