/**
 * @namespace
 * Class WSProxy
 * 
 * This class is intended to encapsulate all the methods that communicate with the server side
 * of the LayersOS. All the requests, logic and callback functions are contained in the methods
 * of this class.
 * <br /><br />
 * Created on 04/03/2009
 * 
 * @class WSProxy
 *
 * @property {Object} referal Who is the responsible for resolve the calls that
 * this object will make. 
 * @author "Pablo Casado (pablo.casado@layers.com)"
 * @version <1.0>
 *  
 */
function WSProxy(referal, osidContext){
  console.info("osidContext at WSProxy");
  console.info(osidContext);
  
	this._referal = referal;
	// Edita
	if(typeof(osidContext) == "undefined" ){
		WSProxy.prototype._osidContext = "";
	}else{
		WSProxy.prototype._osidContext = osidContext;
	}
	
	this._requestsArray = new Array();
}


WSProxy.prototype._referal;
WSProxy.prototype._requestsArray;
// Edita
WSProxy.prototype._osidContext;


WSProxy.prototype.createDOM = function(xmlString){

	// Mozilla and Netscape browsers
    if (document.implementation.createDocument) {
        var parser = new DOMParser()
        doc = parser.parseFromString(xmlString, "text/xml")
    // MSIE
    } else if (window.ActiveXObject) {
        doc = new ActiveXObject("Microsoft.XMLDOM")
        doc.async="false"
        doc.loadXML(xmlString)
    }
    return doc;
}





WSProxy.prototype.makeAsyncRequest = function(url, parameters) {
	console.info("parameters--->");
	console.info(parameters);
	var tmp = new Request();
	//console.info(tmp);
	//console.info(tmp.getRequestSeed());
	
	this._requestsArray[tmp.getRequestSeed()] = tmp;
	//console.info(this._requestsArray[tmp.getRequestSeed()]);
	
	this._requestsArray[tmp.getRequestSeed()].makeRequest(url, parameters);
	//console.info(this._requestsArray[tmp.getRequestSeed()]);
}


/**
 * makeRequest
 * <br />
 * This function is the only endpoint where all the requests will be performed.
 * <br />
 * Created on 08/04/2009
 * 
 * @memberOf WSProxy
 * @function
 * 
 * @param method The HTTP verb of the request.
 * @param url The URL to connect to.
 * @param parameters A string containing all the parameters sent in the request.
 * They must be urlencoded.
 * @param callBackFunction The function that will be called when the request
 * finishes and it is successful.
 * 

 * @author Pablo Casado (pablo.casado@layers.com)
 * @version <1.0>
 * 
 * @return Object
 * 
 * @throws {Exception} In case that the HTTP Response content is not XML
 * @throws {Exception} In case that connection to the server cannot be established
 */
WSProxy.prototype.makeRequest = function(method, url, parameters, callBackFunction) {
	
	var msgRet;
	var me = this;

	
	try{
		$.ajax({
			async: false,
			type: method,
			url: url,
			dataType: 'xml',
			data: parameters,
			success: function(msg){
				try{
					msgRet = eval(callBackFunction(msg));
				}catch(e){
					console.info("exception en function succces "+e.msg);
					a = new Exception().parse(msg);
					throw a;
				}
			},
			error: function(XMLHttpRequest, textStatus, errorThrown){
				if(XMLHttpRequest.status != "200"){
					if(XMLHttpRequest.status != "401"){
						a = new Exception(ERROR00000);
						a.isCritical = true;
						throw a;
					}else{
						a = new Exception(ERROR000001);
						throw a;
					}
				}
			}
		});
	}catch(e){
		throw e;
	}
	
	return msgRet;
}



/*
 *
 */
WSProxy.prototype.goOn = function(reqId, serverToken){
	//Mirar todas las requests que estan en el array y hacer goOn sobre la que toque
	this._requestsArray[reqId].goOn(serverToken);
}



/**
 * endCheckLogin
 * <br />
 * This function is called as the callback function as a checklogin request is performed.
 * <br />
 * <br />
 * Created on 04/03/2009
 * 
 * @memberOf WSProxy
 * @function
 * 
 * @param {data} XMLDocument The XML DOM document that is returned by the makeRequest function.
 *
 * @author Pablo Casado (pablo.casado@layers.com)
 * @version <1.0>
 * 
 * @throws {Exception} In case the parsing of the XML document in order to create an User object fails.
 */
WSProxy.prototype.endCheckLogin = function(data){
	
	try{
		user = new User().parse(data);
		if(user != undefined){
			return user;
		}
	}catch(e){
		if(!os.toolbar._bgUrl_is_layers){
			try{
				var tmp = this.createDOM(data);
				user = new User().parse(tmp);
				
				this._referal.setLoginData(user, false);
			}catch(e){				
				this._referal.setNotLoggedData();
			}
		}else{
			try{
				console.info('entra en ensle')
				var tmp = this.createDOM(data);
				a = new Exception().parse(tmp);
				console.info(a);
				throw a;
			}catch(e){
				console.info(e);
				throw e;
			}
		}
	}
}


/**
 * checkLogin
 * <br />
 * This function is called in order to check if a user is logged into the system. 
 * <br />
 * <br />
 * Created on 04/03/2009
 * 
 * @memberOf WSProxy
 * @function
 * 
 * @author Pablo Casado (pablo.casado@layers.com)
 * @version <1.0>
 * 
 * @throws {Exception} In case the parsing of the XML document in order to create an User object fails.
 */
