﻿if (!window.XboxLiveMarketplace)
	window.XboxLiveMarketplace = {};

XboxLiveMarketplace.API = function(url, rootElement, delegate, nodeName)
{
    this.delegate = delegate;
    this.element = nodeName;
    // create downloader
    this.control = rootElement.GetHost();
    this.load(url);
}
XboxLiveMarketplace.API.prototype = 
{
    DispatchEvent: function()
    {
        if(this.delegate != null)
        {
            this.delegate(this.data);
        }
    },
    load:function(url)
    {
        //alert('loading: ' + url)
        this.downloader = this.control.createObject("downloader");
        // attach event handlers
        this.downloader.addEventListener( "completed", Silverlight.createDelegate(this, this.onDataReady));
        // download
        this.downloader.open("GET", url);
        this.downloader.send();
    },
    onDataReady: function(sender, args)
    {
        //alert(sender.responseText.substring(0, 200));
        // get the response
        var xml = sender.responseText;
        var doc;
        var xmlList;
        var isIE = (navigator.userAgent.indexOf("MSIE")!=-1);
        // create appropiate XML document
        if (isIE)
        {
            doc = new ActiveXObject("Microsoft.XMLDOM");
            doc.async="false";
            doc.loadXML(xml);
        }
        else
        {
            doc = document.implementation.createDocument("","",null);
            var parser = new DOMParser();
            doc = parser.parseFromString(xml,"text/xml");
        }
        xml = doc.getElementsByTagName(this.element)
        this.data = xml;
        //alert('data ready');
        this.DispatchEvent();
    } 
}