/*
    mygsucore - a script to show notes on guitar frets (italian version)
    Copyright (C) 2007-2008  Mauro Panigada

    This program 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.

    This program 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 this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

*/



/* TODO: make it object oriented or something like that;
   TODO: the code can be highly made better... :)
   TODO: syntax relaxing !! :D
*/

/* notes in anglonotation:
        c c# d d# e f f# g g# a a# b
*/
note = new Array("do", "do#", "re", "re#", "mi", "fa", "fa#", "sol", "sol#", "la", "la#", "si");
tuning = new Array("mi", "la", "re", "sol", "si", "mi");
reftuning = tuning;

numberOfFret = 20;


function getFretRange()
{
  var fretr, trange, rr, rangel, rangeh;
  fretr = new Array(2);

  trange = document.getElementById("keyrange").value;
  /* trange is something like: 0-20, ie X-Y
  // checks: 1) X less than Y
  //         2) X >= 0, Y <= numberOfFret
  */
  rr = trange.split("-");
  rr[0] = parseInt(rr[0]);
  rr[1] = parseInt(rr[1]);
  if ( typeof(rr[0]) != "number") { rr[0] = 0; }
  if ( typeof(rr[1]) != "number") { rr[1] = numberOfFret; }
  rangel = Math.min(rr[0], rr[1]);
  rangeh = Math.max(rr[0], rr[1]);
  rangeh = Math.min(rangeh, numberOfFret);
  rangel = Math.max(rangel, 0);
  fretr[0] = rangel;
  fretr[1] = rangeh;
  return fretr;
}

function getFromFret()
{
  return getFretRange()[0];
}

function getToFret()
{
  return getFretRange()[1];
}

function isInArray(arr, el)
{
  var i;
  for (i=0;i<arr.length;i++)
  {
    if ( arr[i] == el ) return true;
  }
  return false;
}

