// Global variables

/*@cc_on @if (@_jscript) IE = true;
@else */ IE = false;
/* @end @*/ 

var Flats;
var NumStrings;
var Tuning = new Array(7, 2, 10, 5, 0, 7, 0, 0); // Default 6-string guitar tuning
var NumFrets = 15;
var NoteSet = new Array();
var Note;
var NoteStr;

//Functions

//Cookie Functions

function SetCookie ( name, value)
{
	var cookie_string = name + "=" + escape ( value );
	cookie_string += "; expires=01/01/2020 00:00:00";
	
	document.cookie = cookie_string;
}

function GetCookie ( cookie_name )
{
	var results = document.cookie.match ( '(^|;) ?' + cookie_name + '=([^;]*)(;|$)' );
	
	if ( results )
		return ( unescape ( results[2] ) );
	else
		return null;
}

function DelCookie ( cookie_name )
{
	var cookie_date = new Date ( );	// current date & time
	cookie_date.setTime ( cookie_date.getTime() - 1 );
	document.cookie = cookie_name += "=; expires=" + cookie_date.toGMTString();
}

function Refresh ()
{
	window.location.reload ();
}

function SetFlats ()
{
	SetCookie ("Flats", 1);
	Refresh ();
}

function SetSharps ()
{
	DelCookie ("Flats");
	Refresh ();
}

function getElementsByName_IE(tag, name) {
		 
	var elem = document.getElementsByTagName(tag);
	var arr = new Array();
	for(i = 0,iarr = 0; i < elem.length; i++) {
		att = elem[i].getAttribute("name");
		if(att == name) {
			arr[iarr] = elem[i];
			iarr++;
		}
	}
	return arr;
}

function ToggleNote (inp)
{
	nv = inp.value; // nv = note value, acquired from toggled input tag's value

	if (IE) {
		n = getElementsByName_IE('input', Note[nv]);
		o = getElementsByName_IE('input', 'O'+ Note[nv]);
	} else {
		n = document.getElementsByName(Note[nv]);
		o = document.getElementsByName('O'+ Note[nv]);
	}
	
	if (NoteSet[nv])
	{
		NoteSet[nv] = false;
		for (i=0;i<n.length;i++)
			n[i].src='string.gif';
		for (i=0;i<o.length;i++)
			o[i].src='Ostring.gif';
	} else
	{
		NoteSet[nv] = true;
		for (i=0;i<n.length;i++)
			n[i].src = n[i].name +'.gif';
		for (i=0;i<o.length;i++)
			o[i].src = o[i].name +'.gif';
	}
}

function SetNumStrings (sel)
{
	ns = sel.options[sel.selectedIndex].value;
	SetCookie ('NumStrings', ns);
	Refresh ();
}	

function SetTuning (sel)
{
	nv = sel.selectedIndex; // acquire nv from passed select tag
	SetCookie ('Tuning' + sel.name, nv);
	Refresh ();
}

function SetupSelNumStrings ()
{
	var sns = document.getElementById('sns');
	
	sns.options[NumStrings-4].selected = true; // array index = numstring - 4, e.g. 5 strings = array index 1
	sns.onchange = function () { SetNumStrings(this); };
}
			
function SetupFretboard ()
{			
	// Draw Fretboard
	var fbt = document.getElementById('fbt');
	//var divfbt = document.getElementById('divfbt');
	var noteval;
	
	for (st=0; st<NumStrings; st++)
	{
		trow = fbt.insertRow(st);
		
		tcell = trow.insertCell(-1); // cell[0] is the tuning select group
		
		// Setup select/option group for selecting tuning
		tselect = document.createElement ('select');
		
		tselect.id = 'selTuning' + st;
		tselect.name = st;
		tselect.onchange = function () { SetTuning(this); };
	
		// setup 12 options, 1 for each note A-G#
	
		for (i=0; i<12; i++)
		{
			toption = document.createElement ('option');
			toption.text = NoteStr[i];
			toption.value = i;
			if (IE) {
				tselect.add (toption); // For IE
			} else {
				tselect.add (toption, null); 
			}
		}
		
		tselect.selectedIndex = Tuning[st];
		tcell.appendChild (tselect);
		
		tcell = trow.insertCell(-1); // cell[1] is the open note for each string
		
		// Setup open note 
		timg = document.createElement('input');
		timg.type = 'image';
		noteval = Tuning[st];
		timg.name = 'O'+ Note[noteval];
		timg.value = noteval;
		timg.src = 'Ostring.gif';
		timg.onclick = function () { ToggleNote(this); };
		tcell.appendChild (timg);
		
		// Setup remaining notes	
		for (fr=1; fr<NumFrets; fr++)
		{
			tcell = trow.insertCell(-1); // cell[1+fret number] are the notes for each fret
			
			timg = document.createElement('input');
			timg.type = 'image';
			noteval = (Tuning[st]+fr) % 12;
			timg.name = Note[noteval];
			timg.value = noteval;
			timg.src = 'string.gif';
			timg.setAttribute ('onclick', 'ToggleNote(' + noteval.toString() + ')');
			timg.onclick = function () { ToggleNote(this); };
			tcell.appendChild (timg);
		}
	}
}
	
window.onload = function () 
{
	// Initialize Globals
	
	// Init notes array depending on flats or sharps
	
	Flats = GetCookie ("Flats");
	if (Flats)
	{
		Note = new Array("A","Bf","B","C","Df","D","Ef","E","F","Gf","G","Af");
		NoteStr = new Array("A","Bb","B","C","Db","D","Eb","E","F","Gb","G","Ab");
	}
	else
	{
		Note = new Array("A","As","B","C","Cs","D","Ds","E","F","Fs","G","Gs");
		NoteStr = new Array("A","A#","B","C","C#","D","D#","E","F","F#","G","G#");
	}
	
	// Set number of strings
	NumStrings = Number (GetCookie ("NumStrings"));
	if (!NumStrings)
	{
		NumStrings = 6;
		SetCookie ('NumStrings', 6);
	}
	for (i=0; i<NumStrings; i++)
	{
		t = GetCookie ("Tuning" + i);
		if (t) Tuning[i] = t;
	}
		
	// Initialise NoteSet array
	for (i=0; i<12; i++)
		NoteSet[i] = false;
	
	// Setup number of strings option group	
	SetupSelNumStrings ();	
		
	// Draw fretboard
	SetupFretboard();
}
	