﻿/// <reference name="MicrosoftAjax.js" />
/// <reference path="NavigationItemModel.js" />
Type.registerNamespace("ServerTransformed");

ServerTransformed.MainNavigationModel = function() 
{
    this._currentPageState = "mainNavigation";
    
    this._navigationItems = new Array();
    
    this._interval = 20000;
    this._onTickHandler = Function.createDelegate(this, this._showImpatientVideo);
    
}

ServerTransformed.MainNavigationModel.prototype =
{
    set_page: function(value)
    {
        this._currentPageState = value;
        this._videoURL = "";

        this._animationName = "Exit";

        var loopVideo = false;
        var videoLocation = "2";
        switch (this._currentPageState)
        {
            case "Security":
            case "Stability":
            case "Manageability":
            case "Web_Innovations":
            case "Customer":
            case "Meet IT 24-7":
            case "Toy Box":
            case "Hyper-V":
            case "Download Trial":
                if (this._updateTimer)
                {
                    this._updateTimer.set_enabled(false);
                    this._updateTimer = null;
                }
                this._animationName = "Exit";
                this._raiseEvent("exit", Sys.EventArgs.Empty);
                break;
            case "mainNavigation":
            default:
                this._resetImpatientTimerHandler = new Function.createDelegate(this, this._resetImpatientTimer);
                this._videoURL = ServerTransformed.Res.URL['mainNavigation'];
                this._impatientVideoURL = ServerTransformed.Res.URL['impatient'];
                this._animationName = "Intro";
                this.set_pageURL("xaml/primary_nav.xaml");
                loopVideo = true;
                videoLocation = "1";
                this._raiseEvent("videoChanged", { loop: loopVideo, location: videoLocation });
                break;
        }


    },

    _resetImpatientTimer: function(sender, eventArgs)
    {
        if (this._updateTimer)
        {
            this._updateTimer.set_enabled(false);
            this._updateTimer = null;
        }
        this._updateTimer = $create(ServerTransformed.Timer, { interval: this._interval, enabled: true }, { tick: this._onTickHandler }, null, null);
    },

    set_pageURL: function(value)
    {
        this._raiseEvent("loadPage", { url: value });
    },

    set_pageXAML: function(value)
    {
        this._pageXAML = value;
        this._raiseEvent("pageLoaded");
    },

    get_pageXAML: function()
    {
        return this._pageXAML;
    },

    get_events: function()
    {
        if (!this._events)
        {
            this._events = new Sys.EventHandlerList();
        }
        return this._events;
    },

    get_closeCommand: function() { return this._currentPageState; },
    set_closeCommand: function(value) { this._currentPageState = value; },

    get_animation: function()
    {
        return this._animationName;
    },

    get_video: function()
    {
        return this._videoURL;
    },

    get_impatientVideo: function()
    {
        return this._impatientVideoURL;
    },

    add_exitHandler: function(handler) { this.get_events().addHandler("exit", handler); },
    remove_exitHandler: function(handler) { this.get_events().removeHandler("exit", handler); },

    add_pageLoadedHandler: function(handler) { this.get_events().addHandler("pageLoaded", handler); },
    remove_pageLoadedHandler: function(handler) { this.get_events().removeHandler("pageLoaded", handler); },

    add_loadPageHandler: function(handler) { this.get_events().addHandler("loadPage", handler); },
    remove_loadPageHandler: function(handler) { this.get_events().removeHandler("loadPage", handler); },

    add_loadFontsHandler: function(handler) { this.get_events().addHandler("loadFonts", handler); },
    remove_loadFontsHandler: function(handler) { this.get_events().removeHandler("loadFonts", handler); },

    add_videoChangedHandler: function(handler) { this.get_events().addHandler("videoChanged", handler); },
    remove_videoChangedHandler: function(handler) { this.get_events().removeHandler("videoChanged", handler); },

    add_fontsLoadedHandler: function(handler) { this.get_events().addHandler("fontsLoaded", handler); },
    remove_fontsLoadedHandler: function(handler) { this.get_events().removeHandler("fontsLoaded", handler); },

    add_playImpatientVideoHandler: function(handler) { this.get_events().addHandler("playImpatientVideo", handler); },
    remove_playImpatientVideoHandler: function(handler) { this.get_events().removeHandler("playImpatientVideo", handler); },

    set_fontsLoaded: function(value)
    {
        this._raiseEvent("fontsLoaded");
    },

    _raiseEvent: function(eventName, eventArgs)
    {
        var handler = this.get_events().getHandler(eventName);

        if (handler)
        {
            if (!eventArgs) eventArgs = Sys.EventArgs.Empty;
            handler(this, eventArgs);
        }
    },

    _pageChanged: function(sender, eventArgs)
    {
        var uri = '/ws2008/';
        var title = 'Home: ';
        switch (eventArgs.name)
        {
            case "Security":
                uri = uri + "security";
                title = title + "Security";
                break;
            case "Stability":
                uri = uri + "stability";
                title = title + "Stability";
                break;
            case "Manageability":
                uri = uri + "manageability";
                title = title + "Manageability";
                break;
            case "Web_Innovations":
                uri = uri + "webinnovations";
                title = title + "Web Innovations";
                break;
            case "Hyper_V":
                uri = uri + "hyperv";
                title = title + "Hyper-V";
                break;
            case "Customer":
                uri = uri + "videos/customer";
                title = title + "Customer Videos";
                break;
            case "Meet IT 24-7":
                uri = uri + "videos/product";
                title = title + "Product Videos";
                break;
            case "Toy Box":
                uri = uri + "toybox";
                title = title + "Toy Box";
                break;
            case "Download Trial":
                uri = uri + "trial";
                title = title + "Download Trial";
                break;
        }
        dcsMultiTrack('DCS.dcsuri', uri, 'WT.ti', title, 'WT.cg_n', 'Home', 'DCSext.ws2008', '', 'WT.dl', '6');
        this.set_page(eventArgs.name);
    },

    _itemMouseOver: function(sender, eventArgs)
    {
        Array.forEach(this._navigationItems,
            function(element, i, array)
        {
            if (element.get_properties().name != this.get_properties().name)
            {
                if ((element.get_properties().hasSubMenu && !this.get_properties().isSubMenu) || !this.get_properties().isSubMenu)
                {
                    element.playMouseNonHoverOverAnimation();
                }
            }
            else
            {
                element.showMoreAnimation();
            }
        }, sender);
    },

    _itemMouseOut: function(sender, eventArgs)
    {
        Array.forEach(this._navigationItems,
            function(element, i, array)
        {
            if (element.get_properties().name != this.get_properties().name)
            {
                if (!this.get_properties().isSubMenu)
                {
                    element.playMouseNonHoverOutAnimation();
                }
            }
            else
            {
                element.hideMoreAnimation();
            }
        }, sender);
    },

    _buildNavItem: function(realName, fileName, hasSubMenu, isSubMenu)
    {
        var name = realName;
        var filter = "Natural";
        if (isSubMenu)
            naturalAnimationSet = this._buildAnimationSet(fileName, "_Hover", "_Natural", "_Natural");
        else
            naturalAnimationSet = this._buildAnimationSet(fileName, "_Natural_Hover", "_Natural_Default", "_Natural_Non_Hover");

        animationSets = [naturalAnimationSet];
        animationSets["Natural"] = animationSets[0];
        var args = {};
        args.name = name;
        args.filter = filter;
        args.animationSets = animationSets;
        args.hasSubMenu = hasSubMenu;
        args.isSubMenu = isSubMenu;

        if (!isSubMenu)
        {
            args.floatingAnimation = fileName + "_Floating_Loop";
            args.showMoreAnimation = fileName + "_Show_More";
            args.hideMoreAnimation = fileName + "_Hide_More";
        }

        var model = new ServerTransformed.NavigationItemModel(args);
        var view = new ServerTransformed.NavigationItemView(this._xamlGetElementByID(fileName.toLowerCase() + "_btn"));
        var presenter = new ServerTransformed.NavigationItemPresenter(model, view);

        if (!hasSubMenu)
            model.add_MouseClickHandler(Function.createDelegate(this, this._pageChanged));
        model.add_MouseOverHandler(Function.createDelegate(this, this._itemMouseOver));
        model.add_MouseOutHandler(Function.createDelegate(this, this._itemMouseOut));
        model.add_AnyEventHandler(Function.createDelegate(this, this._resetImpatientTimerHandler));
        model.floatThem();
        return model;

    },

    _buildAnimationSet: function(name, over, out, non)
    {
        var retVal = {};
        retVal.mouseOverAnimation = this._xamlGetElementByID(name + over);
        retVal.mouseOutAnimation = this._xamlGetElementByID(name + out);
        retVal.mouseNonHoverAnimation = this._xamlGetElementByID(name + non);
        return retVal;
    },

    _xamlGetElementByID: function(id)
    {
        return SLController.get_controller().content.FindName(id);
    },

    configureNavigationItems: function()
    {
        this._navigationItems[this._navigationItems.length] = this._buildNavItem("Security", "Security", false, true);
        this._navigationItems[this._navigationItems.length] = this._buildNavItem("Stability", "Stability", false, true);
        this._navigationItems[this._navigationItems.length] = this._buildNavItem("Manageability", "Manageability", false, true);
        this._navigationItems[this._navigationItems.length] = this._buildNavItem("Web_Innovations", "Web_Innovations", false, true);

        this._navigationItems[this._navigationItems.length] = this._buildNavItem("Hyper-V", "Hyper_V", false, false);
        this._navigationItems[this._navigationItems.length] = this._buildNavItem("Innovations", "Innovations", true, false);
        this._navigationItems[this._navigationItems.length] = this._buildNavItem("Customer", "Customer", false, false);
        this._navigationItems[this._navigationItems.length] = this._buildNavItem("Meet IT 24-7", "Meet_IT_24_7", false, false);
        this._navigationItems[this._navigationItems.length] = this._buildNavItem("Toy Box", "Toy_Box", false, false);
        this._navigationItems[this._navigationItems.length] = this._buildNavItem("Download Trial", "Trial", false, false);
    },

    loadFonts: function()
    {
        this._raiseEvent("loadFonts");
    },

    _showImpatientVideo: function(sender, eventArgs)
    {
        this._raiseEvent("playImpatientVideo");
    },

    exit: function()
    {
        this._raiseEvent("exit");
    }
}

ServerTransformed.MainNavigationModel.registerClass("ServerTransformed.MainNavigationModel");