window.SOL = {

    viewer: {},

    views: [],

    mainNav: [],

    init: function () {
        this.views.push(
			{ name: "Story", menuId: "Story", src: "/view/story.html", public: true },
			{ name: "Character-Profiles", menuId: "Character-Profiles", src: "/view/characters.html", public: true },
			{ name: "Book-Preview", menuId: "Book-Preview", src: "/view/book-preview.html", public: true },
			{ name: "Fact-Vs-Fiction", menuId: "Fact-Vs-Fiction", src: "/view/fact-vs-fiction.html", public: true },
			{ name: "Order-The-Sons-Of-Liberty-Book", menuId: "Order-The-Sons-Of-Liberty-Book", src: "/view/get-the-book.html", public: true },
			{ name: "About-Us", menuId: "About-Us", src: "/view/about-us.html", public: true },
			{ name: "Contact-Us", menuId: "Contact-Us", src: "/view/contact-us.html", public: true },
			{ name: "Educator-Guide", menuId: "Educator-Guide", src: "/view/educator-guide.html", public: true }
		);
    },

    getViewByName: function (name) {
        for (var i = 0; i < SOL.views.length; i++) {
            if (SOL.views[i].name.replace(" ", "").toLowerCase() == name.replace(" ", "").toLowerCase()) {
                return SOL.views[i];
            }
        }
        return null;
    },

    modalWindow: {

        screen: null,

        showScreen: function () {
            if (!this.screen) {
                this.screen = $("<div id=\"OverlayScreen\" class=\"overlayScreen\"></div>");
                $(this.screen).css("opacity", "0.75");
                $(this.screen).appendTo("body");
            }
            $(this.screen).show();
        },

        hideScreen: function () {
            if (this.screen)
                $(this.screen).hide();
        },

        modalContentContainer: null,

        onLoad: { callback: null, callbackData: null },
        onClose: null,

        showModalContent: function (view) {
            console.log("show modal...");
            if (!SOL.modalWindow.modalContentContainer) {
                SOL.modalWindow.modalContentContainer = $("<div class=\"overlayScreenContentContainer\"></div>");
                $(SOL.modalWindow.modalContentContainer).appendTo("body");
            }

            $.get(view.src, function (data) {
                SOL.modalWindow.showScreen();
                data = "<a href=\"#\" class=\"closeModal\">X</a>" + data;
                $(SOL.modalWindow.modalContentContainer).html(data);
                $(SOL.modalWindow.modalContentContainer).show();
                if (typeof SOL.modalWindow.onLoad.callback === "function") {
                    SOL.modalWindow.onLoad.callback(SOL.modalWindow.onLoad.callbackData);
                    SOL.modalWindow.onLoad.callback = null;
                    SOL.modalWindow.onLoad.callbackData = null;
                }
                $(".closeModal").click(SOL.modalWindow.closeModalWindow);
            });
        },

        closeModalWindow: function () {
            if (typeof SOL.modalWindow.onClose === "function") {
                SOL.modalWindow.onClose();
                SOL.modalWindow.onClose = null;
            }
            if (SOL.modalWindow.modalContentContainer) {
                $(SOL.modalWindow.modalContentContainer).empty();
                $(SOL.modalWindow.modalContentContainer).hide();
            }
            if (SOL.modalWindow.screen) {
                $(SOL.modalWindow.screen).fadeOut(500);
            }
        }
    }
}

window.EvtMonitor = {
    initMonitor: function () {
        $(".footerMenuList a").click(this.handleViewLinkClick);
        this.mapUriRequest();
    },

    mapUriRequest: function () {
        var appPathExtension = location.pathname.toLowerCase().replace("/default.aspx", "");
        var view = this.findViewByTail(appPathExtension);
        if (view)
            this.loadView(view);
    },

    viewEvents: {
        profileBind: function (view) {
            $("ul.profile li.toggle").click(EvtMonitor.handleToggleControl);
            if (view.name == "Signup") {
                $("#saveProfileBtn").val("Create Profile");
                $("#saveProfileBtn").click(BBViewActions.createProfileRequest);
            }
            else {
                $("#saveProfileBtn").click(BBViewActions.saveProfileRequest);
            }
        }
    },

    handleViewEventBindings: function (view) {
        var viewBind = (view.symbolicLink) ? view.symbolicLink.replace(" ", "_").toLowerCase() + "Bind" : view.name.replace(" ", "_").toLowerCase() + "Bind";
        if (this.viewEvents[viewBind] && (typeof this.viewEvents[viewBind] === "function"))
            this.viewEvents[viewBind](view);
    },

    handleViewLinkClick: function (evt) {
        /*
        evt.preventDefault();
        var view = EvtMonitor.findView(evt.currentTarget.id);
        if (view != null) {
            $(".viewMenu .selected").text("Settings");
            EvtMonitor.loadView(view);
        }
        */
    },

    findView: function (viewId) {
        for (var i = 0; i < SOL.views.length; i++) {
            if (SOL.views[i].menuId == viewId) {
                return SOL.views[i];
            }
        }
        return null;
    },

    findViewByTail: function (urlPath) {
        for (var i = 0; i < SOL.views.length; i++) {
            if (SOL.views[i].name == urlPath) {
                return SOL.views[i];
            }
        }
        return null;
    },

    handleViewMenuClick: function (evt) {
        $(".viewMenu .menuSelections").toggle();
    },

    handleViewMenuMouseleave: function (evt) {
        $(".viewMenu .menuSelections").hide();
    },

    handleToggleControl: function (evt) {
        var notificationObject = SOL.dataLayer.getNotificationObjectById(evt.currentTarget.id);
        if ($(evt.currentTarget).hasClass("on")) {
            $(evt.currentTarget).removeClass("on").addClass("off");
            $(".toggleValue", evt.currentTarget).text(notificationObject.offValue);
        }
        else {
            $(evt.currentTarget).removeClass("off").addClass("on");
            $(".toggleValue", evt.currentTarget).text(notificationObject.onValue);
        }
    },

    handleViewMenuItemClick: function (evt) {
        var view = EvtMonitor.findView(evt.currentTarget.id);
        if (view != null)
            EvtMonitor.loadView(view);
        $(".viewMenu .selected").text("Account > " + $(evt.currentTarget).text());
        $(".viewMenu .menuSelections").toggle();
    },

    loadView: function (view) {
        $.get(view.src, function (data) {
            $("#AppSwfContainer").hide();
            $(".viewContainer").html(data);
            var completeAction = ((view.symbolicLink) ? view.symbolicLink.replace(/ /g, "_").toLowerCase() : view.name.replace(/ /g, "_").toLowerCase()) + "LoadViewComplete";
            if (BBViewActions[completeAction] && (typeof BBViewActions[completeAction] === "function"))
                BBViewActions[completeAction](view);
            EvtMonitor.handleViewEventBindings(view);
            SOLSwfProxy.closePlayer();
        });
    }

}

$(document).ready(
	function () {
		SOL.init();
		EvtMonitor.initMonitor();
	}
);
