// JavaScript Document
function tabcreator(div){

		this.div = div;
		this.tabctrl = new tabcontrol(this);
		this.tabfunk = new guitarfunctions(this);
		this.chords = new Chords(this);
		this.scales =  new Scales(this);
		this.inst = new guitar(this);
		this.timing = new timing(this);
		
		this.cr = "\r\n";
		this.tabhistory = "";
		this.Spacing;
		this.selString;
		this.spacer = "-";
		this.notes = new Array("E","A","D","G","B","E");
		this.tabfooter = "\t\t\t\t\t\t\thttp://www.guitartabcreator.com";
		
		this.tablegend = "(/) Slide Up  (\\) Slide Down  (h) Hammer On  (p) Pull Off  (b) Bend (r) Release (v) Vibrato";
		this.tabout = document.createElement('textarea');
		this.tabout.id = "taboutput";
		this.tabout.className = "taboutput";
		this.tabout.name = "textarea";
		this.tabout.cols = "95";
		this.tabout.rows = "16";
		//this.googlead = "<script type=\"text/javascript\"><!--google_ad_client = \"pub-4367478993079936\";/* 728x90, created 3/16/08 */google_ad_slot = \"4402083990\";google_ad_width = 728;google_ad_height = 90;google_cpa_choice = \"\"; // on file//-->// --&gt;// --&gt;// --&gt;// --&gt;</script><script type=\"text/javascript\" src=\"http://pagead2.googlesyndication.com/pagead/show_ads.js\"></script>";
		this.div.appendChild(this.tabout);
		//this.div.appendChild(this.googlead);
		
		this.elems = new Array("/","\\","^","h","p","b","v","r");
		this.tracemax = 50;

		this.control = new Array(this.tracemax);
		this.strings = new Array(6);
		this.strings[0] = new Array();
		this.strings[1] = new Array();
		this.strings[2] = new Array();
		this.strings[3] = new Array();
		this.strings[4] = new Array();
		this.strings[5] = new Array();
	
		this.chord = 0;
		this.mute = 0;
		this.tabcounter = 0;
		//this.tracecount = 0;

		this.secs = 0;
		this.timerID = null;
		this.timerRunning = false;
		this.delay = 1000;
		this._insertelem = 0;
		this.bDone = false;
		this.instrument;

		this.addinst = function(type){
			this.instrument = type;
			debug(this.instrument.echo());
			
		}
		
		//format forum
		
		this.formatForum = function(){
			this.printtab(0);
		}
		
		this.email = function(){
			var i,j;
			var staging = "";
				for(i=5;i>=0;i--){
					staging = staging + this.notes[i] + "|" + this.spacey() + this.tostring(i)  + "%0D%0A"; //"|" + 
				}

			var ebody = this.tablegend + "%0D%0A" + "%0D%0A" + this.tabhistory.replace(/\r\n/g,"%0D%0A")  + "%0D%0A" + staging + "%0D%0A" + this.tabfooter;
			parent.location.href='mailto:?body='+ebody+'';
		}
		
		this.adddiv = function(vDiv){
			this.div = vDiv;
		}
	
		this.addcontrol = function(div){
			this.tabctrl.div = div;
			this.tabctrl.allbuttons();
		}
	
		
		this.autopop = function(){
			var i,j;
			for(i=0;i<=5;i++){
				while(this.strings[i].length != 0)
				{
					this.strings[i].pop();
				}
			}
		}
		
		this.tabbit = function() {
			var i,k,l,a=this.tabbit.arguments;
			k=a[0]-1;
			l=a[1];
			if (this._insertelem == 1 && k != this.selString){
				alert("you must choose a note on the same string as the Symbol you just entered");
			} else {
				if (this.tabcounter <= this.tracemax){
					//this will place the initial value
					if(this.mute == 1){
						this.strings[k][this.tabcounter] = 'x' + l;
					} else {
						this.strings[k][this.tabcounter] = l;
					} 
					//fill in every other k with a "." when //not making chords
					if(!this.chord){
						for(i=0;i<=5;i++){
							if(i != k){
								this.strings[i][this.tabcounter] = ".";
							}
						}
					} else{
						//look for numbers and leave alone
						for(i=0;i<=5;i++){
							if(this.strings[i][this.tabcounter] == null){
								this.strings[i][this.tabcounter] = ".";
							}
						}
					}
					this.timing.addtoarray(this.tabcounter,false);
					this.tabcounter +=1;
					if(this.chord == 1){
						this.tabcounter-=1;
			
					}
						this._insertelem = 0;
						this.tabfunk.resetbuttons();
				}

				this.selString = k; 
				if(!this.scales.isopen){
					this.inst.ClrBrd(0);
				}
				this.PB(0);
			}
		
		}

	this.PB = function (){
			this.printtab(0);
			var count = 1;
			var strID;
			var i,j;
			for(j=0;j<=this.tabcounter;j++){
				for(i=5;i>=0;i--){
					if(this.strings[i][j] !=  null){
						if(this.strings[i][j] !=  "."){
							if(isNaN(this.strings[i][j])){
								if(String(this.strings[i][j]).indexOf('x') >= 0){
									if(String(this.strings[i][j]).length == 2){
										strID = (i+1) + "" + String(this.strings[i][j]).substring(1,2);
									} else if(String(this.strings[i][j]).length == 3){
										strID = (i+1) + "" + String(this.strings[i][j]).substring(1,3);
									}
								}  else {
									strID =  (i+1) + "" + String(this.strings[i][j+1]);
								}
							} else {
								strID = (i+1) + "" + String(this.strings[i][j]);
							}
							var strElem = "note_" + strID;
							if(String(this.strings[i][j]).indexOf('x') >= 0){
									$(strElem).innerHTML = "X";
							}
							if($(strElem) != null){
								$(strElem).style.backgroundImage = "url(" + "images/noteblue.gif" + ")";
								$(strElem).style.backgroundPosition = "center";
								$(strElem).style.backgroundRepeat = "no-repeat";
							}
						}
					}
				}
			}
		}
		
		this.formatfooter = function(bVal){
			if(bVal){
				return("[url]http://www.guitartabcreator.com[/url]");
			} else {
				return("\t\t\t\t\t\t\thttp://www.guitartabcreator.com");
			}
		}
		
		this.addcodewrapper = function(type,bVal){
			if(bVal){
				if(type == 'begin'){
					return("[code]"+ this.cr);
				} else if(type == 'end') {
					return("[/code]"+ this.cr);
				} else {
					return("");
				}
			} else {
				return("");
			}
		}
		
		this.printtab = function(){
			var i,j;
			var staging = "";
				for(i=5;i>=0;i--){
					staging = staging + this.notes[i] + "|" + this.spacey() + this.tostring(i)  + this.cr; //"|" + 
				}
			//format forum check
			
			$('taboutput').value = this.addcodewrapper('begin',$('ForumFormat').checked) + this.tablegend + this.timing.showlegend() + this.tabhistory  + this.cr + this.timing.tostring() + this.cr + staging + this.addcodewrapper('end',$('ForumFormat').checked) +  this.formatfooter($('ForumFormat').checked);
			$('taboutput').scrollTop = $('taboutput').scrollHeight;
			
		}
		
		this.printfb = function(){
			var i,j;
			var staging = "";
				for(i=5;i>=0;i--){
					staging = staging + this.notes[i] + "|" + this.spacey() + this.tostring(i)  + this.cr; //"|" + 
				}

			//$('taboutput').value = this.cr + this.tabhistory + staging + this.tabfooter;//this.tablegend + this.cr + this.cr + 
			return(this.cr + this.cr + this.tabhistory + staging);//this.tablegend + this.cr + this.cr + 
			
		}
		
		this.tostring = function(i){
			var tmpstring = "";
			var j;
				for(j=0;j<=this.tabcounter;j++){
					if(this.strings[i][j] != null){
					if(this.iselem(j,i)){
						if(this.hasvib(j)){
							tmpstring = tmpstring + this.spacer + this.spacey();
						} else {
							tmpstring = tmpstring + this.spacer;// + this.spacey();
						}
					} else if(this.isbig(j,i)){
						if(this.ischord(j,i)){
							if(this.strings[i][j] == "."){
								tmpstring = tmpstring + this.spacer + this.spacer;
							} else if(String(this.strings[i][j]).indexOf('x') >= 0){
								tmpstring = tmpstring + String(this.strings[i][j]).substring(0,1) + this.spacer; // + String(strings[i][j]).substring(1)
							} else {
								if(this.S2N(this.strings[i][j]) <= 9){
									tmpstring = tmpstring + this.strings[i][j] + this.spacer;
								} else {
									tmpstring = tmpstring + this.strings[i][j];
								}
							}
						} else {
							if(this.strings[i][j] == "."){
								tmpstring = tmpstring + this.spacer + this.spacer;
							} else {
									tmpstring = tmpstring + this.strings[i][j];
							}
						}
						tmpstring += this.addspace(j);
					} else if(this.S2N(this.strings[i][j]) < 10){
		
						if(this.ischord(j,i)){
							if(this.strings[i][j] == "."){
								tmpstring = tmpstring + this.spacer;
							}else {	
								tmpstring = tmpstring + this.strings[i][j];
							}
						} else {
							if(this.strings[i][j] == "."){
								tmpstring = tmpstring + this.spacer;
							}  else {
								tmpstring = tmpstring + this.strings[i][j];
							}
						}
						tmpstring += this.addspace(j);
					}else if(String(this.strings[i][j]).indexOf('x') >= 0){
								tmpstring = tmpstring + String(this.strings[i][j]).substring(0,1) + this.spacey(); // + String(strings[i][j]).substring(1)
					} else if(this.strings[i][j] == "."){
							tmpstring = tmpstring + this.spacer;
					} else if(this.strings[i][j] == "|"){
								tmpstring = tmpstring + this.strings[i][j] + this.spacey();
					}  else if(this.strings[i][j] == "v"){
								tmpstring = tmpstring + this.strings[i][j] + this.spacey();
					}  else if(this.strings[i][j] == "_"){
								tmpstring = tmpstring + this.spacey();
					}  else { 
						tmpstring = tmpstring + this.strings[i][j];
					}
				}
			}
			
			return(tmpstring);
		}
		
		this.insertElem = function (sVal, objBtn){
			//this.timing.addtoarray(this.tabcounter,true);
			if (this._insertelem == 1){
				alert("Please select a note before inserting another Element");
				 $(objBtn).src = "images/" + $(objBtn).id + ".gif";	
				return;
			}
			if ( this.tabcounter == 0){
				alert("You must place a note before you place an element");
				$(objBtn).src = "images/" + $(objBtn).id + ".gif";		
				return;
			}	
			if(this.chord == 1){
				alert("You cannot add an element while creating a chord");
				$(objBtn).src = "images/" + $(objBtn).id + ".gif";		
				return;
			}

			if (this.tabcounter <= this.tracemax){
				this.strings[this.selString][this.tabcounter] = sVal;
				//backfill other strings with .
						for(i=0;i<=5;i++){
							if(this.strings[i][this.tabcounter] == null){
								this.strings[i][this.tabcounter] = ".";
							}
						}
				this.tabcounter += 1;
				if(sVal != "v"){
					this._insertelem = 1;	
				}
			}
			this.printtab(0);
		}	
		
		this.erase = function (){
			var i,j,k;

			if (this.tabcounter > 0){
				for(i=5;i>=0;i--){
					this.strings[i][this.tabcounter-1] = ".";
				}
				this.inst.ClrBrd(0);
				this.PB(0);
				this.tabcounter -=1;
			}

		//	this.printtab(0);
			
		}
		
		this.AB = function (){
			var i;
			for(i=5;i>=0;i--){
				this.strings[i][this.tabcounter] = "|";
			}
			this.tabcounter +=1;
			this.printtab(0);
			$(this.tabfunk.btnbar).src = "images/btnbar.gif";
		}

		this.AS = function (){
			var i;
			if(!this._insertelem){
				for(i=5;i>=0;i--){
					this.strings[i][this.tabcounter] = "_";
				}
				this.tabcounter +=1;
				this.printtab(0);
			}
		}
		
		this.spacey = function(type){
			var j;
			var sF = "";
			var space_type;
			if(type==null){
				space_type = this.spacer;
			} else {
				space_type = type;
			}
			for(j=1;j<parseInt($(this.tabctrl.selSpace).value);j++){ 
				sF = sF + space_type; //this.spacer;
			}
			return sF;
		}
		
		this.isElem = function (strchr){
			var i;
			var z = 0;
			for(i=0;i<this.elems.length;i++){
				if(strchr == this.elems[i]){
					z=1;
				}
			}
			if(z){
				return true;
			} else {
				return false;
			}
		}
		
		this.iselem = function (wb,k){
				for(i=5;i>=0;i--){
					if(this.isElem(this.strings[i][wb])){
						if(k != i){
							return true;
						}
					} 
				}
			return false; 
		}
		
		this.hasvib =  function (wb){
				for(i=5;i>=0;i--){
					if(this.strings[i][wb] == "v"){
							return true;
					} 
				}
			return false; 
		}
		 this.hasrest = function (wb){
				for(i=5;i>=0;i--){
					if(this.strings[i][wb] == "_"){
							return true;
					} 
				}
			return false; 
		}
		
		this.ischord = function (wb){
			var count=0;
				for(i=5;i>=0;i--){
					if(this.strings[i][wb] != "." && this.strings[i][wb] != null){
							count+=1;
					}
				}
			if(count>1){
				return true;
			} else{
				return false;
			} 
		}
		
		this.hasnote = function (wb){
			var count=0;
				for(i=5;i>=0;i--){
					if(this.strings[i][wb] != "." && this.strings[i][wb] != null && !this.isElem(this.strings[i][wb])){
							count+=1;
					}
				}
			if(count>=1){
				return true;
			} else{
				return false;
			} 
		}
		
		this.isbig = function (wb,k){
				for(i=5;i>=0;i--){
					if(String(this.strings[i][wb]).length > 1){
						if(String(this.strings[i][wb]).charAt(0) != "x"){
							return true;
						}
					}
				}
			return false; 
		}
		
		this.S2N = function (val){
			
			if(val != "."){
				return parseInt(val);
			} else {
				return 0;
			}
		}
		
		this.addspace = function(j){
			var tmpstring = "";
				if(!this.ElemVal(j)){
					tmpstring = tmpstring + this.spacey();
				}
			return tmpstring;
		}
		
		this.ElemVal = function(index){
				//scan the index and next two columns for an element
				if(index >= this.tabcounter){
					return false;
				}
				var bval = false;
				for(i=5;i>=0;i--){
					if(this.isElem(this.strings[i][index])){
						bval = true;
					} else if(this.isElem(this.strings[i][index+1])){
						bval = true;
					}
				}
				return bval;	
		}
		
		this.SO = function (){
			this.inst.ClrBrd(0);
			for(i=5;i>=0;i--){
				while(this.strings[i].length != 0)
				{
					this.strings[i].pop();	
				}
			}
			this.tabhistory ="";
			this.tabcounter = 0;
			this.printtab(0);	
		}
		

		
		this.NL = function (){
			var i;
			var staging = "";
			for(i=5;i>=0;i--){
				staging = staging + this.notes[i] + "|" + this.spacey() + this.tostring(i)  + this.cr; //"|" + 
			}
			this.tabhistory = this.tabhistory + this.cr + this.timing.tostring("history") + this.cr + staging + this.cr;
			this.tabcounter = 0;
				this.clean(0);
		}
		
		this.clean = function (){
			this.inst.ClrBrd(0);
			this.autopop(0);
			this.printtab(0);
			this._insertelem=0;
		}
		
		this.savetab = function(){
			this.createCookie('gtc_history',Object.toJSON(this.tabhistory),365);
  			this.createCookie('gtc_staging',Object.toJSON(this.strings),365);
		}

		this.recall = function(){
			for(var i=0;i<=5;i++){
				for(var j=0;j<=this.tabcounter;j++){
					this.strings[i][j] = ".";
				}
			}
			this.strings = this.readCookie('gtc_staging').evalJSON();
			this.tabcounter = this.strings[1].length;
			this.tabhistory = this.readCookie('gtc_history').evalJSON();
			this.PB(0);
		}

		this.createCookie = function (name,value,days) {
			if (days) {
				var date = new Date();
				date.setTime(date.getTime()+(days*24*60*60*1000));
				var expires = "; expires="+date.toGMTString();
			}
			else var expires = "";
			document.cookie = name+"="+value+expires+"; path=/";
			}

		this.readCookie = function (name)
		{
		  var ca = document.cookie.split(';');
		  var nameEQ = name + "=";
		  for(var i=0; i < ca.length; i++) {
			var c = ca[i];
			while (c.charAt(0)==' ') c = c.substring(1, c.length); //delete spaces
			if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
			}
		  return null;
		}
	
		this.eraseCookie = function (name)
		{
		  this.createCookie(name, "", -1);
		}
		
		this.InitializeTimer = function ()
		{
			var i,j;
			for(j=this.tracemax;j>0;j--){
				for(i=5;i>=0;i--){
								var tick = this.tracemax-j;
								if(tick<=this.tabcounter && this.strings[i][tick] != "." && this.strings[i][tick] != null){
									var strID = (i+1) + "" +  this.strings[i][tick];
									var strElem = "note_" + strID;
									$(strElem).innerHTML = tick+1;
								}
					}
			}
		}	
}


