function Chord(objID,container,chordData,chordIndex,displayType,chordMode)
{
//alert("obj display type is: "+displayType);
//prop
this.objID = objID;
this.container = document.getElementById(container);
this.chordData = chordData;
this.chordIndex = chordIndex;
this.ghostChordIndex = chordIndex;
this.displayType = displayType;
this.chordMode = chordMode;

//this.chordLetters = ["E","A","D","C","G","F","B","B flat"];
this.chordLetters = ["A","B","C","D","E","F","G","A flat","B flat","D flat","E flat","G flat","F sharp"];
this.chordModes = ["major","minor","7","major7"];
//alert("incoming:"+this.chordIndex);

//meth
this.plot = plot;
this.fretboard = fretboard;
this.drawFretboard = drawFretboard;
this.chord = chord;
this.getChordIndex = getChordIndex;
this.getChordLetter = getChordLetter;
this.bar = bar;

		if(this.displayType=="fingers")
		{
		    document.getElementById("hand").style.display="";
		}else{
		    document.getElementById("hand").style.display="none";
		}
		
if(parseInt(this.chordIndex) > -1){
	this.chordIndex = this.getChordIndex(this.chordIndex);
	this.fretboard();
	this.plot();
	
}

	function fretboard()
	{
		this.container.innerHTML = this.drawFretboard();
	}

	function plot()
	{

		//label
		var label = document.createElement("div");
		label.id = "chordLabel";
		label.style.position="absolute";
		label.style.top = this.container.style.top+1;
		label.style.left = this.container.style.left+35;
	
		label.innerHTML = "Currently Hauling "+ this.chordData[this.chordIndex][0]+" "+this.chordData[this.chordIndex].chordType;
		this.container.appendChild(label);
		
		//notes
		if(this.displayType == "notes"){targetArrayNode = 3;}
		if(this.displayType == "fingers"){targetArrayNode = 4;}

		for(var n=0;n<this.chordData[this.chordIndex][1].length;n++)
		{
		

			if(this.chordData[this.chordIndex][1][n] != null)
			{
				var note = document.createElement("span");
				note.id = "noteItem";
				note.style.position="absolute";
				note.className = "note";
				if(this.chordData[this.chordIndex][3][n] == "X"){
					note.className = "noteExclude";
				}
				note.style.top = this.container.style.top+this.chordData[this.chordIndex][1][n];
				note.style.left = this.container.style.left+this.chordData[this.chordIndex][2][n];
				note.innerHTML = convertToAccidental(this.chordData[this.chordIndex][targetArrayNode][n]);
				this.container.appendChild(note)
			}

			if(this.chordData[this.chordIndex].bar != "")
			{
				barData = new String(this.chordData[this.chordIndex].bar);
				barSplit = barData.split(",");
				var bar = document.createElement("span");
				bar.id = "barItem";
				bar.style.position="absolute";
				bar.className = "bar";
				bar.style.top = this.container.style.top+barSplit[0];
				bar.style.left = this.container.style.left+barSplit[1];
				bar.style.height = (this.container.style.left+barSplit[2])+"px";
				bar.style.width = "15px";
				this.container.appendChild(bar)
			}

		}

		

		//document.getElementById(this.container).innerHTML = this.chordData[this.chordIndex];
								
	}

    function chord(index,dispType,chrdMde)
	{
	//alert(index+"_"+dispType);

	if(index > -1)
		{
			new Chord(this.objID,this.container.id,this.chordData,index,dispType,chrdMde);
		}
	}


	function drawFretboard()
	{
		var fretCount = 5;
		var stringCount = 8;
		var fretBoardStrout = new String();

		fretBoardStrout += "<br><table class='fretboard' border='0' cellspacing='1'><tr>";
			for(var f=0;f<fretCount;f++)
			{
				fretBoardStrout += "<td class='nut'>&nbsp;</td>";

				for(var s=0;s<stringCount;s++)
				{
				    innerItem = f+"-"+s;
				    if(innerItem=="2-2" || innerItem=="2-4" || innerItem=="2-6"){
				        innerItem = "<img src='../resources/images/fret_marker.gif'>";
				    }else{
				        innerItem = "";
				    }
					fretBoardStrout += "<td class='fret' valign='middle' align='center'>"+innerItem+"</td>";
				}
				if(f < fretCount-1){
				fretBoardStrout += "</tr><tr>";
				}
			}
		fretBoardStrout += "</tr></table><br><br><br>";
		
		fretBoardStrout += "<div align='center'>";
		
		fretBoardStrout += "<select name='chordName' onChange="+this.objID+".chord(this.value,'"+this.displayType+"','"+this.chordMode+"')>";
		fretBoardStrout += "<option value=''>select</option>";
		
		//chordLetters = ["E","A","D","C"];
		
		for(var c = 0 ;c<this.chordLetters.length;c++)
		{
			
			fretBoardStrout += "<option value='"+c+"'";
				
				if(this.chordIndex > -1){
				if(this.chordData[this.chordIndex][0]==this.chordLetters[c])
				{
					fretBoardStrout += " selected";
					this.ghostChordIndex = c;
				}}
			
			fretBoardStrout += ">"+this.chordLetters[c]+"</option>";
		}	
		fretBoardStrout += "</select>";


		if(this.chordIndex > -1){

		fretBoardStrout += "<select name='chordMode' onChange="+this.objID+".chord("+this.ghostChordIndex+",'"+this.displayType+"',this.value)>";

		for(var m = 0 ;m<this.chordModes.length;m++)
		{
			fretBoardStrout += "<option value='"+this.chordModes[m]+"'";
				
				if(this.chordMode == this.chordModes[m])
				{
					fretBoardStrout += " selected";
				}
			
			fretBoardStrout += ">"+this.chordModes[m]+"</option>";
		}	
		fretBoardStrout += "</select>";


  
		fretBoardStrout += "<select name='displayMode' onChange="+this.objID+".chord("+this.ghostChordIndex+",this.value,'"+this.chordMode+"')>";
		displayModes = ["notes","fingers"];
		
		
		for(var d = 0 ;d<displayModes.length;d++)
		{
			fretBoardStrout += "<option value='"+displayModes[d]+"'";
				
				if(this.displayType == displayModes[d])
				{
					fretBoardStrout += " selected";
				}
			
			fretBoardStrout += ">"+displayModes[d]+"</option>";
			//fretBoardStrout += "<a href='javascript:chord("+c+")'>"+this.chordData[c][0]+"</a>&nbsp;";
		}	
		fretBoardStrout += "</select>";
		}
		fretBoardStrout += "</div>";
		
		//if(this.chordIndex > -1){
		//for(var m=0;m<this.chordModes.length;m++)
		//{
		//    fretBoardStrout += "<a href=javascript:"+this.objID+".chord("+this.ghostChordIndex+",'"+this.displayType+"','"+this.chordModes[m]+"')>"+this.chordModes[m]+"</a> | ";
		//}
		//}
		fretBoardStrout += "";

		return fretBoardStrout;
		this.chordIndex = this.ghostChordIndex;
	}

	function getChordIndex(givenIndex)
	{

		//alert("given:"+givenIndex);
		//alert("thisChordLetter()="+this.getChordLetter()+":::this.chordIndex="+this.chordIndex);
		strout=new String();
		for(var s=0;s<this.chordData.length;s++)
		{
			for(var s2=0;s2<this.chordLetters.length;s2++)
			{
				strout+="thisGHOST="+this.ghostChordIndex+":::s="+s+":::s2="+s2+":::"+this.chordMode+"-"+ this.chordData[s].chordType+":::"+this.chordData[s][0]+"-"+ this.chordLetters[s2] +"::::"+this.chordLetters[s2]+"-"+ this.getChordLetter()+"\n";

				//alert(this.chordMode+"-"+ this.chordData[s].chordType+":::"+this.chordData[givenIndex][0]+"-"+ this.chordLetters[s2] +"::::"+ this.chordData[s][0] +"-"+ this.chordLetters[s2]);
				if(this.chordMode == this.chordData[s].chordType && this.chordData[s][0] == this.chordLetters[s2] && this.chordLetters[s2] == this.getChordLetter())
				{
					strout+="MATCH";
					//alert("return:"+s);
					//alert(strout)
					return s;
				}
			}
		}
	}
	
	function getChordLetter()
	{

		//return this.chordData[this.chordIndex][0];

		return this.chordLetters[this.ghostChordIndex];
	}

	function bar(barHeight)
	{
		return "<div style='height:"+barHeight+"px;width:20px'></div>";
	}

	function convertToAccidental(str)
	{
		strout = new String();
		ltr = new String(str);
		for(var l = 0;l<ltr.length;l++)
		{
			if(ltr.charAt(l) == "#" || ltr.charAt(l) == "b")
			{
				switch(ltr.charAt(l))
				{
					case "#":
					strout+="<img src='../resources/images/sharp.gif'>";
					break;
					case "b":
					strout+="<img src='../resources/images/flat.gif'>";
					break;
				}
			}else{
				strout+=ltr.charAt(l);
			}

		}
		return strout;
	}
}


