/*
CSS widgets
Much more efficient than previous incarnations
*/

	rStar={"Height":15,"Width":17} //Star dimensions
var r_tmp //temporary variable

function StartUp(){
	a=document.getElementsByTagName('div');

	for(i=0;i<a.length;i++){
		if(a[i].className=="Stars" || a[i].className=="MeStars"){ //if it's a widget...
			w_reset(a[i]);
		}
	}

}

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
	}

	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-findPosX(TheDiv))/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=document.getElementById(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=document.getElementById(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' 
}

//BELOW is all copied from rating 8

//THIS IS THE FUN AJAXY PAGE! It submits the rating to another page.
function SubmitRating(Rating,RateType, RateID) {

	//get XmlHttp object and set handler
	if (navigator.userAgent.indexOf("Opera")>=0)
	{
		alert("Sorry, we don't currently support Opera"); 
		return;
	}
	
	if (navigator.userAgent.indexOf("MSIE")>=0)
	{ 
		var strName="Msxml2.XMLHTTP";
		if (navigator.appVersion.indexOf("MSIE 5.5")>=0)
		{
			strName="Microsoft.XMLHTTP";
		} 
		try
		{ 
			RateXmlHttp=new ActiveXObject(strName);
			RateXmlHttp.onreadystatechange=RateResult;
		} 
		catch(e)
		{ 
			alert("Error. Scripting for ActiveX might be disabled"+e); 
			return;
		} 
	} 
	else
		if (navigator.userAgent.indexOf("Mozilla")>=0)
		{
			RateXmlHttp=new XMLHttpRequest();
			RateXmlHttp.onload=RateResult;
			RateXmlHttp.onerror=RateResult;
		}

	
	//show_div(document.getElementById(RatedId));//This turns on the floating div, positioned according to the input form
	SelectedItem=-1;//Sets the selected item to off the page
	
	
	var url="AJAX/RateIt.asp";
	url=url+"?RateType="+RateType;
	url=url+"&RateID="+RateID;
	url=url+"&Rating="+Rating;
	url=url+"&retryScript=SubmitRating("+Rating+","+RateType+","+RateID+");" 
	url=url+"&sid="+Math.random();
	
	//document.location=url;
	//return;
	
	RateXmlHttp.open("GET",url,true);

	RateXmlHttp.send(null);
} 

function RateResult()
{
	if (RateXmlHttp.readyState==4 || RateXmlHttp.readyState=="complete")
	{
		if(RateXmlHttp.responseText !="Success")
			if(RateXmlHttp.responseText.substring(0,6) == "Error1")
			{
				var iChar = RateXmlHttp.responseText.indexOf("retry=");
				if(iChar > 0)
					CallSignInDiv('Please sign in to rate this item',RateXmlHttp.responseText.substring(iChar+6,RateXmlHttp.responseText.length));
				else
					CallSignInDiv('Please sign in to rate this item',null);
			}
			else
				alert(RateXmlHttp.responseText);
			
		//xmlHttp = null;
	}
} 
