﻿function queryStringManager(qs)
{ // optionally pass a querystring to parse
    this.params = {};

    if (qs == null) qs = location.search.substring(1, location.search.length);
    if (qs.length == 0) return;

    qs = qs.replace(/\+/g, ' ');
    var args = qs.split('&');

    for (var i = 0; i < args.length; i++)
    {
        var pair = args[i].split('=');
        var name = decodeURIComponent(pair[0]);

        var value = (pair.length == 2)
		        ? decodeURIComponent(pair[1])
		        : name;

        this.params[name] = value;
    }
}
queryStringManager.prototype.get = function (key, default_)
{
    var value = this.params[key];
    return (value != null) ? value : default_;
};
queryStringManager.prototype.contains = function (key)
{
    var value = this.params[key];
    return (value != null);
};


/* Pop up manager
*********************************************************************************************************************/
function popUpManager()
{
     var _this = this;
     _this.modal = null;
     _this.overlay = null;
     _this.loader = null;
}
popUpManager.prototype =
{
    initialise: function ()
    {
        var _this = this;
        for (var i = 0; i < document.links.length; i++)
        {
            if (document.links[i].className.indexOf("PopUpLink") > -1)
            {
                document.links[i].id = "popup" + i;
                document.links[i].href = "javascript:popupmanager.open('" + document.links[i].href + "', 'popup" + i + "');";
                document.links[i].target = "";
            }
        }

        _this.modal = $('<div class="Overlay-Modal"></div>').appendTo("body");
        _this.loader = $('<div class="Overlay-Loader"><img src="' + WEBSITE_ROOT_URL + 'templates/images/loader-pink.gif" alt="Loading..." /></div>').appendTo("body");
        _this.overlay = $('<div class="Overlay-Form"><iframe id="ifrOverlayForm" class="Frame" frameborder="0" scrolling="no"/></div>').appendTo("body");
        _this.src = "";

        function _getIFrameDocument(iframeID)
        {
            if (document.getElementById(iframeID).contentDocument) return document.getElementById(iframeID).contentDocument;
            if (window.frames[iframeID].document) return window.frames[iframeID].document;
        }

        $("#ifrOverlayForm").load(function ()
        {
            if (this.src != "")
            {
                $(_this.overlay).show();
                var 
                    _doc = _getIFrameDocument("ifrOverlayForm"),
                    _divs = document.getElementsByTagName("div"),
                    _contentDiv = null;

                for (var i = 0; i < _divs.length; i++)
                {
                    if (_divs[i].className == "Content")
                    {
                        _contentDiv = _divs[i];
                        break;
                    }
                }

                $(_this.overlay).width(_contentDiv.offsetWidth);
                $(_this.overlay).height(_doc.body.offsetHeight);
                $(_this.overlay).css({
                    "margin-left": (-1 * (_contentDiv.offsetWidth / 2) - 30) + "px",
                    "margin-top": -(_doc.body.offsetHeight / 2)
                });
                $(_this.overlay).hide();

                _doc.body.style.overflow = "hidden";

                // Position center and animate in.
                var 
                    top = ($(window).height() / 2) - (_doc.body.offsetHeight / 2),
                    left = ($(window).width() / 2) - (_contentDiv.offsetWidth / 2);

                $(_this.overlay).animate({ opacity: "toggle" }, 250, function ()
                {
                    $(_this.loader).fadeOut(150);
                });

                _this.src = this.src;
            }
        });
    },
    open: function (url, elID)
    {
        var _this = this;

        // Needs refining.
        var scrollHeight = $(window).scrollTop() + ($(window).height() / 2) - ($(_this.loader).outerHeight() / 2);

        $(_this.modal).css({
            "height": document.body.offsetHeight
        }).fadeTo(250, 0.4);

        $(_this.loader).css({
            "top": scrollHeight
        }).fadeIn(250, function ()
        {
            $("#ifrOverlayForm")[0].src = url + (url.indexOf("?") > -1 ? "&" : "?") + "popup=true&r=" + parseInt(Math.random() * 10000);
            $(_this.overlay).css({
                "top": $(window).scrollTop() + ($(window).height() / 2)
            }).append('<a href="#" class="Close">Close</a>');

            $(".Close, .Cancel", _this.overlay).click(function (evt)
            {
                evt.preventDefault();
                _this.close();
            });
        });
    },
    openOLD: function (url, elID)
    {
        var _this = this;
        if (!$.browser.msie)
            $(_this.modal).fadeIn(250);
        else
            $(_this.modal).show();

        $(_this.loader).fadeIn(250, function ()
        {
            var xmlHTTP = _this.getXMLHTTP();
            if (xmlHTTP)
            {
                xmlHTTP.open("GET", url, false);
                xmlHTTP.send("");
                var failed = false;

                if (xmlHTTP.status != 200)
                    failed = true
                else
                {
                    var html = xmlHTTP.responseText;
                    var start = html.indexOf("<!-- Pop Up -->");
                    if (start > -1) start = html.indexOf(">", start);
                    if (start > -1)
                    {
                        var end = html.indexOf("<!-- /Pop Up -->");
                        if (end > -1)
                        {
                            // Add close button and behaviour.

                            $(".Frame")[0].src = url + (url.indexOf("?") > -1 ? "&" : "?") + "popup=true";
                            $(_this.overlay).append('<a href="#" class="Close">Close</a>');

                            //                            $(_this.overlay).html(html.substring(start + 1, end))
                            //                                            .append('<a href="#" class="Close">Close</a>');

                            $(".Close, .Cancel", _this.overlay).click(function (evt)
                            {
                                evt.preventDefault();
                                _this.close();
                            });

                            // Check for width override.
                            var qsm = new queryStringManager(url.substring(url.indexOf('?') + 1));
                            if (qsm.get("w", ""))
                            {
                                $(_this.overlay).width(parseInt(qsm.get("w", "")))
                                                .css({ "margin-left": -1 * ($(_this.overlay).outerWidth() / 2 + 30) });
                            }
                            else
                                $(_this.overlay).css({ "width": "", "margin-left": "" });
                            qsm = null;

                            // No one likes ugly radio buttons.
                            BeautifyRadioButtons();

                            // Position center and animate in.
                            var pos = ($(window).height() / 2) - ($(_this.overlay).outerHeight() / 2);

                            $(_this.overlay).css({
                                "top": pos - 50
                            })

                            $(_this.overlay).animate({ top: pos, opacity: "toggle" }, 250, function ()
                            {
                                $(_this.loader).fadeOut(150);
                            });
                        }
                        else
                            failed = true;
                    }
                    else
                        failed = true;
                }

                if (failed) window.open(url);
            }
        });
    },
    close: function ()
    {
        var _this = this;
        $(_this.loader).fadeOut(150);
        $(_this.overlay).fadeOut(250, function ()
        {
            if (!$.browser.msie)
                $(_this.modal).fadeOut(250);
            else
                $(_this.modal).hide();
        });
    },
    getXMLHTTP: function ()
    {
        var obj = null;

        try
        {
            obj = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e) { }

        if (obj == null)
        {
            try
            {
                obj = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch (e) { }
        }

        if ((obj == null) && (typeof XMLHttpRequest != "undefined"))
            obj = new XMLHttpRequest();

        return (obj);
    }
};