//$(document).ready(function(){
    //playMusic();
//});

LAST_SOUND_PLAY_STATUS = "stopped";
PLAYER_IS_READY = 0;

function playMusic() {
    // Don't run this if there isn't a music player on the page.
    if (('#jquery_jplayer').length > 0) {
            $(".jp-playlist-player").show();

            var playItem = 0;

            var myPlayList = [
                {name:"Oh Christmas Tree",mp3:"http://www.christmasgifts.com/christmasmusic/oh-christmas-tree.mp3"},
                {name:"Dance of the Sugar Plum Fairies",mp3:"http://www.christmasgifts.com/christmasmusic/dance-of-the-sugar-plum-faries.mp3"},
                {name:"Silent Night, Holy Night",mp3:"http://www.christmasgifts.com/christmasmusic/silent-night.mp3"},
                {name:"Oh Little Town Of Bethlehem",mp3:"http://www.christmasgifts.com/christmasmusic/oh-little-town-of-bethlehem.mp3"},
                {name:"Jingle Bells",mp3:"http://www.christmasgifts.com/christmasmusic/jingle-bells-slow-mix.mp3"},
                //{name:"Jingle Bells",mp3:"http://www.christmasgifts.com/christmasmusic/jingle-bells.mp3"},
                {name:"Deck the Halls",mp3:"http://www.christmasgifts.com/christmasmusic/deck-the-halls.mp3"},
                {name:"It Came Upon A Midnight Clear",mp3:"http://www.christmasgifts.com/christmasmusic/it-came-upon-a-midnight-clear.mp3"},
                {name:"Oh Holy Night",mp3:"http://www.christmasgifts.com/christmasmusic/oh-holy-night.mp3"},
                {name:"What Child Is This?",mp3:"http://www.christmasgifts.com/christmasmusic/what-child-is-this.mp3"}
            ];

            // Local copy of jQuery selectors, for performance.
            var jpPlayTime = $("#jplayer_play_time");
            var jpTotalTime = $("#jplayer_total_time");

            $("#jquery_jplayer").jPlayer({
                swfPath: ROOT_JS_PATH,
                ready: function() {
                    displayPlayList();
                    var initialStatus = $.cookie('dsPlayMusic'); 
                    var autoplay = (initialStatus!= "stopped");
                    playListInit(autoplay); // Parameter is a boolean for autoplay.
                    demoInstanceInfo(this.element, $("#demo_info")); 
                    PLAYER_IS_READY = 1;
                }
            })
            .jPlayer("onProgressChange", function(loadPercent, playedPercentRelative, playedPercentAbsolute, playedTime, totalTime) {
                //jpPlayTime.text($.jPlayer.convertTime(playedTime));
                //jpTotalTime.text($.jPlayer.convertTime(totalTime));
                if (PLAYER_IS_READY) {
                    var status = (this.element.jPlayer("getData", "diag.isPlaying") ? "playing" : "stopped");
                    if (status != LAST_SOUND_PLAY_STATUS) {
                        LAST_SOUND_PLAY_STATUS = status;
                        $.cookie('dsPlayMusic', status); 
                    }
                }
            })
            .jPlayer("onSoundComplete", function() {
                playListNext();
            });

            $("#jplayer_previous").click( function() {
                playListPrev();
                $(this).blur();
                return false;
            });

            $("#jplayer_next").click( function() {
                playListNext();
                $(this).blur();
                return false;
            });

    }
    else {
        alert ("No music on this page!");
    }

	function displayPlayList() {
		$("#jplayer_playlist ul").empty();
		for (i=0; i < myPlayList.length; i++) {
			var listItem = (i == myPlayList.length-1) ? "<li style=\"display:none\" id=\"playItem" + i + "\" class='jplayer_playlist_item_last'>" : "<li id=\"playItem" + i + "\" style=\"display:none\">";
			listItem += "<a href='#' id='jplayer_playlist_item_"+i+"' tabindex='1'>"+ myPlayList[i].name +"</a></li>";
			$("#jplayer_playlist ul").append(listItem);
			$("#jplayer_playlist_item_"+i).data( "index", i ).click( function() {
				var index = $(this).data("index");
				if (playItem != index) {
					playListChange( index );
				} else {
					$("#jquery_jplayer").jPlayer("play");
				}
				$(this).blur();
				return false;
			});
		}
	}

	function playListInit(autoplay) {
		if(autoplay) {
			playListChange( playItem );
		} else {
			playListConfig( playItem );
		}
	}

	function playListConfig( index ) {
		$("#jplayer_playlist_item_"+playItem).removeClass("jplayer_playlist_current").parent().removeClass("jplayer_playlist_current");
		$("#playItem"+playItem).hide();
		$("#jplayer_playlist_item_"+index).addClass("jplayer_playlist_current").parent().addClass("jplayer_playlist_current");
		$("#playItem"+index).show();

        $("#nowPlaying").html(myPlayList[index].name);

		playItem = index;
		$("#jquery_jplayer").jPlayer("setFile", myPlayList[playItem].mp3);
	}

	function playListChange( index ) {
		playListConfig( index );
		$("#jquery_jplayer").jPlayer("play");
	}

	function playListNext() {
		var index = (playItem+1 < myPlayList.length) ? playItem+1 : 0;
		playListChange( index );
	}

	function playListPrev() {
		var index = (playItem-1 >= 0) ? playItem-1 : myPlayList.length-1;
		playListChange( index );
	}

    function demoStatusInfo(myPlayer, myInfo) {
        var jPlayerStatus = "<p>jPlayer is ";
        jPlayerStatus += (myPlayer.jPlayer("getData", "diag.isPlaying") ? "playing" : "stopped");
        jPlayerStatus += " at time: " + Math.floor(myPlayer.jPlayer("getData", "diag.playedTime")) + "ms.";
        jPlayerStatus += " (tt: " + Math.floor(myPlayer.jPlayer("getData", "diag.totalTime")) + "ms";
        jPlayerStatus += ", lp: " + Math.floor(myPlayer.jPlayer("getData", "diag.loadPercent")) + "%";
        jPlayerStatus += ", ppr: " + Math.floor(myPlayer.jPlayer("getData", "diag.playedPercentRelative")) + "%";
        jPlayerStatus += ", ppa: " + Math.floor(myPlayer.jPlayer("getData", "diag.playedPercentAbsolute")) + "%)</p>"
        myInfo.html(jPlayerStatus);
    } 

    function demoInstanceInfo(myPlayer, myInfo) {
        var jPlayerInfo = "<p>This jPlayer instance is running in your browser using ";
        if(myPlayer.jPlayer("getData", "usingFlash")) {
            jPlayerInfo += "<strong>Flash</strong> with ";
        } else {
            jPlayerInfo += "<strong>HTML5</strong> with ";
        }
    }
}; 

