$(document).ready(function() {
    var config = {
        app_id: "235057909900106",
        link: "http://www.magicvariety.com/listen",
        picture: "http://www.jerseysgreatest.com/mobile/WJRZ_WHITE_130.jpg",
        short_name: "Magic 100.1",
        full_name: escape("Magic 100.1"),
        description: escape("You can hear it right now too: listen live at magicvariety.com."),
        redirect_uri: document.location.href
    };
    updateShareButton(config);
});

function updateShareButton(config) {
    $.ajax({
        type: "GET",
        url: "http://www.magicvariety.com/_SHARED/NowPlaying/RecentlyPlayed.ashx?NumberOfSongs=1",
        dataType: "xml",
        success: function(xml) {
    
	  $(xml).find("Song").each(function() {
                var caption = escape($(this).attr("title") + " by " + $(this).attr("artist") + " is playing on "+ config["short_name"] + ".");
                var fbShare = "<a class='fb share-popup' target='_blank' " +
                    "href='https://www.facebook.com/dialog/feed?app_id="+ config["app_id"] +
                    "&link="+ config["link"] +
                    "&picture="+ config["picture"] +
                    "&name="+ config["full_name"] +
                    "&caption="+ caption +
                    "&description="+ config["description"] +
                    "&display=popup"+
                    "&redirect_uri="+ config["redirect_uri"] +"'>"+
                    "<span class='liketext'>Share This Song</span></a>";
                    
                $("#fb-song-share").html(fbShare); // replace the button with the updated HTML
                
               /*
 * jQuery popup v1 - A jQuery popup plugin.
 * By Jordan Thomas - http://labs.wondergroup.com
 * Licensed under the do whatever you want to license.
 * If you like, keep this message intact so
 * someone else can find the origin.
*/
jQuery.fn.popup=function(options){var defaults={width:screen.width/2,height:screen.height/2,titlebar:true,status:true,resizable:true,toolbar:true,scrollbars:true,menubar:true};var options=jQuery.extend(defaults,options);Boolean.prototype.setProperty=function(){if(this==true){return"yes";}else{return"no";}};jQuery(this).click(function(){var target=this.target;var href=this.href;var posY=(parseInt(screen.height/2))-(parseInt(options.height/2));var posX=(parseInt(screen.width/2))-(parseInt(options.width/2));var win=window.open(href,target,'titlebar='+options.titlebar.setProperty()+', screenX='+posX+', screenY='+posY+', left='+posX+', top='+posY+', status='+options.status.setProperty()+', resizable='+options.resizable.setProperty()+', toolbar='+options.toolbar.setProperty()+', scrollbars='+options.scrollbars.setProperty()+', menubar='+options.menubar.setProperty()+', width='+options.width+', height='+options.height);win.focus();return false;});return this;};   
                $(".share-popup").popup({width: 500,height: 300,resizeable: false,scrollbars: false,titlebar:false,status:false,toolbar:false,menubar:false});
            });

        },     
        complete: function() {
            setTimeout(updateShareButton, 15000, config); // repeat in 15 seconds
        },
        error:function (xhr, ajaxOptions, thrownError){
            console.log(xhr.status);
            console.log(thrownError);
        }
    });
}
