/*
 * Stars rating for voting/ranking/etc
 * Author: kik.lozev AT gmail DOT com
 */
function print_r (obj, return_result) {
	var ret = '';
	for (k in obj) {
		ret += '[' +k+ '] => ' + obj[k] + "\n";
	}
	if (return_result)
		return ret;
	else
		alert (ret);
}
var events_caller = function (e) {
	var targ;
	if (!e) {
		var e=window.event;
	}
	if (e.target) {
		targ=e.target;
	} else if (e.srcElement) {
		targ=e.srcElement;
	}
	if (targ.nodeType==3) {
		targ = targ.parentNode;
	}

	return targ;
}
var RatingStars = function (id, zerrofill, min, max, current) {
	var
		id		= id,
		zerrofill	= zerrofill?true:false,
		min		= min || 0,
		max		= max || 10,
		current		= current || (zerrofill?'':((max-min)/2));
	var self=this, holder, children=[], input;
	holder		= document.createElement('div');
	holder.id	= id + 'holder';
	for (var i=min; i<=max; i++) {
		children[i]		= document.createElement('span');
		children[i].id		= id +'_option_' +i;
		children[i].className	= 'RatingStarsOption';
		children[i].onmouseover	= function (event) { self.option.over(event); };
		children[i].onmouseout	= function (event) { self.option.out(event); };
		children[i].onclick	= function (event) { self.option.click(event); };

		holder.appendChild(children[i]);
	}
	input = document.createElement('input');
	input.type="hidden";
	input.name=id;
	holder.appendChild(input);
	this.select = function () {
		input.value=current;
		for (var i=min; i<=max; i++) {
			if (i<=current)
				children[i].className = 'RatingStarsOption active';
			else
				children[i].className = 'RatingStarsOption';
		}
	}
	this.select();
	this.getElement = function () {
		return holder;
	}
	this.option = {
		over : function (event) {
			var node = events_caller(event);
			var val = node.id.replace(id +'_option_', '');
			for (var i=min; i<=max; i++) {
				if (i<=val)
					children[i].className = 'RatingStarsOption hovered';
				else
					children[i].className = 'RatingStarsOption';
			}
		},
		out : function (event) {
			var node = events_caller(event);
			var val = node.id.replace(id +'_option_', '');
			event.target.className = 'RatingStarsOption';
			self.select();
		},
		click : function (event) {
			var node = events_caller(event);
			var val = node.id.replace(id +'_option_', '');
//			print_r(event || window.event);
			current = val;
			self.select();
		}
	}
	return this;
}