function CreateAjaxGrid(targetDivId, xmlPath)
{
var foo = new XboxLiveMarketplace.AjaxGrid(targetDivId, xmlPath);
foo.init();
}

function SwitchGridTab() { }

String.prototype.toBoolean = function()
{
if (this == undefined)
return false;
var temp = this.replace(/\s/, "").toLocaleLowerCase();
switch (temp)
{
case "true":
case "1":
case "yes":
case "y":
return true;
default:
return false;
}
}

if (!window.XboxLiveMarketplace)
window.XboxLiveMarketplace = {};

XboxLiveMarketplace.createDelegate = function(instance, method) {
return function() {
return method.apply(instance, arguments);
}
}

XboxLiveMarketplace.addEventListener = function(obj, evType, fn)
{
if(obj.addEventListener)
{
obj.addEventListener(evType, fn, true);
return true;
}
else if(obj.attachEvent)
{
var r=obj.attachEvent('on'+evType, fn);
return r;
}
else
{
obj['on'+evType]=fn;
}
}

XboxLiveMarketplace.Downloader = function()
{
this.xmlHttp = CreateAjaxObject();
}

XboxLiveMarketplace.Downloader.prototype =
{
addCompleteEvent:function(instance, callback)
{
this.callbackMethod = callback;
this.callbackInstance = instance;
this.xmlHttp.onreadystatechange = XboxLiveMarketplace.createDelegate(this, this.checkReadyState);
},
checkReadyState:function()
{
if(this.xmlHttp.readyState==4)
{
return this.callbackMethod.call(this.callbackInstance, this.xmlHttp, null);
}
},
getUrl:function(url)
{
this.xmlHttp.open("GET", url);
this.xmlHttp.send("");
}
}

XboxLiveMarketplace.GetXmlNodeValue = function(node)
{
if (node.hasChildNodes()){
if ((node.firstChild.nodeType == 3) || (node.firstChild.nodeType == 4))
{
return node.firstChild.nodeValue;
}
}
return "";
}

XboxLiveMarketplace.gridTabDefinition = function()
{
this.div = null;
this.contentDiv = null;
this.contentTable = null;
this.lists = [];
}
XboxLiveMarketplace.gridListDefinition = function(name, url, iconClass, tableCell, useNumberedLists)
{
this.name = name;
this.url = url;
this.iconClass = iconClass;
this.cell = tableCell;
this.isLoaded = false;
this.useNumberedLists = useNumberedLists;
this.isIE = false; try { this.dizoc = new ActiveXObject("Microsoft.XMLDOM"); this.isIE = true; } catch (Error) { }
}

XboxLiveMarketplace.gridListDefinition.prototype =
{
EnsureLoad: function(contentArea)
{
if (!this.isLoaded)
{
this.isLoaded = true;

this.titleArea = document.createElement("div");
this.titleArea.className = "titleArea";
this.cell.appendChild(this.titleArea);
this.titleArea.innerHTML = "<span class=\"icon " + this.iconClass + "\"></span>" + this.name;

this.listArea = document.createElement("div");
this.listArea.className = "listArea";
this.cell.appendChild(this.listArea);

this.loadingDiv = document.createElement("div");
this.loadingDiv.className = "listLoadingDiv";
this.listArea.appendChild(this.loadingDiv);

if (this.useNumberedLists)
{
this.itemList = document.createElement("ol");
}
else
{
this.itemList = document.createElement("ul");
}
this.listArea.appendChild(this.itemList);

this.downloader = new XboxLiveMarketplace.Downloader();
this.downloader.addCompleteEvent(this, this.onDownloadComplete);
this.downloader.getUrl(this.url);
}
},
onDownloadComplete: function(sender)
{
var doc;

if (this.isIE)
{
doc = new ActiveXObject("Microsoft.XMLDOM");
doc.async = "false";
doc.loadXML(sender.responseText);
}
else
{
doc = document.implementation.createDocument("", "", null);
var parser = new DOMParser();
doc = parser.parseFromString(sender.responseText, "text/xml");
}


var colItemNodes = doc.getElementsByTagName("item");
for (var i = 0; i < colItemNodes.length; i++)
{
var itemName = "";
var itemHeader = "";
var itemUrl = "";
var itemThumbnail = "";
var itemDepartment = colItemNodes[i].getAttribute("department");
for (var j = 0; j < colItemNodes[i].childNodes.length; j++)
{
var propertyNode = colItemNodes[i].childNodes[j];
if (propertyNode.nodeType == 1)
{
switch (propertyNode.nodeName)
{
case "title":
itemName = XboxLiveMarketplace.GetXmlNodeValue(propertyNode);
break;
case "seriesname":
case "gametitle":
case "subtitle":
itemHeader = XboxLiveMarketplace.GetXmlNodeValue(propertyNode);
break;
case "url":
itemUrl = XboxLiveMarketplace.GetXmlNodeValue(propertyNode);
break;
}
}
}

var oLink = document.createElement("li");
oLink.className = "listItem";

var innerHtml = "<a href=\"" + encodeURI(itemUrl) + "\">" + itemName + "</a>";

if (itemHeader != "")
{
if ((itemDepartment == "Tv") || (itemDepartment == "MusicVideos") || (itemDepartment == "DownloadableRelatedVideos") || (itemDepartment == "DownloadableAddOns") || (itemDepartment == "DownloadablePicsAndThemes") || (itemDepartment == "DownloadableAvatarGear") || (itemDepartment == "AvatarGameIP") || (itemDepartment == "AvatarBrand"))
{
innerHtml = "<a href=\"" + encodeURI(itemUrl) + "\">" + itemName + "</a><br />" + itemHeader;
}
}
oLink.innerHTML = innerHtml;

this.itemList.appendChild(oLink);
}

this.loadingDiv.style.display = "none";
}
}

