﻿(function ($) {
	ApDialog = function (opt) {
		this.opt = opt;
		this.dialog = $("#" + opt.dialogID).remove();
		this.window = $(window);
		this.openCallback = (typeof (opt.openCallback) == 'undefined' ? function () { } : opt.openCallback);
		this.closeCallback = (typeof (opt.closeCallback) == 'undefined' ? function () { } : opt.closeCallback);
		this.size = {
			w: this.window.width(),
			h: this.window.height()
		};

		this.overlay = $("<div class='dialog-overlay'></div>");
		if (opt.openID) {
			$("#" + opt.openID).click(delegate(this, this.ShowDialog));
		}
	}

	$.extend(ApDialog.prototype,
    {
    	ShowDialog: function () {
    		this.dialog = this.dialog.appendTo($('body'));
    		this.dialog.find(this.opt.closeSelector).click(delegate(this, this.CloseDialog));

    		this.window.bind("resize", delegate(this, function () {
    			var height = this.window.height();
    			var width = this.window.width();
    			if (this.size.w != width || this.size.h != height) {
    				this.size = {
    					h: height,
    					w: width
    				};

    				this.RecalculatePosition();
    			}
    		}));
    		this.FadeShow();
    		return false;
    	},

    	FadeShow: function () {
    		this.Show();
    		this.dialog.fadeIn(200, delegate(this, this.openCallback));
    	},

    	FadeClose: function () {
    		this.Close();
    		this.dialog.hide();
    		this.closeCallback();
    		this.dialog = this.dialog.remove();
    	},

    	Show: function () {
    		var b = $('body').css('overflow', 'hidden');
    		this.displayedOverlay = this.overlay.css({
    			width: this.GetDocumentWidth(),
    			height: "9999px"
    		}).appendTo(b);

    		this.displayedOverlay.click(delegate(this, this.CloseDialog));
    		this.RecalculatePosition();
    	},

    	RecalculatePosition: function () {
    		this.dialog.css({
    			top: ((document.documentElement.clientHeight - this.dialog.height() -
                    (14 + parseInt(this.dialog.css("padding-top"))) * 2) / 2)
                    + this.GetScrollTop(),
    			left: ((document.documentElement.clientWidth - this.dialog.width() -
                    (14 + parseInt(this.dialog.css("padding-left"))) * 2) / 2)
                    + this.GetScrollLeft()
    		});
    	},

    	Close: function () {
    		this.displayedOverlay.remove();
    		$('body').css('overflow', '');
    	},

    	CloseDialog: function () {
    		this.FadeClose();
    		this.window.unbind("resize");
    	},

    	SubmitDialog: function () {
    		$(this).submit();
    	},

    	GetScrollTop: function () {
    		return document.documentElement.scrollTop > document.body.scrollTop ?
                document.documentElement.scrollTop : document.body.scrollTop;
    	},

    	GetScrollLeft: function () {
    		return document.documentElement.scrollLeft > document.body.scrollLeft ?
                document.documentElement.scrollLeft : document.body.scrollLeft;
    	},

    	GetDocumentHeight: function () {
    		return (document.documentElement.scrollHeight > document.body.scrollHeight) ?
                document.documentElement.scrollHeight : document.body.scrollHeight;
    	},

    	GetDocumentWidth: function () {
    		return (document.documentElement.scrollWidth > document.body.scrollWidth) ?
                document.documentElement.scrollWidth : document.body.scrollWidth;
    	}
    });

	function delegate(that, thatMethod) {
		return function () {
			return thatMethod.call(that);
		}
	}
})(jQuery);