WSProxy.prototype.checkLogin = function(){
	if(os.toolbar._bgUrl_is_layers){
		var me = this;
		
		try{
			data = this.makeRequest("GET", layersOS_spm_site+"services/checklogin/", "", this.endCheckLogin);
			if (data == undefined){
				throw new Exception();
			}else{
				return data;
			}
		}catch(e){
			console.info(e);
			throw e;
		}
	}else{
		console.info("makeAsyncRequest");
		console.info("layersOS_spm_site ->");
		console.info(layersOS_spm_site);
		this.makeAsyncRequest(layersOS_spm_site+"cobrand/CheckLogin/?ocid="+WSProxy.prototype._osidContext, "");
	}
}


/**
* endRegister
* <br />
* This function is called as the callback function as a register request is performed.
* <br />
* <br />
* Created on 04/03/2009
* 
* @memberOf WSProxy
* @function
* 
* @param {data} XMLDocument The XML DOM document that is returned by the makeRequest function.
* @return {User} A user object.
*  
*
* @author Pablo Casado (pablo.casado@layers.com)
* @version <1.0>
* 
* @throws {Exception} In case the parsing of the XML document in order to create an User object fails.
*/
WSProxy.prototype.endRegister = function(data){
	var ex;
		
	try{
		user = new User().parse(data);
		return user;
	}catch(e){
		
		ex = e;
		
		if(!os.toolbar._bgUrl_is_layers){
			try{
				var tmp = this.createDOM(data);
				user = new User().parse(tmp);				
				this._referal.setLoginData(user, true);
			}catch(e){
				var exception = new Exception().parse(tmp);				
				this.error(eval('ERROR' + exception.code));
			}
		}else{		
			try{				
				var tmp = this.createDOM(data);
				a = new Exception().parse(tmp);				
				throw a;
			}catch(e){
				if(a != undefined){
					throw a;
				}else{
					throw ex;	
				}
			}		
		}
	}
}



/*
 *
 */
WSProxy.prototype.error = function(data){
  console.info("WSProxy.prototype.error");
  console.info("returned data is->");
  console.info(data);
  console.info("<-returned data");
  this._referal.showError(data);
}


/**
 * register
 * <br />
 * This function is called in order to create a new user in the system
 * <br />
 * <br />
 * Created on 04/03/2009
 * 
 * @memberOf WSProxy
 * @function
 * 
 * @return {User} A user object.
 * 
 * @author Pablo Casado (pablo.casado@layers.com)
 * @version <1.0>
 * 
 * @throws {Exception} If the username is already taken
 * @throws {Exception} If the email address is already in use
 */
WSProxy.prototype.register = function(username, password, email){

	if(os._bgUrl_is_layers){
		try{
			var xml = "<register>" +
					"<displayname>" +
						username +
					"</displayname>" +
					"<password>" + 
						password + 
					"</password>" +
					"<email>" + 
						email + 
					"</email>" +
				"</register>";
		
			this.makeRequest("PUT",  layersOS_spm_site+"services/Register/", xml, this.endRegister);
		}catch(e){
			throw(e);
		}
	}else{
		this.makeAsyncRequest(layersOS_spm_site+"cobrand/Register?u="+username+"&p="+password+"&e="+email, "");
	}
}



/**
 * endLogin
 * <br />
 * This function is called as the callback function as a login request is performed.
 * <br />
 * <br />
 * Created on 04/03/2009
 * 
 * @memberOf WSProxy
 * @function
 * 
 * @param {data} XMLDocument The XML DOM document that is returned by the makeRequest function.
 * @return {User} A user object.
 *  
 *
 * @author Pablo Casado (pablo.casado@layers.com)
 * @version <1.0>
 * 
 * @throws {Exception} In case the parsing of the XML document in order to create an User object fails.
 */
WSProxy.prototype.endLogin = function(data){
	var ex;
	
	try{
		user = new User().parse(data);
		if(user != undefined){
			return user;
		}
	}catch(e){
		
		ex = e;		
		
		if(!os.toolbar._bgUrl_is_layers){
			try{
			  try{
			    	var tmp = this.createDOM(data);
			  }catch(e){
			    console.info("WSProxy - 395");
			    console.info(e);
			  }
			  
			  try{
			    user = new User().parse(tmp);		
			  }catch(e){
			    console.info("WSProxy - 400");
			    console.info(e);
			  }
			  
			  try{
			    this._referal.setLoginData(user, true);
			  }catch(e){
			    console.info("WSProxy - 407");
			    console.info(e);
			  }			
				
			}catch(e){
				var exception = new Exception().parse(tmp);
				console.info("WSProxy - 412");
				this.error(eval('ERROR' + exception.code));
			}
		}else{		
			try{				
				var tmp = this.createDOM(data);
				a = new Exception().parse(tmp);				
				throw a;
			}catch(e){
				if(a != undefined){
					throw a;
				}else{
			    console.info("WSProxy - 424");
					throw ex;	
				}
				
			}		
		}
	}
}


/**
 * login
 * <br />
 * This function is called in order to log a user into the system.
 * <br />
 * <br />
 * Created on 04/03/2009
 * 
 * @memberOf WSProxy
 * @function
 * 
 * @param {data} XMLDocument The XML DOM document that is returned by the makeRequest function.
 * @return {User} A user object.
 *  
 *
 * @author Pablo Casado (pablo.casado@layers.com)
 * @version <1.0>
 * 
 * @throws {Exception} In case the parsing of the XML document in order to create an User object fails.
 */
WSProxy.prototype.login = function(username, password){
	
	if(os.toolbar._bgUrl_is_layers){
		data = this.makeRequest("POST",  layersOS_spm_site+"services/login/", "username="+username+"&password="+password, this.endLogin);
		console.info(data);
		if (data == undefined){
			throw new Exception("");
		}else{
			return data;
		}
	}else{
		this.makeAsyncRequest(layersOS_spm_site+"cobrand/Login?username="+username+"&password="+password, "");
	}
}

 
/**
 * endLogout
 * <br />
 * This function is called as the callback function as a logout request is performed.
 * <br />
 * <br />
 * Created on 04/04/2009
 * 
 * @memberOf WSProxy
 * @function
 *  
 *
 * @author Pablo Casado (pablo.casado@layers.com)
 * @version <1.0>
 * 
 */
