var _html2Objects_ = [];
var globalIndex = 0;


function RatingStar(id, objParent, srcEmpty, srcSelect, edt) {
	this.id = id;
	this.objectControl = objParent;
	this.sourceEmpty = srcEmpty;
	this.sourceSelect = (srcSelect === null || srcSelect === '')? srcEmpty: srcSelect;
	this.editable = edt;
	this.selected = false;
	return this;
}

RatingStar.prototype = {
	prefixHtmlKey: 'imageStar_',
	
	toString: function() {
		if (this.editable) {
			var key = "" + this.prefixHtmlKey  + globalIndex + '_' + this.id;
			globalIndex = globalIndex + 1;
			_html2Objects_[key] = this;
			var source = this.selected ? this.sourceSelect: this.sourceEmpty;
			return '<img src="' + source +
				'" alt="" class="hand" onclick="selectRating(this)" key="' + key + '" />';
		}
		return '<img src="' + (this.selected? this.sourceSelect: this.sourceEmpty) + '" alt="" />';
	},
	
	isSelected: function() {
		return this.selected;
	},
	
	setSelected: function(val) {
		this.selected = (val === null) ? false: val;
	},
	
	equals: function(star) {
		if (star == this) {
			return true;
		}
		if (star === null) {
			return false;
		}
		if (star instanceof RatingStar) {
			return this.id == star.id;
		}
		return this.id == star;
	}
};

function RatingControl(srcImgEmpty, srcImgSelect, rt, editable, fieldName, ratingId) {
	var imgEmpt = (srcImgEmpty === null || srcImgEmpty === '')? this.sourceEmpty: srcImgEmpty;
	var imgSlct = (srcImgSelect === null || srcImgSelect === '')? this.sourceSelect: srcImgSelect;
	var edtb = (editable === null)? true: editable;
	this.ratingId = ratingId;
	this.rating = [
		new RatingStar(1, this, imgEmpt, imgSlct, edtb), new RatingStar(2, this, imgEmpt, imgSlct, edtb),
		new RatingStar(3, this, imgEmpt, imgSlct, edtb), new RatingStar(4, this, imgEmpt, imgSlct, edtb),
		new RatingStar(5, this, imgEmpt, imgSlct, edtb)];
	if (rt) {
		for (var i = 0; i < rt; i = i + 1) {
			this.rating[i].setSelected(true);
		}
	}
	this.field = document.getElementById(fieldName);
	return this;
}

RatingControl.prototype = {
	sourceEmpty: 'images/star0.gif',
	sourceSelect: 'images/star1.gif',

	toString: function() {
		var str = '';
		for (var i = 0; i < this.rating.length; i = i + 1) {
			str += this.rating[i].toString();
		}
		return str;
	},
	
	select: function(imgIndex) {
		var indx = -1;
		for (var i = 0; i < this.rating.length; i = i + 1) {
			if (this.rating[i].equals(imgIndex)) {
				indx = i;
				break;
			}
		}
		for (var j = 0; j <= indx; j = j + 1) {
			this.rating[j].setSelected(true);
		}
		for (var k = indx + 1; k < this.rating.length; k = k + 1) {
			this.rating[k].setSelected(false);
		}
		if (this.field !== null) {
			this.field.value = this.getRatingValue();
		}
	},
	
	getRatingValue: function() {
		var val = 0;
		for (var i = 0; i < this.rating.length; i = i + 1) {
			if (this.rating[i].isSelected()) {
				val = val + 1;
			} else {
				break;
			}
		}
		return val;
	}
};

var req = null;

function selectRating(img) {
	var objstar = _html2Objects_[img.getAttribute('key')];
	objstar.objectControl.select(objstar.id);
	img.parentNode.innerHTML = '' + objstar.objectControl;
	putrating(objstar.objectControl.ratingId, objstar.objectControl.getRatingValue());
}

function responseHandler() {
	if (req !== null && req.readyState == 4) {
		if (req.status == 200) {
			var hashtable = new Array();
			var pairs = req.responseText.split('&');
			for (var i = 0; i < pairs.length; i = i + 1) {
				var indxKey = pairs[i].indexOf('=');
				if (indxKey > 0) {
					var key = pairs[i].substring(0, indxKey);
					var value = pairs[i].substring(indxKey + 1);
					hashtable[key] = value;
				}
			}
			var votersNode = document.getElementById("voters" + hashtable["ratingId"]);
			votersNode.innerHTML = hashtable["voters"];
			var scoreNode = document.getElementById("score" + hashtable["ratingId"]);
			scoreNode.innerHTML = hashtable["score"];
			hashtable = null;
		}
	}
}

function putrating(ratingId, rate) {
	if (req === null) {
        try {
            req = new XMLHttpRequest();
        } catch (e) {}
	        if (req === null) {
	        try {
	            req = new ActiveXObject('Msxml2.XMLHTTP');
	        } catch (e) {}
        }
        if (req === null) {
        	try {
        		req = new ActiveXObject("Msxml2.XMLHTTP.3.0");
        	} catch (e) {}
        }
        if (req === null) {
	        try {
	            req = new ActiveXObject('Microsoft.XMLHTTP');
	        } catch (e) {}
	    }
	}
	
	if (isCookieEnabled()) {
	    req.open("POST", "competition.html?targetAction=rating", true);
	    req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	    req.onreadystatechange = responseHandler;
	    req.send('ratingId=' + ratingId + '&score=' + rate);
    }
}



