jQuery(document).ready(function() {
    pollstation();
    //refresh the data every 30 seconds
    setInterval(pollstation, 30000);
});

// Accepts a url and a callback function to run.  
function requestCrossDomain( callback ) {  
    // Take the provided url, and add it to a YQL query. Make sure you encode it!  
    var yql = 'http://gomde.org.ua/scripts/flash/yql.php?callback=?';
    // Request that YSQL string, and run a callback function.  
    // Pass a defined function to prevent cache-busting.  
    jQuery.getJSON( yql, cbFunc );
  
    function cbFunc(data) {  
    // If we have something to work with...  
    if ( data ) {  
        // Strip out all script tags, for security reasons. there shouldn't be any, however
        data = data[0].results.replace(/<script[^>]*>[\s\S]*?<\/script>/gi, '');
        data = data.replace(/<html[^>]*>/gi, '');
        data = data.replace(/<\/html>/gi, '');
        data = data.replace(/<body[^>]*>/gi, '');
        data = data.replace(/<\/body>/gi, '');
  
        // If the user passed a callback, and it  
        // is a function, call it, and send through the data var.  
        if ( typeof callback === 'function') {  
            callback(data);  
        }  
    }  
    // Else, Maybe we requested a site that doesn't exist, and nothing returned.  
    else throw new Error('Nothing returned from getJSON.');  
    }  
}  

function pollstation() {
    requestCrossDomain(function(stationdata) {
        //make our data into an array
        var lines = stationdata.split('<br/>');
        
        //update number of listeners
        jQuery('#listeners').html(lines[0]);

        //update the album art
//        jQuery('#songsearch').html('<img src="http://audioprobe.net/art.php?query=' + encodeURIComponent(jQuery.trim(lines[1])) + '" />');            

        //transform the song title into [artist] - [title] ([year])
//        s_info=lines[1].split(" - ");
        
        //remove the artist from the title
//        title=jQuery.trim(s_info[1]);
        
        //remove the year from the title
//        cleantitle=jQuery.trim(s_info[1].replace(/\ \(\d{4}\)/,''));
        
        //keep just the year
//        new_year=title.replace(cleantitle, '');
        
        //get rid of parenthesis around the year
//        new_year=new_year.replace(/\ \(/,'');
//                new_year=new_year.replace(/\)/,'');
        
        //if a special show, let's identify it and properly format it
//        var index = cleantitle.indexOf("[Aural Pleasure]");
//        if(index != -1) {
//            //remove the show title from the title of the song
//           cleantitle=cleantitle.replace(/\ \[Aural Pleasure\]/,'');
//            
//            //replace the year with the song
//            new_year='Aural Pleasure';
//            //update the album art for the show
//            jQuery('#songsearch').html('<img src="http://audioprobe.net/auralpleasure.jpg" alt="Aural Pleasure" />');
//        }
        
        //update the current artist and song title
        
        jQuery('#currentsong').html(lines[1]);
        s_info=lines[1].split(" - ");
        jQuery('#artist').html(s_info[0]);
        jQuery('#album').html(s_info[1]);
        jQuery('#song').html(s_info[2]);
        
        //update the previously played songs
//        for (var i = 1; i <= 10; i++) {            
//            jQuery('#prevsong' + i).html(lines[i+1]);
//        }
           jQuery('#prevsong').html(lines[2]);  

    } );
}
