function setStars(rateVal, starsDivId) {
	var range = document.getElementById(starsDivId);
	if (range) {
		range.style.width = (rateVal / 5 * 100) + '%';
	}
}
function setVal(spanId, val) {
	var rVal = document.getElementById(spanId);
	if (rVal) rVal.innerHTML = val;
}
function showRateMsg(msg) {
	var zone = document.getElementById('rateZone');
	if (zone) { zone.innerHTML = msg; }
}
function rateAudio(audioId, itemId, rateVal) {
	// alert('rate the audio [' + audioId + '] to ' + rateVal);
	var ajaxComp = new SimpleAjaxComp();
	ajaxComp.serverRequest('/ajaxGateway.do?act=rateAudio&itemId='+itemId+'&audioId='+audioId+'&rateVal='+rateVal,
		function(resp) {
			var idsp = resp.indexOf('###');
			var status = resp.substring(0, idsp).replace(/([\s|\t]*\r*\n)/g, '');
			var msg = resp.substring(idsp+3);
			if('OK' == status)
				setStars(rateVal, 'starsRange'); 
			showRateMsg(msg); 
		});
	return false;
}

function AudioRater() {
	this.ajaxComp = new SimpleAjaxComp();
	this.correctness = 2.5;
	this.tempo = 2.5;
	this.clarity = 2.5;
	this.summary = 2.5;
	this.flagged = false;
	this.comment = null;

	this.updateStars = function(prefix, valpf, cs, cy, to, sy) {
		setStars(this.correctness, prefix+cs);
		setStars(this.clarity, prefix+cy);
		setStars(this.tempo, prefix+to);
		setStars(this.summary, prefix+sy);
		setVal(valpf+cs, this.correctness);
		setVal(valpf+cy, this.clarity);
		setVal(valpf+to, this.tempo);
		setVal(valpf+sy, this.summary);
	};
	this.setCorrectness = function(val) {
		if (this.flagged) return;
		this.correctness = val;
		this.updateSummary();
	};
	this.setTempo = function(val) {
		if (this.flagged) return;
		this.tempo = val;
		this.updateSummary();
	};
	this.setClarity = function(val) {
		if (this.flagged) return;
		this.clarity = val;
		this.updateSummary();
	};
	this.setSummary = function(val) {
		if (this.flagged) return;
		this.summary = val;
		this.tempo = this.correctness = this.clarity = val;
	};
	this.updateSummary = function() {
		var avg = (this.tempo + this.correctness + this.clarity) / 3;
		this.summary = avg.toFixed(1);
	};
	this.flagInappropriate = function() {
		this.flagged = true;
		this.correctness = this.clarity = this.tempo = this.summary = 0;
	};
	this.flagAppropriate = function() {
		this.flagged = false;
		this.correctness = this.clarity = this.tempo = this.summary = 2.5;
	};
	this.setComment = function(cmt) {
		if (cmt && cmt.length > 0) {
			this.comment = cmt;
		}
	};
	this.submit = function(audId, itmId) {
		var agree = false;
		if (this.comment == null)
			agreed = window.confirm('Do you want to submit evaluation of this audio right now? You will not be able to change it any more.');
		else 
			agreed = true;
		if (!agreed) return;

		var reqParams = '&audioId='+audId+'&itemId='+itmId
			+'&cs='+(this.correctness <= 0 ? -2.5 : this.correctness)
			+'&cy='+(this.clarity <= 0 ? -2.5 : this.clarity)
			+'&to='+(this.tempo <= 0 ? -2.5 : this.tempo)
			+'&sy='+(this.summary <= 0 ? -2.5 : this.summary);
		if (this.comment)
			reqParams += ('&cmt='+encodeURIComponent(this.comment));

		this.ajaxComp.serverRequest('/ajaxGateway.do?act=rateAudio'+reqParams,
			function(resp) {
				var idsp = resp.indexOf('###');
				var status = resp.substring(0, idsp);
				var msg = resp.substring(idsp+3);
				//alert(msg); 
			});
	};
}
