﻿if (!window.XboxLiveMarketplace)
    window.XboxLiveMarketplace = {};

// prototype
XboxLiveMarketplace.EpixPrototype = function(plugIn, target, epixXml, dataLocation, language, fontName, fontSource)
{
    this.fontSource = fontSource;
    this.dataLocation = dataLocation;
    this.language = language;
    this.fontName = fontName;
    //alert('XboxLiveMarketplace.EpixPrototype constructor invoked');
    this.plugIn = plugIn;
    this.target = target;
    this.initInterval = 0;
    this.mouseLeftInterval = 0;
    this.mouseRightInterval = 0;
    this.loop = false;
    this.items = [];
    this.currentIndex = 0;
    this.epixXml = epixXml;

    this.itemContainer = this.target.findName("epixItemContainer");
    this.controllerContainer = this.target.findName("epixControllerContainer");
    this.left = this.target.findName("MouseLeftHitTarget");
    this.right = this.target.findName("MouseRightHitTarget");
    this.leftArrow = this.target.findName("ArrowLeft");
    this.rightArrow = this.target.findName("ArrowRight");
    this.background = this.target.findName("background");
    this.heading = this.target.findName("heading");
    this.applyFont(this.heading);
    this.api = new XboxLiveMarketplace.ContentAPI(this.itemContainer, Silverlight.createDelegate(this, this.onDataReady));
    this.isIE = (navigator.userAgent.indexOf("MSIE") != -1)
}
XboxLiveMarketplace.EpixPrototype.prototype =
{
    applyFont: function(textfield)
    {
        textfield.setFontSource(this.fontSource);
        textfield.fontFamily = this.fontName;
        try
        {
        }
        catch (error)
        {
        }
    },
    onMouseWheelMove: function(delta)
    {
        //alert('onMouseWheelMove');
        this.move(delta == 1);
    },
    onMouseDown: function(sender, mouseEventArgs)
    {
        //alert(sender);
        window.location.href = this.data[parseInt(sender.findName("uid").Text)].productUrl;
    },
    onHoverOver: function(item)
    {
        if (item != null)
        {
            item.shine["Visibility"] = "Visible";
            item.shine["Opacity"] = .3;
            //item.xaml.Cursor = "Hand";
            //item.leftArrow["Opacity"] = 1;
            //item.rightArrow["Opacity"] = 1;
        }
    },
    onHoverOut: function(item)
    {
        if (item != null)
        {
            item.shine["Visibility"] = "Collapsed";
            item.shine["Opacity"] = .0;
            //item.xaml.Cursor = "Default";
            //item.leftArrow["Opacity"] = 0;
            //item.rightArrow["Opacity"] = 0;
        }
    },
    onItemMouseEnter: function(sender, mouseEventArgs)
    {
        // this just turns the opacity of the arrows on,
        // however, if they are not visible they won't appear
        // they won't be visible unless they are the first or last items in the list
        var idx = parseInt(sender.findName("uid").Text)
        this.onHoverOver(this.items[idx]);
    },
    onItemMouseLeave: function(sender, mouseEventArgs)
    {
        var idx = parseInt(sender.findName("uid").Text)
        this.onHoverOut(this.items[idx]);
    },
    onMouseLeftEnter: function(sender, mouseEventArgs)
    {
        this.rightArrow.Visibility = "Visible";
        this.leftArrow.Visibility = "Visible";
        clearInterval(this.mouseRightInterval);
        clearInterval(this.mouseLeftInterval);
    },
    onMouseLeftLeave: function(sender, mouseEventArgs)
    {
        clearInterval(this.mouseLeftInterval);
        this.rightArrow.Visibility = "Collapsed";
        this.leftArrow.Visibility = "Collapsed";
    },
    onMouseLeftInterval: function(sender, mouseEventArgs)
    {
        //alert('onMouseWheelInterval');
        this.move(false);
    },
    onMouseLeftDown: function(sender, mouseEventArgs)
    {
        //alert('onMouseLeftDown');
        this.Controller.xaml.findName('center').Visibility = "Collapsed";
        this.Controller.xaml.findName('left').Visibility = "Collapsed";
        this.Controller.xaml.findName('farleft').Visibility = "Visible";
        this.Controller.xaml.findName('right').Visibility = "Collapsed";
        this.Controller.xaml.findName('farright').Visibility = "Collapsed";
        this.move(false);
    },
    onMouseRightDown: function(sender, mouseEventArgs)
    {
        //alert('onMouseRightDown');
        this.Controller.xaml.findName('center').Visibility = "Collapsed";
        this.Controller.xaml.findName('left').Visibility = "Collapsed";
        this.Controller.xaml.findName('farleft').Visibility = "Collapsed";
        this.Controller.xaml.findName('right').Visibility = "Collapsed";
        this.Controller.xaml.findName('farright').Visibility = "Visible";
        this.move(true);
    },
    setMoving: function()
    {
    },
    onMouseRightEnter: function(sender, mouseEventArgs)
    {
        this.rightArrow.Visibility = "Visible";
        this.leftArrow.Visibility = "Visible";
        clearInterval(this.mouseLeftInterval);
        clearInterval(this.mouseRightInterval);
    },
    onMouseRightLeave: function(sender, mouseEventArgs)
    {
        clearInterval(this.mouseRightInterval);
        this.rightArrow.Visibility = "Collapsed";
        this.leftArrow.Visibility = "Collapsed";
    },
    onMouseRightInterval: function(sender, mouseEventArgs)
    {
        //alert('onMouseRightInterval');
        this.move(true);
    },
    onMouseEnter: function(sender, mouseEventArgs)
    {
        // xbox.com bug 11172 - we don't want scroll wheel to work anymore
        /*
        // todo - implement for firefox
        if(this.isIE)
        {
        this.mouseWheel = new MouseWheel();
        this.mouseWheel.onMouseWheelMove = Silverlight.createDelegate(this, this.onMouseWheelMove);
        }
        */
    },
    onMouseLeave: function(sender, mouseEventArgs)
    {
        // xbox.com bug 11172 - we don't want scroll wheel to work anymore
        /*
        this.mouseWheel.ignoreMouseWheel();
        */
    },
    init: function(itemXaml)
    {
        this.data = [];
        this.itemXaml = itemXaml;
        this.api.load(this.epixXml, 'slots', 'slot');
    },
    onDataReady: function(xml, parentXml)
    {
        var appName = parentXml.getAttribute("heading");
        if (appName != null)
        {
            this.heading.Text = appName;
        }
        for (var i = 0; i < xml.length; i++)
        {
            // get the data...
            var slideType = xml[i].getAttribute("type");
            var slideName = "";
            var slideTitle = "";
            var slideDescription = "";
            var slideAssetUrl = "";
            var slideUrl = "";
            var curNode;
            var curNodeValue = "";
            for (j = 0; j < xml[i].childNodes.length; j++)
            {
                curNode = xml[i].childNodes[j];
                if (curNode.nodeType == 1)
                {
                    curNodeValue = "";
                    if (curNode.hasChildNodes())
                    {
                        if ((curNode.firstChild.nodeType == 3) || (curNode.firstChild.nodeType == 4)) // plain text or CDATA
                        {
                            curNodeValue = curNode.firstChild.nodeValue;
                        }
                    }
                    switch (curNode.nodeName.toLowerCase())
                    {
                        case "name":
                            slideName = curNodeValue;
                            break;
                        case "title":
                            slideTitle = curNodeValue;
                            break;
                        case "description":
                            slideDescription = curNodeValue;
                            break;
                        case "asseturl":
                            slideAssetUrl = curNodeValue;
                            break;
                        case "url":
                            slideUrl = curNodeValue;
                            break;
                    }
                }
            }
            var data = new XboxLiveMarketplace.EpixItemData(slideName, slideTitle, slideDescription, slideAssetUrl, slideUrl, slideType);

            this.data.push(data);
        }
        this.left.AddEventListener("MouseEnter", Silverlight.createDelegate(this, this.onMouseLeftEnter));
        this.left.AddEventListener("MouseLeave", Silverlight.createDelegate(this, this.onMouseLeftLeave));
        this.left.AddEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.onMouseLeftDown));
        this.right.AddEventListener("MouseEnter", Silverlight.createDelegate(this, this.onMouseRightEnter));
        this.right.AddEventListener("MouseLeave", Silverlight.createDelegate(this, this.onMouseRightLeave));
        this.right.AddEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.onMouseRightDown));
        this.left.AddEventListener("MouseLeftButtonUp", Silverlight.createDelegate(this, this.onControllerUp));
        this.right.AddEventListener("MouseLeftButtonUp", Silverlight.createDelegate(this, this.onControllerUp));

        this.target.AddEventListener("MouseEnter", Silverlight.createDelegate(this, this.onMouseEnter));
        this.target.AddEventListener("MouseLeave", Silverlight.createDelegate(this, this.onMouseLeave));

        // xbox.com bug 11172 - we don't want scroll wheel to work anymore
        /*
        this.mouseWheel = new MouseWheel();
        this.mouseWheel.ignoreMouseWheel();
        */

        this.itemCount = this.data.length;
        this.currentIndex = 1; // zero based
        this.maxIndex = this.data.length - 1; // zero based

        this.createItems(this.itemXaml);
        this.initInterval = setInterval(Silverlight.createDelegate(this, this.onInitInterval), 50);
    },
    onInitInterval: function()
    {
        if (this.currentIndex > 0)
        {
            this.move(false);
        }
        else
        {
            clearInterval(this.initInterval);
        }
    },
    move: function(isForward)
    {

        var item1 = null;
        var item2 = null;
        var item3 = null;
        var item4 = null;
        var item0 = null; // item off the left side of the screen, may not be required
        var item5 = null; // item off the right side of the screen, may not be required

        item0 = this.getItemFromCurrentIndex(-1);
        item1 = this.getItemFromCurrentIndex(0);
        item2 = this.getItemFromCurrentIndex(1);
        item3 = this.getItemFromCurrentIndex(2);
        item4 = this.getItemFromCurrentIndex(3);
        item5 = this.getItemFromCurrentIndex(4);

        if (isForward)
        {
            if (item2 == null)
                return; // can't proceed, there's nothing to scroll into position 1.
            this.currentIndex = this.currentIndex + 1;
            if (item1 != null)
                item1.step6.Begin();
            if (item2 != null)
                item2.step5.Begin();
            if (item3 != null)
                item3.step4.Begin();
            if (item4 != null)
                item4.step3.Begin();
            if (item5 != null)
                item5.step2.Begin();

        } 
        else
        {
            if (item0 == null)
                return; // can't proceed, there's nothing to scroll onto the screen.
            this.currentIndex = this.currentIndex - 1;
            if (item0 != null)
                item0.stepBack6.Begin();
            if (item1 != null)
                item1.stepBack5.Begin();
            if (item2 != null)
                item2.stepBack4.Begin();
            if (item3 != null)
                item3.stepBack3.Begin();
            if (item4 != null)
                item4.stepBack2.Begin();
        }
    },
    getItemFromCurrentIndex: function(offset)
    {
        var index = this.currentIndex + offset;
        if (index < 0)
        {
            return null;
        }
        else if (index > this.maxIndex)
        {
            return null;
        }
        else
        {
            return this.items[index];
        }
    },
    createItems: function(itemXaml)
    {
        //alert('createItems invoked, itemCount: ' + this.itemCount);
        // add the items to the stage
        var item = null;

        for (var i = 0; i < this.itemCount; i++)
        {
            //alert(i);
            item = this.plugIn.content.createFromXaml(itemXaml, true);
            item["Canvas.Left"] = -90;
            item["Canvas.ZIndex"] = (this.itemCount - i) + 10; // first item in the array should have the largest Z-index

            this.itemContainer.children.add(item);

            item.findName("debugText2").Text = item.findName("debugText3").Text = this.data[i].title;
            item.findName("debugText").Text = item.findName("debugText1").Text = this.data[i].description;

            this.applyFont(item.findName("debugText"));
            this.applyFont(item.findName("debugText1"));
            this.applyFont(item.findName("debugText2"));
            this.applyFont(item.findName("debugText3"));

            var slide = new XboxLiveMarketplace.EpixSlide(item, i, -1, -1, i);
            this.items.push(slide);
            slide.init();
            if (this.data[i].assetType == "WMV")
            {
                slide.mediaCanvas.Visibility = "Visible";
                slide.media.Source = this.data[i].assetUrl;
                slide.media.AddEventListener("MediaEnded", Silverlight.createDelegate(this, this.onMediaEnded));
                slide.media.Play();
            }
            else
            {
                slide.image.Source = slide.reflection.Source = this.data[i].assetUrl;
            }
            slide.target.AddEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.onMouseDown));
            slide.target.AddEventListener("MouseEnter", Silverlight.createDelegate(this, this.onItemMouseEnter));
            slide.target.AddEventListener("MouseLeave", Silverlight.createDelegate(this, this.onItemMouseLeave));
            slide.target.Cursor = "Hand";
        }
    },
    onMediaEnded: function(sender, e)
    {
        sender.Source = sender.Source;
    },
    onControllerRelease: function()
    {
        clearInterval(this.mouseRightInterval);
        clearInterval(this.mouseLeftInterval);
    },
    onControllerPositionChange: function(position)
    {
        //alert('onControllerPositionChange');
        if (position != 'left')
        {
            this.move(true);
            this.mouseRightInterval = setInterval(Silverlight.createDelegate(this, this.onMouseRightInterval), 500);
        }
        else
        {
            this.move(false);
            this.mouseLeftInterval = setInterval(Silverlight.createDelegate(this, this.onMouseLeftInterval), 500);
        }
    },
    onControllerLeftDown: function(sender, mouseEventArgs)
    {
        this.Controller.xaml.findName('center').Visibility = "Collapsed";
        this.Controller.xaml.findName('left').Visibility = "Collapsed";
        this.Controller.xaml.findName('farleft').Visibility = "Visible";
        this.Controller.xaml.findName('right').Visibility = "Collapsed";
        this.Controller.xaml.findName('farright').Visibility = "Collapsed";
        this.onMouseLeftDown(null, null);
    },
    onControllerRightDown: function(sender, mouseEventArgs)
    {
        this.Controller.xaml.findName('center').Visibility = "Collapsed";
        this.Controller.xaml.findName('left').Visibility = "Collapsed";
        this.Controller.xaml.findName('farleft').Visibility = "Collapsed";
        this.Controller.xaml.findName('right').Visibility = "Collapsed";
        this.Controller.xaml.findName('farright').Visibility = "Visible";
        this.onMouseRightDown(null, null);
    },
    onControllerUp: function(sender, mouseEventArgs)
    {
        this.Controller.centerHandle();
    },
    SetupController: function(controllerXaml)
    {
        var controller = this.plugIn.content.createFromXaml(controllerXaml, true);
        controller["Canvas.Left"] = 335;
        controller["Canvas.Top"] = -38;
        this.controllerContainer.children.add(controller);
        this.Controller = new XboxLiveMarketplace.EpixController(controller);
        this.Controller.init();
        this.Controller.onChange = Silverlight.createDelegate(this, this.onControllerPositionChange);
        this.Controller.onRelease = Silverlight.createDelegate(this, this.onControllerRelease);
        this.Controller.arrowLeft.AddEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.onControllerLeftDown));
        this.Controller.arrowRight.AddEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.onControllerRightDown));
        this.Controller.arrowLeft.AddEventListener("MouseLeftButtonUp", Silverlight.createDelegate(this, this.onControllerUp));
        this.Controller.arrowRight.AddEventListener("MouseLeftButtonUp", Silverlight.createDelegate(this, this.onControllerUp));
    }
}