var vote_star_on = new Image();
var vote_star_off = new Image();
vote_star_on.src = '/cms/modules/vote_star/images/star_on.gif';
vote_star_off.src = '/cms/modules/vote_star/images/star_off.gif';

vote_stars = new Array();
for (var i = 0; i < 6; i++) {
	vote_stars[i] = new Image();
	vote_stars[i].src = '/cms/modules/vote_star/images/rating' + i +  '.gif'
}

if (isJsEnabled()) {
	addLoadEvent(vote_star_auto_attach);
}

function vote_star_auto_attach() {
	var objs = [];
	var stars = document.getElementsByTagName('a');
	for (var i = 0; star = stars[i]; i++) {
    	if (star && (hasClass(star, 'vote-star-link')) ) {
			// Read in the path to the PHP handler
			var uri = star.getAttribute('title');
			
			// Remove the title, so no tooltip will display
			star.removeAttribute('title');
			
      		// Read in the id
      		var id  = star.getAttribute('id');
			var nid = id.match(/[0-9]+$/);
      		
			// Create an object with this uri.
			if (!objs[uri]) {
				objs[uri] = new voteStar(star, uri, nid);
			}
		}
	}
}

function voteStar(element, uri, nid) {
	var vs = this;
	this.star = element;
	this.uri = uri;
	this.nid  = nid;
	this.star.onclick = function() {
		var msg = document.getElementById('vote-star-msg-' + vs.nid);
		msg.innerHTML = 'saving...';	
		HTTPGet(vs.uri, vs.receive, vs);
		return false;
	}
}

voteStar.prototype.receive = function(string, xmlhttp, vs) {
	if (xmlhttp.status != 200) {
		return alert('An HTTP error '+ xmlhttp.status +' occured.\n'+ vs.uri);
	}

	// get the nid
	var nid = vs.nid;
    var result = xmlhttp.responseXML.getElementsByTagName("result")[0];
    var count = result.getElementsByTagName('count')[0].firstChild.nodeValue;
    var average = result.getElementsByTagName('average')[0].firstChild.nodeValue;
    var vote = xmlhttp.responseXML.getElementsByTagName("vote")[0];
    var value = vote.getElementsByTagName('value')[0].firstChild.nodeValue;
	
	// disable stars events
	var widget_stars = document.getElementById('vote-star-stars-' + nid);
	widget_stars.onmouseout = function() {};
	
	// hides the rating widget
	var widget = document.getElementById('vote-star-widget-' + nid);
	addClass(widget, 'vs-hidden');
	
	// updates the overal rating
	var stars = Math.round(average / 20);
	var overall = document.getElementById('vote-star-overall-' + nid);
	overall.src = vote_stars[stars].src;

	// updates rate message
	value = Math.round(value / 20);
	var msg = document.getElementById('vote-star-msg-' + nid);
	msg.innerHTML = 'You rated this item a  ' + value;
	
	// updates count message
	var msg = document.getElementById('vote-star-value-' + nid);
	if ( !count ) {
		msg.innerHTML = '(not yet rated)';
	} else if ( count == 1) {
		msg.innerHTML = '(from 1 user)';
	} else {
		msg.innerHTML = '(from ' + count + ' users)';
	}
}


function vote_star_reset(nid) {
	for (var i = 1; i < 6; i++) {
		var wsi = document.getElementById('vote-star-' + nid + '-' + i);
		wsi.src = vote_star_off.src;
	}
	// clear message
	var msg = document.getElementById('vote-star-msg-' + nid);
	msg.innerHTML = '';
}

function vote_star_set(nid, idx) {
	for (var i = 1; i <= idx; i++) {
		var wsi = document.getElementById('vote-star-' + nid + '-' + i);
		wsi.src = vote_star_on.src;
	}
	for (var i = idx + 1; i < 6; i++) {
		var wsi = document.getElementById('vote-star-' + nid + '-' + i);
		wsi.src = vote_star_off.src;
	}
	// set the message
	var msg = document.getElementById('vote-star-msg-' + nid);
	msg.innerHTML = 'Your rate is ' + idx;
}