WSProxy.prototype.endLogout = function(){
	this._referal.setNotLoggedData();
}

 

/**
 * logout
 * <br />
 * This function is called in order to log a user out of the system.
 * <br />
 * <br />
 * Created on 04/04/2009
 * 
 * @memberOf WSProxy
 * @function
 *  
 *
 * @author Pablo Casado (pablo.casado@layers.com)
 * @version <1.0>
 * 
 * @throws {Exception} In case the usewr was not logged in.
 */
WSProxy.prototype.logout = function(){
	if(os._bgUrl_is_layers){
		
		var me = this;
		
		 try{
			 this.makeRequest("GET",  layersOS_spm_site+"services/logout/", "", this.endLogout);
		 }catch(e){
			 throw e;
		 }
	}else{
		this.makeAsyncRequest(layersOS_spm_site+"cobrand/Logout/", "");
	}
}


 
/**
 * endLayers
 * <br />
 * Callback function for getLayerById
 * <br />
 * <br />
 * Created on 21/04/2009
 * 
 * @memberOf WSProxy
 * @function
 * 
 * @param {data} XMLDocument The XML DOM document that is returned by the
 * makeRequest function.
 * @return {Layer} The retrieved Layers  
 *
 * @author Pablo Casado (pablo.casado@layers.com)
 * @version <1.0>
 * 
 * @throws {Exception} In case the parsing of the XML document in order to
 * create a Layer object fails.
 */
WSProxy.prototype.endLayers = function(data){
	
	var ex;
	
	try{
		layer = new Layer().parse(data);
		if(layer != null){
			return layer;
		}
	}catch(e){

		var ex = e;
		
		if(!os.toolbar._bgUrl_is_layers){
			try{
				var tmp = this.createDOM(data);
				layer = new Layer().parse(tmp);

				os.setLayer(layer);
			}catch(e){
				var exception = new Exception().parse(tmp);				

				this._bgPage = $("#backgroundPage");
				$("#backgroundPage")[0].src = layersOS_spm_site+"notice";
				$("#loading").hide();
			}
		}else{		
			try{				
				var tmp = this.createDOM(data);
				a = new Exception().parse(tmp);	
			
				throw a;
			}catch(e){
				if(a != undefined){
					throw a;
				}else{
					throw ex;	
				}
				
			}		
		}
	}
	
	
}  

 

 
/**
 * getLayerById
 * <br />
 * Connects to the server and returns a layer object identified by its Id
 * <br />
 * <br />
 * Created on 21/04/2009
 * 
 * @memberOf WSProxy
 * @function
 * 
 * @param {layerId} String The Identifier of the Layer
 * @return {Layer} The retrieved Layers  
 *
 * @author Pablo Casado (pablo.casado@layers.com)
 * @version <1.0>
 * 
 * @throws {Exception} In case the parsing of the XML document in order to
 * create a Layer object fails.
 */
WSProxy.prototype.getLayerById = function(layerId){
	
	try{
		var me = this;
		
		if(os.toolbar._bgUrl_is_layers){			
		
			data = this.makeRequest("GET", layersOS_spm_site+"services/layers/"+layerId, "", me.endLayers);
			if (data == undefined){
				throw new Exception("");
			}else{
				return data;
			}
		}else{
			me.makeAsyncRequest(layersOS_spm_site+"cobrand/layers/"+layerId, "");
		}
	}catch(e){
		//console.info(e);
		throw e;
	}
}
 
 
 
/**
 * endGetLayersOverThisPage
 * <br />
 * This function is called as the callBack function after a call to
 * getLayersOverThisPage.
 * <br />
 * <br />
 * Created on 04/04/2009
 * 
 * @memberOf WSProxy
 * @function
 * 
 * @param {data} XMLDocument The XML DOM document that is returned by the makeRequest function.
 * @return {LayerIterator} A layer iterator  
 *
 * @author Pablo Casado (pablo.casado@layers.com)
 * @version <1.0>
 * 
 * @throws {Exception} In case the parsing of the XML document in order to create a LayerIterator object fails.
 * @throws {Exception} In case the parsing of the XML document in order to create a Layer object fails.
 */

WSProxy.prototype.endSites = function(data){
	
	try{
		layers = new LayerIterator(this).parse(data);
		layers._type = 0;
		return layers;
	}catch(e){
		if(!os.toolbar._bgUrl_is_layers){
			try{
				var tmp = this.createDOM(data);
				layers = new LayerIterator(this).parse(tmp);
				layers._type = 3;
				
				this._referal.setGetLayersOverThisPageData(layers);
			}catch(e){
				//console.info(e);
				this._referal.setGetLayersOverThisPageData( new LayerIterator());
			}
		}else{
			throw e;
		}
	}
		
}



