 
/*'Now Playing' AJAX plugin for radiopilote-fork 0.1. okay_awright, 2008. Internal use only. Released to public domain.*/ 
var url='resources/radio-nowplaying-channel1.xml';
var reloadTime=35;
var skew_offset=600;

function PlayingTrack(a, t, y, r, l, p, z) {
	this.playing_artist = a;
        this.playing_title = t;
        this.playing_year = y;
        this.playing_rule = r;
        this.playing_length = l;
        this.playing_start = p;
	this.playing_type = z;
        this.displayTags = function() {
		var div_obj = document.getElementById('radiopilote_artist');
                if (div_obj != null) div_obj.innerHTML = this.playing_artist;
		div_obj = document.getElementById('radiopilote_track');
                if (div_obj != null) {
			var single_block = this.playing_artist;
			if (this.playing_artist != '' && this.playing_title != '')
				single_block = single_block + ' - ';
			single_block = single_block + this.playing_title;
			if (this.playing_type == 'songs' && this.playing_year != 0 && this.playing_year != '')
				single_block = single_block + ' [' + this.playing_year + ']';  
			div_obj.innerHTML = single_block;
		}
                div_obj = document.getElementById('radiopilote_title');
                if (div_obj != null) div_obj.innerHTML = this.playing_title;
                div_obj = document.getElementById('radiopilote_year');
                if (div_obj != null) div_obj.innerHTML = this.playing_year;
                div_obj = document.getElementById('radiopilote_rule');
                if (div_obj != null) div_obj.innerHTML = this.playing_rule;
		div_obj = document.getElementById('radiopilote_start');
                if (div_obj != null) div_obj.innerHTML = this.playing_start;
		div_obj = document.getElementById('radiopilote_length');
                if (div_obj != null) div_obj.innerHTML = this.playing_length;
       }
}

function createRequestObject() {
	try { return new XMLHttpRequest(); } catch(e) {}
	try { return new ActiveXObject('Msxml2.XMLHTTP'); } catch (e) {}
	try { return new ActiveXObject('Microsoft.XMLHTTP'); } catch (e) {}
	return null;
}

var httpX = createRequestObject();
if( window.addEventListener ) {
	window.addEventListener('load',reloadData,false);
} else if( document.addEventListener ) {
	document.addEventListener('load',reloadData,false);
} else if( window.attachEvent ) {
	window.attachEvent('onload',reloadData);
}

function reloadData() {
	httpX.open('GET', url, true);
	httpX.setRequestHeader('Cache-Control', 'no-cache');
        httpX.setRequestHeader('If-Modified-Since', 'Sat, 1 Jan 1980 00:00:00 GMT');
	httpX.setRequestHeader('Expires', 'Wed, 1 Jan 1980 00:00:00 GMT');
	httpX.setRequestHeader('Connection', 'close');
	//Placer le callback APRES la connexion afin de réutiliser l'instance de l'objet sans en créer un nouveau
	//Sinon utiliser abort()
        httpX.onreadystatechange = handleResponse;
        httpX.send('');

        if (reloadTime != 0) {
                setTimeout('reloadData()', reloadTime*1000);
        }

}

function handleResponse() {
	// only if req shows 'complete'
    	if (httpX.readyState == 4) {
        	// only if 'OK'
        	if (httpX.status == 200) {
			var response = httpX.responseXML.documentElement;
			//Get data from XML
			var current_track = new PlayingTrack(
					(response.getElementsByTagName('artist')[0].firstChild != null ? response.getElementsByTagName('artist')[0].firstChild.data : ''),
					(response.getElementsByTagName('title')[0].firstChild != null ? response.getElementsByTagName('title')[0].firstChild.data : ''),
					(response.getElementsByTagName('year')[0].firstChild != null ? response.getElementsByTagName('year')[0].firstChild.data : ''),
					(response.getElementsByTagName('rule')[0].firstChild != null ? response.getElementsByTagName('rule')[0].firstChild.data : ''),
					(response.getElementsByTagName('duration')[0].firstChild != null ? response.getElementsByTagName('duration')[0].firstChild.data : ''),
					(response.getElementsByTagName('played')[0].firstChild != null ? response.getElementsByTagName('played')[0].firstChild.data : ''),
					(response.getElementsByTagName('type')[0].firstChild != null ? response.getElementsByTagName('type')[0].firstChild.data : '')
				);
			var since = '';
			//Guess if radio is off
			var now = new Date();
                        var elapsed_since_first_played = Math.round( ( now.getTime() / 1000 ) - current_track.playing_start );
                        if ((current_track.playing_length > -1) && (elapsed_since_first_played > (current_track.playing_length + skew_offset))) {
				current_track = new PlayingTrack(
					'Radio is off',
					'',
					'0',
					'No current programme',
					'-1',
					'0'
				);
			} else if (elapsed_since_first_played <= 3600) {
				var minutes = Math.round( elapsed_since_first_played / 60 );
				if (minutes < 1) {
					since = 'since a few seconds';
				} else {
					if (minutes < 2) {
						since = 'since 1 minute';
					} else {
						since = 'since '+minutes+' minutes';
					}
				}
			} else if ((elapsed_since_first_played <= 86400) && (elapsed_since_first_played > 3600)) {
                        	var hours = Math.round(elapsed_since_first_played / 3600);
                        	if (hours <= 1) {
                                	hours = 1;
					since = 'since 1 hour';
                        	} else {
					since = 'since '+hours+' hours';
				}
                	} else if (elapsed_since_first_played > 86400) {
                        	var days = round(elapsed_since_first_played / 86400);
                        	if (days <= 1) {
                                	days = 1;
					since = 'since 1 day';
                        	} else {
					since = 'since '+days+' days';
				}
                	}
			//Display elapsed time since start
			var div_obj = document.getElementById('radiopilote_since');
                        if (div_obj != null) div_obj.innerHTML = since;
			//Display track
			current_track.displayTags();

			var div_obj = document.getElementById('radiopilote_error');
			if (div_obj != null) div_obj.style.visibility = 'hidden';
        	} else {
            		var div_obj = document.getElementById('radiopilote_error');
			if (div_obj != null) {
				div_obj.innerHTML = 'There was a problem retrieving the XML data: ' + httpX.statusText;
				div_obj.style.visibility = 'visible';
			}
        	}
    	}
}
