﻿if (!window.XboxLiveMarketplace)
	window.XboxLiveMarketplace = {};

XboxLiveMarketplace.Scene = function(epixXml, locale) 
{
	this.epixXml = epixXml;
    this.dataPath = "/xweb/lib/Silverlight/MarketplaceEpix/";
    this.fontPath = this.dataPath + "fontData/";
    this.fontDataURI = this.fontPath + 'deployment.xml';
    this.fonts = [];
    this.fontName = null;
    this.fontFilePath = null;
    this.fontSource = null;
    this.locale = locale;
}

XboxLiveMarketplace.Scene.prototype =
{
	handleLoad: function(plugIn, userContext, rootElement) 
	{
	    //alert('[Scene] handleLoad invoked');
		this.plugIn = plugIn;
		this.mainCanvas = plugIn.content.findName("mainCanvas");
		this.epix = null;

        this.fontsAPI = new XboxLiveMarketplace.API(this.fontDataURI, rootElement, Silverlight.createDelegate(this, this.onFontsReady), 'fonts');

    },
    onFontsReady:function(xml, type)
    {
        // parse xml
        this.fonts = [];
        for(z=0; z<xml[0].childNodes.length; z++)
        {
            if (xml[0].childNodes[z].nodeName == "font"){
                this.fonts[xml[0].childNodes[z].getAttribute("locale")] = xml[0].childNodes[z].childNodes[0].nodeValue;
            }
        }
        // set the parameters
        this.fontName = this.fonts['default'];
        this.fontFilePath = this.fontPath + "default/Font.zip";
        if (this.fonts[this.locale] != null) // see if we have a font specifed for this locale
        {
            this.fontName = this.fonts[this.locale];
            this.fontFilePath = this.fontPath  + this.locale + "/Font.zip";
        }
        
        // load the font;
        this.LoadLocaleFont();
    },
    LoadLocaleFont: function()
    {
        var downloader = this.plugIn.createObject("downloader");
        downloader.addEventListener("Completed", Silverlight.createDelegate(this, this.onFontLoaded));
        downloader.open("GET", this.fontFilePath);
        downloader.send();    
    },
    onFontLoaded: function(sender, eventArgs)
    {
        this.fontSource = sender;
		//fetch epix prototype control xaml
	    this.epixDownloader = this.plugIn.createObject("downloader");
        this.epixDownloader.addEventListener("completed", Silverlight.createDelegate(this, this.onEpixCompleted));
        this.epixDownloader.open("GET", this.dataPath + "EpixPrototype.xaml");
	    this.epixDownloader.send();
    },
	onEpixCompleted: function(sender, args)
    {
        if (sender.uri == this.dataPath + "EpixPrototype.xaml")
        {
            var epixXaml = this.plugIn.content.createFromXamlDownloader(sender, "");
            epixXaml["Canvas.Top"] = -50;
            epixXaml["Canvas.Left"] = 50;
            this.mainCanvas.children.add(epixXaml);
            this.epix = new XboxLiveMarketplace.EpixPrototype(this.plugIn, epixXaml, this.epixXml, "", "", this.fontName, this.fontSource);
            
            this.epixDownloader.open("GET", this.dataPath + "EpixSlide.xaml");
	        this.epixDownloader.send();
        }
        else if(sender.uri == this.dataPath + "EpixSlide.xaml")
        {
            this.epix.init(sender.ResponseText);
            
            this.epixDownloader.open("GET", this.dataPath + "EpixController.xaml");
	        this.epixDownloader.send();
        }
        else if(sender.uri == this.dataPath + "EpixController.xaml")
        {
            this.epix.SetupController(sender.ResponseText);
        }
    }
 }