
//----------------------------------------------------------------------------------------------------------------------

function FBService() {
	this.httpRequest = null;
	
	this.onError = null;
	this.onSuccess = null;	
}	

//----------------------------------------------------------------------------------------------------------------------

FBService.prototype._doRequest = function( url, type, params ) {
	this.showProgress();

    this.httpReq = new XMLHttpRequest();
	
    var self = this;
    this.httpReq.onreadystatechange = function() { 
		self._readyStateChanged(); 
	};

	// Default to GET HTTP request if none is provided.
	if ( type == null ) {
		type = "GET";
	}

	if ( params == null ) {
		params = "";
	}
	
	if(type == "GET")
	{
		if(params != "")
			this.httpReq.open( type, url + "?" + params, true );	
		else
			this.httpReq.open( type, url, true );	
		this.httpReq.send( "" );
	}

	if(type == "POST")
	{
		this.httpReq.open( type, url, true );			
    
		this.httpReq.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		//this.httpReq.setRequestHeader("Content-length", params.length);
		//this.httpReq.setRequestHeader("Connection", "close");
		this.httpReq.send(params);
	}
}

//----------------------------------------------------------------------------------------------------------------------

FBService.prototype._readyStateChanged = function() {
    // Complete request?
    if ( this.httpReq.readyState == 4 ) {
        // Attempt to get response status.
        var responseStatus = null;
        try {
            responseStatus = this.httpReq.status;
        } 
		catch (noStatusException) {
			// Alert the user that something has gone wrong.
			alert( StringTable.Code.fbServiceNoStatusAlert );
		}
		
		// Check response status.
		if ( responseStatus == 200 ) {
			var res = null;
			
			// If response doesn't contain xml, forward text.
			res = this.httpReq.responseText;

			// Not needed anymore.
			this.httpReq = null;
			this.handleSuccessResponse.call( this, res );
		} else {
			this.handleErrorResponse.call( this, responseStatus );
		}
    }
}


//======================================================================================================================
//======================================================================================================================
//======================================================================================================================


FBService.prototype.login = function( username, password ){
	this.username = username;
	this.password_md5 = MD5(password); //create pass hash

	// Build URL and do the request.
	var url = "http://" + FBSERVICE_URL + "/api.php?cmd=login";
	var params = "user=" + escape(this.username) + "&pass=" + escape(this.password_md5);
	this._doRequest( url, "POST", params );
}


//======================================================================================================================
//======================================================================================================================
//======================================================================================================================


FBService.prototype.getUpdatesList = function(){

	// Build URL and do the request.
	var url = "http://" + FBSERVICE_URL + "/api.php?cmd=getUpdatesList";
	var params = "user=" + escape(LOGIN_USER) + "&sessid=" + escape(LOGIN_SESSION_ID) + "&items_per_page=" + UPDATES_LIST_ITEMS_LIMIT;
	this._doRequest( url, "POST", params );
}


//======================================================================================================================
//======================================================================================================================
//======================================================================================================================


FBService.prototype.getMessages = function(show, page){

	// Build URL and do the request.
	var url = "http://" + FBSERVICE_URL + "/api.php?cmd=getMessages";
	var params = "user=" + escape(LOGIN_USER) + "&sessid=" + escape(LOGIN_SESSION_ID) + "&show=" + show + "&page=" + page + "&items_per_page=" + MESSAGES_ITEMS_PER_PAGE;
	this._doRequest( url, "POST", params );
}

//----------------------------------------------------------------------------------------------------------------------

FBService.prototype.getMessage = function(show, id){

	// Build URL and do the request.
	var url = "http://" + FBSERVICE_URL + "/api.php?cmd=getMessage";
	var params = "user=" + escape(LOGIN_USER) + "&sessid=" + escape(LOGIN_SESSION_ID) + "&show=" + show + "&id=" + id;
	this._doRequest( url, "POST", params );
}

//----------------------------------------------------------------------------------------------------------------------

FBService.prototype.messageDelete = function(show, id){

	// Build URL and do the request.
	var url = "http://" + FBSERVICE_URL + "/api.php?cmd=messageDelete";
	var params = "user=" + escape(LOGIN_USER) + "&sessid=" + escape(LOGIN_SESSION_ID) + "&show=" + show + "&id=" + id;
	this._doRequest( url, "POST", params );
}

//----------------------------------------------------------------------------------------------------------------------

FBService.prototype.messageMarkUnread = function(id){

	// Build URL and do the request.
	var url = "http://" + FBSERVICE_URL + "/api.php?cmd=messageMarkUnread";
	var params = "user=" + escape(LOGIN_USER) + "&sessid=" + escape(LOGIN_SESSION_ID) + "&id=" + id;
	this._doRequest( url, "POST", params );
}

//----------------------------------------------------------------------------------------------------------------------

FBService.prototype.messageFlag = function(show, id){

	// Build URL and do the request.
	var url = "http://" + FBSERVICE_URL + "/api.php?cmd=messageFlag";
	var params = "user=" + escape(LOGIN_USER) + "&sessid=" + escape(LOGIN_SESSION_ID) + "&show=" + show + "&id=" + id;
	this._doRequest( url, "POST", params );
}

