/* AJAX Star Rating : v1.0.3 : 2008/05/06 */
/* http://www.nofunc.com/AJAX_Star_Rating/ */
/* modified a little by Bogdan DEAKY - Bluemind Software 2009/12 and impostant mods 2010/05 */

function getInternetExplorerVersion() {
    var rv = -1; // Return value assumes failure.
    if (navigator.appName == 'Microsoft Internet Explorer') {
        var ua = navigator.userAgent;
        var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
        if (re.exec(ua) != null)
            rv = parseFloat(RegExp.$1);
    }
    return rv;
}

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.documentElement.scrollLeft,'Y':event.clientY+document.documentElement.scrollTop}:{'X':e.pageX,'Y':e.pageY}; return(v?o[v]:o); }

var rating=0;

star={};

star.mouse=function(e,o) { 
  if(star.stop || isNaN(star.stop)) { 
    star.stop=0;
	var n=o.id.substr(4);
    document.onmousemove=function(e) { 
	  var p=abPos($('star'+n)), x=XY(e), oX=x.X-p.X, oY=x.Y-p.Y;
	  //e = e || window.event;
      //$('rating').innerHTML = oX+'/'+oY+'/'+e.clientY + '/'+document.body.scrollTop;
	  if(oX<1 || oX>84 || oY<0 || oY>19) { 
	    star.stop=1; star.revert(o); 
	  }
	  else {
        $S('starCur'+n).width=oX+'px';
		rating=Math.round(oX/84*100);
		$('starUser'+n).innerHTML=' &nbsp;(your rating: '+rating+'%)';
	  }
	};
  } 
};

/*
star.mouse=function(e,o) { 
  if(star.stop || isNaN(star.stop)) { 

	var n=o.id.substr(4);
	var pos = x=XY(e), p=abPos($('star'+n)), oX=x.X-p.X;
	//oY=x.Y-p.Y;
    $('starUser'+n).innerHTML = pos.X+'/'+oX;

			$S('starCur'+n).width=oX+'px';
			rating=Math.round(oX/84*100);
			$('starUser'+n).innerHTML=' &nbsp;(your rating: '+rating+'%)';
} };
*/



star.update=function(e,o) {
	var n=o.id.substr(4);
	if (uid != ''){
	  n=o.id.substr(4); $('starCur'+n).title=rating;
	  getRateGame(uid, gid, rating);    
    }
	else{
	alert('You have to login in order to submit game ratings and comments! \r\nRegistering a new account is easy. Click the "REGISTER" button on top of the page.');
	}
};

star.revert=function(o) { 
document.onmousemove='';
  var n=o.id.substr(4);
  var avg_rating=parseInt($('starAvg'+n).title); 
  var cur_rating=parseInt($('starCur'+n).title);

  $S('starCur'+n).width=Math.round(cur_rating*84/100)+'px';
  $('starUser'+n).innerHTML=(cur_rating>0?' &nbsp;(your rating: '+Math.round(cur_rating)+'%)':' &nbsp;(you did not rate this game yet)');
};

/*
star.revert=function(o) { 
	alert('revert');
    //document.onmousemove='';
	var n=o.id.substr(4);
	var cur_rating=parseInt($('starCur'+n).title);

	//$S('starCur'+n).width=Math.round(cur_rating*84/100)+'px';
	//$('starUser'+n).innerHTML=(cur_rating>0?' &nbsp;(your rating: '+Math.round(cur_rating)+'%)':' &nbsp;(you did not rate this game yet)');
};
*/

var xmlHttp4;

function getRateGame(uid, gid, rating){
  xmlHttp4=GetXmlHttpObject();
  if (xmlHttp4==null){
    alert ("Your browser does not support AJAX!");
    return;
  } 
  var url="rate_game.php";
  url=url+"?uid="+uid;
  url=url+"&gid="+gid;
  url=url+"&rating="+rating;
  url=url+"&sid="+Math.random();
  xmlHttp4.onreadystatechange=getRateGameStateChanged;
  xmlHttp4.open("GET",url,true);
  xmlHttp4.send(null);
} 

function getRateGameStateChanged() { 
  if (xmlHttp4.readyState==4){ 
    var response = xmlHttp4.responseText.split("*");
	if (response[0] == '0'){
	   alert(response[1]);
	}
	else{
	   rating = response[1];
	   $S('starAvg0').width=Math.round(rating*84/100)+'px';
	   $('avg_rating').innerHTML=(rating>0?(rating)+'%':'You can be the first to rate this game!');
	   $('starAvg0').title=rating;
	}
  }
}
