	var xmlhttp;
	var minofhour;
	var nowPlayingTimer;
	var xml;
	var time_difference;
	
	function loadXMLDoc(url_in) {
                if (location.host == "www.ksmu.org")
                        url = "http://www.ksmu.org/" + url_in;
                else if (location.host == "localhost")
                        url = "http://localhost/ksmu/" + url_in;
                else
                        url = "http://ksmu.org/" + url_in;
		
		$.get(url, function(xml_in) {
			xml = xml_in;
			var now = (new Date()).getTime();
			// We need to sync our server time to the user's time. The user's time could be set to anything.
			time_difference = parseInt($(xml).find('server-time').text())*1000 - now;
			parsePlaylist();
		});
	}
	
	function parsePlaylist() {
                $('#playlist_track').fadeTo(2500, 0, function() {
                        // TWEAK SYSTEM SYNCHRONIZATION WITH THIS VARIABLE.
                        // Positive values mean that the host server is slow. One second is 1000.
                        var server_offset = 158000;
                        var now = (new Date()).getTime() + time_difference + server_offset;

                        var trackPlaying = false;
                        var track = null;
                        // Spin through each track until we find one that is during the current time, then write it to whatson.
                        $(xml).find('track').each(function() {
                                track = $(this);
                                var track_time = new Date(track.find('track_start_time').text());
                                if(now >= track_time.getTime() && now < (track_time.getTime() + parseInt(track.find('track_length').text()))) {
                                        var content = $(xml).find('program').text() +
                                                '<p id="playlist_track" style="opacity: 0; font-size:70%; border-top: 1px solid #000; border-bottom: 1px solid #000; margin-bottom: 0;"><i>' + track.find('artist').text() + '</i><br />' +
                                                '<b>' + track.find('track_title').text() + '</b><br />' +
                                                track.find('album').text() + '</p>' +
                                                '<p style="margin-bottom: 0;">' + $(xml).find('next').text() + '</p>';
                                        $('#whatson').html(content);
                                        $('#playlist_track').fadeTo(2500, 1);
                                        trackPlaying = true;
                                        return false;
                                }
                        });

		
                        if(trackPlaying) {
                                var track_time = new Date(track.find('track_start_time').text());

                                // Call this function again after the length of the current track
                                // We do start time - now because it's possible that we started in the middle of the track
                                setTimeout('parsePlaylist()', track_time.getTime() - now + parseInt(track.find('track_length').text()));

                        // If no track is playing, then that means either this playlist is finished, or there is a gap in the tracks.
                        // We need to look through the playlist and see if any tracks are still in the future, and if so, set a timer
                        // for that track.
                        } else {
                                $('#whatson').html($(xml).find('program').text() + '' +
                                                '<p id="playlist_track" style="margin-bottom: 0;"></p>' +
                                                '<p style="margin: 0;">' + $(xml).find('next').text() + '</p>');

                                // If streamName is true, then the user is listening on the standalone player and we need to set the
                                // title of the window.
                                if(typeof(streamName) != 'undefined') {
                                        if($('#whatson')[0].firstChild.nodeName.toLowerCase() == 'a')
                                                document.title = whatsontag.firstChild.innerHTML + " on " + streamName;
                                        else
                                                document.title = whatsontag.innerHTML + " on " + streamName;
                                }

                                // Look for any track on the current playlist in the future. Maybe none of the tracks have started playing
                                // yet, or maybe there is a break between tracks, for instance if the host is talking. Also subtract
                                // 2500 ms to offset the fadeIn timer.
                                $(xml).find('track').each(function() {
                                        track = $(this);
                                        var track_time = new Date(track.find('track_start_time').text());
                                        if(now <= track_time.getTime()) {
                                                setTimeout('parsePlaylist()', track_time.getTime() - now - 2500);
                                                trackPlaying = true;
                                                return false;
                                        }
                                });

                                // If we still can't find a track to play, then the playlist is done. This probably means that the show is over,
                                // so if the user is still streaming, we need to find the next program. Put a timer on this so that if there is
                                // some downtime between shows we don't get stuck in a loop constantly running ajax calls.
                                if(!trackPlaying)
                                        setTimeout("loadXMLDoc('/extras/whatson.php?type=ext&next=true&stream='+ streamID);", 300000);
                        }
                });
        }

	
	function checkNowPlaying(streamnum) {
	   var d = new Date();
	   minofhour = d.getMinutes();
	   //minofhour = 2;
	   if ((minofhour==2)||(minofhour==32)) {
	      loadXMLDoc('/extras/whatson.php?type=ext&stream='+streamnum);
	   }
	   
	   nowPlayingTimer = setTimeout("checkNowPlaying(" + streamnum + ");",18000);
	}
	
	function stopCheckingNP() {
	   clearTimeout(nowPlayingTimer);
	}
	