//----------------------------------------------------------------------------------------------------------------------

FBService.prototype.messageUnflag = function(show, id){

	// Build URL and do the request.
	var url = "http://" + FBSERVICE_URL + "/api.php?cmd=messageUnflag";
	var params = "user=" + escape(LOGIN_USER) + "&sessid=" + escape(LOGIN_SESSION_ID) + "&show=" + show + "&id=" + id;
	this._doRequest( url, "POST", params );
}

//----------------------------------------------------------------------------------------------------------------------

FBService.prototype.messageSend = function(reply, subject, body){

	// Build URL and do the request.
	var url = "http://" + FBSERVICE_URL + "/api.php?cmd=messageSend";
	var params = "user=" + escape(LOGIN_USER) + "&sessid=" + escape(LOGIN_SESSION_ID) + "&reply=" + reply + "&subject=" + escape(subject) + "&body=" + escape(body);
	this._doRequest( url, "POST", params );
}


//======================================================================================================================
//======================================================================================================================
//======================================================================================================================


FBService.prototype.setGame = function(subject, chip_val){

	// Build URL and do the request.
	var url = "http://" + FBSERVICE_URL + "/api.php?cmd=setGame";
	var params = "user=" + escape(LOGIN_USER) + "&sessid=" + escape(LOGIN_SESSION_ID) + "&subject=" + escape(subject) + "&chip_val=" + escape(chip_val);
	this._doRequest( url, "POST", params );
}

//----------------------------------------------------------------------------------------------------------------------

FBService.prototype.getGameOptions = function(id){

	// Build URL and do the request.
	var url = "http://" + FBSERVICE_URL + "/api.php?cmd=getGameOptions";
	var params = "user=" + escape(LOGIN_USER) + "&sessid=" + escape(LOGIN_SESSION_ID) + "&id=" + id;
	this._doRequest( url, "POST", params );
}

//----------------------------------------------------------------------------------------------------------------------

FBService.prototype.setGameOptions = function(id, option1, option2, option3, option4, option5){

	// Build URL and do the request.
	var url = "http://" + FBSERVICE_URL + "/api.php?cmd=setGameOptions";
	var params = "user=" + escape(LOGIN_USER) + "&sessid=" + escape(LOGIN_SESSION_ID) + "&id=" + id + "&option1=" + escape(option1) + "&option2=" + escape(option2) + "&option3=" + escape(option3) + "&option4=" + escape(option4) + "&option5=" + escape(option5);
	this._doRequest( url, "POST", params );
}

//----------------------------------------------------------------------------------------------------------------------

FBService.prototype.publishGame = function(id){

	// Build URL and do the request.
	var url = "http://" + FBSERVICE_URL + "/api.php?cmd=publishGame";
	var params = "user=" + escape(LOGIN_USER) + "&sessid=" + escape(LOGIN_SESSION_ID) + "&id=" + id;
	this._doRequest( url, "POST", params );
}

//----------------------------------------------------------------------------------------------------------------------

FBService.prototype.searchFriends = function(search){

	// Build URL and do the request.
	var url = "http://" + FBSERVICE_URL + "/api.php?cmd=searchFriends";
	var params = "user=" + escape(LOGIN_USER) + "&sessid=" + escape(LOGIN_SESSION_ID) + "&search=" + escape(search) + "&items_per_page=" + FRIENDS_LIST_ITEMS_LIMIT;
	this._doRequest( url, "POST", params );
}

//----------------------------------------------------------------------------------------------------------------------

FBService.prototype.inviteFriendsToGame = function(id, friends){

	// Build URL and do the request.
	var url = "http://" + FBSERVICE_URL + "/api.php?cmd=inviteFriendsToGame";
	var params = "user=" + escape(LOGIN_USER) + "&sessid=" + escape(LOGIN_SESSION_ID) + "&id=" + id + "&friends=" + escape(friends);
	this._doRequest( url, "POST", params );
}


//======================================================================================================================
//======================================================================================================================
//======================================================================================================================


FBService.prototype.cancel = function() {
	this.showProgress( false );
}

//----------------------------------------------------------------------------------------------------------------------

FBService.prototype.showProgress = function( show ) {
	if (show == null) 
		show = true;

	Helper.show( "loader_popup", show );			
}

//----------------------------------------------------------------------------------------------------------------------

FBService.prototype.handleSuccessResponse = function( arg ) {
	this.showProgress( false );

	// Eval the data.
	var response = eval( "(" + arg  + ")" );

    // Feed fetched and parsed successfully.
	if ( this.onSuccess )
    	this.onSuccess.call( this, response );	
}

//----------------------------------------------------------------------------------------------------------------------

FBService.prototype.handleErrorResponse = function( status ) {
	this.showProgress( false );

	if ( this.onError )
    	this.onError.call( this, status );
}

//----------------------------------------------------------------------------------------------------------------------

