/* 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){
	if(typeof this.channelCollection[chNumber] == "undefined"){
		this.channelCollection[chNumber] = new XMMobileChannel(chName,chKey,chNumber,currArtist,currSong,nHood,nHoodKey,chLink,schedURL,logo,desc);
		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('div');
	newTable.setAttribute("class","channels-table");
	newTable.setAttribute("className","channels-table");
	// Clear target page element
	//target = document.getElementById(this.htmlTargetID);
	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")
			if(collection[channel].neighborhoodKey != lastNeighborhoodKey )
		{

			// 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;
						target = document.getElementById(lastNeighborhoodKey);
				target.innerHTML = "";
	}
	
	// Append the previously generate table to the target page element
	target.appendChild(newTable);

animatedcollapse.addDiv('pop', 'fade=0,speed=300');
animatedcollapse.addDiv('rock', 'fade=0,speed=300');
animatedcollapse.addDiv('dance', 'fade=0,speed=300');
animatedcollapse.addDiv('hiphop', 'fade=0,speed=300');
animatedcollapse.addDiv('country', 'fade=0,speed=300');
animatedcollapse.addDiv('christian', 'fade=0,speed=300');
animatedcollapse.addDiv('jazz', 'fade=0,speed=300');
animatedcollapse.addDiv('classical', 'fade=0,speed=300');
animatedcollapse.addDiv('latin', 'fade=0,speed=300');
animatedcollapse.addDiv('sports', 'fade=0,speed=300');
animatedcollapse.addDiv('entertainment', 'fade=0,speed=300');
animatedcollapse.addDiv('family', 'fade=0,speed=300');
animatedcollapse.addDiv('comedy', 'fade=0,speed=300');
animatedcollapse.addDiv('newspublicradio', 'fade=0,speed=300');
animatedcollapse.addDiv('religion', 'fade=0,speed=300');
animatedcollapse.addDiv('talk', 'fade=0,speed=300');
animatedcollapse.addDiv('showcase', 'fade=0,speed=300');

animatedcollapse.init();

}

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 Logo cell
	tempCell = document.createElement('div');
	tempCell.setAttribute("class", "logo");
	tempCell.setAttribute("className", "logo");
	//var channelPageURL = "http://www.xmradio.com/onxm/channelpage.xmc?ch=" + channel.channelNumber;
	tempLink = document.createElement('a');
	tempLink.setAttribute("href", channel.channelLink);
	tempImg = document.createElement('img');
	var imgpath = channel.logoImagePath;
	tempImg.setAttribute("src", imgpath);
	tempImg.setAttribute("border", "0");
	tempImg.setAttribute("alt", "");
	tempLink.appendChild(tempImg);
	tempCell.appendChild(tempLink);
	tempRow.appendChild(tempCell);
	
	
	
	
	// Channel Name cell
	tempCell = document.createElement('div');
	tempCell.setAttribute("class", "channel");
	tempCell.setAttribute("className", "channel");
	//---
	//var channelPageURL = "/onxm/channelpage.xmc?ch=" + channel.channelNumber;
	var chName=channel.channelName.replace(/\(/g, "");
	chName=chName.replace(/\)/g, "");
	chName=chName.replace(/'/g, "");
	chName=chName.replace(/ /g, "");
	chName=chName.replace(/-/g, "");
	//var chName=chName.replace(/\(/, "_");
	//var chName=chName.replace(/\)/, "_");
	var channelPageURL = "javascript:JStoASExternalInterface('"+ chName+"');javascript:pageTracker._trackPageview('/gatrack/iphone/lineupclick/"+chName+"');";
	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);
	//tempCell.setAttribute('onclick', 'JStoASExternalInterface(\'87\'his)'); 
	tempRow.appendChild(tempCell);
	
	
	// Channel Description cell
	tempCell = document.createElement('div');
	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('div');
	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);
	/*
	*/
	
	return tempRow;
}