/**
 * Class for embedding audio.
 * 
 * Copying: Copying of this code for non-commercial use is permitted under the following conditions:
 *   - Give credit to the original author listed in this header.
 *   - For any reuse/distribution, make clear the same terms.
 *   
 * @author Steve Knaus
 */ 

AudioStudio.CONFIG =
{
   playList :
   {
      oneLove : 
      {
         fileName : 'one-love.mp3',
         title : 'One Love'
      },    
      why : 
      {
         fileName : 'why.mp3',
         title : 'Why?'
      },
      myLady :
      {
         fileName : 'my-lady.mp3',
         title : 'My Lady'
      }
   },

   playerHtmlFileName : 'audio-frame.htm',
   playerFrameName : 'audioFrame',
   trackIdArgName : 'id',
   playerTypeArgName : 'playerType',
   playerTypes :
   {
      flashPlayer :
      {
        displayName : 'Flash',
        argName : 'flash'
      },
      quickTime :
      {
         displayName : 'QuickTime',
         argName : 'qt'
      },
      mediaPlayer :
      {
         displayName : 'Media Player',
         argName : 'mp'
      }
   }
}


function AudioStudio() {
   this.m_config = AudioStudio.CONFIG;
   this.m_playList = this.m_config.playList;
}


AudioStudio.prototype.play = 
function( playerType, trackId ) {
   var url = ''
      + this.m_config.playerHtmlFileName
      + '?'
      + this.m_config.playerTypeArgName
      + '='
      + playerType
      + '&'
      + this.m_config.trackIdArgName
      + '='
      + trackId
      ;

   self.frames[ this.m_config.playerFrameName ].location = url;
}

AudioStudio.prototype.embedPlayer =
function() {
   var site = Site.get();

   this._embedPlayer( site.getParameter( this.m_config.playerTypeArgName ), site.getParameter( this.m_config.trackIdArgName ) );
}

AudioStudio.prototype._embedPlayer =
function( playerType, trackId ) {
   var html = '';   
   if ( this.m_config.playerTypes.quickTime.argName == playerType ) {
      var fileName = this.m_playList[ trackId ].fileName;
      html += ''
         + '<object classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" '
         + '        width="240" '
         + '        height="16" '
         + '        codebase="http://www.apple.com/qtactivex/qtplugin.cab#version=6,0,2,0">'
         + '    <param name="controller" value="true">'
         + '    <param name="type" value="application/sdp">'
         + '    <param name="autoplay" value="true">'
         + '    <param name="src" value="' + fileName + '">'
         + '    <param name="pluginspage" value="http://www.apple.com/quicktime/download/indext.html">'
         + '    <embed width="240" '
         + '           height="16" '
         + '           autoplay="true"'
         + '           controller="true" '
         + '           src="' + fileName + '" '
         + '           type="application/sdp" '
         + '           bgcolor="#000000" '
         + '           border="0" '
         + '           pluginspage="http://www.apple.com/quicktime/download/indext.html">'
         + '    </embed>'
         + '</object>';
   } else if ( this.m_config.playerTypes.mediaPlayer.argName == playerType ) {
      var fileName = this.m_playList[ trackId ].fileName;
      html += ''
         + '<object '
         + '   id="MediaPlayer1" '
         + '   width="240" '
         + '   height="70" '
         + '   classid="CLSID:22D6F312-B0F6-11D0-94AB-0080C74C7E95"'
         + '   codebase="http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=6,4,5,715"'
         + '   type="application/x-oleobject"'
         + '>'
         + '   <param name="autostart" value="true">'
         + '   <param name="filename" value="' + fileName + '">'
         + '   <param name="showcontrols" value="true">'
         + '   <param name="ShowStatusBar" value="true">'
         + '   <embed '
         + '      type="application/x-mplayer2" '
         + '      pluginspage="http://www.microsoft.com/Windows/MediaPlayer/"'
         + '      src="' + fileName + '"'
         + '      width="240"'
         + '      height="70"'
         + '      autostart="1"'
         + '      showcontrols="1"'
         + '      showstatusbar="1"'
         + '   >'
         + '   </embed>'
         + '</object>  '
         ;
   } else if ( this.m_config.playerTypes.flashPlayer.argName == playerType ) {
      var fileName = this.m_playList[ trackId ].fileName;
      html += ''
         + '<embed type="application/x-shockwave-flash"' 
         + '       wmode="window"' 
         + '       src="http://www.google.com/reader/ui/3523697345-audio-player.swf"' 
         + '       flashvars="audioUrl=' + fileName + '&autoPlay=true"'
         + '       quality="best"' 
         + '       allowscriptaccess="never"'
         + '       height="27" width="320"'
         + '       pluginspage="http://get.adobe.com/flashplayer/">'
         + '</embed>'
         ;
   } else {
      html = 'Press Play button to hear a title.';
   }

   self.document.write( html );
}

AudioStudio.prototype.draw = 
function() {
   this._writeIframe();
   this._writePlayList();
}

AudioStudio.prototype._writeIframe =
function() {
   var html = '';
   html += ''
      + '<iframe '
      + '   src="' + this.m_config.playerHtmlFileName + '"' 
      + '   name="' + this.m_config.playerFrameName + '"' 
      + '   id="' + this.m_config.playerFrameName + '"' 
      + '   frameborder="0"'
      + '>'
      + '   Your browser does not support frames.'
      + '</iframe>'
      ;
   self.document.write( html );
}


AudioStudio.prototype._writePlayList =
function() {
   var html = '';

   html += ''
      + '<table border="1">'
      + '   <tr>'
      + '      <th id="title">Title</th>'
      ;

   for ( var playerType in this.m_config.playerTypes ) {
      var playerTypeEntry = this.m_config.playerTypes[ playerType ];
      html += '<th>' + playerTypeEntry.displayName  + '</th>';
   }

   html += '</tr>';

   var count = 0;
   for ( var trackId in this.m_config.playList ) {
      var trackEntry = this.m_config.playList[ trackId ];
      var rowClass = ( count % 2 ? 'odd' : 'even' );

      html += ''
         + '   <tr class="' + rowClass + '">'
         + '      <td>' + trackEntry.title + '</td>'
         ;

      for ( var playerType in this.m_config.playerTypes ) {
         var playerTypeEntry = this.m_config.playerTypes[ playerType ];
         html += ''
            + '      <td>'
            + '         <input type="button" value="Play" class="play-button" onclick="audioStudio.play( \'' + playerTypeEntry.argName + '\', \'' + trackId + '\' );" />'
            + '      </td>'
            ;
      }

      html += '</tr>';
      ++count;
   }

   html += '</table>';
   self.document.write( html );
}


