/*
CSS widgets
Much more efficient than previous incarnations
*/
rStar={"Height":15,"Width":17} //Star dimensions
var r_tmp //temporary variable



function Rate(TheDiv,ev){
	
	if(ev.x){ //gets whichever event listener works. It's different for IE and FF
		r_tmp=Math.ceil((ev.x)/rStar.Width); //IE
	}else{
		r_tmp=Math.ceil((ev.layerX)/rStar.Width); //FF
	}

	if(r_tmp)
	{
		TheDiv.style.backgroundPosition='0px -'+(rStar.Height*r_tmp)+'px';//breaks IE..think of something	
	}
}

function w_reset(TheDiv){ //sets the div to the saved rating
	TheDiv.style.backgroundPosition='0px -'+rStar.Height*(TheDiv.getAttribute('rating'))+'px';
}

function SetRate(TheDiv,ev){ 
	if(ev.x){ //gets whichever event listener works. It's different for IE and FF
		r_tmp=Math.ceil((ev.x)/rStar.Width); //IE
	}else{
		r_tmp=Math.ceil((ev.layerX)/rStar.Width); //FF
	}
	TheDiv.setAttribute('rating',r_tmp);
	TheDiv.setAttribute('MyRating',r_tmp);
	TheDiv.className='MeStars';
	
	SubmitRating(r_tmp,TheDiv.getAttribute('rateType'),TheDiv.getAttribute('rateID'));//THIS IS THE AJAX PART!
}

// Binary widget functionality
function sWidget (TheUl){
		TheUl.getElementsByTagName('li')[2].style.display='block';
	}
	
function hWidget (TheUl){
	TheUl.getElementsByTagName('li')[2].style.display='none';
}
function wUP(id){
	TheUl=$(id);
	SubmitRating(5,TheUl.getAttribute('rateType'), TheUl.getAttribute('rateID'))

	//adds ME to the end of the thumbs up and strips ME from the thumbs down
	TheAnchors=TheUl.getElementsByTagName('a') 
	TheAnchors[0].className=TheAnchors[0].className.replace('ME','')+'ME' 
	TheAnchors[1].className=TheAnchors[1].className.replace('ME','')
}
function wDN(id){
	TheUl=$(id);
	SubmitRating(1,TheUl.getAttribute('rateType'), TheUl.getAttribute('rateID'))
	
	//adds ME to the end of the thumbs DOWN and strips ME from the thumbs up
	TheAnchors=TheUl.getElementsByTagName('a') 
	TheAnchors[0].className=TheAnchors[0].className.replace('ME','')
	TheAnchors[1].className=TheAnchors[1].className.replace('ME','')+'ME' 
}

//other binary widget ratings
function HelpfulRateIt(ID,score)
{
	setTimeout("RateItRestore('ReviewWidget"+ID+"');",3000);
	
	var objDiv = $("ReviewWidget"+ID);
	objDiv.getElements('div')[0].style.display="none";
	objDiv.getElements('div')[1].style.display="block";
	SubmitRating(score,5,ID);
}
function ImageRateIt(ID,score)
{
	setTimeout("RateItRestore('ImageVote"+ID+"');",3000);
	
	var objDiv = $("ImageVote"+ID);
	objDiv.getElements('div')[0].style.display="none";
	objDiv.getElements('div')[1].style.display="block";
	SubmitRating(score,4,ID);
}
function RateItRestore(ID)
{
	var objDiv = $(ID);
	objDiv.getElements('div')[1].style.display="none";
	objDiv.getElements('div')[0].style.display="block";
}

//THIS IS THE FUN AJAXY PAGE! It submits the rating to another page.
function SubmitRating(Rating,RateType, RateID){

	new Json.Remote("Ajax/submitRating.asp", {onComplete: SubmitRatingAction,method: 'get'}).send({'rateType': RateType,'rateID':RateID,'rating': Rating, 'retry':'SubmitRating("'+Rating+'","'+RateType+'","'+RateID+'");','rndId':Math.random()});
}

function SubmitRatingAction(response){
	if(response.success)
	{
		//if a new widget state has been sent back.. overrwrite current html with new
		if(response.success.widgetHMTL)
			$(response.success.widgetID).innerHTML = response.success.widgetHMTL;
	}
	else if(response.error)
	{
		if(response.error.errorCode == 1 && response.error.retry)
			CallSignInDiv("Please sign in to rate this item",response.error.retry);
		else
			alert(response.error.errorDescription);
	}		
	else
		alert("An unknown error has occured. The Cook's Compass team has been alerted to the problem.  Please try again later.");
}