/* 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 $(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); }
function nl2br (str) {var breakTag = '<br />'; return (str + '').replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, '$1'+ breakTag +'$2');}
function showWindow(url, width, height){Window = window.open(url,'','scrollbars=1, toolbar=0, resizable=0, menubar=0, top=10, left=10, width='+width+', height='+height);}

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.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)');
};

function addComment(){
	if (uid != ''){
	  getAddComment();    
    }
	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.');
	}
}

//AJAX

var xmlHttp4;
var xmlHttp5;
var xmlHttp6;

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;
	}
  }
}

function getAddComment(){
  xmlHttp5=GetXmlHttpObject();
  if (xmlHttp5==null){
    alert ("Your browser does not support AJAX!");
    return;
  } 
  var comment = $('comment').value;
  //alert(nl2br(comment));
  var url="comments.php";
  url=url+"?action=add";
  url=url+"&uid="+uid;
  url=url+"&gid="+gid;
  url=url+"&comment="+nl2br(comment);
  url=url+"&sid="+Math.random();
  xmlHttp5.onreadystatechange=getAddCommentStateChanged;
  xmlHttp5.open("GET",url,true);
  xmlHttp5.send(null);
} 

function getAddCommentStateChanged() { 
  if (xmlHttp5.readyState==4){ 
    var response = xmlHttp5.responseText.split("*split*");
	if (response[0] == '0'){
	   alert(response[1]);
	}
	else{
	   $('user-comments').innerHTML=response[1];
	   $('comment-count').innerHTML=response[2];
	}
  }
}

function getDeleteComment(id){
  xmlHttp6=GetXmlHttpObject();
  if (xmlHttp6==null){
    alert ("Your browser does not support AJAX!");
    return;
  } 
  var url="comments.php";
  url=url+"?action=delete";
  url=url+"&id="+id;	
  url=url+"&uid="+uid;
  url=url+"&gid="+gid;	   
  url=url+"&sid="+Math.random();
  xmlHttp6.onreadystatechange=getDeleteCommentStateChanged;
  xmlHttp6.open("GET",url,true);
  xmlHttp6.send(null);
} 

function getDeleteCommentStateChanged() { 
  if (xmlHttp6.readyState==4){ 
    var response = xmlHttp6.responseText.split("*split*");
	if (response[0] == '0'){
	   alert(response[1]);
	}
	else{
	   $('user-comments').innerHTML=response[1];
	   if ($('comment-count')){$('comment-count').innerHTML=response[2]};
	}
  }
}
