$(document).ready(function(){
      
      playlistPopup();
      
      var local = '';
      if (local == 'local') {
	    var prefix = '/wordpress/';
      } else {
	    var prefix = '/';
      }
      
      var playItem = 0;

      var myPlayList = [
	    {author:"Juviley",name:"And Then We Spiral",time:"4:21",mp3:"http://www.box.net/shared/static/ynyj2ivj3g.mp3",ogg:"http://www.box.net/shared/static/3po76na2vh.ogg"},
	    {author:"Juviley",name:"Quiet Drifting",time:"5:16",mp3:"http://www.box.net/shared/static/e0va6euo91.mp3",ogg:"http://www.box.net/shared/static/rcfxthmi1n.ogg"},
	    {author:"Juviley",name:"All We Ever Wanted Was to Lie",time:"4:17",mp3:"http://www.box.net/shared/static/oe7mayhuqc.mp3",ogg:"http://www.box.net/shared/static/b9774304dr.ogg"},
	    {author:"Juviley",name:"Our Choices Rhyme",time:"3:46",mp3:"http://www.box.net/shared/static/svzr1p35eo.mp3",ogg:"http://www.box.net/shared/static/s0qvomik13.ogg"},
	    {author:"Juviley",name:"How Peaceful You Were",time:"5:16",mp3:"http://www.box.net/shared/static/xa3nx2iqtl.mp3",ogg:"http://www.box.net/shared/static/zc635msqtb.ogg"},
	    {author:"Juviley",name:"Marble Hills",time:"2:09",mp3:"http://www.box.net/shared/static/hcyn884y42.mp3",ogg:"http://www.box.net/shared/static/fpbnkfmiht.ogg"},
	    {author:"Juviley",name:"Be Patient",time:"5:00",mp3:"http://www.box.net/shared/static/1xlmnaohzt.mp3",ogg:"http://www.box.net/shared/static/hahvrlpq2v.ogg"},
	    {author:"Juviley",name:"Stay Near",time:"3:43",mp3:"http://www.box.net/shared/static/tpghjjdgdg.mp3",ogg:"http://www.box.net/shared/static/uxdkutncm1.ogg"},
	    {author:"Juviley",name:"My Hands Were Gray",time:"2:46",mp3:"http://www.box.net/shared/static/9n8ezrbio1.mp3",ogg:"http://www.box.net/shared/static/gy1ln33d3e.ogg"},
      ];      
      
      $("#jquery_jplayer").jPlayer({
	    ready: function() {
		  displayPlayList();
		  playListInit(false); // Parameter is a boolean for autoplay.
		  displayCurrent();
		  lyricsControl();
	    },
	    swfPath: "/public/js",
	    oggSupport: true
      })
      .jPlayerId("play", "player_play")
      .jPlayerId("pause", "player_pause")
      .jPlayerId("stop", "player_stop")
      .jPlayerId("loadBar", "player_progress_load_bar")
      .jPlayerId("playBar", "player_progress_play_bar")
      .jPlayerId("volumeMin", "player_volume_min")
      .jPlayerId("volumeMax", "player_volume_max")
      .jPlayerId("volumeBar", "player_volume_bar")
      .jPlayerId("volumeBarValue", "player_volume_bar_value")
      .onProgressChange( function(loadPercent, playedPercentRelative, playedPercentAbsolute, playedTime, totalTime) {
	    var myPlayedTime = new Date(playedTime);
	    var ptMin = (myPlayedTime.getUTCMinutes() < 10) ? "0" + myPlayedTime.getUTCMinutes() : myPlayedTime.getUTCMinutes();
	    var ptSec = (myPlayedTime.getUTCSeconds() < 10) ? "0" + myPlayedTime.getUTCSeconds() : myPlayedTime.getUTCSeconds();
	    $("#play_time").text(ptMin+":"+ptSec);

	    var myTotalTime = new Date(totalTime);
	    var ttMin = (myTotalTime.getUTCMinutes() < 10) ? "0" + myTotalTime.getUTCMinutes() : myTotalTime.getUTCMinutes();
	    var ttSec = (myTotalTime.getUTCSeconds() < 10) ? "0" + myTotalTime.getUTCSeconds() : myTotalTime.getUTCSeconds();
	    $("#total_time").text(ttMin+":"+ttSec);
      })
      .onSoundComplete( function() {
	    playListNext();
      });

      $("#ctrl_prev").click( function() {
	    playListPrev();
	    return false;
      });

      $("#ctrl_next").click( function() {
	    playListNext();
	    return false;
      });
      
      $("#ctrl_prev2").click( function() {
	    playListPrev();
	    return false;
      });

      $("#ctrl_next2").click( function() {
	    playListNext();
	    return false;
      });
      
      $("#jquery_jplayer").volume( 100 );
      

      function lyricsControl() {
	    for (i=0; i < myPlayList.length; i++) {
		  $("a#song_"+i).click( function() {
			var idLength = $(this).attr('id').length;
			var suffix = $(this).attr('id').substring(idLength-1);

			if ($("div#lyric_"+suffix).is(':visible') ) {
			      $("div#lyrics_div").hide();
			} else {
			      $("div#lyrics_div").show();
			}
			$("div.lyrics").hide();
			$("div#lyric_"+suffix).show();
		  });
	    }
		  
	    $('a#lyricsClose').click( function() {
	  	$("#lyrics_div").fadeOut('fast');
	    }); 
      }


      function playlistPopup() {
	    $('#playlist_div').hide();
	    $('#playlistTopBorder').hide();
	    $('#playlistControl').click(function(){
		  $('#playlistTopBorder').toggle();
		  $('#playlist_div').toggle();
		  $('#playlistControl').text($(this).text() == '+' ? '-' : '+');
	    });
	    $('#playlistClose').click(function(){
		  $('div#lyrics_div').fadeOut('fast');
          $('#playlist_div').fadeOut('fast');
		  $('#playlistControl').text('+');
	    });
      }
      
      function displayCurrent() {
	    $("#title").html("<p>"+myPlayList[playItem].name+"</p>");
      }
      
      function displayPlayList() {
	    for (i=0; i < myPlayList.length; i++) {
		  var songNameId = myPlayList[i].name.replace(/ /g, '');
		  $("#playlist_list ul").append("<li id='play_item_"+i+"'><span id='playlist_item_"+i+"'>"+ myPlayList[i].name + "</span><a id='song_" + i + "'>Lyrics</a> <span class='delim'> // </span>" + "<span class='songTime'>" + myPlayList[i].time + "</span>" + "</li>");
		  $("#playlist_item_"+i).data( "index", i ).hover(
			function() {
			      if (playItem != $(this).data("index")) {
				    $(this).addClass("playlist_hover");
			      }
			},
			function() {
			      $(this).removeClass("playlist_hover");
			}
		  ).click( function() {
			var index = $(this).data("index");
			if (playItem != index) {
			      playListChange( index );
			}
		  });
	    }
      }

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

      function playListConfig( index ) {
	    $("#playlist_item_"+playItem).removeClass("playlist_current");
	    $("#playlist_item_"+index).addClass("playlist_current");
	    $("#play_item_"+playItem).removeClass("current");
	    $("#play_item_"+index).addClass("current");
	    playItem = index;
	    $("#jquery_jplayer").setFile(myPlayList[playItem].mp3, myPlayList[playItem].ogg);
      }

      function playListChange( index ) {
	    playListConfig( index );
	    displayCurrent()
	    $("#jquery_jplayer").play();
	    if ($("div#lyrics_div").is(':visible') ) {
		  $("div.lyrics").hide();
		  $("div#lyric_"+index).show();
	    }
      }

      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 );
      }
});
