/*******************************************************************************
 * Functions for dealing with the media player.
 *
 * Prerequisites:
 *  swfobject.js (at http://www.chorderator.com/javascript/swfobject.js)
 *  util.js (at http://www.chorderator.com/javascript/util.js)
 *  Define chorderatorBase
 *  Element with id flashPlayer.
 ******************************************************************************/

function getChorderatorBase()
{
  if (typeof(chorderatorBase) != 'undefined') {
    cb = chorderatorBase;
  } else {
    cb = 'http://www.chorderator.com';
  }
  if (!cb || cb.indexOf('localhost') != -1) {
    cb = 'http://test.eddieboston.com';
  }
  return cb;
}

function loadPlayerAdvanced(id, file, repeat, div, w, h)
{
    if (!h) h = 0;
    if (!w) w = 320;
    h = h + "";
    w = w + "";
    var cb = getChorderatorBase();

    var so = new SWFObject(cb + '/mediaplayer.swf', id, w, h, '8');
    so.addParam('allowscriptaccess','always');
    so.addParam('allowfullscreen','false');
    so.addVariable('height',h);
    so.addVariable('width',w);
    if (file) {
	so.addVariable('file', file);
    }
    so.addVariable('javascriptid', id);
    so.addVariable('searchbar','false');
    so.addVariable('showicons','false');
    so.addVariable('showdigits','false');
    so.addVariable('usefullscreen','false');
    so.addVariable('repeat',repeat);
    so.addVariable('enablejs','true');
    so.write(div);
}
  

function loadPlayer(width, height)
{
    if (!width) width = 320;
    if (!height) height = 0;
    loadPlayerAdvanced('mpl', null, false, 'flashPlayer', width, height);
}

// This is a javascript handler for the player and is always needed.
function thisMovie(movieName) {
  if(navigator.appName.indexOf("Microsoft") != -1) {
    return window[movieName];
  } else {
    return document[movieName];
  }
}

function playInterval(note1, note2, harmonic)
{
  cb = getChorderatorBase();
  if (note2 == null) {
    note2 = note1 + 27;
    note1 = 27;
  }
  if (typeof(note1) == 'number') {
    note1 = NoteNumToName(note1, '#', true);
    note2 = NoteNumToName(note2, '#', true);
  }
  var mp3File = (note1 + '-' + note2).replace(/#/g, 's');
  if (harmonic) {
    mp3File = cb + '/cgi-bin/getChord.py?shift=0&fileName=' + escape(mp3File) + '.mp3';
  } else {
    mp3File = cb + '/cgi-bin/getChord.py?interval=true&fileName=' + escape(mp3File) + '.mp3';
  }
  obj = {file:mp3File};
  thisMovie("mpl").loadFile(obj);
  thisMovie("mpl").sendEvent('prev', null);
}

function playAgain()
{
  thisMovie("mpl").sendEvent('prev', null);
}

function playMp3(mp3File, shift)
{
  if (shift == null) {
    shift = 0.1;
  }
  cb = getChorderatorBase();
  mp3File = mp3File.replace(/#/g, 's')
  mp3File = cb + '/cgi-bin/getChord.py?shift=' + shift + '&fileName=' + escape(mp3File);
  playUrl(mp3File);
}

function playUrl(url)
{
  thisMovie("mpl").loadFile({file:url});
  thisMovie("mpl").sendEvent('prev', null);
}
