/**
 * @copyright Copyright (c) 2008 cron IT GmbH, All rights reserved
 * @author  Franz G. Jahn <franzjahn@cron-it.de>
 * @version $Id: generic_votes.js,v 1.3 2009/04/04 10:55:24 lazarrs Exp $
 * @package cron_dpsa
 */

// aktueller Text bei Bewertungen.
var tx_crondpsa_votes_success;
var tx_crondpsa_votes_average;
var tx_crondpsa_votes_voted = false;
function highlightStars(current) {
	if (tx_crondpsa_votes_voted) { return; }
	var images = $('#tx_crondpsa_votes > li > a > img');
	var image;
	for (var i=0; i<current; i++) {
		image = jQuery(images.get(i));
		image.attr('src', 'fileadmin/templates/images/icon-star-red.gif');
	}
	var text = jQuery(image).attr('title');
	$('#tx_crondpsa_votes_success').text(text);
}
function removeStarsHighlight() {
	if (tx_crondpsa_votes_voted) { return; }
	var images = $('#tx_crondpsa_votes > li > a > img');
	var image;
	for (var i=0; i<5; i++) {
		image = jQuery(images.get(i));
		if (i+0.5 <= tx_crondpsa_votes_average) {
			image.attr('src', 'fileadmin/templates/images/icon-star-yellow.gif');
		} else {
			image.attr('src', 'fileadmin/templates/images/icon-star-grey.gif');
		}
	}
	$("#tx_crondpsa_votes_success").text(tx_crondpsa_votes_success);
}
function callVote(url) {
	if (tx_crondpsa_votes_voted) {
		$("#tx_crondpsa_votes_success").text("");
		return false;
	}
    $.getJSON(url,
        function(data){
        	tx_crondpsa_votes_success = data.success;
        	if (data.average) {
	        	tx_crondpsa_votes_average = data.average;
	        }
			$("#tx_crondpsa_votes_success").text(data.success);
        	removeStarsHighlight();
        	tx_crondpsa_votes_voted = true;
	});
	return false;
}
$(document).ready(function() {
	tx_crondpsa_votes_success = $("#tx_crondpsa_votes_success").text();
	tx_crondpsa_votes_average = $("#tx_crondpsa_votes_average").text(); 
});