/**
* getLayersOverThisPage
* <br />
* This function is called in order to receive a layerIterator containing all
* the layers created by any user, over the viewed page.
* <br />
* <br />
* Created on 04/04/2009
* 
* @memberOf WSProxy
* @function
* 
* @return {LayerIterator} A layer iterator  
*
* @author Pablo Casado (pablo.casado@layers.com)
* @version <1.0>
* 
* @throws {Exception} In case the parsing of the XML document in order to create a LayerIterator object fails.
* @throws {Exception} In case the parsing of the XML document in order to create a Layer object fails.
*/
WSProxy.prototype.getLayersOverThisPage = function(){ 
	
	try{	
		if(os.toolbar._bgUrl_is_layers){
			var me = this;
		
			data = this.makeRequest("POST",  layersOS_spm_site+"services/sites/", "url="+escape(this._referal.getBackgroundURL()), me.endSites);
			if (data == undefined){
				throw new Exception("");
			}else{
				return data;
			}
		}else{
			//me.makeAsyncRequest(layersOS_spm_site+"os/test.php", escape(this._referal.getBackgroundURL()) );
			this.makeAsyncRequest(layersOS_spm_site+"cobrand/Sites/1/30", escape(this._referal.getBackgroundURL()) );
		}
	}catch(e){
		throw e;
	}
}




/**
* endGetLayersOverThisPageByMyFriends
* <br />
* This function is called as the callBack function after a call to
* getLayersOverThisPageByMyFriends.
* <br />
* <br />
* Created on 04/04/2009
* 
* @memberOf WSProxy
* @function
* 
* @param {data} XMLDocument The XML DOM document that is returned by the makeRequest function.
* @return {LayerIterator} A layer iterator  
*
* @author Pablo Casado (pablo.casado@layers.com)
* @version <1.0>
* 
* @throws {Exception} In case the parsing of the XML document in order to create a LayerIterator object fails.
* @throws {Exception} In case the parsing of the XML document in order to create a Layer object fails.
*/
WSProxy.prototype.endSitesFriends = function(data){
	
	try{
		layers = new LayerIterator(this).parse(data);
		if(layers != null){
			layers._type = 3;
			return layers;
		}
	}catch(e){
		if(!os.toolbar._bgUrl_is_layers){
			try{
				var tmp = this.createDOM(data);
				layers = new LayerIterator(this).parse(tmp);
				layers._type = 3;
				
				this._referal.setGetLayersOverThisPageByMyFriendsData(layers);
			}catch(e){
				this._referal.setGetLayersOverThisPageByMyFriendsData( new LayerIterator() );
			}
		}else{
			throw e;
		}
	}

}



/**
* getLayersOverThisPageByMyFriends
* <br />
* This function is called in order to receive a layerIterator containing all
* the layers created by the users that are in his/her following list over the
* specified page.
* <br />
* <br />
* Created on 04/04/2009
* 
* @memberOf WSProxy
* @function
* 
* @param {String} order The way that the layers should be returned in.
* @return {LayerIterator} A layer iterator  
*
* @author Pablo Casado (pablo.casado@layers.com)
* @version <1.0>
* 
* @throws {Exception} In case the parsing of the XML document in order to create a LayerIterator object fails.
* @throws {Exception} In case the parsing of the XML document in order to create a Layer object fails.
*/
WSProxy.prototype.getLayersOverThisPageByMyFriends = function(order){

	try{
		if(os._bgUrl_is_layers){
			console.info("a1");
			var me = this;
			console.info("a2");
			data = this.makeRequest("POST", layersOS_spm_site+"services/sitesfriends/", "url="+escape(this._referal.getBackgroundURL()), me.endSitesFriends);
			console.info("a3");
			if (data == undefined){
				console.info("a4");
				throw new Exception("");
			}else{
				console.info("a5");
				return data;
			}
		}else{
			console.info("b1");
			console.info("b2->");
			console.info(this._referal);
			console.info("<-b3");
			console.info("b4->");
			console.info(this._referal.getBackgroundURL());
			console.info("<-b5");
			
			this.makeAsyncRequest( layersOS_spm_site+"cobrand/SitesFriends/1/30", escape(this._referal.getBackgroundURL()) );
			console.info("b6");
		}
	}catch(e){
		console.info("c1");
		console.info(e);
		throw e;
	}
	  
}



/**
* endGetLayersByMyFriends
* <br />
* This function is called as the callBack function after a call to
* getLayersByMyFriends.
* <br />
* <br />
* Created on 04/04/2009
* 
* @memberOf WSProxy
* @function
* 
* @param {data} XMLDocument The XML DOM document that is returned by the makeRequest function.
* @return {LayerIterator} A layer iterator  
*
* @author Pablo Casado (pablo.casado@layers.com)
* @version <1.0>
* 
* @throws {Exception} In case the parsing of the XML document in order to create a LayerIterator object fails.
* @throws {Exception} In case the parsing of the XML document in order to create a Layer object fails.
*/
WSProxy.prototype.endFriendsLayers = function(data){
	
	try{
		layers = new LayerIterator(this).parse(data);
		if (layers != null){
			layers._type = 2;
			return layers;
		}
		
	}catch(e){
		if(!os.toolbar._bgUrl_is_layers){
			try{
				var tmp = this.createDOM(data);
				layers = new LayerIterator(this).parse(tmp);
				layers._type = 2; 
				
				this._referal.setGetLayersByMyFriendsData(layers);
			}catch(e){
				this._referal.setGetLayersByMyFriendsData( new LayerIterator() );
			}
		}else{
			throw e;
		}
	}
	
}



/**
* getLayersByMyFriends
* <br />
* This function is called in order to receive a layerIterator containing the
* last layers created by the friends of the current user
* <br />
* <br />
* Created on 04/04/2009
* 
* @memberOf WSProxy
* @function
* 
* @param {String} order The way that the layers should be returned in.
* @return {LayerIterator} A layer iterator  
*
* @author Pablo Casado (pablo.casado@layers.com)
* @version <1.0>
* 
* @throws {Exception} In case the parsing of the XML document in order to
* create a LayerIterator object fails.
* 
* @throws {Exception} In case the parsing of the XML document in order to
* create a Layer object fails.
*/
WSProxy.prototype.getLayersByMyFriends = function(order){
	/*
	var xmlStr = "<?xml version='1.0' encoding='utf-8'?>" +
			"<layersListRequest>" +
				"<page>1</page>" +
				"<order>"+order+"</order>" +
			"</layersListRequest>";	
	*/
	try{
		if(os._bgUrl_is_layers){
			var me = this;
	
			data = this.makeRequest("POST", layersOS_spm_site+"services/friendslayers/1/30", "", me.endFriendsLayers);
	
			if (data != undefined){
				return data;
			}else{
				throw new Exception("");
			}
		}else{
			this.makeAsyncRequest(layersOS_spm_site + "cobrand/FriendsLayers/1/30", "" );
		}
	}catch(e){
		throw e;
	}
}




