// nh_player.js
// Interface to SoundManager2
//
// Copyright (c) 2007 by Peter Liebert - www.nipperhead.com
//

var PlayButton = "url('graphics/sp_stopped.gif')";
var StopButton = "url('graphics/sp_playing.gif')";
var smLoaded=false;
var smPlaying=false;
var smGotID3=false;
var oldStatLine="";
var newStatLine="";

var mySongs = new Object();

soundManager.url = 'libraries/soundmanager/soundmanager2.swf';
//soundManager.debugMode = true; 
soundManager.onload = function() { createSMSound(); smLoaded=true; }

function mySong(sID,URL,linkText,toolTip)
{
   this.sID=sID;
   this.URL=URL;
   this.linkText=linkText;
   this.toolTip=toolTip;
   this.songName="";
   this.artist="";
   this.year="";
   this.album="";
   this.track="";
   this.comment="";
   this.titleText=linkText;
   this.loaded=false;
   this.isPlaying = false;
   
   var sl = "<a href='javascript:void(0);' onClick=\"toggleSound('"+this.sID+"');return false;\" title='"+this.toolTip;
        sl += "' onMouseover=\"javascript:window.status=' ';return true;\" ><span id='"+this.sID+"' class='mp3'>"+this.linkText+"</span></a>";
   document.write (sl);
   setImage(this.sID,PlayButton);

}

// Soundmanager has loaded, initialize sounds
function xxxxxxcreateSMSound() {
   for (var soundItem in mySongs) {
      soundManager.createSound({
         id:mySongs[soundItem].sID,
         url:mySongs[soundItem].URL,
         whileplaying:whilePlayingFunc,
         onfinish:onFinishFunc,
         onid3:onID3Func
      });
    var mySMSound = soundManager.getSoundById(mySongs[soundItem].sID); 
    alert(mySMSound.sID+", "+mySMSound.url+", "+typeof(mySMSound.whileplaying));
   }
}

// Create the object to hold the sound data
function soundLink(soundID, URL,linkText, toolTip) {
   mySongs[soundID] = new mySong(soundID,URL,linkText,toolTip);
//   alert(soundID +", "+URL+", "+linkText+", "+toolTip);
}

function setImage(divID,img) {
   document.getElementById(divID).style.backgroundImage = img;
}

function setVisibility(divID,state) {
   document.getElementById(divID).style.visibility = state;
}

function setTitle(divID,str) {
   document.getElementById(divID).title = str;
}

function setContents(divID,str) {
   document.getElementById(divID).innerHTML = str;
}

function getContents(divID) {
   return document.getElementById(divID).innerHTML;
}

function isNotEmpty(str) {
    return !(typeof str == 'undefined' || str == null || str == 'null' || (typeof str == 'string' && str.toLowerCase() == 'n/a' || str.toLowerCase == 'undefined'));
}

function onID3Func() {
   mySongs[this.sID].songName = this.id3.songname;
   mySongs[this.sID].artist = this.id3.artist;
   mySongs[this.sID].year = this.id3.year;
   mySongs[this.sID].album = this.id3.album;
   mySongs[this.sID].track = this.id3.track;
   mySongs[this.sID].comment = this.id3.comment;


//var prop = null;
//var data = '';
//for (prop in this.id3) {
//  data += prop+': '+this.id3[prop]+'\n';
//}
//alert(data);
   
   if (isNotEmpty(mySongs[this.sID].songName)) {
      var titleTxt = '<b>&quot;' + mySongs[this.sID].songName + '&quot;</b>';
      if (isNotEmpty(mySongs[this.sID].artist)) {titleTxt += ' By <i>' + mySongs[this.sID].artist + '</i>';}
      if ((isNotEmpty(mySongs[this.sID].album)) || (!isNaN(mySongs[this.sID].track)) || (isNotEmpty(mySongs[this.sID].year))) {
         titleTxt += "&nbsp;&nbsp;&nbsp;&nbsp;(";
         if (isNotEmpty(mySongs[this.sID].album)) {titleTxt += 'Source:<b>' + mySongs[this.sID].album + "</b>";}
         if (!isNaN(mySongs[this.sID].track)) {titleTxt += ' #<b>' + parseInt(mySongs[this.sID].track) + "</b>";}
         if (isNotEmpty(mySongs[this.sID].year)) {titleTxt += ', Year:<b>' + mySongs[this.sID].year + "</b>";}
         titleTxt += ")";
      }
      mySongs[this.sID].titleText=titleTxt;
   }
}