function CheckNote(nota)
{
  nota = nota.toLowerCase();
  if ( nota.match(/#$/) || nota.match(/b$/) )
  {
    nota = nota.substring(0,nota.length - 1);
  }
  return isInArray(note, nota);
}

function UpdateTuner()
{
  var i, el;
  var t = document.getElementById("tuning");
  var tt = tuning;
  t.value = tt.join();
  for(i=0;i<6;i++)
  {
    el = document.getElementById("corda"+i);
    if ( el == null ) return;
    // el.firstChild.nodeValue = tuning[i];
    var pu = document.createElement("b");
    var pp = document.createTextNode(tuning[i]);
    pu.appendChild(pp);
    el.replaceChild(pu, el.firstChild);
  }
}

function setTuning()
{
   var tuner = document.getElementById("tuning");
   var newtuning = tuner.value;
   var i;

   tuning = newtuning.split(",");
   if ( tuning.length < 6 )
   {
     tuning = reftuning;
     UpdateTuner();
     return;
   }
   for(i=0; i<6; i++)
   {
     if ( CheckNote(tuning[i]) == false )
     {
       tuning[i] = reftuning[i];
     }
   }
   UpdateTuner();
}

function CreaId(a,b)
{
  return "c" + a + "t" + b;
}


function resetFrets()
{
  var j,k, el, pu, pp;


  for(j=0; j<6; j++)
  {
     el = document.getElementById("corda" + j);
     pu = document.createTextNode(tuning[j]);
     el.replaceChild(pu, el.firstChild);

     for(k=0; k< numberOfFret; k++)
     {
        var img = document.getElementById(CreaId(j,k));
        img.setAttribute("src", "img/vuota.png");
     }
  }
}

function ArrIndexOf(el, arr)
{
  var i;
  for (i=0; i < arr.length ; i++)
  {
     if ( arr[i] == el ) return i;
  }
  return -1;
}


/* TODO: write it better !! */
function NormaliseFret(fret)
{
  /* lab -> sol# , fab = mi etc.
  */
  var base;
  if ( fret.match(/#$/) )
  {
     base = fret.substring(0, fret.length - 1);
     if ( !isInArray(note, fret) )
     {
        var k = ArrIndexOf(base, note);
        if ( k < 0 ) { k=0; alert("Error: note " + note + " is not a note!"); }
        return note[ (k + 1) % 12 ];
     }
  } else if ( fret.match(/b$/) ) {
     base = fret.substring(0, fret.length - 1 );
     var k = ArrIndexOf(base, note);
     if ( k < 0 ) { k=0; alert("Error: note " + note + " is not a note!"); }
     return note[ (k + 11) % 12 ];
  }
  return fret;
}

function isInArraySpecial(arr, el)
{
  var i;
  for (i=0;i<arr.length;i++)
  {
    if ( NormaliseFret(arr[i]) == NormaliseFret(el) ) return true;
  }
  return false;
}


function hitFret(fret, col)
{
  var j,k;
  var norfret, ix, fromFret, toFret;

  fromFret = getFromFret();
  toFret = getToFret();

  norfret = NormaliseFret(fret);

  for (j=0; j<6; j++)
  {
      ix = ArrIndexOf(NormaliseFret(tuning[5-j]), note);
      if ( ix < 0 ) { alert("Cannot find " + NormaliseFret(tuning[5-j])); break; }
      if ( norfret == tuning[5-j] )
      {
         var el = document.getElementById("corda" + (5-j));
         var pu = document.createElement("b");
         var pp = document.createTextNode(tuning[5-j]);
         pu.appendChild(pp);
         el.replaceChild(pu, el.firstChild);
      }
      for (k=fromFret; k<toFret; k++)
      {
         if ( note[ (ix+k+1) % 12 ] == norfret )
         {
             var e = document.getElementById(CreaId(j,k));
             e.setAttribute("src", "img/s" + col + ".png");
         }
      }
  }
}


function showNotes()
{
  var i,j,k;
  var nol = document.getElementById("note");
  var nols = (nol.value.toLowerCase()).split(",");
  var limite = nols.length > 7 ? 7 : nols.length;
  var reale = new Array();

  resetFrets();

  for(k=0; k<limite; k++)
  {
     if ( CheckNote(nols[k]) == false ) {
        continue;
     }
     if ( isInArraySpecial(reale, nols[k] ) ) continue;
     reale.push(nols[k]);
  }
  nol.value = reale.join();
  for(k=0; k < 7; k++)
  {
    var el = document.getElementById("p" + k + "t");
    if ( k < reale.length )
    {
      el.firstChild.nodeValue = reale[k];
      hitFret(reale[k], k);
    } else {
      el.firstChild.nodeValue = "?";
    }
  }
}


function GeneraManico()
{
   var i, j, fromFret, toFret;

   fromFret = getFromFret();
   toFret = getToFret();

   document.write("<table>");
   for(j=-1; j<6; j++)
   {
      if ( j < 0 )
      {
        document.write("<tr><td></td><td></td>");
      } else {
        document.write("<tr><td id=\"corda" + (5-j) + "\">" +
                       tuning[5-j] +
                       "</td><td><img src=\"img/capotasto.png\"></td>");
      }
      for(i=fromFret; i<toFret; i++)
      {
         if ( j < 0 )
         {
           document.write("<td style=\"text-align:center\">" + (i+1) + "</td>");
         } else {
           document.write("<td><img id=\"" + CreaId(j,i) + "\" src=\"img/vuota.png\"></td>");
         }
      }
      document.write("</tr>");
   }
   document.write("<tr><td></td><td></td>");  //salta note e capotasto
   for(i=0; i<numberOfFret; i++)
   {
      // 2 4 6 8 (11) 14 16  numerando da 0 i tasti
      // 3 5 7 9  12  15 17
      switch(i)
      {
        case 2:
        case 4:
        case 6:
        case 8:
        case 14:
        case 16:
          document.write("<td><img src=\"img/0.png\"></td>");
          break;
        case 11:
          document.write("<td><img src=\"img/00.png\"></td>");
          break;
        default:
          document.write("<td></td>");
          break;
      }
   }
   document.write("</tr>");
   document.write("</table>");
}