/**
* endSitesMyLayers
* <br />
* This function is called as the callBack function after a call to
* getMyLayersOverThisPage.
* <br />
* <br />
* Created on 04/04/2009
* 
* @memberOf WSProxy
* @function
* 
* @param {data} XMLDocument The XML DOM document that is returned by the makeRequest function.
* @return {LayerIterator} A layer iterator  
*
* @author Pablo Casado (pablo.casado@layers.com)
* @version <1.0>
* 
* @throws {Exception} In case the parsing of the XML document in order to create a LayerIterator object fails.
* @throws {Exception} In case the parsing of the XML document in order to create a Layer object fails.
*/
WSProxy.prototype.endSitesMyLayers = function(data){
	
	try{
		layers = new LayerIterator(this).parse(data);
		if (layers != null){
			layers._type = 1;
			return layers;
		}		
	}catch(e){
		
		if(!os._bgUrl_is_layers){
			try{
				var tmp = this.createDOM(data);
				layers = new LayerIterator(this).parse(tmp);
				layers._type = 1;

				this._referal.setGetMyLayersOverThisPageData(layers);
			}catch(e){
				console.error(e);
				this._referal.setGetMyLayersOverThisPageData( new LayerIterator() );
			}
		}else{
			throw e;
		}
	}

}



/**
* getMyLayersOverThisPage
* <br />
* This function is called in order to receive a layerIterator containing all
* the layers created by the logged user over the current page
* specified page.
* <br />
* <br />
* Created on 04/04/2009
* 
* @memberOf WSProxy
* @function
* 
* @param {String} order The way that the layers should be returned in.
* @return {LayerIterator} A layer iterator  
*
* @author Pablo Casado (pablo.casado@layers.com)
* @version <1.0>
* 
* @throws {Exception} In case the parsing of the XML document in order to create a LayerIterator object fails.
* @throws {Exception} In case the parsing of the XML document in order to create a Layer object fails.
*/
WSProxy.prototype.getMyLayersOverThisPage = function(){

	try{
		if(os._bgUrl_is_layers){
			var me = this;
			data = this.makeRequest("POST", layersOS_spm_site+"services/sitesmylayers/", "url="+escape(this._referal.getBackgroundURL()), me.endSitesMyLayers);
			console.info(data);
			if (data == undefined){
				throw new Exception("");
			}else{
				return data;
			}
		}else{
			this.makeAsyncRequest(layersOS_spm_site+"cobrand/SitesMyLayers/1/30", escape(this._referal.getBackgroundURL()));
		}
	}catch(e){
		throw e;
	}
	  
}


/**
* endSitesNotFriends
* <br />
* This function is called as the callBack function after a call to
* getLayersOverThisPageNotFriends.
* <br />
* <br />
* Created on 04/04/2009
* 
* @memberOf WSProxy
* @function
* 
* @param {data} XMLDocument The XML DOM document that is returned by the makeRequest function.
* @return {LayerIterator} A layer iterator  
*
* @author Antonio Perez (tony@layers.com)
* @version <1.0>
* 
* @throws {Exception} In case the parsing of the XML document in order to create a LayerIterator object fails.
* @throws {Exception} In case the parsing of the XML document in order to create a Layer object fails.
*/
WSProxy.prototype.endSitesNotFriends = function(data){	
	try{
		layers = new LayerIterator(this).parse(data);
		if (layers != null){
			layers._type = 4;
			return layers;
		}		
	}catch(e){
		
		if(!os._bgUrl_is_layers){
			try{
				var tmp = this.createDOM(data);
				layers = new LayerIterator(this).parse(tmp);
				layers._type = 4; 

				this._referal.setGetNotFriendsLayersOverThisPageData(layers);
			}catch(e){
				console.error(e);
				this._referal.setGetNotFriendsLayersOverThisPageData( new LayerIterator() );
			}
		}else{
			throw e;
		}
	}
}



/**
* getLayersOverThisPageNotFriends
* <br />
* This function is called in order to receive a layerIterator containing all
* the layers created by the any user other than the ones in his/her following list over the
* specified page.
* <br />
* <br />
* Created on 04/04/2009
* 
* @memberOf WSProxy
* @function
* 
* @param {String} order The way that the layers should be returned in.
* @return {LayerIterator} A layer iterator  
*
* @author Antonio Perez (tony@layers.com)
* @version <1.0>
* 
* @throws {Exception} In case the parsing of the XML document in order to create a LayerIterator object fails.
* @throws {Exception} In case the parsing of the XML document in order to create a Layer object fails.
*/
WSProxy.prototype.getLayersOverThisPageNotFriends = function(order){
	try{
		if(os._bgUrl_is_layers){
			var me = this;
			data = this.makeRequest("POST", layersOS_spm_site+"services/sitesnotfriends/", "url="+escape(this._referal.getBackgroundURL()), me.endSitesNotFriends);

			if (data == undefined){
				throw new Exception("");
			}else{
				return data;
			}
		}else{
			this.makeAsyncRequest( layersOS_spm_site+"cobrand/SitesNotFriends/1/30", escape( this._referal.getBackgroundURL() ) );
		}
	}catch(e){
		throw e;
	}
	  
}

