/* starRatings.js */
var starRatingsClass = Class.create();
starRatingsClass.prototype = {
	//Properties
	ul: null,
	nodetype: null,
	nodeid: null,
	label: null,
	lirating: null,
	li: null,
	starIndex: null,
	ajax: null,
	ajaxUrl: null,
	originalRating: null,

	//Constructor
	initialize: function(el) {
		this.ajaxUrl = window.location;//'/voting/flash/';
		this.ul = el;
		this.nodetype = this.ul.id.split('_')[0] || null;
		this.nodeid = this.ul.id.split('_')[1] || null;
		this.label = document.getElementsBySelector('span.rating_text', el.parentNode)[0];
		this.lirating = this.ul.getElementsByTagName('li')[0];
		$A(this.ul.getElementsByTagName('li')).each(this.observeLi.bind(this));
	},

	//Methods
	clickLi: function (e) {
		if (Element.hasClassName(this.ul,'rated'))
			return null;
		el = e.currentTarget || Event.findElement(e, 'li');
		this.starIndex = $A(this.ul.getElementsByTagName('li')).indexOf(el);
		this.AjaxOut();
	},
	observeLi: function(el, index) {
		Event.observe(el, 'click', this.clickLi.bind(this), false);
		if(this.label)
		{
			Event.observe(el, 'mouseover', this.setOverRatingsLabel.bind(this, el));
			Event.observe(el, 'mouseout', this.setOutRatingsLabel.bind(this));
		}
	},
	AjaxOut: function() {
		var vars = '__ajax__=RateContentBlock&content_id=' + escape(this.nodeid) + '&content_type=' + escape(this.nodetype) + '&vote=' + escape(this.starIndex);
		//alert(vars);
		this.ajax = new Ajax.Request(this.ajaxUrl,
			{
				parameters: vars,
				onSuccess: this.AjaxIn.bind(this)
			}
		);
	},
	AjaxIn : function(req) {
		//alert(req.responseText);
		var result = req.responseXML.getElementsByTagName('result')[0];
		//Element.removeClassName(this.ul,'rateable');
		//Element.addClassName(this.ul,'rated');
		//this.lirating.style.width = parseInt(result.getAttribute('avg') / 5 * 100) + '%';
		this.lirating.style.width = parseInt(result.getAttribute('avg') * 20) + '%';
		if(this.label)
			this.label.firstChild.nodeValue = parseInt(result.getAttribute('avg') * 10) / 10 + '/5';
		this.originalRating = parseInt(result.getAttribute('avg') * 10) / 10 + '/5';
	},
	getElementTextNS: function (prefix, local, parentElem, index) {
		var result = "";
		if (prefix && document.all)
			// IE/Windows way of handling namespaces
			result = parentElem.getElementsByTagName(prefix + ":" + local)[index];
		else
			result = parentElem.getElementsByTagName(local)[index];
		if (result && result.childNodes.length > 0) {
			// get text, accounting for possible whitespace (carriage return) text nodes
			if (result.childNodes.length > 1)
				return result.childNodes[1].nodeValue;
			else
				return result.firstChild.nodeValue;
		}
		else
			return null;
	},
	setOverRatingsLabel: function(el, e) {
		this.originalRating = this.label.firstChild.nodeValue;
		//alert (this.originalRating);
		var el = e.currentTarget || Event.findElement(e, 'li');
		var starIndex = $A(this.ul.getElementsByTagName('li')).indexOf(el);
		this.label.firstChild.nodeValue = starIndex+'/5';
		if (Element.hasClassName(this.ul,'rated'))
		{
			this.avgRating = this.lirating.style.width;
			//this.lirating.style.width = this.starIndex / 5 * 100 + '%';
			this.lirating.style.width = this.starIndex * 20 + '%';
		}
	},
	setOutRatingsLabel: function(e) {
		this.label.firstChild.nodeValue = this.originalRating; //'Average Rating';
		if (Element.hasClassName(this.ul,'rated'))
			this.lirating.style.width = this.avgRating;
	}
};
/*
Behaviour.register({
	'ul.rateable' : function(el) {
		setTimeout(function(el) {
			new starRatingsClass(el);
		}.bind(this, el), 1000);
	}
});
*/

EventSelectors.register({
	'ul.rateable': function(el) {
		new starRatingsClass(el);
	}
}, true);

/*var starRatings_Rules= {
	'ul.rateable' : function(el){
		new starRatingsClass (el);
	}
};
Behaviour.register(starRatings_Rules);*/