/**
 * NOTE: requires mootools-release-1.11.js
 * SVN $Header$
 **/
 
/* Constants */

GameServices.prototype.VERSION = "$Rev: 12316 $";
GameServices.prototype.DEBUG = false;
GameServices.prototype.ALERT_ERRORS = false;

/* Public Member Variables */

GameServices.prototype.host;
GameServices.prototype.siteID;

/* Private Member Variable */

GameServices.prototype._communityURL;
GameServices.prototype._communityLoginURL;

/* Constructor */

function GameServices(host, siteID) {
    this.host = host;
    this.siteID = siteID;
    this._communityURL = "http://" + host + "/community.php";
    this._communityLoginURL = "http://" + host + "/communitylogin.php";// for testing only without flux
}

/* Private Methods */

GameServices.prototype._sendRequest = function(data) {
	if (!Cookie.get('SiteAuth')) {
		var value = Cookie.get('RtxAuth2407');
		if (!value && this.ALERT_ERRORS) alert("NO FLUX COOKIE PRESENT - CANNOT PASS TO GS"); 
		if (this.DEBUG) alert("SETTING FLUX COOKIE: " + value);
		Cookie.set('SiteAuth', value, {domain: '/', path: '/', duration: 0});
	}
	if (this.DEBUG) alert(Json.toString(data));					
	data.gs_response_format = "json";
	var ajax = new Ajax(	(data.fn == "UserLogin") ? this._communityLoginURL : this._communityURL, 
							{	method: 'post', 
								async: false, 
								data: data}	);
	ajax.request();
	if (this.DEBUG) alert(ajax.response.text);
	var object =  Json.evaluate(ajax.response.text);
	if (object.results.status.success == 0 && this.ALERT_ERRORS) {
		alert("GAME SERVICES SERVER ERROR (fn=" + object.results.passthru.fn + "): " + object.results.status.message + "\n" + object.results.status.extended_info);
	}
	return object;
}

/* Public Methods */

GameServices.prototype.addOrUpdateVoteByURI = function(voteGroupURI, voteURI, voteValue) {
	var data = {	fn: "AddOrUpdateVoteByURI",
					vote_group_uri: voteGroupURI, 
					uri: voteURI, 
					vote_value: voteValue, 
					site_id: this.siteID	};
	return this._sendRequest(data);	
}

GameServices.prototype.getNumberOfVotesPerURIGroup = function(voteGroupURI) {
	var data = {	fn: "GetNumberOfVotesPerURIGroup", 
					vote_group_uri: voteGroupURI,
					site_id: this.siteID	};
	return this._sendRequest(data);	
}

GameServices.prototype.userLogin = function(userName, password) {
	var data = {	fn: "UserLogin",
					user_name: userName,
					password: password,
					site_id: this.siteID	};
	return this._sendRequest(data);
}