/**
* endFollow
* <br />
* This function is called in order to start following a viewd user
* <br />
* <br />
* Created on 21/04/2009
* 
* @memberOf WSProxy
* @function
* 
*
* @author Pablo Casado (pablo.casado@layers.com)
* @version <1.0>
*
*/
WSProxy.prototype.endFollow = function(){
	return;
}


/**
* follow
* <br />
* Follow the activity of another identity
* <br />
* <br />
* Created on 21/04/2009
* 
* @memberOf WSProxy
* @function
* 
* @param {String} whoToFollow The identifier of the identity to start following
*
* @author Pablo Casado (pablo.casado@layers.com)
* @version <1.0>
* 
* @throws {Exception} In case the parsing of the XML document in order to create a LayerIterator object fails.
* @throws {Exception} In case the parsing of the XML document in order to create a Layer object fails.
*/
WSProxy.prototype.follow = function(whoToFollow){
	if(os._bgUrl_is_layers){
		try{
			var me = this;
			this.makeRequest("GET", layersOS_spm_site+"services/follow/"+whoToFollow, "", this.endFollow);
		}catch(e){
			throw e;
		}
	}else{
		this.makeAsyncRequest(layersOS_spm_site+"cobrand/Follow/"+whoToFollow);
	}
}
	


/**
* endStopFollow
* <br />
* This function is called in order to stop following a viewed user
* <br />
* <br />
* Created on 21/04/2009
* 
* @memberOf WSProxy
* @function
* 
*
* @author Pablo Casado (pablo.casado@layers.com)
* @version <1.0>
*
*/
WSProxy.prototype.endStopFollow = function(){
	return;
}


/**
* stopFollow
* <br />
* Stop following another identity
* <br />
* <br />
* Created on 21/04/2009
* 
* @memberOf WSProxy
* @function
* 
* @param {String} whoToFollow The identifier of the identity to stop following
*
* @author Pablo Casado (pablo.casado@layers.com)
* @version <1.0>
* 
* @throws {Exception} In case the parsing of the XML document in order to create a LayerIterator object fails.
* @throws {Exception} In case the parsing of the XML document in order to create a Layer object fails.
*/
WSProxy.prototype.stopFollow = function(whoToFollow){
	if(os._bgUrl_is_layers){
		try{
			var me = this;
			this.makeRequest("GET", layersOS_spm_site+"services/stopfollow/"+whoToFollow, "", me.endStopFollow);
		}catch(e){
			throw e;
		}
	}else{
		this.makeAsyncRequest(layersOS_spm_site+"cobrand/StopFollow/"+whoToFollow);
	}
}



/**
* endRateLayer
* <br />
* Callback function for rateLayer
* <br />
* <br />
* Created on 21/04/2009
* 
* @memberOf WSProxy
* @function
* 
*
* @author Pablo Casado (pablo.casado@layers.com)
* @version <1.0>
*
*/
WSProxy.prototype.endRateLayer = function(){
	return;
}



/**
* rateLayer
* <br />
* Updates the rating of the selected Layer
* <br />
* <br />
* Created on 21/04/2009
* 
* @memberOf WSProxy
* @function
* 
* @param {layerId} String the identifier of the layer to rate
* @param {rate} Number The rating fot the layer
*
* @author Pablo Casado (pablo.casado@layers.com)
* @version <1.0>
* 
*/
WSProxy.prototype.rateLayer = function(layerId, rate){
	var xmlData = "<rate>"+rate+"</rate>";
	this.makeRequest("PUT", layersOS_spm_site+"services/rate/"+layerId, xmlData, this.endRateLayer);
}

/**
* likeLayer
* <br />
* Updates the rating of the selected Layer
* <br />
* <br />
* Created on 21/04/2009
* 
* @memberOf WSProxy
* @function
* 
* @param {layerId} String the identifier of the layer to rate
* @param {rate} Number The rating fot the layer
*
* @author Pablo Casado (pablo.casado@layers.com)
* @version <1.0>
* 
*/
WSProxy.prototype.likeLayer = function(layerId){
	try{
		if(os._bgUrl_is_layers){
			var me = this;
			this.makeRequest("PUT", layersOS_spm_site+"services/Like/"+layerId, "", this.endLikeLayer());
		}else{
						this.makeAsyncRequest(layersOS_spm_site+"cobrand/Like/"+layerId, "");
		}
	}catch(e){
		throw e;
	}	
}




/**
* endLike
* <br />
* Callback function for rateLayer
* <br />
* <br />
* Created on 21/04/2009
* 
* @memberOf WSProxy
* @function
* 
*
* @author Pablo Casado (pablo.casado@layers.com)
* @version <1.0>
*
*/
WSProxy.prototype.endLike = function(){
	this._referal.setLikedLayer();
}


/**
* reportLayer
* <br />
* Reports a possibly unintended layer
* <br />
* <br />
* Created on 15/07/2009
* 
* @memberOf WSProxy
* @function
* 
* @param {layerId} String the identifier of the layer to be reported
*
* @author "Antonio Perez (tony@layers.com)"
* @version <1.0>
* 
*/
WSProxy.prototype.reportLayer = function(layerId){
	try {
		if (os._bgUrl_is_layers) {
			var me = this;
			this.makeRequest("PUT", layersOS_spm_site + "services/report/" + layerId, "", this.endReport());						
		}
		else {
			this.makeAsyncRequest(layersOS_spm_site + "cobrand/report/" + layerId, "");
		}
	} 
	catch (e) {
		throw e;
	}
}	


