/*
Author: Addam M. Driver
Date: 10/31/2006
*/

var sMax;	// Isthe maximum number of stars
var holder; // Is the holding pattern for clicked state
var preSet; // Is the PreSet value onces a selection has been made
var rated;



function setImageOn(me,restid,val)
 {	 val=Math.ceil(val);
	 var txt='';
	 var classname;
	 var titletxt;
	 var title;
	 for(var i=1;i<=5;i++) {
		 if(i==1) { titletxt="Unacceptable"; }
		 else if(i==2) { titletxt="Below expectation"; }
		 else if(i==3) { titletxt="Average"; }
		 else if(i==4) { titletxt="Good"; }
		 else if(i>4) { titletxt="Outstanding"; }
		if(i<=val)
		 { classname="on";
		 title=titletxt }
		 else { classname="off"; }
	txt +='<a onclick="rateIt('+this+',\''+restid+'\')" id="'+restid+'_'+i+'" title="'+titletxt+'" class="'+classname+'" onmouseover="rating('+this+',\''+restid+'\')" onmouseout="off('+this+',\''+restid+'\')"></a>'
	 }
	 document.getElementById('rateMe'+restid).innerHTML=txt;
	 document.getElementById('rateStatus'+restid).innerHTML='Rated:' +title;
	//alert( document.getElementById('rateMe'+restid).innerHTML);
 }

// Rollover for image Stars //
function rating(num,restid){
	sMax = 0;	// Isthe maximum number of stars
	for(n=0; n<num.parentNode.childNodes.length; n++){
		if(num.parentNode.childNodes[n].nodeName == "A"){
			sMax++;	
		}
	}
	
	if(!rated){
		//s = num.id.replace("_", ''); // Get the selected star
		s1 = num.id.split("_"); // Get the selected star
		s=s1[1];
		a = 0;
		for(i=1; i<=sMax; i++){		
			if(i<=s){
				document.getElementById(restid+"_"+i).className = "on";
				document.getElementById("rateStatus"+restid).innerHTML = num.title;	
				holder = a+1;
				a++;
			}else{
				document.getElementById(restid+"_"+i).className = "";
			}
		}
	}
}

// For when you roll out of the the whole thing //
function off(me,restid,val){
	if(!rated){
		if(!preSet){	
			for(i=1; i<=sMax; i++){		
			   if(i<=val) {
				   	document.getElementById(restid+"_"+i).className = "on";
			   }
			   else {
				   	document.getElementById(restid+"_"+i).className = "";
			   }
			
				document.getElementById("rateStatus"+restid).innerHTML = me.parentNode.title;
			}
		}else{
			rating(preSet);
			document.getElementById("rateStatus"+restid).innerHTML = document.getElementById("ratingSaved"+restid).innerHTML;
		}
	}
}

// When you actually rate something //
function rateIt(me,restid){
	if(!rated){
	  	s1 = me.id.split("_"); // Get the selected star
		s=s1[1];
	 var res=  callajax('AJAX_Star_Vote.php?vote='+(s/100)+'&rest='+restid)
	// document.getElementById("rateStatus"+restid).innerHTML = document.getElementById("ratingSaved"+restid).innerHTML + " :: "+me.title;
		//preSet = me;
		//rated=1;
	  setImageOn(me,restid,res)
		sendRate(me);
		
		//rating(me);
	}
}

// Send the rating information somewhere using Ajax or something like that.
function sendRate(sel){
	alert("Your rating was: "+sel.title);
}

function callajax(url)
{   
  if (window.XMLHttpRequest) {              
    AJAX=new XMLHttpRequest();              
  } else {                                  
    AJAX=new ActiveXObject("Microsoft.XMLHTTP");
  }
  if (AJAX) {
     AJAX.open("GET", url, false);                             
     AJAX.send(null);
	 return AJAX.responseText;                                         
  } else {
     return false;
  }                                             

}


