﻿/* 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)
            {
                var additionalClass = "";
                if (document.links[i].className.indexOf("PopUp-") != -1)
                {
                    var classes = document.links[i].className.split(' ');
                    for (var j = 0; j < classes.length; j++)
                    {
                        if (classes[j].indexOf("PopUp-") != -1)
                        {
                            additionalClass = classes[j];
                            break;
                        }
                    }
                }

                document.links[i].id = "popup" + i;
                if (additionalClass != "")
                    document.links[i].href = "javascript:popupmanager.open('" + document.links[i].href + "', 'popup" + i + "', '" + additionalClass + "');";
                else
                    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);
                // HACK - some browsers (firefox!) auto zoom the image to fit to the tiny iframe
                $("ifrOverlayForm").height($(_contentDiv).height());
                //document.getElementById("ifrOverlayForm").style.height = "400px";
                $(_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, popupClass)
    {
        var _this = this;

        // Needs refining.
        var scrollHeight = $(window).scrollTop() + ($(window).height() / 2) - ($(_this.loader).outerHeight() / 2);

        if (popupClass)
            $(_this.overlay).addClass(popupClass);
        else
            $(_this.overlay).removeClass().addClass("Overlay-Form");

        $(_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 ()
        {
            $(".Close", _this.overlay).remove();
            if (!$.browser.msie)
                $(_this.modal).fadeOut(250);
            else
                $(_this.modal).hide();
        });
    },
    resize: function (height, callback)
    {
        var _this = this;
        $(_this.overlay).animate({ "height": height }, 500);
        if (callback)
            callback();
    },
    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);
    }
};