/**
* endReportLayer
* <br />
* Callback function for reportLayer
* <br />
* <br />
* Created on 15/07/2009
* 
* @memberOf WSProxy
* @function
* 
*
* @author "Antonio Perez (tony@layers.com)"
* @version <1.0>
*
*/
WSProxy.prototype.endReport = function(){
	this._referal.setReportedLayer();
}

/**
 * endSearchUsers
 * <br />
 * Callback function for seachUsers
 * <br />
 * <br />
 * Created on 05/05/2009
 * 
 * @memberOf WSProxy
 * @function
 * 
 * @return {XMLDocument}  The users that match the queryStr
 *
 * @author Pablo Casado (pablo.casado@layers.com)
 * @version <1.0>
 * 
 */
WSProxy.prototype.endSearchUsers = function(data){
	array = new Array();
	tmp = data.getElementByTagName("user");
	
	var i = 0;
	while(i < tmp.length){
		array[i] = new User().parse(tmp[i]);
		i++;
	}
	
}



/**
 * searchUsers
 * <br />
 * Returns an array containing the identity names of all the identities that
 * match the string passed as an argument
 * <br />
 * <br />
 * Created on 05/05/2009
 * 
 * @memberOf WSProxy
 * @function
 * 
 * @param {String} queryStr The rating for the layer
 *
 * @author Pablo Casado (pablo.casado@layers.com)
 * @version <1.0>
 * 
 * @throws {Exception} In case the parsing of the XML document fails.
 * @throws {Exception} In case the request to the server fails.
 */
WSProxy.prototype.searchUsers = function(queryStr){
	
	var xmlStr = "<?xml version='1.0' encoding='utf-8'?>"
		+ "<searchUsers>" +queryStr+ "</searchUsers>";
		
	try{
		var me = this;
		res = this.makeRequest("PUT", layersOS_spm_site+"services/searchusers/", xmlStr, me.endSearchUsers);
		return res;
	}catch(e){
		throw e;
	}
}



/**
 * endEmail
 * <br />
 * Callback function for sendEmail
 * <br />
 * <br />
 * Created on 05/05/2009
 * 
 * @memberOf WSProxy
 * @function
 * 
 *
 * @author Pablo Casado (pablo.casado@layers.com)
 * @version <1.0>
 * 
 */
WSProxy.prototype.endEmail = function(){
	this._referal.setSendEmail();
}



/**
 * sendMail
 * <br />
 * Sends a mail to the specified email addresses and identitites containing the
 *  text passed as an argument. 
 * <br />
 * <br />
 * Created on 05/05/2009
 * 
 * @memberOf WSProxy
 * @function
 * 
 * @param {Array} users An array containing the names of the identites that will
 * receive the email.
 * @param {Array} emails An array containing the emails of non-registered users 
 * that will receive the email.
 * @param {String} text The user defined text of the email
 * @param {String} title The user defined title of the email
 *
 * @author Pablo Casado (pablo.casado@layers.com)
 * @version <1.0>
 * 
 * @throws {Exception} In case the parsing of the XML document fails.
 * 
 */
 WSProxy.prototype.sendMail = function(users, emails, text, title){
	
	var xmlStr = "<?xml version='1.0' encoding='utf-8'?><sendmail>";
	
	
	
	if( users.length > 0 ){
		xmlStr = xmlStr + "<users>";
		
		var i = 0;
		
		while(i < users.length){
			xmlStr = xmlStr + "<user>"+users[i]+"</user>";
			i++;
		}
		xmlStr = xmlStr + "</users>";
	}	
	
	if( emails.length > 0 ){
		xmlStr = xmlStr + "<emails>";
		
		var j = 0;
		
		while(j < emails.length){
			xmlStr = xmlStr +emails[j]+",";
			j++;
		}
		xmlStr = xmlStr + "</emails>";
	}
	
	xmlStr = xmlStr + "<permalink>" + layersOS_spm_site + this._referal.getCurrentIdentity()._displayName + "/" + this._referal.getCurrentLayer()._id + "</permalink>";
	
	xmlStr = xmlStr + "<title>"+ title + "</title>";
	
	xmlStr = xmlStr + "<text>"+ text + "</text></sendmail>";
	
	try{
		
		if (os._bgUrl_is_layers) {
			var me = this;
			res = this.makeRequest("PUT", layersOS_spm_site+"services/email/", xmlStr, this.endEmail);					
		}
		else {
			this.makeAsyncRequest(layersOS_spm_site + "cobrand/Email", xmlStr);
		}
		
		return res;
	}catch(e){
		throw e;
	}
}
 
 
 
/**
 * endSaveLayer
 * <br />
 * Callback function for saveLayer
 * <br />
 * <br />
 * Created on 12/05/2009
 * 
 * @memberOf WSProxy
 * @function
 * 
 * @return {XMLDocument}  The users that match the queryStr
 *
 * @author Pablo Casado (pablo.casado@layers.com)
 * @version <1.0>
 * 
 */
WSProxy.prototype.endSaveLayer = function(data){
	try {
		layer = new Layer().parse(data);
		if (layer != null) {
			this._referal.setSavedLayer(layer);
		}
		
	} 
	catch (e) {
	
		ex = e;
		
		if (!os.toolbar._bgUrl_is_layers) {
			try {
				var tmp = this.createDOM(data);
				var layer = new Layer(this).parse(tmp);
				this._referal.setSavedLayer(layer);
				console.info(layer);
			} 
			catch (e) {
				var exception = new Exception().parse(tmp);
				this.error(eval('ERROR' + exception.code));
				// Enable share button again to allow the user to retry.
				os.toolbar._shareButton.enable();
			}
		}
		else {
			try {
				var tmp = this.createDOM(data);
				a = new Exception().parse(tmp);
				throw a;
			} 
			catch (e) {
				if (a != undefined) {
					throw a;
				}
				else {
					throw ex;
				}
				
			}
		}
	}		
}

 
 
