// Guitar Theory

/**********
* Copyright 2007 Gregg B Larson
* All Rights reserved
***********/

function Scales(tctrl){
	this.tabcreator = tctrl;
	this.fs = 'S';
	this.isopen = true;
	//Key Select Dropdown
	this.selKey = document.createElement('select');
	this.selKey.name = "selKey";
	this.selKey.id = "selKey";
	this.selKey.options[0] = new Option("C", "C",false);
	this.selKey.options[1] = new Option("Cs", "C#/Db",false);
	this.selKey.options[2] = new Option("D", "D",false);
	this.selKey.options[3] = new Option("Ds", "D#/Eb",false);
	this.selKey.options[4] = new Option("E", "E",false);
	this.selKey.options[5] = new Option("F", "F",false);
	this.selKey.options[6] = new Option("Fs", "F#/Gb",false);
	this.selKey.options[7] = new Option("G", "G",false);
	this.selKey.options[8] = new Option("Gs", "G#/Ab",false);
	this.selKey.options[9] = new Option("A", "A",true);
	this.selKey.options[10] = new Option("As", "A#/Bb",false);
	this.selKey.options[11] = new Option("B", "B",false);
	
	//Mode Select Dropdown
	this.selMode = document.createElement('select');
	this.selMode.name = "selMode";
	this.selMode.id = "selMode";
//	this.selMode.options[0] = new Option("none", "none",true);
	this.selMode.options[0] = new Option("Ionian (major)", "ionian",true);
	this.selMode.options[1] = new Option("Aeolian", "aeolian",false);
	this.selMode.options[2] = new Option("Harmonic minor", "harmonicminor",false);
	this.selMode.options[3] = new Option("Melodic minor", "melodicminor",false);
	this.selMode.options[4] = new Option("Diminished", "diminished",false);
	this.selMode.options[5] = new Option("Dorian", "dorian",false);
	this.selMode.options[6] = new Option("Phrygian", "phrygian",false);
	this.selMode.options[7] = new Option("Lydian", "lydian",false);
	this.selMode.options[8] = new Option("Mixolydian", "mixolydian",false);
	this.selMode.options[9] = new Option("Locrian", "locrian",false);
	this.selMode.options[10] = new Option("Major Pent", "majorpent",false);
	this.selMode.options[11] = new Option("Minor Pent", "minorpent",false);
	this.selMode.options[12] = new Option("Minor Blues", "minorblues",false);
	
	//button return image
	this.btnreturn = new Image();	
	this.btnreturn.src = "images/button.gif";
	//this.btnv.id = "btnv";
	//this.btnv.title = "Vibrato'";
	this.btnreturn.alt = "return";
	this.btnreturn.width = "10";
	this.btnreturn.height = "10";
	
	//button clear image
	this.btnclear = new Image();	
	this.btnclear.src = "images/btnclear.gif";
	this.btnclear.alt = "clear";

	
	//button show image
	this.btnshow = new Image();	
	this.btnshow.src = "images/btnshow.gif";
	//this.btnv.id = "btnv";
	//this.btnv.title = "Vibrato'";
	this.btnshow.alt = "show";
	
	this.table = document.createElement('table');
	this.table.border = "0";
	this.table.width = "100%";
	this.table.style.backgroundColor = "#999999";
	
	this.tr1 = document.createElement('tr');

	this.td1 = document.createElement('td');
	this.td1.style.backgroundColor = "#003366";
	this.td1.style.fontSize = "10px";
	this.td1.style.color = "#FFFFFF";
	this.td1.height="10";
	this.td1.align="left";
	
		
	this.helplink = document.createElement('a');
	this.helplink.setAttribute('class', 'tiny_help');
	this.helplink.setAttribute('target', 'help');
	this.helplink.setAttribute('href', 'http://blog.guitartabcreator.com/2010/02/using-scale-helper.html');
	this.helplink.innerHTML = "help";
		
	this.spanlabel = document.createElement('span');
	//this.spanlabel.class="style7";
	this.spanlabel.id="lblcontrols";
	this.spanlabel.innerHTML = "Scale Helper";
	this.spanlabel.appendChild(this.helplink);
	
	this.td1.appendChild(this.btnreturn);
	this.td1.appendChild(this.spanlabel);
	this.tr1.appendChild(this.td1);
	//this.table.appendChild(this.tr1);
	
	this.tr2 = document.createElement('tr');
	this.td2 = document.createElement('td');
	this.td2.style.backgroundColor = "#999999";
	this.td2div = document.createElement('div');
	this.td2div.id="muteoff";
	this.td2div.align="left";
	this.td2.appendChild(this.td2div);
	this.tr2.appendChild(this.td2);
	this.tbody = document.createElement('tbody');
	this.tbody.appendChild(this.tr1);
	this.tbody.appendChild(this.tr2);
	this.table.appendChild(this.tbody);
	
		//table to format button positions
		this.tableFormat = document.createElement('table');
		this.tableFormat.border = "0";
		this.tableFormat.cellPadding = "0";
		this.tableFormat.cellSpacing = "0";
		this.tableFormat.width = "135px";
		//this.tableFormat.style.backgroundColor = "#999999";
		
		this.tableFormat_tr1 = document.createElement('tr');

		this.tableFormat_td1 = document.createElement('td');
		//this.td1.style.backgroundColor = "#003366";
		//this.tableFormat_td1.style.fontSize = "12px";
		//this.tableFormat_td1.style.color = "#000000";
		//this.tableFormat_td1.height="10";
		//this.tableFormat_td1.align="right";
		//this.tableFormat_td1.align="left";
		this.tableFormat_td1.appendChild(this.selKey);
		this.tableFormat_tr1.appendChild(this.tableFormat_td1);
		this.tableFormat_tr2 = document.createElement('tr');
		this.tableFormat_td2 = document.createElement('td');
		this.tableFormat_td2.style.backgroundColor = "#999999";
		this.tableFormat_td2.appendChild(this.selMode);
		this.tableFormat_tr2.appendChild(this.tableFormat_td2);
		this.tableFormat_tr3 = document.createElement('tr');
		this.tableFormat_td3 = document.createElement('td');
		this.tableFormat_td3.style.backgroundColor = "#999999";
		this.tableFormat_td3.colSpan = "2";
		this.tableFormat_td3.align="center";
		this.tableFormat_td3.appendChild(this.btnshow);
		this.tableFormat_td3.appendChild(this.btnclear);
		this.tableFormat_tr3.appendChild(this.tableFormat_td3);
		this.tableFormat_tbody = document.createElement('tbody');
		this.tableFormat_tbody.appendChild(this.tableFormat_tr1);
		this.tableFormat_tbody.appendChild(this.tableFormat_tr2);
		this.tableFormat_tbody.appendChild(this.tableFormat_tr3);
		this.tableFormat.appendChild(this.tableFormat_tbody);
		//just keep it open
		this.td2div.appendChild(this.tableFormat);
		//end table to format button positions
		
		//show scales
		Event.observe($(this.td1), 'click', function(event){
			// this.DisplayScale();
		}.bindAsEventListener(this));
	
		//clear scale
		Event.observe($(this.btnclear), 'click', function(event){
			 this.tabcreator.inst.ClrBrd(0);
			Effect.Fade('scaletable');
		}.bindAsEventListener(this));
		
		//show scale
		Event.observe($(this.btnshow), 'click', function(event){
			 this.tabcreator.inst.ClrBrd(0);
			 this.SM("F");
		}.bindAsEventListener(this));
	
	this.showControl = function(div){
		this.div = div;
		this.div.appendChild(this.table);
		
	}

	
	this.drawscale = function (i,j,notearray){
		var noteval = notearray.split("_");
		var h;
		if(j+4>21){
			h = 21;
		} else {
			h =j+4;
		}	
				
		for(i=6;i>=1;i--){
			for(k=j;k<=h;k++){
			//	alert(k);
				for(l=0;l<noteval.length;l++){
					if(noteval[l] != ""){
								if(noteval[l].toString().length == 1){
									if(noteval[l] == $(i + "" + k).title){
										//$("note_" + i + "" + k).style.backgroundColor = "#FF0000";
										$("note_" + i + "" + k).style.backgroundImage = "url(" + "images/notered.gif" + ")";
									}
								} else {
									if($(i + "" + k).title.toString().indexOf(noteval[l].toString()) != -1 ){
											//$("note_" + i + "" + k).style.backgroundColor = "#FF0000";
											$("note_" + i + "" + k).style.backgroundImage = "url(" + "images/notered.gif" + ")";
									}
								}
					}
				}
			}
		}
		
	}
	
	this.scale = function (noteval,notearray){
				//create a scale
	this.showscale(notearray);
	var i = 1;
	for(j=0;j<=21;j++){
		//need to check the id name against an array that represents the mode
		if(noteval.toString().length == 1){
			if(noteval == $(i + "" + j).title){
					this.drawscale(i,j,notearray);
			}
		} else {
			if($(i + "" + j).title.toString().indexOf(noteval.toString()) != -1 ){
				this.drawscale(i,j,notearray);
			}
		}			
	}
	ctrltab.PB();
	}
	
	/*
	function SS(){
			for(i=6;i>=1;i--){
				for(j=0;j<=21;j++){
					MM_swapImage("Image_" + i + "" + j,'','images/' + document.getElementsByName("Image_" + i + "" + j)[0].id + '.gif',1);
				}
			}
	}
	*/
	
	this.SM = function (flatsharp){
			//create a scale
				var kval = $(this.selKey).value ;   //document.frmTabCreator.selKey.options[document.frmTabCreator.selKey.selectedIndex].value;
				var mval = $(this.selMode).value;  //document.frmTabCreator.selMode.options[document.frmTabCreator.selMode.selectedIndex].value;
					var fs;
				if(flatsharp == "F"){
					fs = flatsharp;
				} else if(flatsharp == "S"){
					fs = flatsharp;
				} else if(flatsharp == "C"){
					//need to choose
					if(document.frmTabCreator.rgSharpFlat != null){
						for (var i=0; i < document.frmTabCreator.rgSharpFlat.length; i++)
						{
						   if (document.frmTabCreator.rgSharpFlat[i].checked)
							  {
							  fs = document.frmTabCreator.rgSharpFlat[i].value;
							  }
						}
					} else {
						fs = "S";
					}
				}
				//this.tabcreator.inst.ClrBrd(0);
				new Ajax.Updater('scaletable', 'cof.php', {parameters:'keyval=' + kval + '&modeval=' + mval + '&funct=PS' + '&fs=' + fs + '',onComplete:function(){ new Effect.Appear('scaletable');},asynchronous:true});
				new Ajax.Request('cof.php', {parameters:'keyval=' + kval + '&modeval=' + mval + '&funct=CS' + '&fs=' + fs + '',
														   onSuccess: function(t){
															 //  var tmpstring = t.responseText;
																var noteval = t.responseText.split("_");
																var k = 0;
																var strID;
																var strElem;
																for(i=6;i>=1;i--)
																{
																		for(j=0;j<=21;j++)
																		{
																			//need to check the id name against an array that represents the mode
																			for(k=0;k<noteval.length;k++)
																			{
																				strID = i + "" + j; 
																				strElem = "note_" + strID;
																				if(noteval[k] != ""){
																					if(noteval[k].toString().length == 1 && noteval[k].toString() == $(strID).title){
																							$(strID).style.backgroundColor = "#00FF00";
																							$(strElem).innerHTML = noteval[k].replace(/s/,"#");
																							if($(strElem).style.backgroundImage != "url(" + "images/noteblue.gif" + ")"){
																								$(strElem).style.backgroundImage = "url(" + "images/notegreen.gif" + ")";
																							}
																							$(strElem).style.backgroundPosition = "center";
																							$(strElem).style.backgroundRepeat = "no-repeat";
																							k=noteval.length;
																					} else if(noteval[k].toString().length != 1 && $(strID).title.toString().indexOf(noteval[k].toString()) != -1 ){
																							$(strID).style.backgroundColor = "#00FF00";
																							$(strElem).innerHTML = noteval[k].replace(/s/,"#");
																							if($(strElem).style.backgroundImage != "url(" + "images/noteblue.gif" + ")"){
																								$(strElem).style.backgroundImage = "url(" + "images/notegreen.gif" + ")";
																							}
																							$(strElem).style.backgroundPosition = "center";
																							$(strElem).style.backgroundRepeat = "no-repeat";
																							k=noteval.length;
																					}
																				}
																				
																			}
																			
																		}
																} 
																ctrltab.PB();
														   }
														   });
	
			
			
	}
	
	this.showscale = function (notearray)
		{
			var noteval = notearray.split("_");
			//debug(notearray);
			//debug(noteval.length);
			var k = 0;
			var strID;
			var strElem;
			for(i=6;i>=1;i--)
			{
					for(j=0;j<=21;j++)
					{
						//need to check the id name against an array that represents the mode
						
						for(k=0;k<noteval.length;k++)
						{
							strID = i + "" + j; 
							strElem = "note_" + strID;
							if(noteval[k] != ""){
								if(noteval[k].toString().length == 1 && noteval[k].toString() == $(strID).title){
										$(strID).style.backgroundColor = "#00FF00";
										$(strElem).innerHTML = noteval[k];
										$(strElem).style.backgroundImage = "url(" + "images/notegreen.gif" + ")";
										$(strElem).style.backgroundPosition = "center";
										$(strElem).style.backgroundRepeat = "no-repeat";
										k=noteval.length;
								} else if(noteval[k].toString().length != 1 && $(strID).title.toString().indexOf(noteval[k].toString()) != -1 ){
										$(strID).style.backgroundColor = "#00FF00";
										$(strElem).innerHTML = noteval[k];
										$(strElem).style.backgroundImage = "url(" + "images/notegreen.gif" + ")";
										$(strElem).style.backgroundPosition = "center";
										$(strElem).style.backgroundRepeat = "no-repeat";

										k=noteval.length;
								}
							}
							
						}
						
					}
			}
			
		}

	
	this.DisplayScale = function(){
		
		if(this.spanlabel.innerHTML == "click to open scale tool"){
			//document.frmTabCreator.btnChord.value = "Record a Chord";
			//chord = 0;l
			//new Ajax.Updater('muteoff', 'scale.php', {onComplete:function(){ new Effect.Highlight('muteoff');},asynchronous:true, evalScripts:true});
			//this.td2div.appendChild(this.selKey);
			//this.td2div.appendChild(this.selMode);
			//this.td2div.appendChild(this.btnshow);
			//Effect.BlindDown('muteoff');
			this.isopen = true;
			this.td2div.appendChild(this.tableFormat);
			this.spanlabel.innerHTML = "click to close scale tool";
			
			
		} else {
			this.isopen = false;
			this.tabcreator.inst.ClrBrd();
			ctrltab.PB();
			//document.getElementById('scaletable').innerHTML  = "";
			//document.getElementById('muteoff').innerHTML  = "";
			//Effect.BlindUp('muteoff');
			Effect.Fade('scaletable');
			$('muteoff').innerHTML  = "";
			this.spanlabel.innerHTML = "click to open scale tool";
	
		}
		
					
	}
}