function calcTime(objThis) {
   var dur = parseInt((mySongs[objThis.sID].loaded?objThis.duration:objThis.durationEstimate)/1000);
   var pos = parseInt(objThis.position/1000);
   var totalsecs = dur - pos; 
   var mins = Math.floor(totalsecs / 60); 
   var secs = totalsecs - (mins*60);
   var mins0 = (mins<=9)?'0':'';  
   var secs0 = (secs<=9)?'0':'';
   return mins0+mins+":"+secs0+secs;
}   

function whilePlayingFunc() {
//var estr = "." + getContents("soundmanager-debug");
//setContents("soundmanager-debug",estr);
   newStatLine = 'Playing '+mySongs[this.sID].titleText+'&nbsp;&nbsp;<b>['+calcTime(this)+']</b> ';
   if (newStatLine!=oldStatLine) {
      showStatus(newStatLine);
      oldStatLine = newStatLine;
   }
}

function onLoadedFunc() {
   mySongs[this.sID].loaded=true;
}

function onFinishFunc() {
   setImage(this.sID,PlayButton);
   setTitle(this.sID,mySongs[this.sID].toolTip);
   clearStatus(); 
   mySongs[this.sID].isPlaying = false;
}

//function initSound(soundID, soundURL) {
//   var initSoundStr = "<script type='text/javascript'>soundManager.onload = function() {\
//         soundManager.createSound({id:'"+soundID+"',url:'"+soundURL+"',whileplaying:whilePlayingFunc,\
//         onfinish:onFinishFunc,\
//         onid3:onID3Func\
//      }); smLoaded=true;}</script>";
//   document.write(initSoundStr);
//}

// Soundmanager has loaded, initialize sounds
function createSMSound() {
   for (var soundItem in mySongs) {
      soundManager.createSound({
         id:mySongs[soundItem].sID,
         url:mySongs[soundItem].URL,
         whileplaying:whilePlayingFunc,
         onfinish:onFinishFunc,
         onid3:onID3Func,
         autoLoad: false
      });
//   alert('Created: '+mySongs[soundItem].sID+', '+mySongs[soundItem].URL);
   }
}


function startPlaying(soundID) {
   for (var soundItem in mySongs) {
      if (mySongs[soundItem].isPlaying) {
         stopPlaying(mySongs[soundItem].sID);
      }
   }
   soundManager.play(soundID);
   setImage(soundID,StopButton);
   setTitle(soundID,'Click to stop');
//   setVisibility("statusBar","visible");
   showStatus(mySongs[soundID].titleText);
   mySongs[soundID].isPlaying = true;
}

function stopPlaying(soundID) {
   soundManager.stop(soundID);
   setImage(soundID,PlayButton);
   setTitle(soundID,mySongs[soundID].toolTip);
//   setVisibility("statusBar","hidden");
   clearStatus();
   mySongs[soundID].isPlaying = false;
}

// Toggle the Stopped/Playing state of the sound
function toggleSound(soundID) {
   if (!smLoaded) return false;

   if (mySongs[soundID].isPlaying) {
      stopPlaying(soundID);
   } else {
      startPlaying(soundID);
   }   
}

// Write a status message
function showStatus(statusMsg) {
   setContents("statusBar",statusMsg);
   setVisibility("statusBar","visible");
//   return true ;
}

function clearStatus() {
   setVisibility("statusBar","hidden");
   setContents("statusBar"," ");
//   return true ;
}

