﻿function DialogBase(width, height, x, y)
{
    this.height = height;
    this.width = width;
    this.x = x;
    this.y = y;
    this.borderWidth = 5;
    this.dialogPadding = 10;
    this.dialogShift = 0;

    this.backgroundStyle = "LightBackground opacity0";
    this.borderStyle = "LightBackground opacity75";
    this.dialogPaddingStyle = "SmallDialogPadding";

    this.dialogType = "";
    this.isShown = false;
    
}

DialogBase.prototype =
{
    close: function()
    {
        $(document).trigger(events.Modal.BeforeClose);
        
        //Still support till we remove alot of inheritence
        if (this.onBeforeClose)
            this.onBeforeClose();

        this.background.remove();
        this.container.remove();

        if (this.onClose)
            this.onClose();

        if (this.onAfterClose)
            this.onAfterClose();


        this.isShown = false;

    },

    _positionDialog: function(left, top)
    {
        this.dialog.css('margin-left', left + this.borderWidth + 'px')
        this.border.css('margin-left', left + 'px')

        this.border.css('margin-top', top + 'px');
        this.dialog.css('margin-top', top + this.borderWidth + 'px');
    },

    _ieHackReapplyZIndex: function()
    {
        var body = $("body");
        if (body.hasClass('ie6') || body.hasClass('ie7'))
        {
            this.background.css('z-index', 9998);
            this.container.css('z-index', 9999);
        }
    },

    _initializeDialog: function()
    {
        var background = "<div dialogtype='" + this.dialogType + "' class='" + this.backgroundStyle + " ModalBackground'></div>";
        var modalContainer = "<div dialogtype='" + this.dialogType + "' class='DialogContainer'></div>";
        var border = "<div id='DialogBorder' dialogtype='" + this.dialogType + "' class='" + this.borderStyle + " BorderBackground'></div>";
        var dialog = "<div id='Dialog' dialogtype='" + this.dialogType + "' class='" + this.dialogPaddingStyle + "'></div>";
        var dialogContent = "<div id='DialogContent' class='dialogContent' dialogtype='" + this.dialogType + "'>" + this.content + '</div>';

        var body = $("body");

        body.append(background);
        body.append(modalContainer);

        this.container = $('div[dialogtype="' + this.dialogType + '"][class="DialogContainer"]');
        this.container.append(dialog);
        this.container.append(border);


        this.dialog = $('div[dialogtype="' + this.dialogType + '"][class="' + this.dialogPaddingStyle + '"]');

        this.dialog.append(dialogContent);

        this.dialogContent = $('div[dialogtype="' + this.dialogType + '"][class="dialogContent"]');

        this.background = $('div[dialogtype="' + this.dialogType + '"][class*="ModalBackground"]');
        this.border = $('div[dialogtype="' + this.dialogType + '"][class*="' + this.borderStyle + '"]');

        this._addAdditionalContent();

        this.dialog.bgiframe();
        this.border.bgiframe();

        this._ieHackReapplyZIndex();
    },

    _addAdditionalContent: function()
    {

    },


    _resizeBackground: function()
    {
        if ($("body").hasClass("ie6"))
        {
            this.background.width(document.documentElement.scrollWidth);
            var height = Math.max(document.documentElement.scrollHeight, $(window).height());
            this.background.height(height);
        }
    },

    _getLeftMargin: function()
    {
        var window = $(window);
        var wWidth = window.width();
        var scrollbarWidth = 17
        var mouseOffset = 15;

        if (this.x + mouseOffset + this.border.width() < wWidth || this.border.width() > wWidth)
        {
            return this.x + mouseOffset;
        }
        return wWidth - this.border.width();
    },

    _getTopMargin: function()
    {
        var window = $(window);
        var wHeight = window.height();
        var mouseOffset = 15;

        if (this.y + mouseOffset + this.height + this._getPadding() < wHeight)
        {
            return this.y + mouseOffset;
        }
        return wHeight - (this.height + this._getPadding());
    },

    _sizeDialog: function()
    {
        this.dialog.width(this.width)

        this.border.width(this.dialog.width() + this._getPadding())

        this.border.height(this.dialog.height() + this._getPadding());
    },

    _getPadding: function()
    {
        return (this.borderWidth * 2) + (this.dialogPadding * 2)
    },

    _show: function()
    {
        if (!this.isShown)
        {
            this.isShown = true;
            this._initializeDialog();
            this._sizeDialog();
            this._positionDialog(this._getLeftMargin(), this._getTopMargin());
            this._resizeBackground();
            this._wireupEvents();

            this.border.show();
            this.background.show();
            this.dialog.show();
            this.dialog.focus();
        }
    },

    _wireupEvents: function()
    {

    }
};

function Dialog(width, height, x, y)
{
    this.base = DialogBase;
    this.base(width, height, x, y);
    this.closeButtonStyle = "close_small";
}

Dialog.prototype = new DialogBase();


Dialog.prototype._addAdditionalContent = function()
{
    var close = "<a href=\"javascript:void(0)\" id='DialogClose' class='" + this.closeButtonStyle + "'></a>";
    this.dialog.append(close);
    this.closeButton = $('#Dialog #DialogClose');
};

Dialog.prototype._wireupEvents = function()
{
	var context = this;
	this.background.click(
        function(evt)
        {
        	context.close();
        });

	this.closeButton.click(
        function(evt)
        {
        	evt.preventDefault();
        	context.close();
        });
};

Dialog.prototype.onClose = null;
Dialog.prototype.onBeforeClose = null;
Dialog.prototype.onAfterClose = null;

function ModalDialog(width, height)
{
    this.base = Dialog;
    this.base(width, height, 0, 0);
    this.borderWidth = 10;
    this.dialogPadding = 16;

    this.backgroundStyle = "DarkBackground opacity50";
    this.borderStyle = "LightBackground opacity75";
    this.closeButtonStyle = "close_large";
    this.dialogPaddingStyle = "LargeDialogPadding";
}

ModalDialog.prototype = new Dialog();

ModalDialog.prototype._getLeftMargin = function()
{
    var window = $(window);
    var wHeight = window.height();
    var wWidth = window.width();

    return ((wWidth - this.width) / 2);
};

ModalDialog.prototype._getTopMargin = function()
{
    return $(window).scrollTop() + 200 - this.dialogShift;
};

ModalDialog.prototype._recalculateBorderHeight = function()
{
    this.border.height(this.dialog.height() + 52);
};

function alertWindow(width, height)
{
    this.base = ModalDialog;
    this.base(width, height);

    this.backgroundStyle = "DarkBackground opacity50";
    this.borderStyle = "alertBackground almost-opaque";
}

alertWindow.prototype = new ModalDialog();

alertWindow.prototype._wireupEvents = function()
{
    var context = this;
    this.background.click(
        function(evt)
        {
            context.close();
        });
};

alertWindow.prototype._addAdditionalContent = function(){};
