/* PAD XML object */
function XML_obj(){
	this.xmlDoc = "";
	this.rootNode = "";
	this.channelCollection = new Object();
	this.channelNumberAry = new Array();
	this.neighborhoods = new Object();
	this.XMLrequestURL = "";
	this.htmlTargetID = "";
	this.statusCode = "";
}


XML_obj.prototype.addChannel = function(chName,chKey,chNumber,currArtist,currSong,nHood,nHoodKey,chLink,schedURL,logo,desc,onAlltel,onAtt,onBberry){
	if(typeof this.channelCollection[chNumber] == "undefined"){
		this.channelCollection[chNumber] = new XMMobileChannel(chName,chKey,chNumber,currArtist,currSong,nHood,nHoodKey,chLink,schedURL,logo,desc,onAlltel,onAtt,onBberry);
		if(!this.neighborhoods[nHoodKey]){
			this.neighborhoods[nHoodKey] = new XM_neighborhood(nHoodKey,nHood);
		}
		this.neighborhoods[nHoodKey].addChannel(chNumber,this.channelCollection[chNumber]);
	}
}

XML_obj.prototype.processLoading = function(){
	if(typeof document.getElementById(this.htmlTargetID) != "undefined"){
		targetNode = document.getElementById(this.htmlTargetID);
		targetNode.innerHTML = "";
		loadingDiv = document.createElement('DIV');
		loadingDiv.setAttribute("id","loadingDiv");
		loadingTxt = document.createTextNode('Loading...');
		loadingDiv.appendChild(loadingTxt);
		/*
		loadingImg = document.createElement('IMG');
		loadingImg.setAttribute("src","/mobile/i/tt.gif");
		loadingImg.setAttribute("width","134");
		loadingImg.setAttribute("height","113");
		loadingImg.setAttribute("border","0");
		loadingImg.setAttribute("alt","Loading...");
		loadingDiv.appendChild(loadingImg);
		*/
		targetNode.appendChild(loadingDiv);
	}else{
		alert("HTML target not present");
	}
}

XML_obj.prototype.processResults = function(xmlDoc_response){
	/*
	1	an HTML element
	2	an element attribute
	3	text
	8	an HTML comment
	9	a document
	10	a document type definition
	*/
	this.xmlDoc = xmlDoc_response;
	this.rootNode = this.xmlDoc.getElementsByTagName('paddata').item(0);
	this.events = this.xmlDoc.getElementsByTagName('event');
	var currentChannel;
	for(idx=0;idx < this.rootNode.childNodes.length; idx++){
		if(this.rootNode.childNodes[idx].nodeType == 1 && this.rootNode.childNodes[idx].nodeName == "event"){
			currentChannel = this.rootNode.childNodes[idx].getElementsByTagName('channelnumber').item(0).firstChild.data;
			this.channelNumberAry[this.channelNumberAry.length] = currentChannel;
			if(typeof this.channelCollection[currentChannel] == "undefined" ){
				/*
				this.addChannel(
					this.rootNode.childNodes[idx].getElementsByTagName('channelname').item(0).firstChild.data,
					this.rootNode.childNodes[idx].getElementsByTagName('channelnumber').item(0).firstChild.data,
					this.rootNode.childNodes[idx].getElementsByTagName('artist').item(0).firstChild.data,
					this.rootNode.childNodes[idx].getElementsByTagName('songtitle').item(0).firstChild.data,
					"",
					"",
					"",
					"",
					"yea, for it is a channel"
					)
				*/
			}else{
				//if(!this.channelCollection[currentChannel].channelNumber) this.channelCollection[currentChannel].artist  = this.rootNode.childNodes[idx].getElementsByTagName('channelnumber').item(0).firstChild.data
				this.channelCollection[currentChannel].artist = (this.rootNode.childNodes[idx].getElementsByTagName('artist').item(0).firstChild ) ? this.rootNode.childNodes[idx].getElementsByTagName('artist').item(0).firstChild.data : " ";
				//alert("this.rootNode.childNodes[idx].getElementsByTagName('songtitle').item(0).firstChild= " +this.rootNode.childNodes[idx].getElementsByTagName('songtitle').item(0).firstChild);
				this.channelCollection[currentChannel].songtitle = (this.rootNode.childNodes[idx].getElementsByTagName('songtitle').item(0).firstChild) ? this.rootNode.childNodes[idx].getElementsByTagName('songtitle').item(0).firstChild.data : " ";
			}
		}
	}
	if(this.currentDisplay == "all"){
		this.displayResults(this.channelCollection);
	}else{
		this.displayResults(this.neighborhoods[this.currentDisplay].channelCollection);
	}
}

XML_obj.prototype.dataNotAvailable = function(msg,statusCode){
	var stopVar = 0;
	this.statusCode = statusCode;
	for(prop in this.channelCollection){
		this.channelCollection[prop].artist = "";
		this.channelCollection[prop].songtitle = msg;
	}
	if(this.currentDisplay == "all"){
		this.displayResults(this.channelCollection);
	}else{
		this.displayResults(this.neighborhoods[this.currentDisplay].channelCollection);
	}
}
 
