// Morse tutor functions to send morse
// using Reinier Zwitserloot's javascript audio code:
// see http://www.zwitserloot.com/files/soundkit/soundcheck.html

//  Copyright 1998-2008 "Nosey" Nick Waterman, Ward Cunningham and Jim Wilson
//  Distributed under the GNU GPL V2 license.
//  See http://noseynick.net/va3nnw/cw/ and http://c2.com/morse
//
//  This file is part of the Nilex Morse Tutor.
//
//  The Nilex Morse Tutor is free software; you can redistribute it
//  and/or modify it under the terms of the GNU General Public License
//  as published by the Free Software Foundation; either version 2 of
//  the License, or (at your option) any later version.
//
//  The Nilex Morse Tutor is distributed in the hope that it will be
//  useful, but WITHOUT ANY WARRANTY; without even the implied warranty
//  of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//  General Public License for more details.
//
//  You should have received a copy of the GNU General Public License along
//  with the Nilex Morse Tutor: http://noseynick.net/va3nnw/cw/license.txt
//  If not, write to the Free Software Foundation, Inc., 59 Temple
//  Place, Suite 330, Boston, MA  02111-1307 USA

function make_url(freq, speed, letter) {
  // return ("http://noseynick.net/va3nnw/cw/cw.cgi/" + freq + "hz/" + speed + "wpm/" + letter + ".mp3");
  return ("cw.cgi/" + freq + "hz/" + speed + "wpm/" + letter + ".mp3");
}

// In this version, we don't actualy need to know the symbols, as
// they're played out of mp3 files. It IS useful to know how long each
/// one lasts (in multiples of a dit) though:

var CodeLen = [
  0 ,  0, 15,  0, 15,  0,  0, 19, // sp ! " # $ % & '
  15, 19,  0, 13, 19, 15, 17, 13, //  ( ) * + , - . /
  19, 17, 15, 13, 11,  9, 11, 13, //  0 1 2 3 4 5 6 7
  15, 17, 17, 17,  0, 13,  0, 15, //  8 9 : ; < = > ?
  17,  5,  9, 11,  7,  1,  9,  9, //  @ A B C D E F G
  7 ,  3, 13,  9,  9,  7,  5, 11, //  H I J K L M N O
  11, 13,  7,  5,  3,  7,  9,  9, //  P Q R S T U V W
  11, 13, 11                      //  X Y Z
];

var playing = null;
var probably_cached = new Object;

window.onload = function() {
  chg_all(); // setup
};

function test_cq() {
  // no special init to do here?
  // unless we're able to pre-cace the mp3s somehow? +++
  send_cw("cq");
}

// 20090926 - Thanks to Arjun Prasad for helping me find the missing
//            comma -- NN
var symsmap = {
  ".": "stop",
  "/": "slash",
  "=": "equal",
  "?": "question",
  ",": "comma",
  "CQ": "cq",
};

function send_cw(letter) {
  sending();
  debug( "send_cw(" + letter + ")" );
  letter = letter.toUpperCase();
  var dits = 0;
  
  if (" " <= letter && letter <= "Z")  {
    // If in CodeLen[] table, see how long it is in dits
    dits = CodeLen[letter.charCodeAt(0) - 32];
  }
  if      (symsmap[letter]) { letter = symsmap[letter]; }
  else if ("A" <= letter && letter <= "Z")  {}
  else if ("0" <= letter && letter <= "9")  {}
  else { debug("Invalid send_cw"); return; }
  
  var url = make_url(freq, speed, letter);
  // Estimate time to play
  // X WPM = X * "PARIS" / min
  // PARIS = 50 dots total (inc spaces), so X WPM = X*50 DPM
  // X*50 dots = 60000 msecs
  // 1 dot = 1200 msecs / X WPM
  // Doesn't actually need to be TOO precise as long as it's "close enough"
  var ettp = (dits * 1200) / speed;
  var plus = 0;
  if (! probably_cached[url]) {
    debug("pad time a bit for loading");
    probably_cached[url] = plus = 1000;
  }
  
  debug("play " + dits + "-dit sound " + url + " for " + ettp + "ms");
  
  // Slight variation on R Zwitserloot's code:
  // 1) <object> is the new <embed> +++ BUT DOESN'T WORK ON OLDER IE
  // 2) AdblockPlus can end up adding one <a> for every <object> or <embed>
  // 3) player var can drop out of scope immediately, no need for player=null
  
  var div = _("player");
  while (div.hasChildNodes()) {
    div.removeChild(div.firstChild);
  }
  // var player = document.createElement("object");
  // player.setAttribute("type", "audio/mpeg");
  // player.setAttribute("data", url);
  var player = document.createElement("embed");
  player.setAttribute("src", url);
  // no need to hide - may even play better in opera?
  // player.setAttribute("hidden", true);
  player.setAttribute("autostart", true);
  div.appendChild(player);
  
  playing = setTimeout( "guess_played("+plus+")", ettp );
}

function guess_played(pad) {
  playing = null;
  if (pad) {
    sending();
    debug("May be un-cached - padding time by " + pad + "ms");
    setTimeout( "done_sending()", pad );
  } else {
    done_sending();
  }
}

function still_sending () {
  return (playing);
}
