/*
Created by Matej Rokos for WebCorp
Based on rating by Addam M. Driver
Date: 2009-01-28
*/

document.observe('dom:loaded', function() {
  initStars();
});

function initStars() {
  var me = document.getElementById("ratings"); 
  if (me != null ) {
  var divs = me.getElementsByTagName("div");
  for (i=0; i<divs.length; i++) {
    var basename = divs[i].id.split("_");
    var numero = divs[i].className.charAt(divs[i].className.length-1);
    
    for(a=1; a<=numero; a++){
        //alert(basename[1]);
        if (document.getElementById(basename[1]+"_"+a) != null)	{	
				document.getElementById(basename[1]+"_"+a).className = "on";
				}
	}
	
  }
  }
  
}

/* Rollover for image Stars */
function rating(me){
	if(me.parentNode.className != "rated"){
		var num = me.id.charAt(me.id.length-1);
		var basename = me.id.substr(0,me.id.length-1);
    var totalstars = me.parentNode.getElementsByTagName("a").length;
    for(i=1; i<=totalstars; i++){
      document.getElementById(basename+i).className = "";
    }
    
		for(i=1; i<=num; i++){		
				document.getElementById(basename+i).className = "on";	
		}
	}
}

/* For when you roll out of the the whole thing */
function off(me){
    // var num = me.id.charAt(me.id.length-1);
    var num = me.parentNode.getElementsByTagName("a").length;
		var basename = me.id.substr(0,me.id.length-1);
	  var preset = me.parentNode.className.charAt(me.parentNode.className.length-1);
	
  if(me.parentNode.className != "rated"){
		for(i=1; i<=num; i++){
        if (i<=preset) {
				  document.getElementById(basename+i).className = "on";          
        }
        else {		
				  document.getElementById(basename+i).className = "";
				}
			}
			
		}
}

// When you actually rate something //
/*
function rateIt(me){
	if(!rated){
		document.getElementById("rateStatus").innerHTML = document.getElementById("ratingSaved").innerHTML + " :: "+me.title;
		preSet = me;
		rated=1;
		sendRate(me);
		rating(me);
	}
}
*/

// Send the rating information somewhere using Ajax or something like that.
function rateClub(me, type, stars, clubs_id){
	/* alert("Your rating was: "+sel.title); */
  if(me.parentNode.className != "rated"){
    if (stars == 1) {
		  document.getElementById("rateStatus").innerHTML =  me.parentNode.title+" má "+stars+" hvězdičku!";
		}
		if ((stars >= 2) && (stars <= 4)) {
		  document.getElementById("rateStatus").innerHTML =  me.parentNode.title+" má "+stars+" hvězdičky!";
		}
		if (stars == 5) {
		  document.getElementById("rateStatus").innerHTML =  me.parentNode.title+" má "+stars+" hvězdiček!";
		}
		rating(me);
		me.parentNode.className = "rated";

		new Ajax.Updater('ratingtable_placeholder', '/ajax.php', {
		parameters: {
				'module': 'rateclub',
				'rating_kind': type,
				'rating': stars,
				'clubs_id': clubs_id
		},
		onComplete: function( e ) {
			// alert(e.responseText)
		}
	});
	
	//alert("Hodnoceno: "+me.title);
	}
}

