/* AJAX Star Rating : v1.0.3 : 2008/05/06 */
/* http://www.nofunc.com/AJAX_Star_Rating/ */

function $S(o) { return((typeof(o)=='object'?o:$(o)).style); }
function agent(v) { return(Math.max(navigator.userAgent.toLowerCase().indexOf(v),0)); }
function abPos(o) { var o=(typeof(o)=='object'?o:$(o)), z={X:0,Y:0}; while(o!=null) { z.X+=o.offsetLeft; z.Y+=o.offsetTop; o=o.offsetParent; }; return(z); }
function XY(e,v) { var o=agent('msie')?{'X':event.clientX+document.body.scrollLeft,'Y':event.clientY+document.body.scrollTop}:{'X':e.pageX,'Y':e.pageY}; return(v?o[v]:o); }

star={};

star.mouse=function(e,o) { if(star.stop || isNaN(star.stop)) { star.stop=0;

	document.onmousemove=function(e) { 
		var n=star.num;
		var p=abPos($('star'+n)), x=XY(e), oX=x.X-p.X, oY=x.Y-p.Y; star.num=o.id.substr(4);

		if(oX<1 || oX>getWidth() || oY<0 || oY>19) { star.stop=1; star.revert(); }
			
		else {
			$S('starCur'+n).width=oX+'px';
			$S('starUser'+n).color='#111';
			$('starUser'+n).innerHTML=roundScore(oX/getWidth()*star.numStars)+' '+star.ratingName;
		}
	};
} };

// Parameters are e=event, o=rating object, f=function to call afterwards. Must accept one single argument which is the rating value. 
star.update=function(e,o,f) { var n=star.num, v=parseFloat($('starUser'+n).innerHTML);
	n=o.id.substr(4); $('starCur'+n).title=v;
	f(v);
};

star.revert=function() { var n=star.num, v=parseFloat($('starCur'+n).title);
	$S('starCur'+n).width=roundScore(v*getWidth()/star.numStars)+'px';
	$('starUser'+n).innerHTML=(v>0? star.label+roundScore(v)+' '+star.ratingName:'Not rated');
	$('starUser'+n).style.color='#888';
	document.onmousemove='';
};

function initRatings()
{
	//getStyle('#star ul.star').width=getWidth()+'px';
	//getStyle('#star li').width=getWidth()+'px';
	//getStyle('#star div.user').left=getWidth()+10+'px';
	setPixelAttribute(getStyle('#star ul.star'),getWidth(),'width');
	setPixelAttribute(getStyle('#star li'),getWidth(),'width');
	setPixelAttribute(getStyle('#star div.user'),getWidth()+10,'left');

	//TODO - find a better way of doing this....
	for(i=0; true; i++)
	{
		try
		{
			var n=i, v=$('starCur'+n).title;
			$('starCur'+n).style.width=(v*getWidth()/star.numStars)+'px';
			if($('star'+n).title=='Rate This!')
			{
				$('starUser'+n).innerHTML=(v>0? star.label+roundScore(v) +' '+star.ratingName:'Not rated');
			}
		}catch(e){
			if(i>2)
				break;
		}			
	}

};

function roundScore(value)
{
	return (Math.round(value/star.segmentSize) * star.segmentSize).toFixed(star.decimalPlaces);
};

function getStyle(sText) 
{ 
  for(n=0;n<document.styleSheets.length;n++)
  {
	  var S = document.styleSheets[n] 
	  var rules = S.rules||S.cssRules // IE || others 
	  for(var r=0;r<rules.length;r++)
	  {
		  if(rules[r].selectorText.toLowerCase()==sText.toLowerCase()) 
			  return rules[r].style
	  }
  }
  return null; 
};

function getWidth()
{
	//64=width of 5 star image. If you change the background image you will need to change this value. 
	//alert('numStars='+star.numStars + ' so width='+64*star.numStars/5);
	return 64*star.numStars/5;
};

function setPixelAttribute(passedInElement,width,attribute)
{
	eval('passedInElement.'+attribute+'=width;');
	if(eval('passedInElement.'+attribute) != width)
	{
		eval('passedInElement.'+attribute+'=width+\'px\'');
	}
	
}

star.num=0;

//Defaults... you can reset these in the calling HTML if you wish to use different values (eg to rate as percentages etc... 
star.numStars=5;
star.segmentSize=0.5;
star.decimalPlaces=1;
star.ratingName='stars';
star.label='Your rating: ';
