$(document).ready(function(){
 var playerState=0;
 jQuery.fn.createSO = function(){
  if(sMready) {
   return soundManager.createSound({
    id: "state"+playerState+"song"+$(this).prevAll("a").length,
    url: $(this).attr("href").replace("/mp3/","/mp3/stream/"),
    whileloading: updateLoad,
    whileplaying: updatePlay,
    onbeforefinish: preBufferNext,
    onfinish: playNext
   });
  } else { return false; }
 }
 jQuery.fn.playerButtonShow = function(){
  if($(this).attr("id")=="btnplay") {
   $("#btnpause").hide(); $(this).show(); setPlaying(false);
  } else if($(this).attr("id")=="btnpause") {
   $("#btnplay").hide(); $(this).show(); setPlaying(true);
  }
 }
 function setPlaying(playing){
  playshown=(document.title.substr(0,2)=='\u25B6 ')?true:false;
  if(playing && !playshown) {
   document.title='\u25B6 '+document.title;
  } else if(!playing && playshown) {
   document.title=document.title.substr(2);
  }
 }
 $("#btnplay").click(function(){
  if(sMready) {
   if(typeof soundObject != "undefined") {
    $("#btnpause").playerButtonShow();
    soundObject.resume();
   } else {
    if($("#playlist a.current").length!=1) { $("#playlist a").first().makeCurrent(); }
    if($("#playlist a.current").length==1) {
     $("#btnpause").playerButtonShow();
     soundObject=$("#playlist a.current").createSO();
     soundObject.play();
     if(typeof nextSoundObject != "undefined") { nextSoundObject.destruct(); delete nextSoundObject; }
    }
   }
  }
 });
 $("#btnpause").click(function(){
  if(sMready && typeof soundObject != "undefined") { soundObject.pause(); }
  $("#btnplay").playerButtonShow();
 });
 $("#btnstop").click(function(){
  if(sMready) {
   soundManager.stopAll();
   if(typeof soundObject != "undefined") { soundObject.destruct(); delete soundObject; }
   if(typeof nextSoundObject != "undefined") { nextSoundObject.destruct(); delete nextSoundObject; }
   $("#progressplay").width(0);
   $("#btnplay").playerButtonShow();
  }
 });
 $("#progress").click(function(e){
  if(sMready && typeof soundObject != "undefined") {
   soundObject.setPosition((e.pageX - $(this).offset().left)*soundObject.durationEstimate/$(this).width());
   updatePlay();
  }
 });
 $("#btndl").click(function(){
  if($("#playlist a.current").length==1) { document.location.href=$("#playlist a.current").attr("href"); }
 });
 $("#playlist").delegate('a','click',function(e){
  e.preventDefault();
  $(this).makeCurrent();
  $("#btnstop").click(); $("#btnplay").click();
  return false;
 });
 function preBufferNext(){
  if(sMready && $("#playlist a.current").hasClass("prebuffernext")) {
   if(typeof nextSoundObject != "undefined") { nextSoundObject.destruct(); delete nextSoundObject; }
   nextSoundObject=$("#playlist a.current").next().createSO();
   nextSoundObject.load();
  }
 }
 function playNext(){
  if(sMready && typeof nextSoundObject != "undefined") {
   soundManager.stopAll();
   nextSoundObject.play();
   if(typeof soundObject != "undefined") { soundObject.destruct(); delete soundObject; }
   soundObject = nextSoundObject;
   delete nextSoundObject;
   $("#playlist a.current").next().makeCurrent();
  } else {
   if($("#playlist a.current").next().length==1) {
    $("#playlist a.current").next().click();
   } else {
    $("#playlist a.current").removeClass("current");
    $("#btnstop").click();
   }
  }
 }
 function updateLoad(){
  if(sMready && typeof soundObject != "undefined") {
   $("#progressload").width($("#progress").width()*(soundObject.bytesLoaded/soundObject.bytesTotal));
  }
 }
 function updatePlay(){
  if(sMready && typeof soundObject != "undefined") {
   $("#progressplay").width($("#progress").width()*(soundObject.position/soundObject.durationEstimate));
  }
 }
 $(window).resize(function(){ updateLoad(); updatePlay(); });
 jQuery.fn.makeCurrent = function(){
  $(this).siblings("a.current").removeClass("current");
  $(this).addClass("current");
 }
 jQuery.fn.playlistInit = function(){
  if(sMready) {
   if(typeof soundObject != "undefined" && !$(this).children("a.current").length && $(this).children("a[href='"+soundObject.url+"']").length) {
    $(this).children("a[href='"+soundObject.url+"']").makeCurrent();
    if(typeof nextSoundObject != "undefined") { nextSoundObject.destruct(); delete nextSoundObject; }
    playerState++;
   } else {
    $("#btnstop").click(); $("#progressload").width(0); $("#btnplay").click();
   }
  } 
 }
});

var sMready=false;
soundManager.url = '/soundmanager2/';
soundManager.flashVersion = 9;
soundManager.useFlashBlock = false;
soundManager.onready(function() {
 sMready=true;
 document.getElementById("player").className='ready';
});

