HideMP3 =
{
  playimg: null,
  go: function()
  {
    // get all the img tags
    var all = document.getElementsByTagName( "img" );

    for ( var i = 0, o ; o = all[i] ; i++ )
    {
      if ( o.nextSibling == null )
        continue;
        
      // only process img tags where the next tag is link to
      // an mp3 file
      if ( o.nextSibling.href && o.nextSibling.href.match( /\.mp3$/i ) )
      {
        // get the current location (minus filename)
        var location = window.location.href.substr( 0,
          window.location.href.lastIndexOf( '/' ) + 1 );

        // get the sub directory
        var file = o.nextSibling.href;//.substr( location.length );

        // update the img onclick event function
        o.onclick = HideMP3.playFile( o, file );
      }
    }
  },
  play: function( img, url )
  {
    if ( HideMP3.playimg == img )
    {
      // the user clicked on the stop button (i.e. mp3 is already playing)
      // so call the toggle function to stop it
      Delicious.Mp3.toggle( img, url );

      // now there is no mp3 file playing to set playimg to null
      HideMP3.playimg = null;

      return;
    }

    // store the currently playing mp3
    HideMP3.playimg = img;

    if ( window.XMLHttpRequest )
    {
      // code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp = new XMLHttpRequest();
    }
    else if ( window.ActiveXObject )
    {
      // code for IE6, IE5
      xmlhttp = new ActiveXObject( "Microsoft.XMLHTTP" );
    }
    else
    {
      // AJAX is not supported
      xmlhttp = null;
    }

    if ( xmlhttp != null )
    {
      xmlhttp.onreadystatechange = HideMP3.playResonse;
      xmlhttp.open( "GET", "http://www.everydayjones.com/get_mp3.php?file=" + url + "&uid=" + Math.floor(Math.random()*10000), true );
      xmlhttp.setRequestHeader( "X-Requested-With", "XMLHttpRequest" );
      xmlhttp.send( null );
    }
  },
  playFile: function( img, url )
  {
    return function()
    {
      HideMP3.play( img, url );
    }
  },
  playResonse: function()
  {
    if ( this.readyState == 4 && this.responseText != "" ) // done
    {
      // the AJAX call was successful and returned
      // the file so attempt to play it
      Delicious.Mp3.toggle( HideMP3.playimg, this.responseText );
    }
  }
}

HideMP3.addLoadEvent = function(f)
{
  var old = window.onload;

  if ( typeof old != 'function' )
  {
    window.onload = f;
  }
  else
  {
    window.onload = function()
    {
      old();
      f();
    }
  }
}

HideMP3.addLoadEvent( HideMP3.go );