﻿/// <reference Name="MicrosoftAjax.js" />

Type.registerNamespace("ServerTransformed");

ServerTransformed.ToyBoxModel = function(pageName)
{
    this._name = pageName;    
    switch(this._name)
    {
        case "Web_Innovations":
            this._fileName = "web_innovations";
            break;
        case "Meet IT 24-7":
            this._fileName = "meet_it_24_7";
            break;
        case "Toy Box":
            this._fileName = "toy_box";
            break;
        case "Security":
        case "Stability":
        case "Manageability":
        case "Customer":
        default:
            this._fileName = pageName.toLowerCase();
            break;
    }
    
    this._introVideoURL = ServerTransformed.Res.URL[this._fileName];
    this._exitVideoURL = ServerTransformed.Res.URL[this._fileName + "Reverse"];
    this._navigationItems = new Array();
    this._nextPage = "Home";
    this._currentAnimation = "Intro";
    this._tabName = "";
    this._currentTabContent = "";
    this._initializedTabs = new Array();
}

ServerTransformed.ToyBoxModel.prototype =
{
    init: function()
    {
        this.set_pageURL("xaml/" + this._fileName + ".xaml");
        this._tabNameChangedDelegate = Function.createDelegate(this, this._tabNameChanged);

        this.add_tabChangedHandler(this._tabNameChangedDelegate);
    },

    loadFonts: function() { this._raiseEvent("loadFonts"); },

    exit: function() { this._raiseEvent("exit"); },

    set_pageURL: function(value)
    {
        this._raiseEvent("loadPage", { url: value });
    },

    set_pageXAML: function(value)
    {
        this._pageXAML = value;
        this._raiseEvent("pageLoaded");
    },

    get_currentTabContent: function() { return this._currentTabContent; },
    set_currentTabContent: function(value)
    {
        this._tabItems = new Array();
        for (var i = 0; i < value.childNodes.length; i++)
        {
            var currentXAMLNode = this._xamlGetElementByID(value.childNodes[i].id);
            if (value.childNodes[i].nodeName == "DIV" && value.childNodes[i].id == "Trial_1")
            {
                continue;
            }
            var currentIndex = this._tabItems.length;
            this._tabItems[currentIndex] = {};
            this._tabItems[currentIndex].ID = value.childNodes[i].id;
            this._tabItems[currentIndex].Links = new Array();
            for (var j = 0; j < value.childNodes[i].childNodes.length; j++)
            {
                var currentLink = "";
                if (value.childNodes[i].childNodes[j].nodeName == "IMG")
                {
                    this._xamlGetElementByID(value.childNodes[i].id + "_Thumbnail_Image").Source =
                        value.childNodes[i].childNodes[j].src;
                    continue;
                }
                else if (value.childNodes[i].childNodes.length == 2)
                {
                    currentLink = this._xamlGetElementByID(value.childNodes[i].id + "_Download");
                }
                else
                {
                    currentLink = this._xamlGetElementByID(value.childNodes[i].id + "_Link" + j);
                }

                var text = value.childNodes[i].childNodes[j].innerHTML;
                var url = value.childNodes[i].childNodes[j].href;
                this._tabItems[currentIndex].Links[this._tabItems[currentIndex].Links.length] = { name: text, value: url };
                currentLink.Text = text;


                if (!Array.contains(this._initializedTabs, value.id))
                {
                    var delegate = Function.createDelegate(this, this._findURL);
                    currentLink.addEventListener("MouseLeftButtonUp", delegate);
                }
            }
        }
        this._currentTabContent = value.id;
        if (!Array.contains(this._initializedTabs, value.id))
            this._initializedTabs[this._initializedTabs.length] = value.id;
        this._raiseEvent("tabContentChanged", Sys.EventArgs.Empty);
    },

    _findURL: function(sender, eventArgs)
    {
        //this is an ugly hack. I need to send the number or name in some other way
        var nameSplit = sender.name.split('_');
        // this reads:
        // get the integer that I know by convention is located in the second to last element in this array
        // then, because arrays are zero based, subtract one from that number.
        var itemNumber = nameSplit[nameSplit.length - 2] - 1;
        var currentSection = this._tabItems[itemNumber];
        for (var i = 0; i < currentSection.Links.length; i++)
        {
            if (currentSection.Links[i].name != sender.Text)
            {
                continue;
            }

            var uri = "/ws2008/toybox/" + this._tabName.toLowerCase().replace(' ', '').replace('_', '') + '/download';
            var title = "Toy Box: " + this._tabName.replace('_', ' ') + ' Download';
            dcsMultiTrack('DCS.dcsuri', uri, 'WT.ti', title, 'WT.cg_n', 'Toy Box', 'DCSext.ws2008', '', 'WT.dl', '6');

            // VV
            // added 3/5/2008 for downloading screensavers
            var URLstring = currentSection.Links[i].value;

            //if screen saver, open download dialog   
            if ((URLstring.search(/.exe/ig)) != -1)
            {

                //if firefox
                if (Sys.Browser.name.indexOf("Firefox") >= 0)
                {
                    window.open(currentSection.Links[i].value);
                }
                else
                {
                    window.navigate(currentSection.Links[i].value);
                }
            }

            //if other download, open new window
            else
            {
                window.open(currentSection.Links[i].value);
            }

            //window.open(currentSection.Links[i].value);


            break;
        }
    },

    get_pageXAML: function() { return this._pageXAML; },

    get_animation: function() { return this._currentAnimation; },

    get_currentName: function() { return this._tabName; },
    set_currentName: function(value) { this._tabName = value; },

    get_introVideo: function() { return this._introVideoURL; },

    get_exitVideo: function() { return this._exitVideoURL; },

    get_closeCommand: function() { return this._nextPage; },
    set_closeCommand: function(value) { this._nextPage = value; },

    add_tabChangedHandler: function(handler) { this.get_events().addHandler("tabNameChanged", handler); },
    remove_tabChangedHandler: function(handler) { this.get_events().removeHandler("tabNameChanged", handler); },

    add_contentChangedHandler: function(handler) { this.get_events().addHandler("tabContentChanged", handler); },
    remove_contentChangedHandler: function(handler) { this.get_events().removeHandler("tabContentChanged", handler); },

    add_loadFontsHandler: function(handler) { this.get_events().addHandler("loadFonts", handler); },
    remove_loadFontsHandler: function(handler) { this.get_events().removeHandler("loadFonts", handler); },

    add_loadPageHandler: function(handler) { this.get_events().addHandler("loadPage", handler); },
    remove_loadPageHandler: function(handler) { this.get_events().removeHandler("loadPage", handler); },

    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_pageUnloadHandler: function(handler) { this.get_events().addHandler("unloadPage", handler); },
    remove_pageUnloadHandler: function(handler) { this.get_events().removeHandler("unloadPage", handler); },

    add_fontsLoadedHandler: function(handler) { this.get_events().addHandler("fontsLoaded", handler); },
    remove_fontsLoadedHandler: function(handler) { this.get_events().removeHandler("fontsLoaded", handler); },

    set_fontsLoaded: function(value)
    {
        this._raiseEvent("fontsLoaded");
    },

    get_events: function()
    {
        if (!this._events)
        {
            this._events = new Sys.EventHandlerList();
        }
        return this._events;
    },

    _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)
    {
        this._nextPage = eventArgs.name;

        if (this._nextPage == "Back")
        {
            this._currentAnimation = "Reverse";
            this._raiseEvent("unloadPage");
            return;
        }
        else
        {
            eventArgs.tabType = "toyBox";
            this._raiseEvent("tabNameChanged", eventArgs);
        }
    },

    _buildNavItem: function(realName, fileName)
    {

        var name = realName;
        var filter = "Natural";
        naturalAnimationSet = this._buildAnimationSet(fileName, "_btn_Hover", "_btn_Default", "_btn_Hot");
        animationSets = [naturalAnimationSet];
        animationSets["Natural"] = animationSets[0];
        var args = {};
        args.name = name;
        args.filter = filter;
        args.animationSets = animationSets;

        var model = new ServerTransformed.NavigationItemModel(args);
        var view = new ServerTransformed.NavigationItemView(this._xamlGetElementByID(fileName + "_btn"));
        var presenter = new ServerTransformed.NavigationItemPresenter(model, view);

        model.add_MouseClickHandler(Function.createDelegate(this, this._pageChanged));

        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._navigationItems["Trial"] = this._buildNavItem("Trial", "Trial");
        this._navigationItems[this._navigationItems.length] = this._navigationItems["Wallpapers"] = this._buildNavItem("Wallpapers", "Wallpapers");
        this._navigationItems[this._navigationItems.length] = this._navigationItems["Screen_Savers"] = this._buildNavItem("Screen_Savers", "Screen_Savers");
        this._navigationItems[this._navigationItems.length] = this._navigationItems["Buddy_Icons"] = this._buildNavItem("Buddy_Icons", "Buddy_Icons");
        this._navigationItems[this._navigationItems.length] = this._navigationItems["Back"] = this._buildNavItem("Back", "Back");
        this._navigationItems["Trial"].set_enabled(false);
    },

    removeHandlersForExit: function()
    {
        Array.forEach(this._navigationItems, function(element) { element.exit(); }, this);
    },

    _tabNameChanged: function(sender, eventArgs)
    {
        this.trackEvent();
        Array.forEach(this._navigationItems,
            function(element, i, array)
        {
            if (this._tabName == element.get_properties().name)
                element.set_enabled(false);
            else
                element.set_enabled(true);
        }, this);
    },

    trackEvent: function()
    {
        var uri = "/ws2008/toybox/" + this._tabName.toLowerCase().replace(' ', '').replace('_', '');
        var title = "Toy Box: " + this._tabName.replace('_', ' ');
        dcsMultiTrack('DCS.dcsuri', uri, 'WT.ti', title, 'WT.cg_n', 'Toy Box', 'DCSext.ws2008', '', 'WT.dl', '6');
    }
}

ServerTransformed.ToyBoxModel.registerClass("ServerTransformed.ToyBoxModel");