XML_obj.prototype.updateData = function(dataSet,flashDiv){
	if(flashDiv){
		if(typeof document.getElementById(flashDiv) != "undefined"){
			so.addVariable("defaultKey", dataSet);
			so.write(flashDiv);
		}
	}
	this.xmlDoc = false;
	makeRequest(this.XMLrequestURL,this);
	this.currentDisplay = dataSet;
}

XML_obj.prototype.parseASCII = function(str){
	if(str.indexOf("&#") > -1){
		/* long way...
		substr = str.substring((str.indexOf("&#") + 2) , str.indexOf(";") );
		replaceChar = String.fromCharCode(substr);
		remStr = "&#" + substr + ";";
		newStr = str.replace(remStr,replaceChar,"gi");
		*/
		newStr = str.replace("&#" + str.substring((str.indexOf("&#") + 2) , str.indexOf(";") ) + ";",String.fromCharCode(str.substring((str.indexOf("&#") + 2) , str.indexOf(";") )),"gi");
		return newStr;
	}else{
		return str;
	}
}
XML_obj.prototype.displayResults = function(collection){
	var str = "";
	var tempRow;
	var tempCell;
	var tempLink;
	var tempData = "";
	var tempTextNode = "";
	//var newTable = document.createElement('TABLE');
	var newTable = document.createElement('div');
	newTable.setAttribute("class","channels-table");
	newTable.setAttribute("className","channels-table");
	
	/*
	newTable.setAttribute("summary","Music Channels Lineup");
	newTable.setAttribute("class","channels fullWidth");
	newTable.setAttribute("className","channels fullWidth");
	newTable.setAttribute("cellpadding","0");
	newTable.setAttribute("cellPadding","0");
	newTable.setAttribute("cellspacing","0");
	newTable.setAttribute("cellSpacing","0");
	newTable.setAttribute("border","0");
	var tHead = document.createElement('THEAD');
	var headRow = document.createElement('TR');
	headRow.setAttribute("class","th");
	var thCellData = new Array("Neighborhood","Channel Name","Channel Number","Description","What's On Right Now","Show Schedule");
	for(i=0;i<thCellData.length;i++){
		tempCell = document.createElement('TH');
		if(i==0){
			tempCell.setAttribute("class","thFirstCell");
			tempCell.setAttribute("className","thFirstCell");
		}
		if(i==(thCellData.length-1)){
			tempCell.setAttribute("class","thLastCell");
			tempCell.setAttribute("className","thLastCell");
		}
		tempCell.setAttribute("scope","col");
		tempTextNode = document.createTextNode(thCellData[i]);
		if(thCellData[i] == "Channel Name"){
			
			tempBr = document.createElement('BR');
			tempSpan = document.createElement('SPAN');
			tempSpan.setAttribute("class","noBold");
			tempSpan.setAttribute("className","noBold");
			tempSpanContent = document.createTextNode("* Also Available On XMRO");
			tempSpan.appendChild(tempSpanContent);
			
			tempCell.appendChild(tempTextNode);
			
			tempCell.appendChild(tempBr);
			tempCell.appendChild(tempSpan);
			tempCell.setAttribute("nowrap","true");
			tempCell.setAttribute("noWrap","true");
			
		}else{
			tempCell.appendChild(tempTextNode);
		}
		headRow.appendChild(tempCell);
	}
	tHead.appendChild(headRow);
	newTable.appendChild(tHead);
	*/
	//var tBody = document.createElement('TBODY');
	
	// Clear target page element
	target = document.getElementById(this.htmlTargetID);
	target.innerHTML = "";
	
	var dataRowCount = 0;
	var lastNeighborhoodKey;
	for(channel in collection){
		
		// Check if we need to create a new genre table
		if(collection[channel].neighborhoodKey != lastNeighborhoodKey && collection[channel].neighborhoodKey != "dance")
		{
			// Generate first row header is first record
			if(dataRowCount == 0)
			{
				headerRow = this.generateHeaderRow(collection[channel].neighborhoodKey, true);
			} else {
				
				// Wrap up for previous genre table..
				// Create Table Foote
				var footRow = document.createElement('p');
				footRow.setAttribute("class","footer");
				footRow.setAttribute("className","footer");
				tempCell = document.createElement('span');
				tempTextNode = document.createTextNode("");
				tempCell.appendChild(tempTextNode);
				footRow.appendChild(tempCell);
				newTable.appendChild(footRow);
				// Append the previously generated table to the target page element
				target.appendChild(newTable);
				
				// Generate the new genre table header
				headerRow = this.generateHeaderRow(collection[channel].neighborhoodKey, false);
			}
			
			// Generate New table for this genre's channels
			newTable = document.createElement('div');
			newTable.setAttribute("class","channels-table");
			newTable.setAttribute("className","channels-table");
			
			newTable.appendChild(headerRow);
		}
		
		// Generate the channel row
		tempRow = this.generateRow(collection[channel]);
		newTable.appendChild(tempRow);
		dataRowCount++;
		
		// Store last neighborhoodKey
		lastNeighborhoodKey = collection[channel].neighborhoodKey;
	}
	
	// Append the previously generate table to the target page element
	target.appendChild(newTable);
	
	
}