XboxLiveMarketplace.AjaxGrid = function(targetDivId, xmlPath)
{
this.rootXmlUrl = xmlPath;
this.targetDiv = document.getElementById(targetDivId);
this.isIE = false; try{this.dizoc = new ActiveXObject("Microsoft.XMLDOM");this.isIE = true;}catch(Error){}
this.rootDownloader = new XboxLiveMarketplace.Downloader();


}

XboxLiveMarketplace.AjaxGrid.prototype =
{
init: function()
{

this.loadingDiv = document.createElement("div");
this.loadingDiv.className = "loadingDiv";
this.targetDiv.appendChild(this.loadingDiv);
var oTabArea = document.createElement("div");
oTabArea.className = "tabArea";
this.targetDiv.appendChild(oTabArea);
this.headerArea = document.createElement("div");
this.headerArea.className = "header";
oTabArea.appendChild(this.headerArea);
this.tabRow = document.createElement("div");
this.tabRow.className = "tabRow";
oTabArea.appendChild(this.tabRow);
this.contentArea = document.createElement("div");
this.contentArea.className = "contentArea";
this.targetDiv.appendChild(this.contentArea);


this.showLoading();
this.rootDownloader.addCompleteEvent(this, this.onRootDownloadComplete);
this.rootDownloader.getUrl(this.rootXmlUrl);
},
showLoading: function()
{
if (this.loadingDiv != null)
{
this.loadingDiv.style.display = "block";
}
},
hideLoading: function()
{
if (this.loadingDiv != null)
{
this.loadingDiv.style.display = "none";
}
},
onRootDownloadComplete: function(sender)
{
var doc;

if (this.isIE)
{
doc = new ActiveXObject("Microsoft.XMLDOM");
doc.async = "false";
doc.loadXML(sender.responseText);
}
else
{
doc = document.implementation.createDocument("", "", null);
var parser = new DOMParser();
doc = parser.parseFromString(sender.responseText, "text/xml");
}
this.rootXml = doc;
this.CreateStringsFromRootXml();
},
CreateStringsFromRootXml: function()
{
if (this.rootXml.documentElement == undefined)
{
return;
}
for (var i = 0; i < this.rootXml.documentElement.childNodes.length; i++)
{
var curNode = this.rootXml.documentElement.childNodes[i];
if ((curNode.nodeType == 1) && (curNode.nodeName == "strings"))
{
for (var j = 0; j < curNode.childNodes.length; j++)
{
var stringNode = curNode.childNodes[j];
if (stringNode.nodeType == 1)
{
switch (stringNode.nodeName)
{






case "description":
var description = document.createElement("div");
description.className = "description";
description.innerHTML = XboxLiveMarketplace.GetXmlNodeValue(stringNode);
this.headerArea.appendChild(description);
break;
}
}
}
}
}
this.CreateTabsFromRootXml();
},
CreateTabsFromRootXml: function()
{
this.tabs = [];
this.tabList = [];
for (var i = 0; i < this.rootXml.documentElement.childNodes.length; i++)
{
var curNode = this.rootXml.documentElement.childNodes[i];
if ((curNode.nodeType == 1) && (curNode.nodeName == "categories"))
{

var colCategories = curNode.getElementsByTagName("category");
for (var j = 0; j < colCategories.length; j++)
{
var tabNode = colCategories[j];

var tabName = tabNode.getAttribute("name");
var useNumberedLists = tabNode.getAttribute("usenumberedlists");
var bUseNumberedLists = useNumberedLists.toBoolean();

var oTab = document.createElement("a");
oTab.href = "javascript:SwitchGridTab()";
oTab.className = "tab";
XboxLiveMarketplace.addEventListener(oTab, "click", XboxLiveMarketplace.createDelegate(this, this.TabClick));
oTab.innerHTML = "<div class=\"rightCurve\"></div>" + tabName;
oTab.setAttribute("tabkey", tabName);
this.tabRow.appendChild(oTab);

this.tabs[tabName] = new XboxLiveMarketplace.gridTabDefinition();
this.tabs[tabName].div = oTab;
this.tabList.push(tabName);

this.tabs[tabName].contentDiv = document.createElement("div");
this.contentArea.appendChild(this.tabs[tabName].contentDiv);

this.tabs[tabName].contentTable = document.createElement("table");
this.tabs[tabName].contentTable.cellSpacing = 0;
this.tabs[tabName].contentTable.cellPadding = 0;
this.tabs[tabName].contentDiv.appendChild(this.tabs[tabName].contentTable);


var colRows = tabNode.getElementsByTagName("row");
for (var k = 0; k < colRows.length; k++)
{
var rowNode = colRows[k];
var oCurrentRow = this.tabs[tabName].contentTable.insertRow(-1);


var colLists = rowNode.getElementsByTagName("list");
for (var l = 0; l < colLists.length; l++)
{
var oCurrentCell = oCurrentRow.insertCell(-1);
oCurrentCell.className = "list";
oCurrentCell.setAttribute("width", colLists[l].getAttribute("width"));

var listName = colLists[l].getAttribute("name");
var listUrl = colLists[l].getAttribute("url");
var iconClass = colLists[l].getAttribute("icon");

this.tabs[tabName].lists.push(new XboxLiveMarketplace.gridListDefinition(listName, listUrl, iconClass, oCurrentCell, bUseNumberedLists));
}
}
}
}
}
this.hideLoading();
this.LoadTab(this.tabs[this.tabList[0]]);
},
GetNodeValue: function(node)
{
if (node.hasChildNodes())
{
if ((node.firstChild.nodeType == 3) || (node.firstChild.nodeType == 4))
{
return node.firstChild.nodeValue;
}
}
return "";
},
TabClick: function(event)
{
var oTab;
if (event.srcElement)
{
oTab = event.srcElement;
}
else if (event.currentTarget)
{
oTab = event.currentTarget;
}
if (oTab == null)
{
return;
}

var tabKey = oTab.getAttribute("tabkey");
this.LoadTab(this.tabs[tabKey]);
},
LoadTab: function(tab)
{
this.showLoading();

if (tab.contentDiv == null)
{
tab.contentDiv = document.createElement("div");
this.contentArea.appendChild(tab.contentDiv);
}

for (var i = 0; i < this.contentArea.childNodes.length; i++)
{
this.contentArea.childNodes[i].style.display = "none";
}
tab.contentDiv.style.display = "block";

for (var j = 0; j < tab.lists.length; j++)
{
tab.lists[j].EnsureLoad(tab.contentDiv);
}


this.HighlightTab(tab);
this.hideLoading();
},
HighlightTab: function(tab)
{

for (var i = 0; i < this.tabList.length; i++)
{
this.tabs[this.tabList[i]].div.className = "tab";
}

tab.div.className = "tab tabHot";
}
}
