var kfCount = 1 ;

function setup() {
	if (document.getElementById("kfTemplate")) {
		Event.observe($("kfTemplate").getElementsByTagName("input")[0], "keyup", textChange, false) ;
		addFactor() ;
	}
	setupBTKeydef() ;
}

function setupBTKeydef() {
	if (!$("consultant-keydef")) return ;
	var myTable = $("consultant-keydef") ;
	var myRows = myTable.getElementsByTagName("tbody")[0].getElementsByTagName("tr") ;
	for (var i=1; i<myRows.length+1; i++) {
		var presetInput = myRows[i-1].getElementsByTagName("input")[0]
		var presetVal = (presetInput && presetInput.value && presetInput.value/1==presetInput.value) ? Math.round(presetInput.value*100) : 50 ;
		myRows[i-1].getElementsByTagName("span")[1].innerHTML = presetVal + "%" ;
		initialiseSlider(i, presetVal) ;
	}
}
function initialiseSlider(i, presetVal) {
	var slider = new Control.Slider('handle' + i, 'track' + i, {
		range: $R(0,100),
		sliderValue: presetVal,
		startSpan: "span" + i,
		onChange: function(v){ showNewValue(i, v) ; }
	});
}

function textChange(e) {
	var rowTypedIn = Event.findElement(e, "tr") ;
	if (rowTypedIn.className == "inactive" && rowTypedIn.getElementsByTagName("input")[0].value.length > 0) {
		addFactor() ;
		rowTypedIn.removeClassName("inactive") ;
		if (isNaN(parseInt(rowTypedIn.getElementsByTagName("input")[1].value))) {
			rowTypedIn.getElementsByTagName("span")[1].innerHTML = "50%" ;
			rowTypedIn.getElementsByTagName("input")[1].value = "50" ;
		}
	}
}

function showNewValue(sliderID, val) {
	/* alert(sliderID + " --> " + val) ;*/
	document.getElementById("result" + sliderID).innerHTML = Math.round(val) + "%" ;
	document.getElementById("factorScore" + sliderID).value = Math.round(val) ;
}

function deleteKF(e) {
	if (!confirm("Are you sure you want to delete this factor?")) return ;
	var rowToKill = Event.findElement(e, "tr") ;
	if (rowToKill) {
		rowToKill.parentNode.removeChild(rowToKill) ;
	}
}

function addFactor(presetText, presetValue) {
	// Copy the model row to above the model row
	var modelRow = $("kfTemplate") ;
	var newRow = modelRow.cloneNode(true) ;
	// Set the ids of the track#, and handle#
	newRow.getElementsByTagName("div")[1].id = "span" + kfCount ;
	newRow.getElementsByTagName("div")[2].id = "track" + kfCount ;
	newRow.getElementsByTagName("span")[0].id = "handle" + kfCount ;
	newRow.getElementsByTagName("span")[1].id = "result" + kfCount ;
	Event.observe(newRow.getElementsByTagName("input")[0], "keyup", textChange, false) ;
	Event.observe(newRow.getElementsByTagName("a")[0], "click", deleteKF, false) ;
	newRow.getElementsByTagName("input")[0].name = "factor_desc[]" ;
	newRow.getElementsByTagName("input")[1].id = "factorScore" + kfCount ;
	newRow.getElementsByTagName("input")[1].name = "factor_score[]" ;
	
	if (presetText) {
		newRow.removeClassName("inactive") ;
		newRow.getElementsByTagName("input")[0].value = presetText ;
	}
	if (presetValue) {
		newRow.removeClassName("inactive") ;
		newRow.getElementsByTagName("input")[1].value = presetValue ;
		newRow.getElementsByTagName("span")[1].innerHTML = presetValue + "%" ;
	}
	
	// Insert
	modelRow.parentNode.insertBefore(newRow, modelRow) ;
	newRow.id = "" ;
	
	// Initialise
	var curCount = kfCount ;
	var slider = new Control.Slider('handle' + curCount, 'track' + curCount, {
		range: $R(0,100),
		sliderValue: presetValue ? presetValue : 50,
		startSpan: "span" + curCount,
		onChange: function(v){ showNewValue(curCount, v) ; }
	});
	
	kfCount++ ;
}

Event.observe(window, 'load', setup);