( function( $ ) {

var ListenLive = {
    openedPlayerWindow: null,
    refreshInterval: 90000,
    checkStatusIntervalID: null,
    checkStatusInterval: 5000,
    stationCallName: window.ROGERS_PLAYER_STATION,

    init: function(station_name) {
        var self = this;
        if(station_name){
            this.stationCallName = station_name;
        }

        //var $listenLiveLinks = jQuery('#listen-live-player-link, #footer-widgets a[href^="http://' + window.ROGERS_PLAYER_LISTEN_LIVE_LINK + '/"], #footer-widgets a[href^="http://' + window.ROGERS_PLAYER_LISTEN_LIVE_LINK + '"]');
        /*var listenLiveURL = 'http://' + window.ROGERS_PLAYER_LISTEN_LIVE_LINK;
        var $listenLiveLinks = jQuery('.listen-live-player-link');
        $listenLiveLinks.attr('target', 'ListenLiveWindow');
        $listenLiveLinks.attr('href', listenLiveURL);*/

        // Open the player popup when clicking on the listen live link; falls back to opening in a new window (if popups are blocked)
        /*$listenLiveLinks.click(function(e){
            self.openedPlayerWindow = window.open(
                $(this).attr('href'),
                'ListenLiveWindow',
                'width=' + window.ROGERS_PLAYER_WIDTH + ',height=' + window.ROGERS_PLAYER_HEIGHT + ',status=0,resizable=0'
            );
            if(self.openedPlayerWindow){
                e.preventDefault();
                self.openedPlayerWindow.focus();
            }
        });*/

        this.fill(this.stationCallName);

        // Set an interval timer to display the on-now section when we get back the JSON response
        this.checkStatusIntervalID = setInterval(this.checkStatus(), this.checkStatusInterval);

        //Enable auto-refresh per RADIOTEMP-1065 request
        var that = this;
        setInterval(function() {
            that.fill(that.stationCallName)
        }, this.refreshInterval);
    },

    fill: function(station_name){
        this.get_on_air_widget(station_name);
        this.get_now_playing_widget(station_name);
       // this.get_recently_played_widget(station_name, window.ROGERS_RECENTLY_PLAYED_COUNT, 1);
    },

    checkStatus: function() {
        $('.data-on-air').each(function() {
            if ( $( this ).text() === '' )
                return false;
        });
        clearInterval(this.checkStatusIntervalID);
        $('#on-now').show();
    },

    get_on_air_widget: function (station_call_name) {
        var $ = jQuery;
        $.ajax({
            url: "https://" + window.ROGERS_PLAYER_SERVER + "/"+ station_call_name+"/widget/on_air",
            dataType: 'jsonp',
            jsonp: 'jsoncallback',
            jsonpCallback: 'll_onair',
            cache: true,
            success: function(data) {
                $('.data-on-air-now-name').html(data.name);
                if (data.show_url) {
                    $('.data-on-air-now-name').attr('href',  decodeURIComponent(data.show_url) );
                }
                /*
                * Other values:
                * data.description
                * data.show_url
                */
            }
        });
    },

    get_now_playing_widget: function (station_call_name){
        var $ = jQuery;
        $.ajax({
            url: "https://" + window.ROGERS_PLAYER_SERVER + "/"+ station_call_name+"/widget/now_playing",
            dataType: 'jsonp',
            jsonp: 'jsoncallback',
            jsonpCallback: 'll_nowplaying',
            cache: true,
            success: function(data) {
                $('.data-now-playing-song').html(data.song_title);
                $('.data-now-playing-artist').html(data.artist);
                if(data.art_url){
                    $('#live-bar .album-art .data-now-playing-album-art').attr("src", decodeURIComponent(data.art_url));
                    $('#live-bar .album-art').addClass('loaded');
                }
                else {
                    $('#live-bar .album-art .data-now-playing-album-art').attr("src", "/wp-includes/images/blank.gif");
                    $('#live-bar .album-art').removeClass('loaded');
                }
                //$('#now_playing_image').attr("alt", data.artist);
               // $('.data-now-playing-itunes-url')
                 //   .attr("href", data.itunes_purchase_url ? decodeURIComponent(data.itunes_purchase_url) : '');
                    //.toggle(!!data.itunes_purchase_url);
                /*
                * Other values:
                * data.album
                */
            }
        });
    },

    get_recently_played_widget: function (station_call_name, num_per_page, page_num){
        var $ = jQuery;
        $.ajax({
            url: "https://" + window.ROGERS_PLAYER_SERVER + "/" + station_call_name + "/widget/recently_played?num_per_page=" + num_per_page + "&page=" + page_num,
            dataType: 'jsonp',
            jsonp: 'jsoncallback',
            jsonpCallback: 'll_recentlyplayed',
            cache: true,
            success: function(data) {
                var $headRow = $(
                    "<tr class='header'>" +
                        "<th class='time'>Time</th>" +
                        "<th class='title'>Song Name</th>" +
                        "<th class='album'>Album</th>" +
                        "<th class='artist'>Artist</th>" +
                        "<th class='purchase'>Buy</th>" +
                    "</tr>"
                );

                var $rowTpl = $(
                    "<tr>" +
                        "<td class='time'></td>" +
                        "<td class='title'></td>" +
                        "<td class='album'></td>" +
                        "<td class='artist'></td>" +
                        "<td class='purchase'><a class='button-buy' href='' target='_blank'>Buy<span title='Buy'></span></a></td>" +
                    "</tr>"
                );

                var $table = $('#rc_list');

                $table.empty();
                $table.append($headRow);

                $.each(data, function(i,rc){
                    var $row = $rowTpl.clone();

                    if (i==0) {
                        $row.addClass('first');
                    }

                    $row.find('.time').text(rc.started_at);
                    $row.find('.title').text(rc.song_title);
                    $row.find('.album').text(rc.album);
                    $row.find('.artist').text(rc.artist);
                    $row.find('a.button-buy')
                        .attr('href', rc.itunes_purchase_url ? $.url.decode(rc.itunes_purchase_url) : '')
                        .toggle(!!rc.itunes_purchase_url);

                    $table.append($row);
                });
                /*
                * Other values:
                * data.art_url
                */
            }
        });
    }
}
ListenLive.init();    

} )( jQuery );