
var baseURL = "http://www.Wooglie.com/scripts/ajax/";

function GetUnity() {
	if (typeof unityObject != "undefined") {
		return unityObject.getObjectById("unityPlayer");
	}
	return null;
}	
        
function HelloWooglie(){                    
    if($('reviewDiv')){$('reviewDiv').style.background = "rgb(0,255,0)";}
    setTimeout("replyPlayer()",1000);
    
}
function replyPlayer(){
    if (typeof unityObject != "undefined") {
		unityObject.getObjectById("unityPlayer").SendMessage("Wooglie", "WoogliePong","Hello back!");   
	}
}
          
 function VoteGame(game, rate){
	new Ajax.Request(baseURL+"general.php",
	{
	method: "post",
	contentType: "application/x-www-form-urlencoded",
	encoding: "UTF-8",
	parameters: {action: 1, vote: rate, gameID: game},
	onSuccess: function( t) {
		eval(t.responseText);
	},
	on0: function( t) {		 	
	 	//Failed	
		 alert("There was a connection problem while trying to rate the game.");	 
	}
	}); 	
}

function countCaps(myText)  
{  
   	var regexp = /[A-Z]/g;  
   	var text = myText;
   	var matchList = text.match(regexp);     
   	var percentage = matchList.length/text.length * 100 ;      
	return percentage;   
}  

function LoadNextReviewPage(gameID){
    var wantedPage = parseInt($('revPage').innerHTML) + 1;
    var lastPage =  parseInt($('revPages').innerHTML);
    if(wantedPage>=1 && wantedPage<=lastPage)
        GetReviewPage(wantedPage, gameID);
}
function LoadPreviousReviewPage(gameID){
    var wantedPage = parseInt($('revPage').innerHTML) - 1;
    var lastPage =  parseInt($('revPages').innerHTML);
    if(wantedPage>=1 && wantedPage<=lastPage)
        GetReviewPage(wantedPage, gameID);
}

function GetReviewPage(page, gameID){
    new Ajax.Request(baseURL+"general.php",
	{
	method: "post",
	contentType: "application/x-www-form-urlencoded",
	encoding: "UTF-8",
	parameters: {action: 6, page: page, gameID: gameID},
	onSuccess: function( t) {
		eval(t.responseText);
	},
	on0: function( t) {		 	
	 	//Failed	
		  alert("There was a connection problem while trying to post your comment.");	 
	}
	}); 
}

function postReview(revTitle, myComment, gameID){

	/*
	//if(countCaps(myName)>=50){
	//	myName = myName.toLowerCase(); 
	//}
	//if(countCaps(myComment)>=30){
	//	myComment = myComment.toLowerCase(); 
	//}
	*/
	
	new Ajax.Request(baseURL+"general.php",
	{
	method: "post",
	contentType: "application/x-www-form-urlencoded",
	encoding: "UTF-8",
	parameters: {action: 2, revTitle: revTitle, myComment: myComment, gameID: gameID},
	onSuccess: function( t) {
		eval(t.responseText);
	},
	on0: function( t) {		 	
	 	//Failed	
		  alert("There was a connection problem while trying to post your comment.");	 
	}
	}); 	
}

function flagComment(commentID){
	new Ajax.Request(baseURL+"general.php",
	{
	method: "post",
	contentType: "application/x-www-form-urlencoded",
	encoding: "UTF-8",
	parameters: {action: 3, commentID: commentID},
	onSuccess: function( t) {
		eval(t.responseText);
	},
	on0: function( t) {		 	
	 	//Failed	
		  alert("There was a connection problem while trying to flag the comment.");	 
	}
	}); 
}

var playingGame = 0;

function sendSignal(gameID){
		playingGame=gameID;
		new Ajax.Request(baseURL+"general.php",
		{
		method: "post",
		contentType: "application/x-www-form-urlencoded",
		encoding: "UTF-8",
		parameters: {action: 4, gameID: gameID},
		onSuccess: function( t) {
			eval(t.responseText);
		},
		on0: function( t) {		 	
		 	//Failed	
			 // alert("There was a connection problem while trying to flag the comment.");	 
		}
		});	
}

function exitPage(){
	if(playingGame>0){
		sendSignal(playingGame);
	}
}


///////
function checkTotalGameHits(devID){
		new Ajax.Request(baseURL+"general.php",
		{
		method: "post",
		contentType: "application/x-www-form-urlencoded",
		encoding: "UTF-8",
		parameters: {action: 5, devID: devID},
		onSuccess: function( t) {
			eval(t.responseText);
		}
		});	
}

function startCounter(newHits){
	var oldValue = parseInt($('totalGames').innerHTML);
	var diff = newHits-oldValue;
	var persecond = parseInt(diff/6);
	var refreshRate=1000;

	if(persecond<=1){
		if(persecond*3>=1){
			persecond=1;
			refreshRate=3000;
		}else{
			persecond=1;
			refreshRate=6000;
		}				
	}else if(persecond>=4){
		persecond = persecond/2;
		refreshRate =500;
	}
	setInterval('raiseCounter('+persecond+')', refreshRate);
}

function raiseCounter(amount){
	$('totalGames').innerHTML = amount+parseInt($('totalGames').innerHTML);
}
/////



