/* PAD XML object */
function XML_obj(){
	this.xmlDoc = "";
	this.rootNode = "";
	this.channelCollection = new Object();
	this.channelNumberAry = new Array();
	this.neighborhoods = new Object();
	this.tier = "";
	this.order = "";
	this.sort = "";
	this.allowSort = true;
	this.XMLrequestURL = "";
	this.htmlTargetID = "";
	this.statusCode = "";
	this.contextURL ="/servlet/ContentServer?pagename=XM/ChannelLineup/XMChannelLineup&c=XMChannellineup&cid=1229017904689";
}

XML_obj.prototype.addChannel = function(chName,chKey,chNumber,currArtist,currSong,editorsChoice,nHood,nHoodKey,nHoodUrl,chLink,schedURL,logo,desc){
	if(typeof this.channelCollection[chNumber] == "undefined"){
		this.channelCollection[chNumber] = new XMChannel(chName,chKey,chNumber,currArtist,currSong,editorsChoice,nHood,nHoodKey,nHoodUrl,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");
		loadingImg = document.createElement('IMG');
		loadingImg.setAttribute("src","/images/global/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
	*/



	/* jsa */	   
	try{
		if(typeof(this.neighborhoods[this.currentDisplay])=="undefined"){
			this.currentDisplay = "all";
		}
	}
	catch(err){
		//alert(err.message);
	}	
	/******/




	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);
//                alert("  Response processed  !!!");
//                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){
		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');
	newTable.setAttribute("id","channelLineupTable");
	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","What's Hot","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(this.allowSort){
			var switchOrder = function(oldOrder) {
				if (oldOrder == "" || oldOrder == "asc") {
					return "desc";
				}
				return "asc";
			}
		}
		switch(thCellData[i]) {
			
			case "Neighborhood":
				var tempSpan = document.createElement('SPAN');
				if(this.allowSort){
					var newOrder = this.order;
					if (this.sort == "neighborhood")newOrder = switchOrder(this.order);
					var tempSortLink = document.createElement("A");
					var link = this.contextURL+"&sort=neighborhood&order="+newOrder;				
				
					if (this.tier != "") link += "&tier="+this.tier;
				
					//tempSortLink.setAttribute("href", link);
					tempSortLink.setAttribute("href", "");
					tempSortLink.setAttribute("title", "Sort by Neighborhood");
					tempSortLink.appendChild(tempTextNode);
					
					if (this.sort == "neighborhood") {
						if (this.order == "asc") {
							tempCell.setAttribute("class", 'descending thFirstCell');
							tempCell.setAttribute("className", 'descending thFirstCell');
						}else {
							tempCell.setAttribute("class", 'ascending thFirstCell')
							tempCell.setAttribute("className", 'ascending thFirstCell')
						}
					}
					tempSpan.setAttribute("class", 'arrow');
					tempSpan.setAttribute("className", 'arrow');
					tempSpan.appendChild(tempTextNode);
				}else{ 
					tempSpan.appendChild(tempTextNode);
				}
				tempCell.setAttribute("class", 'thFirstCell')
				tempCell.setAttribute("className", 'thFirstCell')

				tempCell.appendChild(tempSpan);
				break;
				
				
			case "Channel Name":
				var tempSpan = document.createElement('SPAN');
				if(this.allowSort){
					var newOrder = this.order
					if (this.sort == "name") {
						newOrder = switchOrder(this.order);
					}
					var tempSortLink = document.createElement("A");
					var link = this.contextURL+"&sort=name&order="+newOrder;
					if (this.tier != "") link += "&tier="+this.tier;
					tempSortLink.setAttribute("href", "/");
					//tempSortLink.setAttribute("href", link);
					tempSortLink.setAttribute("title", "Sort by Name");
					tempSortLink.appendChild(tempTextNode);

					if (this.sort == "name") {
						if (this.order == "asc") {
							tempCell.setAttribute("class", 'descending');
							tempCell.setAttribute("className", 'descending');
						}else {
							tempCell.setAttribute("class", 'ascending');
							tempCell.setAttribute("className", 'ascending');
						}
					}
					tempSpan.setAttribute("class", 'arrow');
					tempSpan.setAttribute("className", 'arrow');
					tempSpan.appendChild(tempTextNode);
				}else{
					tempSpan.appendChild(tempTextNode);
				}
				tempCell.appendChild(tempSpan);
				
				tempCell.setAttribute("nowrap","true");
				tempCell.setAttribute("noWrap","true");			
				
				break;
			
			case "What's Hot":
				var tempSpan = document.createElement('SPAN');
				if(this.allowSort) {
					var newOrder = this.order
					if (this.sort == "editorschoice") {
						newOrder = switchOrder(this.order);
					}
					var tempSortLink = document.createElement("A");
					var link = this.contextURL+"&sort=editorschoice&order="+newOrder;
					if (this.tier != "") 
						link += "&tier="+this.tier;
					//tempSortLink.setAttribute("href", link);
					tempSortLink.setAttribute("href", "/");
					tempSortLink.setAttribute("title", "Sort by What's Hot");
					tempSortLink.setAttribute("class", 'editorsChoice');
					tempSortLink.setAttribute("className", 'editorsChoice');
					tempSortLink.appendChild(tempTextNode);	 
				
					if (this.sort == "editorschoice") {
						if (this.order == "asc") {
							tempCell.setAttribute("class", 'descending editorschoice');
							tempCell.setAttribute("className", 'descending editorschoice');
						}else {
							tempCell.setAttribute("class", 'ascending editorschoice');
							tempCell.setAttribute("className", 'ascending editorschoice');
						}
					}else{
							tempCell.setAttribute("class", 'editorschoice');
							tempCell.setAttribute("className", 'editorschoice');					
					}
					
					tempSpan.setAttribute("class", 'arrow');
					tempSpan.setAttribute("className", 'arrow');
					tempSpan.appendChild(tempTextNode)
				}else{
					tempSpan.appendChild(tempTextNode);	
				}
				if(typeof ttipHover != "undefined"){
					//ttipHover was setup outside of this js file.				
					tempSpan.innerHTML = tempSpan.innerHTML + ttipHover;
				}
				tempCell.appendChild(tempSpan);
				
				break;
				
			
			case "Channel Number":
			//case "tasty goodness":
				tempSpan = document.createElement('SPAN');
				if(this.allowSort) {
					var newOrder = this.order
					if (this.sort == "number") {
						newOrder = switchOrder(this.order);
					}
					var tempSortLink = document.createElement("A");
					var link =this.contextURL+ "&sort=number&order="+newOrder;
					
					if (this.tier != "") link += "&tier="+this.tier;
					//tempSortLink.setAttribute("href", link);
					tempSortLink.setAttribute("href", "/");
					tempSortLink.setAttribute("title", "Sort by Number");
					tempSortLink.appendChild(tempTextNode);
					
					
					if (this.sort == "number") {
						if (this.order == "asc") {
							tempCell.setAttribute("class", 'descending number');
							tempCell.setAttribute("className", 'descending number');
						}else {
							tempCell.setAttribute("class", 'ascending number');
							tempCell.setAttribute("className", 'ascending number');
						}
					}
					tempSpan.setAttribute("class", 'arrow');
					tempSpan.setAttribute("className", 'arrow');
					tempSpan.appendChild(tempTextNode);
				}else{
					tempSpan.appendChild(tempTextNode);
				}
				
				//tempCell.appendChild(tempBr);
				tempCell.appendChild(tempSpan);
				tempCell.setAttribute("nowrap","true");
				tempCell.setAttribute("noWrap","true");		
				
				break;
			default:
				tempCell.appendChild(tempTextNode);
		}
		headRow.appendChild(tempCell);
	}

	tHead.appendChild(headRow);
	newTable.appendChild(tHead);
	var tBody = document.createElement('TBODY');
	var dataRowCount = 0;
	for(channel in collection){
		dataRowCount++;
		tempRow = document.createElement('TR');
		channelPageURL = collection[channel].channelLink;
		nHoodURL = collection[channel].neighborhoodUrl;
		
		// NHood cell cell
		tempCell = document.createElement('TD');
		if(dataRowCount == 1){
			tempCell.setAttribute("class","row1firstCell");
			tempCell.setAttribute("className","row1firstCell");
		}else{
			tempCell.setAttribute("class","firstCell");
			tempCell.setAttribute("className","firstCell");
		}
		tempLink = document.createElement('A');
		tempLink.setAttribute("href",nHoodURL);
		tempTextNode = document.createTextNode(collection[channel].neighborhood);
		tempLink.appendChild(tempTextNode);
		tempCell.appendChild(tempLink);
		tempRow.appendChild(tempCell);
		
		// Channel Name cell
		tempCell = document.createElement('TD');
		tempLink = document.createElement('A');
		tempLink.setAttribute("href",channelPageURL);
		collection[channel].channelName = this.parseASCII(collection[channel].channelName)
		tempTextNode = document.createTextNode(collection[channel].channelName);
		tempLink.appendChild(tempTextNode);
		tempCell.appendChild(tempLink);
		tempRow.appendChild(tempCell);
		
		// Editor's choice cell
		tempCell = document.createElement('TD');
		collection[channel].editorsChoice = this.parseASCII(collection[channel].editorsChoice)
		if(collection[channel].editorsChoice == "true") {
			tempCell.innerHTML = '<span style="visibility:hidden">1</span><img src="/images/onXM/channel_lineup/16thNote.gif" alt="Editors Choice" class="editorsChoice" className="editorsChoice">';
			
		}else{
			tempCell.innerHTML = '<span style="visibility:hidden">2</span><img src="/images/whatIsXM/compare-pricing/spacer.gif" width="55" height="1" />';
		}
		tempRow.appendChild(tempCell);
		
		// Channel Number cell
		tempCell = document.createElement('TD');
		tempLink = document.createElement('A');
		tempLink.setAttribute("href",channelPageURL);
		tempTextNode = document.createTextNode(collection[channel].channelNumber);
		tempLink.appendChild(tempTextNode);
		tempCell.appendChild(tempLink);
		tempRow.appendChild(tempCell);
		
		// Channel Description cell
		tempCell = document.createElement('TD');
		collection[channel].description = this.parseASCII(collection[channel].description)
		tempTextNode = document.createTextNode(collection[channel].description);
		tempCell.appendChild(tempTextNode);
		tempRow.appendChild(tempCell);
		
		// What's On cell (temp)
		tempCell = document.createElement('TD');
		tempData = "";
		if(collection[channel].artist != ""){
			tempData = collection[channel].artist;
		}
		if(collection[channel].songtitle != ""){
			if(collection[channel].artist != "") tempData += " - ";
			tempData += collection[channel].songtitle;
		}
		tempTextNode = document.createTextNode(tempData);
		tempCell.appendChild(tempTextNode);
		tempRow.appendChild(tempCell);
		
		// Schedule Cell
		tempCell = document.createElement('TD');
		if(dataRowCount == 1){
			tempCell.setAttribute("class","row1lastCell");
			tempCell.setAttribute("className","row1lastCell");
		}else{ 
			tempCell.setAttribute("class","lastCell");
			tempCell.setAttribute("className","lastCell");
		}
		if(collection[channel].scheduleURL != ""){
			tempLink = document.createElement('A');
			tempLink.setAttribute("href",collection[channel].scheduleURL);
			//tempData = "View";
			tempTextNode = document.createTextNode("View");
			tempLink.appendChild(tempTextNode);
			tempCell.appendChild(tempLink);
		}else{
			tempData = " ";
			tempTextNode = document.createTextNode(tempData);
			tempCell.appendChild(tempTextNode);
		}
		tempRow.appendChild(tempCell);
		
		tBody.appendChild(tempRow);
		
	}

	var footRow = document.createElement('TR');
	footRow.setAttribute("class","lastRow");
	footRow.setAttribute("className","lastRow");

	tempCell = document.createElement('TD');
	tempCell.setAttribute("class","lastRowFirstCell");
	tempCell.setAttribute("className","lastRowFirstCell");
	tempData = " ";
	tempTextNode = document.createTextNode(tempData);
	tempCell.appendChild(tempTextNode);
	footRow.appendChild(tempCell);
	
	tempCell = document.createElement('TD');
	tempCell.setAttribute("colspan","5");
	tempCell.setAttribute("colSpan","5");
	tempData = " ";
	tempTextNode = document.createTextNode(tempData);
	tempCell.appendChild(tempTextNode);
	footRow.appendChild(tempCell);
	
	tempCell = document.createElement('TD');
	tempCell.setAttribute("class","lastRowLastCell");
	tempCell.setAttribute("className","lastRowLastCell");
	tempData = " ";
	tempTextNode = document.createTextNode(tempData);
	tempCell.appendChild(tempTextNode);
	footRow.appendChild(tempCell);
	
	var tfootRow = document.createElement('TFOOT');
	tfootRow.appendChild(footRow);
	newTable.appendChild(tfootRow);
	
	newTable.appendChild(tBody);
	target = document.getElementById(this.htmlTargetID);
	target.innerHTML = "";
	target.appendChild(newTable);		
	
	// jsa 10/06/09
	if(typeof sortSetup == 'function') {
		sortSetup();
	}	
	
	$$('.ttip').each(function(elm){
		var tip = new ttipH(elm);
		tip.init();
		allToolTips.push(tip);
		
		//get children anchor tages. ie: one may be assigned to question mark.
		elm.getElementsBySelector('a').each( function(anchorElm){
													 
			//now lets make sure to cancel an anchor for the hover element if its href="#"
			Event.observe(anchorElm, 'click', stopHashAnchorClick, false);											
		})
	})
	Event.observe(window, 'resize', onWindowResize);

	
}
