var starGo=0;

//Defined in Prototype
/*function $(v) {
	return(document.getElementById(v));
}*/

function $S(v) {
	return(document.getElementById(v).style);
}
function agent(v) {
	return(Math.max(navigator.userAgent.toLowerCase().indexOf(v),0));
}
function absPos(o) {
	var r= {
		x:o.offsetLeft,y:o.offsetTop
	};
	
	if(o.offsetParent) {
		var v=absPos(o.offsetParent);
		r.x+=v.x;
		r.y+=v.y;
	}
	return(r);
}
function createXMLHttpRequest() {
  var xmlhttplocal;

  try {
    xmlhttplocal= new ActiveXObject("Msxml2.XMLHTTP")
  } catch (e) {
    try {
      xmlhttplocal= new ActiveXObject("Microsoft.XMLHTTP")
    } catch (E) {
      xmlhttplocal=false;
    }
  }

  if (!xmlhttplocal && typeof XMLHttpRequest!='undefined') {
    try {
      var xmlhttplocal = new XMLHttpRequest();
    } catch (e) {
      var xmlhttplocal=false;
      alert('Couldn\'t create xmlhttp object');
    }
  }
  
  return(xmlhttplocal);
}

function xy(e,v) {
	return(v?(agent('msie')?event.clientY+document.body.scrollTop:e.pageY):(agent('msie')?event.clientX+document.body.scrollTop:e.pageX));
}


function callback(serverData, serverStatus) {
  starRevert();
  if (serverData == 'f') {
    $('starMessage').innerHTML = 'You cannot rate this work!';
  } else {
    rating = Math.round(parseInt(serverData));
    
    $('starCurr').title = rating;
    $S('starCurr').width=Math.round(rating*84.0/100)+'px';
    $('starUser').innerHTML=(rating>0 ? rating+'%' : '');
    $('starNumVotes').innerHTML=parseInt($('starNumVotes').innerHTML) + 1;
    
    $('starMessage').innerHTML = 'Thank you for rating!';
  }
  $('star').onmousemove='';
  $('star').onclick='';
  document.onmousemove='';
  
  $S('star').cursor='default';
  $S('starCurr').cursor='default';
  $S('starUser').color='#111';
  $S('starUser2').color='#111';
}

function starUpdate(e,o,w) {
	var v=parseFloat($('starUser').innerHTML);
  $('starMessage').innerHTML = 'Voting...';
	req=createXMLHttpRequest();
  req.onreadystatechange = function() {
    if (req.readyState==4 || req.readyState=="complete") {
      callback(req.responseText, req.status);
    }
  }
	req.open('GET','inc/rate_ajax.php?work='+w+'&rating='+v,false);
	req.send(null);
}

function starRevert() {
	var v=parseFloat($('starCurr').title);
	
	$S('starCurr').width=Math.round(v*84.0/100)+'px';
	$('starUser').innerHTML=(v>0?Math.round(v)+'%':'');
	
	$('starUser').style.color='#888';
	document.onmousemove='';
}

function starCurr(e,o) {
	function starMove(e) {
	
    if (agent('msie')) {
      if (!e) e = event;
      
      eX = e.x
      eY = e.y
    } else {
      var p=absPos($('star'));
      var eX=xy(e)-p.x, eY=xy(e,1)-p.y;
    }
    
		if(eX<1 || eX>84 || eY<0 || eY>19) {
			starGo=0;
			starRevert();
		} else {
			$S('starCurr').width=eX+'px';
			$S('starUser').color='#111';
			$('starUser').innerHTML=Math.round(eX/84*100)+'%';
		}
	}
	
	if(starGo==0) {
		starGo=1;
		document.onmousemove=starMove;
	}
}