//////////////////////////////////////////////////////////////////////////////////////////////////
// Global variable
//////////////////////////////////////////////////////////////////////////////////////////////////
var audioElts;
 
//////////////////////////////////////////////////////////////////////////////////////////////////
//Displays an error
//////////////////////////////////////////////////////////////////////////////////////////////////
function error( err )
{
   alert( err );
}
 
//////////////////////////////////////////////////////////////////////////////////////////////////
//Ask the user to update the browser
//////////////////////////////////////////////////////////////////////////////////////////////////
function updatePlz()
{
   alert( "Update to a new HTML5 capable browser to play audio\n\nTry chrome at http://www.google.com/chrome/" );
}
 
//////////////////////////////////////////////////////////////////////////////////////////////////
//Triggered when an audio element starts loading it's content
//////////////////////////////////////////////////////////////////////////////////////////////////
function loadstart(evt)
{
   var duree = $(".duree").get(evt.target.index);
   duree.innerHTML = "Loading";
}
 
//////////////////////////////////////////////////////////////////////////////////////////////////
//Triggered when an audio element is ready to play
//////////////////////////////////////////////////////////////////////////////////////////////////
function loadeddata(evt)
{

}
 
//////////////////////////////////////////////////////////////////////////////////////////////////
//Triggered when the current time member of an audio element is updated
//////////////////////////////////////////////////////////////////////////////////////////////////
function timeupdate(evt)
{
   var dureeSpan = $(".duree").get(evt.target.index);
   var timeInSec = 0;
 
   if( evt.target.paused )
   {
      timeInSec = parseInt(evt.target.duration);
   }
   else
   {
      timeInSec = parseInt(evt.target.currentTime);
   }
	
   var timeInMin = parseInt(timeInSec/60);
   timeInSec = timeInSec % 60;

   var time = "" + timeInMin + ":" + (timeInSec<10? "0":"") + timeInSec;

   dureeSpan.innerHTML = time; 
}
 
//////////////////////////////////////////////////////////////////////////////////////////////////
//Triggered when a play/stop button is clicked
//////////////////////////////////////////////////////////////////////////////////////////////////
function audioAction( controlImg )
{
   //Index
   var index = $(".play").index(controlImg);

   //Gets the associated audio element
   if( index > audioElts.length )
   {
      error( "Wrong index:"+index+" length:"+ audioElt.length );
      return;
   }

   var audioElt = audioElts[index];	
   if( !audioElt )
   {
      error( "Cannot get associated audio element:" );
      return;
   }
	
   if( audioElt.paused )
   {
      //Start the audio file if paused
      audioElt.play();
      //Change the button img to stop
      $(controlImg).attr("src", "Img/BtStop.jpg");	
   }
   else
   {
      //Stops the audio
      audioElt.pause();
      //Go back to the beginning of the file
      audioElt.currentTime = 0;
      //Change the button img to start
      $(controlImg).attr("src", "Img/BtPlay.jpg");
   }
}


//////////////////////////////////////////////////////////////////////////////////////////////////
//Load an audio file regarding the index
//////////////////////////////////////////////////////////////////////////////////////////////////
function createAudioElt( name )
{
   var audioElt = document.createElement("audio");

   //TODO check error management
   //$(audioElt).error( function(){ error("Audio elt error" ); });

   //Is the element created successfully ?
   if( !(audioElt instanceof HTMLAudioElement)  )
   {
      error( "Please update to an HTML5 compatible browser" );
   }
   //Can we load an ogg file ?
   else if( audioElt.canPlayType( 'audio/ogg; codecs=vorbis' ) != "" )
   {
      audioElt.src = name + ".ogg";
   }

   //Can we load a mp3 file ?
   else if( audioElt.canPlayType( "audio/mpeg" ) != "" )
   {
      audioElt.src =  name + ".mp3";
   }


   //Add some event handler if needed
   if( audioElt != null )
   {
      audioElt.preload = "auto";
      audioElt.addEventListener("timeupdate", timeupdate, true);
      audioElt.addEventListener("loadstart", loadstart, true);
      audioElt.addEventListener("loadstart", loadeddata, true);
   }

	
   return audioElt;
	
}
 
//////////////////////////////////////////////////////////////////////////////////////////////////
//Document initialization
//////////////////////////////////////////////////////////////////////////////////////////////////
$(document).ready(function() 
{
   //First thing check if the browser is audio tag ready
   if( typeof(HTMLAudioElement) == "undefined" )
   {
      $(".play").click(function(){
         updatePlz(this);
      });
   }
   else
   {
      var audioFiles = new Array();
      var i = 0;

      //Creates audio file list
      audioFiles[i++] = "Audio/01_Rock_The_Harem";
      audioFiles[i++] = "Audio/02_Holly_Mushroom";
      audioFiles[i++] = "Audio/03_Deep";
      audioFiles[i++] = "Audio/04_Spirits";
      audioFiles[i++] = "Audio/05_13Th_Warrior";
      audioFiles[i++] = "Audio/06_The_Blacksmith";
      audioFiles[i++] = "Audio/07_Watery_Grave";
      audioFiles[i++] = "Audio/08_Birth_Of_A_Succubus";
      audioFiles[i++] = "Audio/09_Sailing_South";
      audioFiles[i++] = "Audio/10_Happy_Drunk_Friend";
      audioFiles[i++] = "Audio/11_13Th_Warrior_With_Reneys_orchestra_Bonus";


      //Creates the audio elements
      audioElts = new Array();
      for( i=0; i<audioFiles.length; i++ )
      {	
         audioElts[i] = createAudioElt( audioFiles[i] );
         if( audioElts[i] != null )
         {
            audioElts[i].index = i;
         }
      }

      $(".play").click(function(){
         audioAction(this);
      });
   }
});



