/**
* Olof Montin, 24 nov 2006
**/
var P3 = {
	/**
	* Variables
	* Global variables used by every process in this object.
	**/
	// Radio - Tips
	tipsDisplayInterval: 1000 * 30, // Defines what interval speed to use when displaying sound clip tips
	// Radio - Display
	displayItemUrl: '/p3/chart/current.xml', // Which url to use when fetching display xml data
	displayImageDir: '/p3/justnu/images/', // Where all the right-now-playing-item-images are
	displayTimout: (60 * 1000) * 2, // Sets the get-items-for-display-timout
	/**
	* Init function
	* Use this function to start all P3 content processes.
	* <body onload="P3.init();"> [...]
	**/
	init: function() {
		// Start soundclips tips display interval
		P3.radio.tips.display();
		// Fetch display item with ajax
		P3.radio.display.getItem();
	},
	/**
	* Radio object
	**/
	radio: {
		/**
		* Tips object
		**/
		tips: {
			/**
			* Store displayed items in this array
			**/
			displayed: new Array(),
			/**
			* Function used to display each item by an interval
			**/
			display: function() {
				// Fetch the div which contains all tips-li-items
				var list = document.getElementById('id_164_14');
				// End this story if there isn't any list...
				if (!list) return;
				// Fetch first non displayed item and display it...
				var items = list.getElementsByTagName('li');
				// Loop though items and undisplay them
				for (var i = 0; i < items.length; i++) {
					items[i].style.display = 'none';
				}
				if (P3.radio.tips.displayed.length >= items.length) {
					// All items has been displayed.
					// Reset array and execute this function again.
					P3.radio.tips.displayed = new Array();
					P3.radio.tips.display();
				} else {
					// Displayed array is still not too big,
					// display next item.
					var d = -1;
					while (d < 0) {
						d = Math.floor(Math.random() * items.length);
						for (var i = 0; i < P3.radio.tips.displayed.length; i++) {
							if (d == P3.radio.tips.displayed[i]) {
								d = -1;
								break;
							}
						}
					}
					items[d].style.display = 'block';
					P3.radio.tips.displayed.push(d);
					// Execute next displaying by an interval
					setTimeout(P3.radio.tips.display, P3.tipsDisplayInterval);
				}
			}
		},
		/**
		* Display object
		* Handles the radio display with a right-now-playing-item
		**/
		display: {
			/**
			* Fetches the right-now-playing-item/programme
			* with ajax.
			**/
			getItem: function() {
				// Create the ajax/xml-fetching-object.
				try {
					// Try to create the Internet Explorer's ajax-xml object.
					var xml = new ActiveXObject('Msxml2.XMLHTTP');
				} catch (e) {
					// This browser could not create the ActiveX Object Msxml2.XMLHTTP.
					try {
						// Try another Explorer usable ajax-xml object.
						var xml = new ActiveXObject('Microsoft.XMLHTTP');
					} catch (e) {
						// Well, i'll try to create the standardized ajax-xml object.
						var xml = new XMLHttpRequest();
					}
				}
	
				// Define onReadyStateChange event with a function.
				// This event will run everytime something new happens
				// in the xml fetching process.
				xml.onreadystatechange = function() {
					// The readyState status is 4, which means that
					// everything is loaded and complete.
					// Now run the item rendering function.
					if (xml.readyState == 4) {
						if (xml.status == 200)
							P3.radio.display.renderItem(xml);
					}
				}
	
				// Define which url to use and how to get all data.
				xml.open('GET', P3.displayItemUrl + "?" + (new Date()).getTime(), true);
				// Run the fetching process.
				xml.send(null);
			},
			/**
			* Renders fetched item
			**/
			renderItem: function(xml) {
				// Fetch display elements
				var image = document.getElementById('p3_radio_display_image');
				var desc = document.getElementById('p3_radio_display_desc');
				var add = document.getElementById('p3_radio_display_add');
				// If the chart element, then end this function
				if (!image || !desc || !add) return;
				// If the xml object doesn't contain a proper xml response
				if (!xml.responseXML) return;
				// End this function if the xml object doesn't contain
				// a valid document element.
				if (!xml.responseXML.documentElement) return;
	
				// Fetch all programmes from document element
				var items = xml.responseXML.documentElement.getElementsByTagName('programme');
				// Quit if there are no programmes
				if (items.length <= 0) return;

				// Set description
				desc.innerHTML = '';
				var xTitle = items[0].getElementsByTagName('title');
				xTitle = xTitle.length > 0 && xTitle[0].firstChild ? xTitle[0].firstChild.nodeValue : '';
				var xDesc = items[0].getElementsByTagName('desc');
				xDesc = xDesc.length > 0 && xDesc[0].firstChild ? xDesc[0].firstChild.nodeValue : '';
				var xDescAdd = items[0].getElementsByTagName('descadd');
				xDescAdd = xDescAdd.length > 0 && xDescAdd[0].firstChild ? xDescAdd[0].firstChild.nodeValue : '';
				desc.innerHTML = '' +
					'<strong>' + xTitle + '</strong><br>'
					'<small>' + xDesc + '</small><br>' +
					'<small>' + xDescAdd + '</small>';
				// Set image properties
				image.innerHTML = '';
				var xImage = items[0].getElementsByTagName('image');
				if (xImage.length > 0) image.innerHTML = '<img src="' + P3.displayImageDir + '' + xImage[0].firstChild.nodeValue + '" alt="' + xTitle + '">';
				// Set artist and song
				add.innerHTML = '';
				var xPerf = items[0].getElementsByTagName('performance');
				if (xPerf.length > 0) {
					var xArtist = xPerf[0].getElementsByTagName('artist');
					xArtist = xArtist.length > 0 && xArtist[0].firstChild ? xArtist[0].firstChild.nodeValue : '';
					if (xArtist.length <= 0) xArtist = '&nbsp;';
					var xSong = xPerf[0].getElementsByTagName('song');
					xSong = xSong.length > 0 && xSong[0].firstChild ? xSong[0].firstChild.nodeValue : '';
					if (xSong.length <= 0) xSong = '&nbsp;';
					// Show artist and song if they exist.
					// Hide element otherwise.
					if (xArtist.length > 0 || xSong.length > 0) {
						add.style.display = 'block';
						add.innerHTML = '<strong>' + xArtist + '</strong><br>' + xSong;
					} else
						add.style.display = 'none';
				}

				// Set timout for next item fetch
				setTimeout(P3.radio.display.getItem, P3.displayTimout);
			}
		}
	}
}
