Distance = new Array (0, 2, 4, 5, 7, 9, 11, 12);
NoteNames = new Array ( "C", "D", "E", "F", "G", "A", "B" );
Accident = new Array ( "bb", "b", "", "#", "##" );
ToneSemi = new Array ( "S", "T", "T+S", "H", "W", "W+H" );
GuidoAccident = new Array ( "&&", "&", "", "#", "##" );
ModeNames = new Array ( "Ionian","Dorian","Phyrgian","Lydian","Mixolydian","Aeolian","Locrian" );
DegreeNames = new Array ( "I", "II", "III", "IV", "V", "VI", "VII", "VIII" );

function PlayScale(root, acc, mode, oct)
{
   var i=0;
   var o=0;
   var s="";

   for (o=0; o <= oct; o++)
      for (i=0; i<7; i++)
      {
	s = s + ((12+Distance[(i+mode)%7] - Distance[mode])%12 + Distance[root] + acc + 48 + o * 12) + "%3A";
	if (o==oct)
	    break;
      }
   top.js.PlayPassage(s+"%1A");
}

function PlaySelected(oct)
{
    PlayScale(document.getElementById("Key").value.substring(0,1), 
	     document.getElementById("Key").value.substring(2,4),
	     document.getElementById("Mode").selectedIndex,
	     oct);
}

function Guido(String)
{
   document.getElementById("FRED").src = "http://clef.cs.ubc.ca/scripts/salieri/gifserv.pl?zoomfactor=0.8&pagesizeadjust=yes&outputformat=gif87&gmndata="+escape(String)+"&gmnurl=&submit=+++Send++";
}

function Notation(root,acc,mode)
{
    var t="";
    var title="";
    var major="";
    o=1;
    t='[';
    for (i=0; i<8; i++)
    {
	t = t + '{' +
	 NoteNames[(i+root)%7]+
		GuidoAccident[(Distance[(i+mode)%7]-Distance[mode]-Distance[(i+root)%7]+Distance[root])%12+2+acc]+o +"} ";
	if((i+root)%7==6) o++;
    }

    major='Degree '+DegreeNames[mode] + ' of the ' + NoteNames[(root-mode+7)%7]+ 
 Accident[(Distance[root]-Distance[(root-mode+7)%7]-Distance[mode])%12+2+acc]+ ' Major Scale';

    document.getElementById("MajorScale").innerHTML = major;

    title = '"'+NoteNames[root]+Accident[acc+2] + '  '+ModeNames[mode] + ' Mode"';
        t='[\\title<name='+title+',fsize=0.6cm,dx=-20>\\autoBreak<system="off",page="off">'+t.toLowerCase()+']';
    Guido(t);
}

function getElement(name, i)
{
   return document.getElementById(name+i);
}

function SetScale(root,acc,mode)
{
    var index = root * 7;
    for (i=0; i<8; i++)
    {
	getElement("SD", 1+i).className = "CSD";
	getElement("SN", 1+i).className = "CSN";
	getElement("SA", 1+i).className = "CSA";

	getElement("SN", 1+i).innerHTML =
	 NoteNames[(i+root)%7]+
		Accident[(Distance[(i+mode)%7]-Distance[mode]
			 -Distance[(i+root)%7]+Distance[root])%12+2+acc];
	getElement("SA", 1+i).innerHTML = 
		Accident[(-Distance[i]+Distance[(i+mode)%7]-Distance[mode])%12+2];
	if (i<7)
	{
	    getElement("SS", 1+i).className = "CSS";
	    getElement("SS", 1+i).innerHTML = 
		ToneSemi[(Distance[(1+i+mode)%7]-Distance[(i+mode)%7]-1)%12+document.getElementById("StepType").value)];
	}
    }
    Notation(root,acc,mode);
}




function Update()
{
    SetScale(document.getElementById("Key").value.substring(0,1), 
	     document.getElementById("Key").value.substring(2,4),
	     document.getElementById("Mode").value.selectedIndex);
}
