/*
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 = new Array(); // Is the PreSet value onces a selection has been made
var rated = new Array();

// Rollover for image Stars //
function rating(num, rid){
	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[rid]){
		s = num.id.replace("rid_"+rid+"_", ''); // Get the selected star
		a = 0;
		for(i=1; i<=sMax; i++){		
			if(i<=s){
				document.getElementById("rid_"+rid+"_"+i).className = "on";
				document.getElementById("rateStatus_"+rid).innerHTML = num.title;	
				holder = a+1;
				a++;
			}else{
				document.getElementById("rid_"+rid+"_"+i).className = "";
			}
		}
	//}
}

// For when you roll out of the the whole thing //
function off(me, rid){
	if(!rated[rid]){
		if(!preSet[rid]){	
			for(i=1; i<=sMax; i++){		
				document.getElementById("rid_"+rid+"_"+i).className = "";
				document.getElementById("rateStatus_"+rid).innerHTML = me.parentNode.title;
			}
		}else{
			rating(preSet[rid]);
			document.getElementById("rateStatus_"+rid).innerHTML = document.getElementById("ratingSaved_"+rid).innerHTML;
		}
	}
}

// When you actually rate something //
function rateIt(me, rid, uid, tag) {
	if( uid == 0 ) {
		window.location = "/signup.php";
	} else {
//		if(!rated[rid]){
			document.getElementById("rateStatus_"+rid).innerHTML = document.getElementById("ratingSaved_"+rid).innerHTML + " : "+me.title;
			preSet[rid] = me;
			rated[rid]=1;
			sendRate(me, rid, uid, tag);
			rating(me, rid);
//		}
	}
}

// Send the rating information somewhere using Ajax or something like that.
function sendRate(sel, rid, uid, tag){
	score = sel.id.replace("rid_"+rid+"_", '');	

	$.post("/action.php", { action: 'saveRating', rid: rid,	score: score, uid: uid, tag: tag}, 
		function(data) { 
			$("#td_"+rid).html(data);
		});	
}

// loop through and set classes to reflect saved rating
// when mousing out
function batchRateSet(rid,val,txt) {
	for(x=1;x<=sMax;x++) {
		var el = document.getElementById("rid_"+rid+'_'+x);
		if(x <= val)
			el.className='on';
		else
			el.className='';
	}
	document.getElementById('rateStatus_'+rid).innerHTML=txt;
}