/**
 * saveLayer
 * <br />
 * Saves the especified Layer and its objects
 * <br />
 * <br />
 * Created on 05/05/2009
 * 
 * @memberOf WSProxy
 * @function
 * 
 * 
 * @author Pablo Casado (pablo.casado@layers.com)
 * @version <1.0>
 * 
 * @throws {Exception} In case the parsing of the XML document fails.
 * 
 */
WSProxy.prototype.saveLayer = function(){
	var xmlStr = os.getCurrentLayer().toXML();
	console.info(os.getCurrentLayer());
	console.info(os.getCurrentLayer().toXML());
	
	if(os._bgUrl_is_layers){
		try{
			var me = this;
			res = this.makeRequest("PUT", layersOS_spm_site+"services/savelayer/", xmlStr, me.endSaveLayer);
			
			if(res != null){
				//TODO Usar el layer recibido del servidor como v‡lido. Por ahora continuamos usando currentLayer y por eso borramos los objetos vac’os.
				for (object in this._referal.getCurrentLayer()._data){
					if (this._referal.getCurrentLayer()._data[object]._cdata == '') delete this._referal.getCurrentLayer()._data[object];
				}
			
				return res;	
			}
		}catch(e){
			console.error(e);
			throw e;
		}
	}else{
		console.info(layersOS_spm_site);
		this.makeAsyncRequest(layersOS_spm_site+"cobrand/SaveLayer", xmlStr);
	}
	
	
}
 
 
 
/**
 * endPublishLayer
 * <br />
 * Callback function for saveLayer
 * <br />
 * <br />
 * Created on 15/05/2009
 * 
 * @memberOf WSProxy
 * @function
 * 
 * @return {XMLDocument}  The users that match the queryStr
 *
 * @author Pablo Casado (pablo.casado@layers.com)
 * @version <1.0>
 * 
 */ 
WSProxy.prototype.endPublishLayer = function(){
	return;
}

 

 
/**
 * publishLayer
 * <br />
 * Published the specified layer
 * <br />
 * <br />
 * Created on 15/05/2009
 * 
 * @memberOf WSProxy
 * @function
 * 
 * @param {String} layerId The identifier of the layer that is going to be published 
 * 
 * @author Pablo Casado (pablo.casado@layers.com)
 * @version <1.0>
 * 
 * 
 */
 WSProxy.prototype.publishLayer = function(layerId, title, description){
	var xmlStr = "<?xml version='1.0' encoding='utf-8'?><publish>";
	xmlStr = xmlStr+"<description>"+htmlEntitiesEncode(description)+"</description>";
	xmlStr = xmlStr+"<title>"+htmlEntitiesEncode(title)+"</title></publish>";
	
	 try{
		 data = this.makeRequest("PUT", layersOS_spm_site+"services/publish/"+layerId, xmlStr, this.endPublishLayer);
	 }catch(e){
		 throw e;
	 }
 }
 
/**
 * endChangeIdentity
 * <br />
 * Callback function for changeIdentity
 * <br />
 * <br />
 * Created on 17/05/2009
 * 
 * @memberOf WSProxy
 * @function
 * 
 *
 * @author Antonio Perez (tony@layers.com)
 * @version <1.0>
 * 
 */ 
WSProxy.prototype.endChangeIdentity = function(){
	os.removeCurrentLayer();
	os.toolbar.drawLogged();
}

 

 
/**
 * changeIdentity
 * <br />
 * Change the specified identity
 * <br />
 * <br />
 * Created on 17/05/2009
 * 
 * @memberOf WSProxy
 * @function
 * 
 * @param {String} identityId The identifier of the identity to be set in the session 
 * 
 * @author Antonio Perez (tony@layers.com)
 * @version <1.0>
 * 
 * 
 */
 WSProxy.prototype.changeIdentity = function(identityId){		
 
 	try{
		var me = this;
		
		if(os._bgUrl_is_layers){			
			this.makeRequest("PUT", layersOS_spm_site+"services/changeidentity/"+identityId, "", this.endChangeIdentity());

			if (data == undefined){
				throw new Exception("");
			}else{
				return data;
			}
		}else{
			this.makeAsyncRequest(layersOS_spm_site+"cobrand/changeIdentity/"+identityId, "");
		}
	}catch(e){
		throw e;
	}	 
}


/**
* commentLayer
* <br />
* Reports a possibly unintended layer
* <br />
* <br />
* Created on 15/07/2009
* 
* @memberOf WSProxy
* @function
* 
* @param {comment} String the text entered by the user.
* @param {layerId} String layer id the comment will be associated to.
*
* @author "Antonio Perez (tony@layers.com)"
* @version <1.0>
* 
*/
WSProxy.prototype.commentLayer = function(layerId, comment){
	try {
		if (os._bgUrl_is_layers) {
			var me = this;
			this.makeRequest("PUT", layersOS_spm_site + "services/AddComment/" + layerId, comment, this.endAddCommentLayer());						
		}
		else {
			console.info(comment);
			this.makeAsyncRequest(layersOS_spm_site + "cobrand/AddComment/" + layerId, comment);
		}
	} 
	catch (e) {
		throw e;
	}
}	


/**
* endAddCommentLayer
* <br />
* Callback function for commentLayer
* <br />
* <br />
* Created on 15/07/2009
* 
* @memberOf WSProxy
* @function
* 
*
* @author "Antonio Perez (tony@layers.com)"
* @version <1.0>
*
*/
WSProxy.prototype.endAddComment = function(){
	this._referal.setCommentLayer();
}

//End Definition for Web Services Proxy

