var xmlhttp = false;
var warned = false;
var serverDelta = 0;
var theInterval = 0;
/*@cc_on @*/
/*@if (@_jscript_version >= 5 )
try {
        xmlhttp = new ActiveXObject( "Msxml2.XMLHTTP" );
} catch (e) {
        try {
                xmlhttp = new ActiveXObject( "Microsoft.XMLHTTP" );
        } catch(e) {
                xmlhttp = false;
        }
}
@else
        alert( "You must have JScript version 5 or above." );
        xmlhttp = false;
        warned = true;
@end @*/

if( !xmlhttp && !warned ) {
        try {
                xmlhttp = new XMLHttpRequest();
				if( xmlhttp.overrideMimeType ) {
  					xmlhttp.overrideMimeType('text/xml');
				}	
        } catch(e) {
                alert( "You need a browser which supports an XML request" );
        }
		
}

function newXML() {
        if( xmlhttp.readyState == 4 ) {
                var xml = null;
                var next = -1;

				if( navigator.appName.indexOf('Microsoft') != -1 ) {
					xml = xmlhttp.responseXML;
	                for( var x = 0; x < xml.childNodes.length; x++ ) {
	                        if( xml.childNodes[x].nodeName == 'PlayList' ) {
	                                var y = xml.childNodes[x].childNodes;
									//alert( y[0].nodeName );
	                                setData( 'nowPlaying', y[0] );
	                                setData( 'nextUp', y[1] );
									var thisDate = new Date();
	                                next = (serverDelta+Date.parse(y[1].getAttribute('StartTime'))) - thisDate.getTime();
									//window.status = "Next update = " + next;
	                        }
	                }

				} else {
					var np = 1;
					xml = xmlhttp.responseXML.firstChild;
					for( x=0;x<xml.childNodes.length;x++ ) {
						
						if( xml.childNodes[x].nodeName == 'Entry' ) {
							if( np == 1) {
								setData( 'nowPlaying', xml.childNodes[x] );
							    np = 2;
							} else if( np == 2 ) {
								setData( 'nextUp', xml.childNodes[x] );
								var thisDate = new Date();
								next = (serverDelta+Date.parse(xml.childNodes[x].getAttribute('StartTime'))) - thisDate.getTime();
								np = 3;
								//window.document.title = "Next update = " + next;
							}
						}
					}
				}
				
                if( theInterval != 0 )
                        window.clearInterval( theInterval );

                if( next < 0 )
                        next = 5000;

                theInterval = window.setInterval( doUpdate, next );


        }
}

function setData( what, node ) {
        var x = document.getElementById(what);
		//alert( node.nodeName );
        x.innerHTML = '<b>'+node.getAttribute('Title')+'</b> by <b>'+node.getAttribute('Artist')+
		      '</b> <span style="font-size:smaller;font-style:italic;">('+toMS(node.getAttribute('Duration'))+
		      ')</span>';

}

function two(x) { return ((x>9)?'':'0')+x; }

function toMS( ms ) {
        var sec = Math.floor(ms / 1000);
        ms = ms % 1000;
        var min = Math.floor(sec/60);
        sec = sec % 60;

        return two(min)+':'+two(sec);
}

function doUpdate() {
        xmlhttp.open( "GET", "http://www.sayradio.net/assets/imediatouch/CSCA_FM.xml", true );
        xmlhttp.onreadystatechange = newXML;
        xmlhttp.send(null);
}

function serverDate() {
        if( xmlhttp.readyState == 4 ) {
				var thisDate = new Date();
                var now = Date.parse( thisDate.toGMTString() );
                serverDelta = Date.parse(xmlhttp.getResponseHeader("Date") ) - now;
                doUpdate();
        }
}

function startList() {
		doUpdate();
}