$(document).ready(function(){
    var currentTrack = 0;
    
    var playList = [
        { title: "40 Day Dream",        album: "Up From Below", file: "/music/UpFromBelow-40DayDream.m4a" },
        { title: "Janglin",             album: "Up From Below", file: "/music/UpFromBelow-Janglin.m4a" },
        { title: "Up from Below",       album: "Up From Below", file: "/music/UpFromBelow-UpFromBelow.m4a" },
        { title: "Carries On",          album: "Up From Below", file: "/music/UpFromBelow-CarriesOn.m4a" },
        { title: "Jade",                album: "Up From Below", file: "/music/UpFromBelow-Jade.m4a" },
        { title: "Home",                album: "Up From Below", file: "/music/UpFromBelow-Home.m4a" },
        { title: "Desert Song",         album: "Up From Below", file: "/music/UpFromBelow-DesertSong.m4a" },
        { title: "Black Water",         album: "Up From Below", file: "/music/UpFromBelow-BlackWater.m4a" },
        { title: "Come In Please",      album: "Up From Below", file: "/music/UpFromBelow-ComeInPlease.m4a" },
        { title: "Simplest Love",       album: "Up From Below", file: "/music/UpFromBelow-SimplestLove.m4a" },
        { title: "Kisses Over Babylon", album: "Up From Below", file: "/music/UpFromBelow-KissesOverBabylon.m4a" },
        { title: "Brother",             album: "Up From Below", file: "/music/UpFromBelow-Brother.m4a" },
        { title: "Om Nashi Me",         album: "Up From Below", file: "/music/UpFromBelow-OmNashiMe.m4a" }
    ];

// init    
      $("#jquery_jplayer_1").jPlayer({
        ready: function () {
          $(this).jPlayer("setMedia", {
            m4a: playList[currentTrack].file
          });
        },
        swfPath: "/wp-content/themes/sharpe/scripts",
        supplied: "m4a",
        solution: "html, flash"
      }).bind($.jPlayer.event.ended + ".jp-repeat", function(event) { // Using ".jp-repeat" namespace so we can easily remove this event
    playListNext(); // Add a repeat behaviour so media replays when it ends. (Loops)
        
  });      
    $("#playerSongInfo").html(playList[currentTrack].album + " - " + playList[currentTrack].title + "");
    

    // Switch track
    function playListChange(index, play) {
        if (play == null)
            play = true;
            
        currentTrack = index;
        $("#playerSongInfo").html(playList[currentTrack].album + " - " + playList[currentTrack].title + "");
        $("#jquery_jplayer_1").jPlayer("setMedia", {m4a: playList[currentTrack].file});
        
        if (play == true)
            $("#jquery_jplayer_1").jPlayer("play");
    }
    
    $(".jp-previous").click(function() { playListPrev(); }); // Listen for Previous Track button click
    $(".jp-next").click(function() { playListNext(); }); // Listen for Next Track button click
	$(".jp-buynow").click(function() {window.open("http://itunes.apple.com/us/artist/edward-sharpe-the-magnetic/id315875820", "", "width=1000px, height=700px, resizable");});
	
    // Play next track. If already on last track, start back at the begining
    function playListNext() {
        var index = (currentTrack + 1 < playList.length) ? currentTrack + 1 : 0;
        playListChange(index);
    }

    // Play previous track. If already on first track, start back at the end
    function playListPrev() {
        var index = (currentTrack - 1 >= 0) ? currentTrack - 1 : playList.length-1;
        playListChange(index);
    }    
    
    $(".jp-popout").click(function(){
            
            var settings = {
                centerBrowser:0, // center window over browser window? {1 (YES) or 0 (NO)}. overrides top and left
                centerScreen:0, // center window over entire screen? {1 (YES) or 0 (NO)}. overrides top and left
                height:200, // sets the height in pixels of the window.
                left:0, // left position when the window appears.
                location:0, // determines whether the address bar is displayed {1 (YES) or 0 (NO)}.
                menubar:0, // determines whether the menu bar is displayed {1 (YES) or 0 (NO)}.
                resizable:0, // whether the window can be resized {1 (YES) or 0 (NO)}. Can also be overloaded using resizable.
                scrollbars:0, // determines whether scrollbars appear on the window {1 (YES) or 0 (NO)}.
                status:0, // whether a status line appears at the bottom of the window {1 (YES) or 0 (NO)}.
                width:440, // sets the width in pixels of the window.
                windowName:null, // name of window set from the name attribute of the element that invokes the click
                windowURL:null, // url used for the popup
                top:0, // top position when the window appears.
                toolbar:0 // determines whether a toolbar (includes the forward and back buttons) is displayed {1 (YES) or 0 (NO)}.
            };
            
            $("#jquery_jplayer_1").jPlayer("stop");    
                        
            var windowFeatures =    'height=' + settings.height +
                                    ',width=' + settings.width +
                                    ',toolbar=' + settings.toolbar +
                                    ',scrollbars=' + settings.scrollbars +
                                    ',status=' + settings.status + 
                                    ',resizable=' + settings.resizable +
                                    ',location=' + settings.location +
                                    ',menuBar=' + settings.menubar;

                    settings.windowName = this.name || settings.windowName;
                    settings.windowURL = this.href || settings.windowURL;
                    var centeredY,centeredX;
                
                    if(settings.centerBrowser){
                            
                        if ($.browser.msie) {//hacked together for IE browsers
                            centeredY = (window.screenTop - 120) + ((((document.documentElement.clientHeight + 120)/2) - (settings.height/2)));
                            centeredX = window.screenLeft + ((((document.body.offsetWidth + 20)/2) - (settings.width/2)));
                        }else{
                            centeredY = window.screenY + (((window.outerHeight/2) - (settings.height/2)));
                            centeredX = window.screenX + (((window.outerWidth/2) - (settings.width/2)));
                        }
                        window.open(settings.windowURL, settings.windowName, windowFeatures+',left=' + centeredX +',top=' + centeredY).focus();
                    }else if(settings.centerScreen){
                        centeredY = (screen.height - settings.height)/2;
                        centeredX = (screen.width - settings.width)/2;
                        window.open(settings.windowURL, settings.windowName, windowFeatures+',left=' + centeredX +',top=' + centeredY).focus();
                    }else{
                        window.open(settings.windowURL, settings.windowName, windowFeatures+',left=' + settings.left +',top=' + settings.top).focus();	
                    }
                    return false;
                });
                
    });
    
    
