﻿
var PageType = {
    External: 1,
    Discounts: 2,
    Benefits: 3,
    LifeEvents: 4
}

function PageController() {

    this.ChildNavigation;
    this.ModalWindow;
    this.SearchBox;

    this.Init = function(pageType) {

        this.SearchBox = new SearchBox();
        this.SearchBox.SearchField = $(".SearchControlsContainer").find("input");
        this.SearchBox.Init();

        this.ModalWindow = new ModalWindow();
        this.ModalWindow.Overlay = $("#divModalWindowOverlay");
        this.ModalWindow.Container = $("#divModalWindowContainer");
        this.ModalWindow.Init();

        switch (pageType) {
            case PageType.Discounts:
                this.InitDiscountsPage();
                break;
            case PageType.Benefits:
                this.InitBenefitsPage();
                break;
            case PageType.LifeEvents:
                this.InitLifeEventsPage();
                break;
            case PageType.ExternalPage:
            default:
                this.InitExternalPage();
                break;
        }

        delete this.Init;
    }

    this.InitDiscountsPage = function() {

        delete this.InitDiscountsPage;
    }

    this.InitBenefitsPage = function() {
    
        delete this.InitBenefitsPage;
    }

    this.InitLifeEventsPage = function() {
    
        delete this.InitLifeEventsPage;
    }

    this.InitExternalPage = function() {

        this.ModalWindow = new ModalWindow();
        this.ModalWindow.Overlay = $("#divModalWindowOverlay");
        this.ModalWindow.Container = $("#divModalWindowContainer");
        this.ModalWindow.Init();

        delete this.InitExternalPage;
    }
}

var ModalWindowType = {
    Ajax: 1, 
    HtmlElement: 2,
    IFrame: 3
};

function ModalWindow() {

    this.Container;
    this.CloseButton;
    this.Overlay;    
    this.ContentContainer;

    this.Visible = false;

    this.Init = function() {

        var modalWindow = this;
        this.ContentContainer = this.Container.find(".ModalWindowContentContainer");
        this.CloseButton = this.Container.find(".ModalCloseButton").click(function() { modalWindow.HideModal() });
        this.Overlay.click(function() { modalWindow.HideModal() });

        var modalImages = new Array();
        modalImages[0] = "/Images/Layout/NewLayout/ModalTopLeft.png";
        modalImages[1] = "/Images/Layout/NewLayout/ModalTop.png";
        modalImages[2] = "/Images/Layout/NewLayout/ModalTopRight.png";
        modalImages[3] = "/Images/Layout/NewLayout/ModalLeft.png";
        modalImages[4] = "/Images/Layout/NewLayout/ModalRight.png";
        modalImages[5] = "/Images/Layout/NewLayout/ModalBottomLeft.png";
        modalImages[6] = "/Images/Layout/NewLayout/ModalBottom.png";
        modalImages[7] = "/Images/Layout/NewLayout/ModalBottomRight.png";
        Utilities.PreLoadImages(modalImages);
        
        delete this.Init;
    };

    this.ShowModal = function(width, height, type, content, scrollbar) {

        var modalWindow = this;

        switch (type) {
            case ModalWindowType.Ajax:
                modalWindow.LoadAjaxContent(content);
                break;
            case ModalWindowType.HtmlElement:
                modalWindow.AppendHtmlContent(content);
                break;
            case ModalWindowType.IFrame:
                modalWindow.LoadIFrameContent(content);
                break;
            default:
                break;
        }

        this.Container.css(
            {
                "display": "block",
                "width": width,
                "height": height + 20,
                "top": this.GetPositionTop(height + 20),
                "left": this.GetPositionLeft(width)
            }
        );

        this.ContentContainer.css(
            {
                "width": width,
                "height": height,
                "overflow-y": scrollbar == null ? "hidden" : scrollbar ? "scroll" : "hidden"
            }
        );

        this.Overlay.css(
            {
                "display": "block",
                "height": $(document).height()
            }
        );

        if ($.browser.msie && $.browser.version.substr(0, 1) < 7) {
            $(document).find("select").css("visibility", "hidden");
        }

        this.Visible = true;

        return false;
    };

    this.HideModal = function() {

        this.Container.css("display", "none");
        this.Overlay.css("display", "none");

        if ($.browser.msie && $.browser.version.substr(0, 1) < 7) {
            $(document).find("select").css("visibility", "visible");
        }

        this.Visible = false;

        return false;
    };

    this.GetPositionTop = function(height) {
        return (($(window).height() - height) / 2);
    };

    this.GetPositionLeft = function(width) {
        return (($(window).width() - width) / 2);
    };

    this.LoadAjaxContent = function(url) {

        $.ajax(
            {
                url: url,
                success: function(data) {
                    contentLoaded(data);
                }
            });

        var modalWindow = this;
        var contentLoaded = function(data) {
            modalWindow.ContentContainer.html(data);
        }
    };

    this.LoadIFrameContent = function(url) {
        this.ContentContainer.find("iframe")
            .attr({ "src": url
            }).css("display", "block");
    }

    this.AppendHtmlContent = function(contentElement) {
        contentElement.css("display", "block");
        this.ContentContainer.html(contentElement);
    };
}

function SearchBox() {

    this.SearchField;
    this.BlurredValue;

    this.Init = function() {
        var searchBox = this;
        this.BlurredValue = this.SearchField.val();
        this.SearchField.focus(function() {
            searchBox.SearchFocus();
        });
        this.SearchField.blur(function() {
            searchBox.SearchBlur();
        });
        delete this.Init;
    };

    this.SearchFocus = function() {
        if (this.SearchField.val() == this.BlurredValue) {
            this.SearchField.val("");
        }
    };

    this.SearchBlur = function() {
        if (this.SearchField.val() == "") {
            this.SearchField.val(this.BlurredValue);
        }
    };
}

var Utilities = {

    UnescapeHtml: function(html) {
        var temp = document.createElement("div");
        temp.innerHTML = html;
        var unescapedString = temp.childNodes[0].nodeValue;
        temp.removeChild(temp.firstChild);
        return unescapedString;
    },

    PreLoadImages: function(images) {
//        for (var i in images) {
//            $("body").append($(document.createElement("img")).attr("src", images[i]).css().css("display", "none"));
//        }
    }
};

function MethodX() {
    $(".GlobalHeaderContainer").css("background-image", "url(https://images-na.ssl-images-amazon.com/images/G/01/gno/images/holiday09/holiday09_sprite._V228084651_.png)")
}