function Rhythm() {

	this.req 			= null;
	this.noBeatsInTab 	= 0;
	this.noRows			= null;
	this.noBars			= 0;
	this.beatsPerBar	= 0;
	this.notesPerBar	= 0;
	
	this.init = function() {
	
	}

	this.addRowButtons = function (rhythmTimingID, drumTypeID) 
	//(noNotesInBar, noNotesPerBeat, uniqueID)
	{
		$("#addrow").show(300);
		
		
	}
	
	this.deleteBar = function (thisBar) {
		var previousBar = thisBar-1;
		$("#notationrow"+(previousBar)+" .deleterow").show(300);
		$(".deleterow").click(function() { Rhythm.deleteBar((previousBar)) });
		$("#notationrow"+thisBar).remove();
		
		this.noBeatsInTab = this.noBeatsInTab - this.beatsPerBar;
		
	}
	this.addBar = function (rhythmTimingID, drumTypeID) 
	{
		$.get("ajax-get-timing-details.php", { timingID: rhythmTimingID, drumID: drumTypeID },
		function(data){
			
			var dataArray 		= data.split("|");
			Rhythm.beatsPerBar 	= dataArray [0];
			Rhythm.notesPerBeat = dataArray [1];
			var drumNoChannels  = dataArray [2];
			Rhythm.notesPerBar		= Rhythm.beatsPerBar * Rhythm.notesPerBeat;
			
			// alert('in addbar no bars is ' + Rhythm.noBars);
			// alert('Rhythm.noBeatsInTab is ' + Rhythm.noBeatsInTab);
			Rhythm.noBars ++;
			$("#tabinputboxes").append("<div class=\"addnotationrow\" id=\"notationrow"+Rhythm.noBars+"\">");
			
			//alert('no channels is ' + drumNoChannels);
			for(var i=1;i<=drumNoChannels;i++)
			{
				if (i > 1)
					$("#notationrow"+Rhythm.noBars).append("<br/>");	
				Rhythm.printBoxes(i);
			}
			$(".noteinput").focus(function() {
				
				$("#taboptions").show(300);
				
				$(".selected").each(function() { 
					$(this).removeClass("selected");	
				});
				$(this).addClass("selected");
				
				
			});
			
			$(".deleterow").hide(300);
			$("#notationrow"+Rhythm.noBars).append("<input class=\"deleterow\" type=\"button\" value=\"deleterow\"/>");
			$(".deleterow").click(function() { Rhythm.deleteBar(Rhythm.noBars) });
			
			$("#tabinputboxes").append("</div>");
			
			Rhythm.noBeatsInTab = Rhythm.noBeatsInTab + (Rhythm.notesPerBar / Rhythm.notesPerBeat) ;
			
		});	
		
		
	}
	this.printBoxes = function (voice) {
		
		if (voice > 9) alert ('more than 9 voices passed. Open cTab.php and search for "regular expression needed"');
		
		//code is v1-n-4_5 means - voice 1 note 4.5
		var noteNo = 1;
		var noteNameIncrement = 1 / Rhythm.notesPerBeat;
		var notesInRow = 0;
		var noteName;
		
		//$("#notationrow"+Rhythm.noBars).append("<div class='notationrowvoice'>");
		for (var i=0;i<Rhythm.notesPerBar;i++)
		{
			var classname = 'noteinput';
			if ((noteNo.toFixed(5))  == Math.round(noteNo))
				classname = classname + ' onbeat';		
			
			noteName = 'v-'+voice+'-n-' + (parseFloat(noteNo.toFixed(3)) + parseInt(Rhythm.noBeatsInTab));
			noteName = noteName.replace(".", "_")
			$("#notationrow"+Rhythm.noBars).append("<input type='text' value='' class='"+classname+"' name='"+noteName+"'" + "' id='"+noteName+"' >");
			$("#tabinputboxes").show();
			
			noteNo = noteNo + noteNameIncrement;
			
		}
		
		
	}
	this.removeRowFromTable = function()
	{
	  var tbl = document.getElementById('tabtable');
	  var lastRow = tbl.rows.length;
	  if (lastRow > 2) tbl.deleteRow(lastRow - 1);
	}

	this.insertafter = function (newChild, refChild) { 
		refChild.parentNode.insertBefore(newChild,refChild.nextSibling); 
	}
	
	this.splitNote = function (prevNote, nextNote, splitInto, selectedNote) {
		
		var nextName = nextNote.attr('name');
		
		if (prevNote.length==0)
		{
			var prevPosition = '0';
			
		}
		else
		{
			
			var prevName 		= prevNote.attr('name');	
			var prevPosition 	= this.parseNotePosition(prevName);		
			//alert(prevName);
		}
		
		var tempNote = "<input id=\"tempstart\" name=\"tempstart\" class=\"noteinput splitnote\"/>";
		selectedNote.after(tempNote);
		prevNote = $("#tempstart");
		
		var nextPosition		= this.parseNotePosition(nextName);
		var selectedPosition 	= this.parseNotePosition(selectedNote.attr('name'));
		
		var Note			= function () {
			
		};
		
		Note.rowNum 		= this.getRowNumber(selectedNote);
		//Note.size 			= (nextPosition - prevPosition) / 2;
		
		
		if (nextNote.attr('value')=='deleterow')
		{
			Note.size 		= selectedPosition - prevPosition;
			//alert('selectedPosition is ' + selectedPosition);
			//alert('prevPosition is ' + prevPosition);
			
		}else
			Note.size 		= nextPosition - selectedPosition;
			
		//alert("Note.size is " + Note.size);
		
		Note.position		= this.parseNotePosition(selectedNote.attr('name'));
		Note.voice			= this.partseNoteVoice(selectedNote.attr('name'));
		Note.width			= selectedNote.width();
		
		var newNote			= new Note();
		
		newNote.size 		= Note.size/splitInto;
		
		
		newNote.position 	= Note.position;
		newNote.voice		= Note.voice;
		newNote.width 		= Note.width / splitInto;
		
		selectedNote.remove();
		var insertAfterNote 	= prevNote;
		//alert("newNote.size1 is " +  parseFloat(newNote.size));
		for(var i=1;i<=splitInto;i++)
		{//v-1-n-1.75
			//alert("newNote.size 2 is " +  newNote.size);
			if (i== splitInto)
				var lastSplit = 1;
			//newNote.position = newNote.position.toFixed(5);
			var newPosition = newNote.position;
			newPosition = newPosition.toFixed(3);
			//alert("newPosition is " + newPosition);
			newPosition = String(newPosition);
			newPosition = newPosition.replace('.',"_");
			
			//alert(newPosition);
			var insertNote = "<input id=\"v-"+newNote.voice+"-n-"+newPosition+"\" name=\"v-"+newNote.voice+"-n-"+newPosition+"\" class=\"noteinput splitnote";
			if (lastSplit && splitInto == 3)
				insertNote += " last-splitnote3";
			if (lastSplit && splitInto == 2)
				insertNote += " last-splitnote2";
			insertNote += "\" type=\"text\" style=\"width:"+newNote.width+"px\" value=\"\">"
			
			
			//alert("insert after" + insertAfterNote);
			insertAfterNote.after(insertNote);
			//alert("Just added is "+"v-"+newNote.voice+"-n-"+newPosition);
			insertAfterNote = $("#v-"+newNote.voice+"-n-"+newPosition);
			//nextNotePosition += newNote.size;	

			//alert("newnote.position is " + newNote.position);
			//alert("newNote.size is " +  newNote.size);
			
			newNote.position 	= newNote.position + newNote.size;
			//newNote.position 	= newNote.position;
			
		}
		$("#tempstart").remove();
		return false;
		
	}
	
	this.getRowNumber = function (selectedNote) {
		
		var rowIDname = selectedNote.closest('div').attr('id');
		var startPosition = rowIDname.indexOf("notationrow");	
		var rowNumber = rowIDname.substr(startPosition + 11);

		return rowNumber;
		
	}
	this.numRows = function () {
		
		return $('.addnotationrow').size();
		
	}
	this.parseNotePosition = function (nameValue) {
		
		var startPosition = nameValue.indexOf("n-");		
		var nextPos = nameValue.substr(startPosition + 2);	
		var nextPos = nextPos.replace('_',".");
		
		nextPos = parseFloat(nextPos);
		
		return nextPos;
	}
	this.partseNoteVoice = function (nameValue) {
		
		var startPosition = nameValue.indexOf("v-");	
		var endPosition = (nameValue.indexOf("-n-") - 1)
		var thisVoice = nameValue.substring(startPosition + endPosition);	
		var thisVoice = thisVoice.replace('_',".");
		
		thisVoice = parseFloat(thisVoice);
		
		return thisVoice;
	}
}
