function Stream(title, url){
  this.title = title;
  this.url = url;
  this.song = '';
}

jQuery.fn.fadeTo = function(speed,to,callback) { 
    return this.animate({opacity: to}, speed, function() { 
        if (to == 1 && jQuery.browser.msie)  
            this.style.removeAttribute('filter');  
        if (jQuery.isFunction(callback)) 
            callback();  
    }); 
}; 

var new_text = new Array();
var now_playing = $('#now_playing .content dl');
var streams = new Array(
  new Stream('Shine.FM 89.7', '/sf/nowplaying/WONUNowPlaying.xml'),
  new Stream('Shine Worship', '/sf/nowplaying/WONU2nowplaying.xml')
//  ,new Stream('Shine HD', '/sf/nowplaying/WONU3nowplaying.xml')
);

$(function(){
  update_now_playing();
  setInterval('update_now_playing()', 60000);
});

function update_now_playing(){
  $.each(streams, function(i, stream){
    $.ajax({
      url: stream.url,
      cache: false,
      dataType: 'xml',
      success: function(data, textStatus){ 
        var event_data = $(data).find('Event_Type:contains("MUSIC"):first').parent();
        var event_type = $(event_data).find('Event_Type').text();

        new_text[i] = '<dt>' + stream.title + '</dt>';

        if (event_type){
          new_text[i] += '<dd>' + $(event_data).find('Title').text() + ' by ' +
            $(event_data).find('Artist').text() + '</dd>';
          stream.song = new_text[i]; 
        } else if (stream.song != ''){
          new_text[i] = stream.song
        } else {
          new_text[i] += '<dd>Music coming up next.</dd>';
        }
      }
    });
  });

  $(now_playing).fadeTo('slow', 0, function(){
    $(now_playing).html(new_text.join('\n'));
    $(now_playing).fadeTo('fast', 1);
  });
}
