﻿/// <reference path="jquery-1.3.2.js" />

(function ($) {
    var c_modal = "#modal";
    var c_modalBack = "#modal-back";
    var c_modalBottom = "#modal-bottom";
    var c_modalClose = ".modal-close";
    var c_modalContainer = "#modal-container";
    var c_modalContent = "#modal-content";
    var c_modalTop = "#modal-top";

    var c_regModal = "reg-modal";

    var _buildStructure = function () {
        $(document.body).append("<div id=\"modal-back\">"
		+ "<div id=\"modal-container\">"
		+ "<div id=\"modal-top\"></div>"
		+ "<div id=\"modal-content\"><div id=\"modal\"></div></div>"
		+ "<div id=\"modal-bottom\"></div>"
		+ "<a class=\"modal-close text-replace\" href=\"#\">CLOSE X</a>"
		+ "</div></div>");

        $(c_modalClose).click(function (evt) { evt.preventDefault(); $.modalClose(); });
    };

    var _structureExists = function () {
        if ($(c_modal).length > 0)
            return true;

        return false;
    };

    $.modal = function (target, loadCallback) {
        if ($.browser.msie && $.browser.version < 7)
            return;

        $(target).click(function (evt) {
            var $this = $(this);
            var isReg = $this.hasClass("registration-link");
            var windowHeight = $(window).height();
            var isProduct = $this.hasClass("product-link");

            $(c_modal).children().remove();

            if ((isReg && windowHeight < 300 && $(".registration-form").length == 0) || (!isReg && windowHeight < 480))
                return;

            evt.preventDefault();

            if (!_structureExists())
                _buildStructure();

            if (isReg)
                $(c_modalBack).addClass(c_regModal);
            else {
                $(c_modalBack).removeClass(c_regModal);
            }

            $(c_modal).html("");
            $(c_modalBack).css({ display: "block" });

            var url = $this.attr("href");
            if (url && url != "") {
                $(c_modal).load(url, function (responseText, textStatus, xhr) {
                    if (textStatus == "error") {
                        $(c_modal).html("<p>An error has occured, please try again later.</p>");
                        return;
                    }

                    if (responseText == "logoff")
                        window.location.assign("/Licensee.ic/Login");

                    if ($.isFunction(loadCallback))
                        loadCallback(isReg, textStatus, url);

                    var contentHeight = $(".scroll-content").height() - 85 + "px";

                    if (isProduct) { contentHeight = 530 + "px"; }

                    //alert(contentHeight);

                    $(c_modalContent).animate({ "height": contentHeight }, { duration: 500 });
                });
            }
        });
    };

    $.modalClose = function () {
        $(c_modalBack).hide();
        $(c_modalContent).height("100px");
    };
})(jQuery);
