
    var ModalBox = {

        created : false,

        updated: false,

        lastScrollTop: 0,

        createModal : function()
        {
            ModalBox.updated = true;
            if(!ModalBox.created) {
                var modalBox            = jQuery("#ModalBox");
                var firstNode           = jQuery("body div:first");
                var insertHtml          = new String();
                    insertHtml         += '<div id="ModalBox"></div>';
                    //insertHtml         += '<div id="ModalBoxBackground"></div>';
                    insertHtml         += '<div id="ModalBoxContent"></div>';
                    insertHtml         += '<div id="ModalBoxLoader"></div>';
                    //insertHtml         += '<div id="ModalBoxCloseButton"></div>';

                // Append to body
                jQuery(insertHtml).insertBefore(firstNode);
                ModalBox.created = true;
                ModalBox.updated = false;
                ModalBox.Referer = '';
                ModalBox.lastScrollTop = jQuery(window).scrollTop();
            }

            // Positioning
            this.positioning();

            jQuery(window).scrollTop(215);

            // On resize
            jQuery(window).resize(function()
            {
                ModalBox.positioning();
            });
        },

        positioning : function()
        {
            var modalBox            = jQuery("#ModalBox");
            //var modalBoxBackground  = jQuery("#ModalBoxBackground");
            var modalBoxContent     = jQuery("#ModalBoxContent");
            var modalBoxLoader      = jQuery("#ModalBoxLoader");
            //var modalBoxCloseButton = jQuery("#ModalBoxCloseButton");
            var windowSizes         = {
                width   : jQuery(window).width(),
                height  : jQuery(window).height()
            };

            // Insert the new ModalBox attributes
            modalBox.css("position", "absolute");
            modalBox.css("width", windowSizes.width + "px");
            modalBox.css("height", windowSizes.height + "px");
            modalBox.css("left", 0);
            modalBox.css("top", 0);
            modalBox.css("overflow", "hidden");
            modalBox.css("z-index", 991);
            modalBox.css("background-color", "#0c2e1c");
            modalBox.fadeTo(0, 0.80);

            // Insert the new ModalBox attributes
            /*
            modalBoxBackground.css("position", "absolute");
            modalBoxBackground.css("width", windowSizes.width + "px");
            modalBoxBackground.css("min-width", jQuery("#Layout").width() + "px");
            modalBoxBackground.css("height", windowSizes.height + "px");
            modalBoxBackground.css("left", 0);
            modalBoxBackground.css("z-index", 992);
            modalBoxBackground.css("background-color", "#000000");
            modalBoxBackground.css("background-position", "top center");
            modalBoxBackground.css("background-repeat", "no-repeat");
            */

            // Insert the new ModalBoxContent attributes
            modalBoxContent.css("position", "absolute");
            modalBoxContent.css("width", (jQuery("#Layout").width() - 40) + "px");
            modalBoxContent.css("min-width", (jQuery("#Layout").width() - 40) + "px");
            modalBoxContent.css("padding-top", "215px");
            modalBoxContent.css("padding-left", "20px");
            modalBoxContent.css("padding-right", "20px");
            modalBoxContent.css("left", ((windowSizes.width / 2) - (jQuery("#Layout").width() / 2)) + "px");
            modalBoxContent.css("background", "none");
            modalBoxContent.css("z-index", 993);

            var newHeight = windowSizes.height;

            if(modalBoxContent.height() > newHeight)
                newHeight = modalBoxContent.height();

            if(jQuery(document).height() > newHeight)
                newHeight = jQuery(document).height();

            modalBox.css("height", newHeight + "px");

            // Insert the new ModalBox attributes
            modalBoxLoader.css("position", "absolute");
            modalBoxLoader.css("width", windowSizes.width + "px");
            modalBoxLoader.css("min-width", jQuery("#Layout").width() + "px");
            modalBoxLoader.css("height", windowSizes.height + "px");
            modalBoxLoader.css("left", 0);
            modalBoxLoader.css("z-index", 994);
            modalBoxLoader.css("background-image", "url('images/loader.gif')");
            modalBoxLoader.css("background-position", "center center");
            modalBoxLoader.css("background-repeat", "no-repeat");
            modalBoxLoader.css("display", "none");

            /*
            modalBoxCloseButton.css("position", "absolute");
            modalBoxCloseButton.css("width", "23px");
            modalBoxCloseButton.css("height", "23px");
            modalBoxCloseButton.css("top", "118px");
            modalBoxCloseButton.css("left", (Math.floor(windowSizes.width/2) + 339) + "px");
            modalBoxCloseButton.css("z-index", 995);
            modalBoxCloseButton.css("background-image", "url('/images/modal-close-button.gif')");
            modalBoxCloseButton.css("background-position", "center center");
            modalBoxCloseButton.css("background-repeat", "no-repeat");
            modalBoxCloseButton.css("cursor", "pointer");
            if(!ModalBox.updated) {
                modalBoxCloseButton.unbind("click");
                modalBoxCloseButton.click(function()
                {
                    // Remove ModalBox
                    jQuery("div[id^=ModalBox]").each(function()
                    {
                        jQuery(this).remove();
                    });

                    // Remove ModalBoxBackground
                    jQuery("div[id^=ModalBoxBackground]").each(function()
                    {
                        jQuery(this).remove();
                    });

                    // Remove ModalBoxContent
                    jQuery("div[id^=ModalBoxContent]").each(function()
                    {
                        jQuery(this).remove();
                    });

                    // Remove ModalBoxLoader
                    jQuery("div[id^=ModalBoxLoader]").each(function()
                    {
                        jQuery(this).remove();
                    });

                    // Set the created flag to false
                    ModalBox.created = false;

                    jQuery(window).scrollTop(ModalBox.lastScrollTop);
                });
            }
            */

        },

        close : function()
        {
            // Remove ModalBox
            jQuery("div[id^=ModalBox]").each(function()
            {
                jQuery(this).remove();
            });

            // Remove ModalBoxBackground
            /*
            jQuery("div[id^=ModalBoxBackground]").each(function()
            {
                jQuery(this).remove();
            });
            */

            // Remove ModalBoxContent
            jQuery("div[id^=ModalBoxContent]").each(function()
            {
                jQuery(this).remove();
            });

            // Remove ModalBoxLoader
            jQuery("div[id^=ModalBoxLoader]").each(function()
            {
                jQuery(this).remove();
            });

            // Set the created flag to false
            ModalBox.created = false;

            jQuery(window).scrollTop(ModalBox.lastScrollTop);
        },

        parse : function(inner)
        {
            addModal = "";
            if(inner) {
                addModal = "#ModalBoxContent ";
            }
            jQuery(addModal + "a[rel=ModalBox]").each(function()
            {
                jQuery(this).click(function(event)
                {
                    // Prevent to Open
                    event.preventDefault();

                    // Append Content
                    ModalBox.content(jQuery(this).attr("href"));
                });
            });
            /*
            jQuery(addModal + "form[rel=ModalBox]").each(function()
            {
                // Post Ajax Data
                jQuery(this).ajaxForm({beforeSend: function (request) {
                        request.setRequestHeader('ModalBoxReferer', ModalBox.Referer);
                        request.setRequestHeader('X-Requested-With', 'ModalBox');
                        ModalBox.showLoader();
                    },
                    success: function(response)
                    {
                        // Create Modal
                        ModalBox.createModal();
                        //jQuery(this).reset();
                        ModalBox.write(response);
                    }
                });
            });
            */
        },

        content : function(url)
        {
            // Create Modal
            ModalBox.createModal();

            // Get Ajax Content
            ModalBox.ajax("get", url);

        },

        write : function(content)
        {
            ModalBox.updated = true;
                ModalBox.hideLoader();
            var modalBoxContent = jQuery("#ModalBoxContent");
                modalBoxContent.empty();
                modalBoxContent.html(content);

                // Resize
                ModalBox.positioning();
                ModalBox.parse(true);
        },

        showLoader : function()
        {
            var modalBoxLoader = jQuery("#ModalBoxLoader");
                modalBoxLoader.css("display", "block");
        },

        hideLoader : function()
        {
            var modalBoxLoader = jQuery("#ModalBoxLoader");
                modalBoxLoader.css("display", "none");
        },

        ajax : function(method, url, data)
        {
            ModalBox.updated = true;
            ModalBox.showLoader();

            switch(method)
            {
                case "get":
                    jQuery.get(url, function(response)
                    {
                        ModalBox.write(response);
                    });
                break;

                case "post":
                    jQuery.post(url, data, function(response)
                    {
                        ModalBox.write(response);
                    });
                break;

                case "script":
                    jQuery.getScript(url, function(response)
                    {
                        ModalBox.write(response);
                    });
                break;

                case "json":
                    jQuery.getJSON(url, data, function(response)
                    {
                        ModalBox.write(response);
                    });
                break;
            }
            ModalBox.Referer = url;
        }
    };

    jQuery(document).ready(function()
    {
        ModalBox.parse();
    });

    jQuery.fn.extend({ reset: function() {
        return this.each(function() {
            $(this).is('form') && this.reset();
        });
    } });