
/* AJAX Star Rating : v1.0.3 : 2008/05/06 */
/* http://www.nofunc.com/AJAX_Star_Rating/ */

/* UPDATE : zss.2009.08.03 */
/*
function $(v,o) { return((typeof(o)=='object'?o:document).getElementById(v)); }*/
//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); }

// Event ABS position from top/left (by zss)
function getXY(e) {
	var scrOfX = 0, scrOfY = 0;
	if( agent('msie') ) {
		if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
			scrOfY = event.clientY + document.body.scrollTop;
			scrOfX = event.clientX + document.body.scrollLeft;
		} else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
			scrOfY = event.clientY + document.documentElement.scrollTop;
			scrOfX = event.clientX + document.documentElement.scrollLeft;
		}
	}
	else {
		scrOfY = e.pageY;
		scrOfX = e.pageX;	
	}
	return {'X':scrOfX, 'Y':scrOfY};
}

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);
		var p=abPos($('star'+n)), x=getXY(e), oX=x.X-p.X, oY=x.Y-p.Y; star.num=o.id.substr(4);
		
		if(oX<1 || oX>84 || oY<0 || oY>19) { star.stop=1; star.revert(); }
		
		else {
			if (star.is_ajax_running_on != n || isNaN(star.is_ajax_running_on)) {
				//$S('starCur'+n).width=oX+'px';
				//$S('starUser'+n).color='#111';
				/*alert(oX);
				alert(x.X);
				alert(x.Y);				
				alert(p.X);
				alert(p.Y);*/
				$('starCur'+n).setStyle({
					width: oX+'px'
				});
				$('starUser'+n).setStyle({
					color: '#111'
				});				
				$('starUser'+n).innerHTML=Math.round(oX/84*100)+'%';
			}
		}
	};
} };

star.update=function(e,o,table,id) {	
	var n=star.num, v=parseInt($('starUser'+n).innerHTML);
	n=o.id.substr(4); $('starCur'+n).title=v;

	//req=new XMLHttpRequest(); req.open('GET','/AJAX_Star_Vote.php?vote='+(v/100),false); req.send(null); 

	//$S('star'+n).cursor='default';
	$('star'+n).setStyle({
		cursor: 'default'
	});	
	o.onmousedown = '';
	star.is_ajax_running_on = n;
	
	ajax("nibble", "rateit", [table, id, n, v, name], function(response) {
		if (response) {
			var div_content = $('rateit_star_'+n);
			div_content.update(response);
			//star.stop=1;
		}
	});   

};

star.revert=function() { var n=star.num, v=parseInt($('starCur'+n).title);

	//$S('starCur'+n).width=Math.round(v*84/100)+'px';
	$('starCur'+n).setStyle({
		width: Math.round(v*84/100)+'px'
	});

	$('starUser'+n).innerHTML=(v>0?Math.round(v)+'%':'');
	$('starUser'+n).style.color='#888';
	
	document.onmousemove='';

};

star.num=0;