XML_obj.prototype.generateHeaderRow = function(neighborhoodKey, isFirstRow)
{
	tempRow = document.createElement('div');
	tempRow.setAttribute("class", "header");
	tempRow.setAttribute("className", "header");
	
	// If first row, generate availability image
	if(isFirstRow)
	{
		tempCell = document.createElement('img');
		tempCell.setAttribute("class", "availability");
		tempCell.setAttribute("className", "availability");
		tempCell.setAttribute("src", "/mobile/i/headers/availability.gif");
		tempCell.setAttribute("width", "410");
		tempCell.setAttribute("height", "10");
		tempCell.setAttribute("border", "0");
		tempCell.setAttribute("alt", "");
		
		tempRow.appendChild(document.createTextNode("\n"));
		tempRow.appendChild(tempCell);
		tempRow.appendChild(document.createTextNode("\n"));
	}
	//
	
	// Genre Header
	tempCell = document.createElement('h3');
	tempImg = document.createElement('img');
	var imgpath = "/mobile/i/headers/" + neighborhoodKey + ".gif";
	tempImg.setAttribute("src", imgpath);
	//tempImg.setAttribute("width", "106");
	//tempImg.setAttribute("height", "16");
	tempImg.setAttribute("border", "0");
	tempImg.setAttribute("alt", "");
	tempCell.appendChild(tempImg);
	tempRow.appendChild(tempCell);
	
	return tempRow;
}

XML_obj.prototype.generateRow = function(channel)
{
	var WHATS_ON_MAX_LENGTH = 60;
	
	tempRow = document.createElement('p');
	
	// Channel Name cell
	tempCell = document.createElement('span');
	tempCell.setAttribute("class", "channel");
	tempCell.setAttribute("className", "channel");
	//---
	//var channelPageURL = "/onxm/channelpage.xmc?ch=" + channel.channelNumber;
	var channelPageURL = "http://www.xmradio.com/onxm/channelpage.xmc?ch=" + channel.channelNumber;
	tempLink = document.createElement('a');
	tempLink.setAttribute("href", channelPageURL);
	//---
	channel.channelName = this.parseASCII(channel.channelName)
	tempTextNode = document.createTextNode(channel.channelName);
	tempLink.appendChild(tempTextNode);
	tempCell.appendChild(tempLink);
	tempRow.appendChild(tempCell);
	
	// Channel Description cell
	tempCell = document.createElement('span');
	tempCell.setAttribute("class", "genre");
	tempCell.setAttribute("className", "genre");
	channel.description = this.parseASCII(channel.description)
	tempTextNode = document.createTextNode(channel.description);
	tempCell.appendChild(tempTextNode);
	tempRow.appendChild(tempCell);
	
	// What's On cell (temp)
	tempCell = document.createElement('span');
	tempCell.setAttribute("class", "track");
	tempCell.setAttribute("className", "track");
	
	tempData = "";
	if(channel.artist != ""){
		tempData = channel.artist;
	}
	if(channel.songtitle != ""){
		if(channel.artist != "") tempData += " - ";
		tempData += channel.songtitle;
	}
	tempData = truncate(tempData, WHATS_ON_MAX_LENGTH);
	tempTextNode = document.createTextNode(tempData);
	tempCell.appendChild(tempTextNode);
	tempRow.appendChild(tempCell);
	
	// Carrier Availability
	if(channel.onAlltel)
	{
		tempCell = document.createElement('img');
		tempCell.setAttribute("class", "alltel");
		tempCell.setAttribute("className", "alltel");
		tempCell.setAttribute("width", "8");
		tempCell.setAttribute("height", "8");
		tempCell.setAttribute("border", "0");
		tempCell.setAttribute("src", "/mobile/i/site/bullet_alltel.gif");
		tempCell.setAttribute("alt", "Available on AllTel");
		tempRow.appendChild(tempCell);
	}
	if(channel.onAtt)
	{
		tempCell = document.createElement('img');
		tempCell.setAttribute("class", "att");
		tempCell.setAttribute("className", "att");
		tempCell.setAttribute("width", "8");
		tempCell.setAttribute("height", "8");
		tempCell.setAttribute("border", "0");
		tempCell.setAttribute("src", "/mobile/i/site/bullet_att.gif");
		tempCell.setAttribute("alt", "Available on AT&T");
		tempRow.appendChild(tempCell);
	}
		if(channel.onBberry)
	{
		tempCell = document.createElement('img');
		tempCell.setAttribute("class", "bberry");
		tempCell.setAttribute("className", "bberry");
		tempCell.setAttribute("width", "8");
		tempCell.setAttribute("height", "8");
		tempCell.setAttribute("border", "0");
		tempCell.setAttribute("src", "/mobile/i/site/bullet_bberry.gif");
		tempCell.setAttribute("alt", "Available on Blackberry&reg;");
		tempRow.appendChild(tempCell);
	}
	
	return tempRow;
}