


/*
 * 
 * TO DO
 * 
 */
function htmlEntitiesDecodema(myString) {
	var encString;
	encString = myString.replace( /&amp;/g, "&" );
	encString = encString.replace( /&lt;/g, "<" );
	encString = encString.replace( /&apos;/g, "'" );
	return encString;	
};


function htmlEntitiesEncode(myString) {		
	var encString;
	//encString = myString.replace(/'/g, "&apos;" );
	encString = myString.replace( /</g, "&lt;" );
	encString = myString.replace( /\&/g, "&amp;" );
	return encString;
};


/**
 * @namespace
 * Class Exception
 * <br /><br />
 * Utility class that wraps an exception
 * <br />
 * Created on 04/03/2009
 * 
 * @class Exception
 *
 * @property {string} msg A descriptive message of the exception
 * @property {number} code The code for the exception
 * @property {alternatives} Array An array containing the alternatives for a
 * username. This is only used in case of exception in the register function.
 * 
 * @author "Pablo Casado (pablo.casado@layers.com)"
 * @version <1.0>
 * 
 * @function
 * @constructor
 * @param {msg} String The description of the exception.
 */

function Exception(msg){
	this.isCritical = false;
	this.msg = msg;
	this.code = 0;
	this.alternatives = new Array();
};

Exception.prototype.isCritical;
Exception.prototype.msg;
Exception.prototype.code;
Exception.prototype.alternatives;



/**
 * Parses a XML Document that contains a marshalled layer.
 * @memberOf Exception
 * @function
 * 
 * @param mydata An XML Document.
 * 
 * Created on 08/04/2009
 * @author Pablo Casado (pablo.casado@layers.com)
 * @version <1.0>
 * 
 * @return Exception
 * @throws {Exception} In case that the XML does not contain a marshalled Exception.
 */

Exception.prototype.parse = function(mydata){
	
	try{
		myException = mydata.getElementsByTagName("error")[0];
		
		if(myException != undefined){
			this.code = myException.getElementsByTagName("code")[0].childNodes[0].nodeValue;
			this.msg = myException.getElementsByTagName("message")[0].childNodes[0].nodeValue;
			
			//Used in case that there is an error in register
			try{
				this.alternatives = new Array();
				tmp = myException.getElementsByTagName("options")[0].getElementsByTagName("option");
				
				var i = 0;
				while (tmp2[i]){
					this.alternatives[i] = tmp2[i].childNodes[0].nodeValue;
					i = i + 1;
				}
				
			}catch(e){
				//Do nothing, alternatives is already an empty array
			}
			
		}
		
	}catch(e){
		throw e;
	}
	
	return this;
	
};



/**
 * @namespace
 * Class LayersOS
 * <br /><br />
 * This is the main class of the LayersOS. It encapsulates all the business logic of the
 * client-side of the project.
 * <br /><br />
 * Created on 04/03/2009
 * 
 * @class LayerOS
 *
 * @property {LayersOSWSProxy} proxy A LayerOSWSProxy object.
 *
 * @author Pablo Casado (pablo.casado@layers.com)
 * @version <1.0>
 * 
 */
function LayerOS(referal){
	this.toolbar = referal;
	this.openedLayersArray = {};
	this.openedApps = {};
	this.activeApp = '';
	this.initLayerOS();
	this._bgUrl_is_layers = false;
};

LayerOS.prototype.proxy;
LayerOS.prototype.currentObject;
LayerOS.prototype.currentLayer;
LayerOS.prototype.currentUser;
LayerOS.prototype.currentIdentity;
LayerOS.prototype.openedApps;
LayerOS.prototype.activeApp;
LayerOS.prototype._bgUrl_is_layers;
LayerOS.prototype._popUpBoard;
LayerOS.prototype._osidContext; // Edita

LayerOS.prototype.toolbar;



/**
 * initLayerOS
 * <br /><br />
 * This function sets up the LayersOS environment and crerates all the objects needed for its execution.
 * <br /><br />
 * Created on 04/03/2009
 * @memberOf LayerOS
 * @function
 * 
 * Created on 08/04/2009
 * @author Pablo Casado (pablo.casado@layers.com)
 * @version <1.0>
 * 
 */
LayerOS.prototype.initLayerOS = function(){
	this.proxy = new WSProxy(this, osidContextIndentifier);	
};



/**
 * importScript
 * 
 * This function is used to import a script from the same or another domain.
 *
 * Created on 04/03/2009
 * @author "Pablo Casado (pcasado@layers.com)"
 * @version <1.0.1>
 * 
 * @param {String} url The URL where the script must be fetched.
 * @param {String} id The id for the html element.
 * @param {String} fn The function to be executed when the script is loaded.
 * 
 * @return void 
 */
LayerOS.prototype.importScript = function(url, id, fn){
	/*	
	console.info("LayerOS.prototype.importScript");
	console.info(document);
	console.info("makeCall to->");
	console.info(url);
	$.ajax({
	  type: "GET",
	  id: id,
	  cache: true,
	  url: url,
	  dataType: "script",
	  success: fn
	});
	*/

	
	console.info("LayerOS.prototype.importScript");
	console.info(document);
	console.info("makeCall to->");
	console.info(url);
	
	var _script = document.createElement('script');
	_script.type = 'text/javascript';	
	_script.src = url;
	_script.id = id;
	try{
		document.getElementsByTagName("head")[0].appendChild(_script);
	}catch(e){
		throw new Exception();
	}

	_script.onload = function () {
		if(fn){
			console.info("onload!");
			try{
				fn.call();
			}catch(e){
				console.info("dentro del onload");
				console.info(e);
			}
		}
	}
};


/**
 * importCSS
 * 
 * This function is used to import a script from the same or another domain.
 *
 * Created on 28/04/2009
 * @author "Francesc Navarro <francesc.navarro@layers.com>"
 * @version <1.0>
 * 
 * @param {String} url The URL where the script must be fetched.
 * @param {String} id The id for the html element.
 * @param {String} fn The function to be executed when the script is loaded.
 * @param {Object} page The window page where you want to import the css.
 * 
 * @return void 
 */
LayerOS.prototype.importCSS = function(url, id, fn,page){
	//Si este script con este id ya esta cargado salimos
	if ( !page && $('#layerOS_css_' + id).length > 0) {
		if (fn) {
			try {
				$('#layerOS_css_' + id).ready(fn);
			}catch(e){
				$('#layerOS_css_' + id).load(fn);
			}
		}		
		return false;
	}
	
	
	if (page){
		if ($(page).find('#layerOS_css_' + id).length > 0){
			return false;
		}
	}
	
	//Cargamos el script
	console.info("importing: "+url);
	
	if (page) {
		var tag = page.createElement("link");
	}
	else {
		var tag = document.createElement("link");
	}
	
    if(id){
    	tag.id = "layerOS_css_" + id;
    }
    tag.type="text/css";
	tag.rel="stylesheet";
    tag.href = url;
	
	if (page) {
		page.getElementsByTagName("HEAD")[0].appendChild(tag);
	}else{
		document.getElementsByTagName("HEAD")[0].appendChild(tag);
	}	
    
	if(fn){
			try {
				$('#layerOS_css_' + id).ready(fn);
			}catch(e){
				$('#layerOS_css_' + id).load(fn);
			}
    }
};


/*
 *
 */
LayerOS.prototype.computeIframeSrc = function(bgUrl){
	
	var iframeSrc = '/notice';

	if ((bgUrl + '/').indexOf(layersOS_spm_site) < 0){
		bgUrl = bgUrl.replace('http://','');
		bgUrl = bgUrl.replace('https://','');
		iframeSrc = layersOS_spm_site  + "bgp/" + bgUrl;
		this._bgUrl_is_layers = false;
	}else{
		iframeSrc = bgUrl;		
		this._bgUrl_is_layers = true;
	}

	return iframeSrc;
};


/*
 * Returns the URL of the background page
 */
LayerOS.prototype.getBGPageURL = function(){
	var iframeSrc;

  var tmp = this.getURL();
	if (typeof(tmp) != undefined) {
		iframeSrc = this.computeIframeSrc(tmp);
		console.info("layersOS - 299"); 
		console.info("iframeSrc ->");
		console.info(iframeSrc);
		return iframeSrc;
		//os.getCurrentLayer()._siteURL = os.getURL(); --> esto sólo se debe hacer en el createLayer
	} else {
		console.info("estoy en el else");
		var user = this.getUserFromUrl();
		var layerId = this.getIdFromUrl();
		
		//Pedimos la información del layer y hacemos un set del objeto layer actual al recibido del servidor.
		if(layerId){
			try{
				this.currentLayer = this.getLayer(layerId);

				if( this.currentLayer != null){
					this._bgUrl = this.getCurrentLayer()._siteURL;
					iframeSrc = this.computeIframeSrc(os.getCurrentLayer()._siteURL);
					console.info("layersOS - 317");
					console.info("iframeSrc ->");
					console.info(iframeSrc);
					return iframeSrc;
				}

			}catch(e){
			  console.info("layersOS - 324");
				console.error(e);
				throw(e);
			}
		}else{
		  console.info("layersOS - 329");
		}
	}
};


/*
 * @param {Jquery} AnObject
 */
LayerOS.prototype.setPopUpBoard = function(anObject){
	this._popUpBoard = anObject;
};



/*
 * getPopUpBoard
 * @return {Jquery} A Jquery object containing the pop up board of the layersOS
 */
LayerOS.prototype.getPopUpBoard = function(){
	return this._popUpBoard;
};



/**
 * checkExistenceOfPreviousLayersOS
 * <br /><br />
 * TO DO!!!!!
 * <br /><br />
 * This checks if there is another LayersOS object in the loaded page in order to avoid collisions if
 * a user tries to start two different instances of the client.
 * <br /><br />
 * Created on 04/03/2009
 * @memberOf LayerOS
 * @function
 * 
 * @return Boolean
 * 
 * Created on 08/04/2009
 * @author Pablo Casado (pablo.casado@layers.com)
 * @version <1.0>
 * 
 */
LayerOS.prototype.checkExistenceOfPreviousLayersOS = function(){
	//TODO
};



/**
 * getLanguage
 * <br /><br />
 * This function returns the language that the user has configured in his/her browser
 * Useful for developing i18n applications
 * <br /><br />
 * Created on 01/04/2009
 * @memberOf LayerOS
 * @function
 * 
 * @return String
 * 
 * Created on 08/04/2009
 * @author Pablo Casado (pablo.casado@layers.com)
 * @version <1.0>
 * 
 */
LayerOS.prototype.getLanguage = function(){
	return lang;
};




/**
 * getURL
 * <br /><br />
 * This function returns the URL where the LayerOS is being executed.
 * <br /><br />
 * Created on 01/04/2009
 * @memberOf LayerOS
 * @function
 * 
 * @return String
 * 
 * Created on 08/04/2009
 * @author Pablo Casado (pablo.casado@layers.com)
 * @version <1.0>
 * 
 */
LayerOS.prototype.getURL = function(){

	/*
	var urlParam = document.location.href; 
		
	if ( urlParam != null ){
		var url = new String(window.top.location);
		console.info("layersOS - 426");
		console.info( url.substring(url.indexOf('url=') + 4) );
		return url.substring(url.indexOf('url=') + 4);	
	}
	*/
	return document.location.href;
	
};





/**
 * getUserFromUrl
 * <br /><br />
 * This function returns the User from the url param.
 * <br /><br />
 * Created on 01/04/2009
 * @memberOf LayerOS
 * @function
 * 
 * @return String
 * 
 * Created on 21/04/2009
 * @author Francesc Navarro <francesc.navarro@layers.com>
 * @version <1.0>
 * 
 */
LayerOS.prototype.getUserFromUrl = function(){
	return jQuery.url.param("user");
};




/**
 * getIdFromUrl
 * <br /><br />
 * This function returns the Id from the url param.
 * <br /><br />
 * Created on 01/04/2009
 * @memberOf LayerOS
 * @function
 * 
 * @return String
 * 
 * Created on 21/04/2009
 * @author Francesc Navarro <francesc.navarro@layers.com>
 * @version <1.0>
 * 
 */
LayerOS.prototype.getIdFromUrl = function(){
	return jQuery.url.segment(1);
};



/**
 * checkLogin
 * 
 * This function is the responsible to check if a user is logged into the system or not
 *
 * Created on 01/04/2009
 * @author "Pablo Casado (pablo.casado@layers.com)"
 * @version <1.0>
 *  
 * @return User
 * 
 * @throws {Exception} If the user is not logged in.
 */
LayerOS.prototype.checkLogin = function(){
	
	if(this.currentUser == undefined){
		try{
			var theUser = this.proxy.checkLogin();
			if(theUser != undefined){
				this.currentUser = theUser;
				this.currentIdentity = theUser.getMainIdentity();
				return theUser;
			}
		}catch(e){
			throw new Exception(ERROR000001);
		}
	}else{
		return this.currentUser;
	}
};
 
 
 
/**
 * createLayer
 * 
 * Creates a new Layer and sets it to the currentLayer attribute
 *
 * Created on 01/04/2009
 * @author "Pablo Casado (pablo.casado@layers.com)"
 * @version <1.0>
 * 
 */
LayerOS.prototype.createLayer = function(){
	 this.currentLayer = new Layer();
};


/*
 * getMyObjectsFromCurrentLayer
 * 
 * @param {String} The name of an application
 *
 * @return {Array} An Array containing the objects from the currentLayer that were created using the especified app
 */
LayerOS.prototype.getMyObjectsFromCurrentLayer = function(appName){

	var a = new Array();
	a.push(this.currentLayer.getObjects()[appName]);
	return a;
};

/*
 * openLayer
 * 
 * Runs all the applications that created the objects in the layer
 * @param {layer} Layer The layer to be opened
 * 
 */
LayerOS.prototype.openLayer = function(layer){
	
	this.setCurrentLayer(layer);
	
	try{		
		// Check if the current user is the current layer's owner and activate the publish button, and make title editable if so
		if (this.currentLayer._owner == this.getCurrentIdentity()._displayName || this.currentLayer._owner == ''){
			
			var emptyLayer = true;

			for (key in os.currentLayer.getObjects() ){
				emptyLayer = false;
				break;
			}
			
			if(emptyLayer){
				this.toolbar._shareButton.setLabel(STRING_share_layer);
			}else{
				this.toolbar._applicationBar.activeAllButtons();
				this.toolbar._applicationBar._displayable.show();
				
				this.toolbar._shareButton.setLabel(STRING_update_layer);
			}
			
			
			this.toolbar._layerTitleButton.enable();
			this.toolbar.setTitleText(this.currentLayer._title);
			this.toolbar._infoButton.enable();
			this.toolbar._emailButton.enable();
			this.toolbar._commentButton.enable();
			this.toolbar._likeButton.disable();
			this.toolbar._reportButton.disable();
		}else{
			this.toolbar._shareButton.disable();
			this.toolbar._layerTitleButton.disable();
			this.toolbar.setTitleText(this.currentLayer._title);
			this.toolbar._infoButton.enable();
			this.toolbar._emailButton.enable();
			this.toolbar._commentButton.enable();
			this.toolbar._likeButton.enable();
			this.toolbar._likeButton.setState();
			this.toolbar._reportButton.enable();
		}	
	}catch(e){
		// User is not logged in.
	}


	var objects = layer.getObjects();
	console.info(objects);

	var me = this;

	var apps = this.getApplicationList();
	var appsId = new Array();

	var ai = 0;

	while(ai<apps.length){
		var tmp = apps[ai]._id;
		appsId[tmp] = ai;
		ai++;
	}

	var i = 0;
	for( i in objects){
	
		var app = objects[i]._application;
	
		//Check if the application that created the object exists
		try{
			var appToStart =apps[appsId[app]];

			os.loadApp(appToStart,
				function(anApp){
					try{
						console.info(anApp);
						// Tell apps to initialize by rendering the objects, and telling them we have not yet loaded the backgroundPage
						anApp.init(true, false);
					}catch(e){
						console.error(e);
						throw e;
					}
				
				}
			);			
		}catch(e){
			console.error("Application does not exist!");
			throw e;
		}
	}
};

/*
 * openLayerAfterBGPLoad
 * 
 * Opens the rest of the objects pending to be oppened after the BackgroundPage is loaded
 * @param {layer} Layer The layer to be opened
 * 
 */
LayerOS.prototype.openLayerAfterBGPLoad = function(layer){

	try{		
		// Check if the current user is the current layer's owner and activate the publish button, and make title editable if so
		if (this.currentLayer._owner == this.getCurrentIdentity()._displayName || this.currentLayer._owner == ''){
		
			var emptyLayer = true;

			for (key in os.currentLayer.getObjects() ){
				emptyLayer = false;
				break;
			}
		
			if(emptyLayer){
				console.info("layer empty");
				this.toolbar._shareButton.setLabel(STRING_share_layer);
				this.toolbar._shareButton.disable();
			}else{
				console.info("layer not empty");
				this.toolbar._applicationBar.activeAllButtons();
				this.toolbar._applicationBar._displayable.show();
				
				this.toolbar._shareButton.setLabel(STRING_update_layer);
				this.toolbar._shareButton.disable();
			}
		
		
			this.toolbar._layerTitleButton.enable();
			this.toolbar.setTitleText(this.currentLayer._title);
			this.toolbar._infoButton.enable();
			this.toolbar._emailButton.enable();
			this.toolbar._commentButton.enable();
			this.toolbar._likeButton.disable();
			this.toolbar._reportButton.disable();
		}else{
			this.toolbar._shareButton.disable();
			this.toolbar._layerTitleButton.disable();
			this.toolbar.setTitleText(this.currentLayer._title);
			this.toolbar._infoButton.enable();
			this.toolbar._emailButton.enable();
			this.toolbar._commentButton.enable();
			this.toolbar._likeButton.enable();
			this.toolbar._likeButton.setState();
			this.toolbar._reportButton.enable();
		}	
	}catch(e){
		// User is not logged in.
	}


	var objects = layer.getObjects();

	var i = 0;
	for (i in objects) {
		try{
			var app = objects[i]._application;
			this.openedApps[app].init(true, true);
		}catch(e){
			//console.error(e);
		}	
	}
};



/**
 * getLayer
 * 
 * Returns a Layer object identified by layerId
 *
 * Created on 01/04/2009
 * @author "Pablo Casado (pablo.casado@layers.com)"
 * @version <1.0>
 *  
 * @return Layer
 * 
 * @throws {Exception} If the user cannot view that layer
 * @throws {Exception} If the layer does not exist
 *
 */
LayerOS.prototype.getLayer = function(layerId){
	try{
		var theLayer = this.proxy.getLayerById(layerId);
		return theLayer;
	}catch(e){
		console.info(e.msg);
		throw new Exception(e.msg);
	}
};



/**
 * login
 * <br /><br />
 * This function logs a User into the system
 * <br /><br />
 * Created on 01/04/2009
 * 
 * @memberOf LayerOS
 * @function
 * 
 * @param {username} String The username
 * @param {passwrod} String The password
 * 
 * @return User
 * 
 * @throws {Exception} If the username or the password do not match
 * 
 * @author Pablo Casado (pablo.casado@layers.com)
 * @version <1.0>
 * 
 */
LayerOS.prototype.login = function(username, password){
	
	if(username == ""){
		console.info(ERROR000002);
		throw Exception(ERROR000002);
	}else if(password == ""){
		console.info(ERROR000003);
		throw Exception(ERROR000003);
	}else{
		try{
			var theUser = this.proxy.login(username, password);
			if(theUser != null){
				this.currentUser = theUser;
				this.currentIdentity = theUser.getMainIdentity();
				return theUser;
			}
		}catch(e){
			throw new Exception(e.msg);
		}
	}
};



/**
 * logout
 * <br /><br />
 * This function logs a User out of the system
 * <br /><br />
 * Created on 09/04/2009
 * 
 * @memberOf LayerOS
 * @function
 
 * 
 * @throws {Exception} If the user os not logged into the system
 * 
 * @author Pablo Casado (pablo.casado@layers.com)
 * @version <1.0>
 * 
 */
LayerOS.prototype.logout = function(){
	 try{
		this.proxy.logout();
	}catch(e){
		throw e;
	}
};



/**
 * getCurrentUser
 * <br /><br />
 * This function returns the current User (that who logged into the system)
 * <br /><br />
 * Created on 22/04/2009
 * 
 * @memberOf LayerOS
 * @function
 * 
 * @returns {USer} The current logged in User
 * 
 * @author Pablo Casado (pablo.casado@layers.com)
 * @version <1.0>
 */
LayerOS.prototype.getCurrentUser = function(){
	return this.currentUser;
};


 
/**
  * getApplicationList
  * <br /><br />
  * This function returns an Array containing Application Objects
  * <br /><br />
  * Created on 22/04/2009
  * 
  * @memberOf LayerOS
  * @function
  * 
  * @returns {Array} An array containing the Applications that the User can use
  * 
  * @author Pablo Casado (pablo.casado@layers.com)
  * @version <1.0>
  */
LayerOS.prototype.getApplicationList = function(){
	  res = new Array();
	  res[0] = new Application("HighLight", layersOS_spm_site+"apps/HighLight/HighLight.js", "http://edita.layers.com/deploy/img/apps_highlight.gif",false);
	  res[1] = new Application("Annotate", layersOS_spm_site+"apps/Annotate/Annotate.js", "http://edita.layers.com/deploy/img/apps_annotate.gif",true);
	  //res[2] = new Application("Twitty", layersOS_spm_site+"apps/Twitty/Twitty.js", "http://agenciadigital.es/Annotate/img/annotate_ico.gif",true);
	  
	  return res;
};


/*
 *
 *
 */
LayerOS.prototype.getOpenedApps = function(){
	return this.openedApps;
};


/*
 *
 *
 */
LayerOS.prototype.startApp = function(app){
	console.info("Starting: " + app._id);
	
	try{
	  this.openedApps[app._id] = eval("new " + app._id +"()");
  }catch(e){
    console.info("LayerOS.prototype.startApp - 882");
	  console.info(e);
	}
	
  	console.info(this.openedApps[app._id]);
  	//this.openedApps[app._id].init();
  
  try{
    this.openedApps[app._id].setAppInfo(app);
  }catch(e){
    console.info("LayerOS.prototype.startApp - 82");
	  console.info(e);
	}
  	
  	return this.openedApps[app._id];
  	
};


/*
 *
 *
 */
LayerOS.prototype.loadApp = function(app,fn){
	var jsUrl = app._codeURL;
	
	
	console.info("LayerOS.prototype.loadApp");
	console.info(app._id);
	
	for (myapp in this.openedApps){
		console.info(this.openedApps[myapp._id]);	
	}
	
	var me = this;
	var appObj;
	
	this.importScript(jsUrl, app._id,
		function(){
			appObj = me.startApp(app);
			console.info(appObj);
			if(fn)fn(appObj);
		}
	);
};


/**
 * getCurrentIdentity
 * <br /><br />
 * This function returns the name of the current Identity of the logged User
 * <br /><br />
 * Created on 22/04/2009
 * 
 * @memberOf LayerOS
 * @function
 * 
 * @returns {String} The name of the current identity
 * 
 * @author Pablo Casado (pablo.casado@layers.com)
 * @version <1.0>
 */
LayerOS.prototype.getCurrentIdentity = function(){
	return this.currentIdentity;
};




/**
 * setCurrentIdentity
 * <br /><br />
 * This function sets the identity of the current logged User to that which
 * name is passed as an argument to the function
 * <br /><br />
 * Created on 22/04/2009
 * 
 * @memberOf LayerOS
 * @function
 * 
 * @returns {String} The name of the desired identity
 * 
 * @throws {Exception} If the argument desiredIdentity is not one of the
 * identities of the currently logged User
 * @throws {Exception} If there is not any logged user
 * 
 * @author Pablo Casado (pablo.casado@layers.com)
 * @version <1.0>
 */
LayerOS.prototype.setCurrentIdentity = function(desiredIdentity){	
	if(this.currentUser == undefined){
		throw Exception(ERROR00000);
	}
	
	var tmp = this.currentUser.getIdentities();
	
	i = 0;
	found = false;	
	while(!found && i < tmp.length){
		
		if(tmp[i]._displayName == desiredIdentity){
			found = true;
		}else{
			i++;
		}
	}
		
	if(i == tmp.length) {
		throw Exception(ERROR00000);
	}else{
		console.info('identidad: ' + tmp[i]._displayName);
		this.proxy.changeIdentity(tmp[i]._displayName);
		this.currentIdentity = tmp[i];
	}
};




/**
 * setCurrentLayer
 * <br /><br />
 * This function sets the current Layer to that received as an argument
 * <br /><br />
 * Created on 22/04/2009
 * 
 * @memberOf LayerOS
 * @function
 * 
 * @param {Number} LayerId The identifier of the current Layer 
 * 
 * 
 * @author Pablo Casado (pablo.casado@layers.com)
 * @version <1.0>
 */
LayerOS.prototype.setCurrentLayer = function(layer){
	this.currentLayer = layer;
};



/**
 * setCurrentObject
 * <br /><br />
 * This function sets the current object to that received as an argument
 * <br /><br />
 * Created on 04/05/2009
 * 
 * 
 * @memberOf LayerOS
 * @function
 * 
 * @param {Object} Object An object in the screen
 * 
 * @author Pablo Casado (pablo.casado@layers.com)
 * @version <1.0>
 */
 /*
LayerOS.prototype.setCurrentObject = function(anObject){

}
*/


/**
 * getCurrentObject
 * <br /><br />
 * This function returns the current object
 * <br /><br />
 * Created on 04/05/2009
 * 
 * 
 * @memberOf LayerOS
 * @function
 * 
 * @returns {Object} The current Object
 * 
 * @author Pablo Casado (pablo.casado@layers.com)
 * @version <1.0>
 * 
 */
LayerOS.prototype.getCurrentObject = function(){
	 return this.currentObject;
};
 
 
 
 

/**
 * register
 * <br /><br />
 * This function implements the creation of a new User in the system
 * <br /><br />
 * Created on 01/04/2009
 * 
 * @memberOf LayerOS
 * @function
 * 
 * @param {username} String The username
 * @param {password} String The password
 * @param {email} String The email
 * 
 * @return User
 * 
 * @throws {Exception} If the username already exists
 * @throws {Exception} If the email is already registered
 * 
 * Created on 08/04/2009
 * @author Pablo Casado (pablo.casado@layers.com)
 * @version <1.0>
 * 
 */
LayerOS.prototype.register = function(username, password, email){
	if(username == ""){
		console.info(ERROR000002);
		throw Exception(ERROR000002);
	}else if(password == ""){
		console.info(ERROR000003);
		throw Exception(ERROR000003);
	}else if(email == ""){
		console.info(ERROR000005);
		throw Exception(ERROR000005);
	}else{
		try{
			var theUser = this.proxy.register(username, password, email);
			if(theUser != null){
				return theUser;
			}
		}catch(e){
			console.info("Error in register");
			throw e;
		}
	}
};



/**
 * getLayersByMyFriends
 * <br /><br />
 * This function return a LayerIterator object containing all the Layers created
 * by the friends of the current users over any page
 * <br /><br />
 * Created on 01/04/2009
 * 
 * @memberOf LayerOS
 * @function
 * 
 * @return LayerIterator
 * 
 * @throws {Exception} If there is any problem with the connection, or the returnes
 * XML does not contain a marshalled LayerIterator
 * 
 * Created on 08/04/2009
 * @author Pablo Casado (pablo.casado@layers.com)
 * @version <1.0>
 * 
 */
LayerOS.prototype.getLayersByMyFriends = function(){
 	try{
		var layerIterator = this.proxy.getLayersByMyFriends();
		if(layerIterator != null) return layerIterator;
	}catch(e){
		console.info("Error in getLayersByMyFriends");
		throw e;
	}
};


/*
 * getLayersOverThisPageCallBack
 * 
 */
LayerOS.prototype.getLayersOverThisPageCallBack = function(data){
	//console.info("getLayersOverThisPageCallBack");
	this.toolbar.fillLayersAccordion(data);
};

/**
 * getLayersOverThisPage
 * <br /><br />
 * This function return a LayerIterator object containing all the Layers that
 * the current user of the system can see over the page that he/she is visiting
 * <br /><br />
 * Created on 01/04/2009
 * 
 * @memberOf LayerOS
 * @function
 * 
 * @return LayerIterator
 * 
 * @throws {Exception} If there is any problem with the connection, or the returnes
 * XML does not contain a marshalled LayerIterator
 * 
 * Created on 08/04/2009
 * @author Pablo Casado (pablo.casado@layers.com)
 * @version <1.0>
 * 
 */
LayerOS.prototype.getLayersOverThisPage = function(){
	try{
		var layerIterator = this.proxy.getLayersOverThisPage();		
		
		if(LayerIterator != null){
			return layerIterator;
		}
	}catch(e){
		console.info("Error in getLayersOverThisPage");
		throw e;
	}
};


/**
 * getLayersOverThisPageByMyFriends
 * <br /><br />
 * This function return a LayerIterator object containing all the Layers
 * published by the friends of the current user (those layers created by people
 * in his/her following list) over the current page.
 * <br /><br />
 * Created on 20/04/2009
 * 
 * @memberOf LayerOS
 * @function
 * 
 * @return LayerIterator
 * 
 * @throws {Exception} If there is any problem with the connection, or the returned
 * XML does not contain a marshalled LayerIterator
 * 
 * Created on 08/04/2009
 * @author Pablo Casado (pablo.casado@layers.com)
 * @version <1.0>
 * 
 */
LayerOS.prototype.getLayersOverThisPageByMyFriends = function(){
	try{
		var layerIterator = this.proxy.getLayersOverThisPageByMyFriends();
		
		if(layerIterator != null){
			return layerIterator;
		}
	}catch(e){
		console.info("Error in getLayersOverThisPageByMyFriends");
		throw e;
	}
};



/**
 * getMyLayersOverThisPage
 * <br /><br />
 * This function return a LayerIterator object containing all the Layers
 * created by the current user
 * <br /><br />
 * Created on 20/04/2009
 * 
 * @memberOf LayerOS
 * @function
 * 
 * @return LayerIterator
 * 
 * @throws {Exception} If there is any problem with the connection, or the
 * returned XML does not contain a marshalled LayerIterator
 * 
 * Created on 08/04/2009
 * @author Pablo Casado (pablo.casado@layers.com)
 * @version <1.0>
 * 
 */
LayerOS.prototype.getMyLayersOverThisPage = function(){
	try{
		var layerIterator = this.proxy.getMyLayersOverThisPage();
		if(layerIterator != null){
			return layerIterator;
		}
	}catch(e){
		console.info("Error in getMyLayersOverThisPage");
		throw e;
	}
};


/**
 * getLayersOverThisPageNotFriends
 * <br /><br />
 * This function return a LayerIterator object containing all the Layers
 * created by any user other than the current user's friends.
 * <br /><br />
 * Created on 21/05/2009
 * 
 * @memberOf LayerOS
 * @function
 * 
 * @return LayerIterator
 * 
 * @throws {Exception} If there is any problem with the connection, or the
 * returned XML does not contain a marshalled LayerIterator
 * 
 * Created on 21/05/2009
 * @author Antonio Perez (tony@layers.com)
 * @version <1.0>
 * 
 */
LayerOS.prototype.getLayersOverThisPageNotFriends = function(){
	try{
		var layerIterator = this.proxy.getLayersOverThisPageNotFriends();
		if(layerIterator != null){
			return layerIterator;
		}
	}catch(e){
		console.info("Error in getLayersOverThisPageNotFriends");
		throw e;
	}
};



/**
 * follow
 * <br /><br />
 * By calling this function, the logged user will start following the one that
 * is viewing.
 * <br /><br />
 * Created on 12/04/2009
 * 
 * @memberOf LayerOS
 * @function
 * 
 * 
 * @throws {Exception} If there is any problem with the connection
 * 
 * @author Pablo Casado (pablo.casado@layers.com)
 * @version <1.0>
 * 
 */
LayerOS.prototype.follow = function(whoToFollow){
 	try{
		this.proxy.follow(whoToFollow)
	}catch(e){
		console.info("Error in follow");
		throw e;
	}
};
 
 
 
 
/**
 * stopFollowing
 * <br /><br />
 * By calling this function, the logged user will stop following the one that
 * is viewing.
 * <br /><br />
 * Created on 12/04/2009
 * 
 * @memberOf LayerOS
 * @function
 * 
 * @throws {Exception} If there is any problem with the connection
 * 
 * @author Pablo Casado (pablo.casado@layers.com)
 * @version <1.0>
 * 
 */
LayerOS.prototype.stopFollowing = function(who){
 	try{
		this.proxy.stopFollow(who);
	}catch(e){
		console.info("Error in stopfollow");
		throw e;
	}
};
 
 
 
 
/**
 * getCurrentLayer
 * <br />
 * Returns the active layer in the LayerOS
 * <br />
 * <br />
 * Created on 21/04/2009
 * 
 * @memberOf LayerOS
 * @function
 * 
 * @return {Layer} The current Layer.
 *
 * @author Pablo Casado (pablo.casado@layers.com)
 * @version <1.0>
 * 
 */
LayerOS.prototype.getCurrentLayer = function(){
	return this.currentLayer;
};
 

/**
 * removeCurrentLayer
 * <br />
 * Set the current Layer to null
 * <br />
 * <br />
 * Created on 21/04/2009
 * 
 * @memberOf LayerOS
 * @function
 *
 * @author Pablo Casado (pablo.casado@layers.com)
 * @version <1.0>
 * 
 */
LayerOS.prototype.removeCurrentLayer = function(){
	$("#"+this.currentLayer._seed).remove();

	var application = '';
	console.info("openedApps->");
	console.info(this.openedApps);
	console.info("<-openedApps");
		
	for (application in this.openedApps){
	  try{
	    this.openedApps[application].clear();
	  }catch(e){console.info(e);}
	}

	console.info(this.openedApps);
	try{
	  this.currentLayer = new Layer();
	}catch(e){
	  console.info(e);
	}
	
	console.info(this.currentLayer);
};



 
/**
 * rateLayer
 * <br />
 * Updates the rating of the current Layer
 * <br />
 * <br />
 * Created on 21/04/2009
 * 
 * @memberOf LayerOS
 * @function
 * 
 * @param {rate} Number The rating for the layer
 *
 * @author Pablo Casado (pablo.casado@layers.com)
 * @version <1.0>
 * 
 */
LayerOS.prototype.rateLayer = function(rate){
	return this.proxy.rateLayer(this.currentLayer._id, rate);
};

 
 
 
/**
 * 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 LayerOS
 * @function
 * 
 * @param {rate} Number The rating for the layer
 *
 * @author Pablo Casado (pablo.casado@layers.com)
 * @version <1.0>
 * 
 */
LayerOS.prototype.searchUsers = function(queryStr){
	return this.proxy.searchUsers(queryStr);
};


 

/**
 * 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 LayerOS
 * @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.
 * @throws {Exception} In case the parsing of the XML document fails.
 */
LayerOS.prototype.sendMail = function(users, emails, text, title){
	this.proxy.sendMail(users, emails, text, title);
};

 
 
 /**
  * saveLayer
  * <br />
  * Saves a Layer and its objects into the remote system. 
  * <br />
  * <br />
  * Created on 12/05/2009
  * 
  * @memberOf LayerOS
  * @function
  * 
  * @param {Array} data An array of the objects that the layer will contain
  * 
  */
LayerOS.prototype.saveLayer = function(data){
	/*
	  console.info("LayerOS.prototype.saveLayer");
	  
	  // Update currentLayer data
//	  this.updateCurrentLayer(data);
	  
	  console.info("voy a hacer la llamada");
	  //Save the Layer
//	  var tmpLayer = this.proxy.saveLayer();	  
	  	  	  
	  //Update the layer with the info returned from the server	  
	  this.currentLayer._id = tmpLayer._id;
	  this.currentLayer._title = tmpLayer._title;
	  this.currentLayer._description = tmpLayer._description;
	  
	  var x = 0;
	  
	  while(x < tmpLayer._data.length){		  							  
		  this.getCurrentLayer()._data[x]._cdata = tmpLayer._data[x]._cdata;
		  this.getCurrentLayer()._data[x]._id = tmpLayer._data[x]._id;
		  x++;
	  }
		  	  
	  tmpArray = null;
	  tmpLayer = null;
	  return 
	*/
	this.updateCurrentLayer(data);
	console.info("currentLayer");
	console.info(os.getCurrentLayer());

/*	
	this.updateCurrentLayer(data);
	this.currentLayer._id = tmpLayer._id;
  this.currentLayer._title = tmpLayer._title;
  
  var x = 0;
  
  while(x < tmpLayer._data.length){		  							  
	  this.getCurrentLayer()._data[x]._cdata = tmpLayer._data[x]._cdata;
	  this.getCurrentLayer()._data[x]._id = tmpLayer._data[x]._id;
	  x++;
  }
  */
	try{
		this.currentLayer.setDescription( this.toolbar._applicationBar.getLayerDescriptionFromApplication() );
	}catch(e){
		//console.error(e);
		this.currentLayer.setDescription("My Description");
	}
	
	console.info("LayersOS saveLayer - openedApps");
	console.info(this.openedApps);
	
	if(this.openedApps.length == 1){
		console.info(this.openedApps[0]._id);
		this.currentLayer._application = this.openedApps[0]._id;
	}
	
	this.proxy.saveLayer();
};


 /**
  * updateCurrentLayer
  * <br />
  * Updates currentLayer data. Data should be synchronized to allow future updates on the server side. Nothing is saved in the server here, just the currentLayer object is updated. 
  * <br />
  * <br />
  * Created on 12/05/2009
  * 
  * @memberOf LayerOS
  * @function
  * 
  * @param {Array} data An array of the objects that the layer will contain
  * 
  */
LayerOS.prototype.updateCurrentLayer = function(data){
	  console.info("LayerOS.prototype.updateCurrentLayer");
	  
	  // Prepare the Layer to be saved
	  //this.getCurrentLayer()._data = this.getCurrentLayer()._data.concat(data);
	  console.info("concat");
	  console.info(this.getCurrentLayer()._data);	  
	  	  	  
	  var i = 0;
	  
	  // We keep track if the object we have in currentLayer has been found inside the data received.
	  var found = true;
	  
	  while(i < this.getCurrentLayer()._data.length){	 		  		  
		  
		  found = false;	  
		  
		  var tmpObj = this.getCurrentLayer()._data[i];		 
		  
		  //var j=i;
		  var j= 0;
		  while( j < data.length ){			  
			  try{
			  	console.info(data[j]._seed);
				console.info(tmpObj._seed);
				  if(tmpObj._seed == data[j]._seed){					  		  
				  	// Objects have the same seed, so they are the same. Copy all values from the one we are receiving to the one we have in memory.					
					  this.getCurrentLayer()._data[i]._cdata = data[j]._cdata;
					  this.getCurrentLayer()._data[i]._title = data[j]._title;					  
					  this.getCurrentLayer()._data[i]._dirtyCdata = data[j]._dirtyCdata;
					  this.getCurrentLayer()._data[i]._dirtyTitle = data[j]._dirtyTitle;
					  data.splice(j,1);
					  found = true;					  				  
				  }
			  }catch(e){
				  console.error(e);
			  }
			  j++;
			  console.info(j);
		  }
		  console.info(found);
		  if ( !found )
		  {
		  	console.info('layer no encontrado');
		  	// If the previous object was not found inside the data objects received, we set its cdata to null. LayersOSObject will build the proper XML to let the server know he has to delete that object.
		  	this.getCurrentLayer()._data[i]._cdata = '';			
		  }
		  	
		  i++;
		  console.info(i);
	  }
	  
	  // We check if there are still objects in data received. If so, we add them to the current layer.	  
	  if ( data.length > 0 ) {
	  	this.getCurrentLayer()._data = this.getCurrentLayer()._data.concat(data);
	  }
	  	  
	  console.info("after merging");	  
	  
	  console.info(this.getCurrentLayer());
	  	  
	  return; 
};

  
/**
 * publishLayer
 * 
 * Sets the currentLayer as visible by all the users of Layers
 * 
 * Created on 15/05/2009
 * 
 * @memberOf LayerOS
 * @function
 * 
 * @author "Pablo Casado (pablo.casado@layers.com)"
 * @version <1.0>
 * 
 * @throws {Exception} In case the publishing cannot be performed
 *
 */
LayerOS.prototype.publishLayer = function(title, description){
	try{
		
		//Check whether the layer about to be published is publishable
		if(this.getCurrentLayer()._id != null && this.getCurrentLayer()._id != undefined && this.getCurrentLayer()._id != ""){
			this.getCurrentLayer()._description = description;
			this.proxy.publishLayer(this.getCurrentLayer()._id, title, description);
		}
		else throw new Exception("Please, save your layer before publishing");
	}catch(e){
		throw e;
	}
};


/**
 * likeLayer
 * 
 * Sets the currentLayer as visible by all the users of Layers
 * 
 * Created on 15/05/2009
 * 
 * @memberOf LayerOS
 * @function
 * 
 * @author "Pablo Casado (pablo.casado@layers.com)"
 * @version <1.0>
 * 
 * @throws {Exception} In case the publishing cannot be performed
 *
 */
LayerOS.prototype.likeLayer = function(){
	try{		
		this.proxy.likeLayer( this.getCurrentLayer()._id );					
	}catch(e){
		throw e;
	}
};


/**
 * reportLayer
 * 
 * Report a possibly unintended layer
 * 
 * Created on 15/07/2009
 * 
 * @memberOf LayerOS
 * @function
 * 
 * @author "Antonio Perez (tony@layers.com)"
 * @version <1.0>
 * 
 *
 */
LayerOS.prototype.reportLayer = function(){
	try{
		this.proxy.reportLayer( this.getCurrentLayer()._id );		
	}catch(e){
		throw e;
	}
};


/**
 * commentLayer
 * 
 * Add a comment to a layer
 * 
 * Created on 15/07/2009
 * 
 * @memberOf LayerOS
 * @function
 * 
 * @author "Antonio Perez (tony@layers.com)"
 * @version <1.0>
 * 
 *
 */
LayerOS.prototype.commentLayer = function(comment){
	try{			
		this.proxy.commentLayer( this.getCurrentLayer()._id, comment );		
	}catch(e){
		throw e;
	}
};

/**
 * getBackgroundPageObject
 * 
 * Returns the page where is the contain of the web to create the layer
 *
 * Created on 08/04/2009
 * 
 * @memberOf LayerOS
 * @function
 * 
 * @author "Francesc Navarro (francesc.navarro@layers.com)"
 * @version <1.0>
 * 
 * @return element
 */
LayerOS.prototype.getBackgroundPageObject = function(){
  console.info("LayerOS.prototype.getBackgroundPageObject");
	if(document.frames) {
		//IE
	  console.info(document);
		return document; 
	}else {
		console.info(document);
    //Others
		return document;
	}
};





/**
 * getBackgroundContent
 * 
 * Returns the content of backgroundPage
 *
 * Created on 09/04/2009
 * 
 * @memberOf LayerOS
 * @function
 * 
 * @author "Francesc Navarro (francesc.navarro@layers.com)"
 * @version <1.0>
 * 
 * @return element
 */
LayerOS.prototype.getBackgroundContent = function(){
	return $('#edita-cms-frame').contents();
};
 

/**
 * getBackgroundURL
 * 
 * Legacy!!!
 *
 * Returns the background page URL
 *
 * Created on 25/05/2009
 * 
 * @memberOf LayerOS
 * @function
 * 
 * @author "Antonio Perez (tony@layers.com)"
 * @version <1.0>
 * 
 * @return string real URL of the background page, stripping the proxy out.
 */
LayerOS.prototype.getBackgroundURL = function(){
	//var regexp = /.*bgp\/(.*)/gi;		
	var siteURL = document.location.href; 	
	console.info("siteURL");
	console.info(siteURL);
	return siteURL; //.replace(regexp,'http:\/\/$1');		
};


/**
 * Finds the real position of an element. Very Important!
 * 
 *
 * Created on 10/03/2009
 * 
 * @memberOf LayerOS
 * @function
 * 
 * @author	"Francesc Navarro"
 * @version	0.1.0
 * @todo	TODO
 * @type	{Object}
 *
 * @param obj
 * @return pos object with top and left position
 * 
 */
LayerOS.prototype.findPos = function(obj){
	if(!$.browser.safari){
		return [parseInt(obj.getBoundingClientRect().left), parseInt(obj.getBoundingClientRect().top)];	
	}else{
		var curleft = curtop = 0;
		//var el = obj;
		if (obj.offsetParent) {
			do {
				curleft += obj.offsetLeft;
				curtop += obj.offsetTop;
			}
			while (obj = obj.offsetParent);
		}
		return [curleft, curtop];
	}
};

/**
 * getWindowTopScrollOffset
 * 
 *
 * Created on 18/06/2009
 * 
 * @memberOf LayerOS
 * @function
 * 
 * @author	"Francesc Navarro"
 * @version	0.1.0
 * @todo	TODO
 * @type	{Object}
 *
 * @return
 * 
 */
LayerOS.prototype.getWindowTopScrollOffset = function()
{
	 if(window.pageYOffset){
		return window.pageYOffset;
	 }
	 else {
		return  Math.max(document.body.scrollTop, document.documentElement.scrollTop);
	 }
};

 
 
/**
 * getWindowLeftScrollOffset
 * 
 *
 * Created on 18/06/2009
 * 
 * @memberOf LayerOS
 * @function
 * 
 * @author	"Francesc Navarro"
 * @version	0.1.0
 * @todo	TODO
 * @type	{Object}
 *
 * @return
 * 
 */
LayerOS.prototype.getWindowLeftScrollOffset = function(){
	 if(window.pageXOffset){
		return window.pageXOffset;
	 }
	 else {
		return  Math.max(document.body.scrollLeft, document.documentElement.scrollLeft);
	 }
};

/**
 * getTopScrollOffset
 * 
 *
 * Created on 10/03/2009
 * 
 * @memberOf LayerOS
 * @function
 * 
 * @author	"Antonio Perez"
 * @version	0.1.0
 * @todo	TODO
 * @type	{Object}
 *
 * @return
 * 
 */
LayerOS.prototype.getTopScrollOffset = function()
{
	 if(this.getBackgroundPageObject().pageYOffset){
		return this.getBackgroundPageObject().pageYOffset;
	 }
	 else {
		return  Math.max(this.getBackgroundPageObject().body.scrollTop, this.getBackgroundPageObject().documentElement.scrollTop);
	 }
};

 
 
/**
 * getTopScrollOffset
 * 
 *
 * Created on 10/03/2009
 * 
 * @memberOf LayerOS
 * @function
 * 
 * @author	"Antonio Perez"
 * @version	0.1.0
 * @todo	TODO
 * @type	{Object}
 *
 * @return
 * 
 */
LayerOS.prototype.getLeftScrollOffset = function()
{
	 if(this.getBackgroundPageObject().pageXOffset){
		return this.getBackgroundPageObject().pageXOffset;
	 }
	 else {
		return  Math.max(this.getBackgroundPageObject().body.scrollLeft, this.getBackgroundPageObject().documentElement.scrollLeft);
	 }
};


/**
 * getTabBarHeight
 * 
 *
 * Created on 10/03/2009
 * 
 * @memberOf LayerOS
 * @function
 * 
 * @author	"Antonio Perez"
 * @version	0.1.0
 * @todo	TODO
 * @type	{Object}
 *
 * @return
 * 
 */
LayerOS.prototype.getTabBarHeight = function()
{
	return parseInt($('#layersOS_dock').height());
};

/**
 * objectToString
 * <br />
 * Converts the object to a string to be stored in the cdata
 * <br />
 *
 * Created on 21/04/2009
 * 
 * @memberOf LayerOS
 * @function
 * 
 * @author	"Francesc Navarro"
 * @version	0.1.0
 * @todo	TODO
 *
 * @param object The object to be transformed
 * @return {String} the object passed to String
 * 
 */
LayerOS.prototype.objectToString = function(object){
	return escape(JSON.stringify(object));
};





/**
 * stringToObject
 * <br />
 * Converts the string to a javascript object
 * <br />
 *
 * Created on 21/04/2009
 * 
 * @memberOf LayerOS
 * @function
 * 
 * @author	"Francesc Navarro"
 * @version	0.1.0
 * @todo	TODO
 *
 * @param obj
 * @return {Object} the object
 * 
 */
LayerOS.prototype.stringToObject = function(str){
	var myObject = eval( unescape(str) );
	return myObject;
};




/********************************************************************************/
/*                              CALLBACK FUNCTIONS                              */
/********************************************************************************/

LayerOS.prototype.showError = function(data, aFunction){	
	
	if( data != 401){
		try{
		  try{
		    this.toolbar._loadingGauge.hide();
		  }catch(e){}
			
			var pop= this.getPopUpBoard();		
			pop.show();
			this.toolbar._messageBoard.show(data, aFunction);
		}catch(e){
		  console.error("os- 2036");
			this._bgPage = $('#edita-cms-frame');
			this.toolbar._messageBoard.show(data, aFunction);
		}
	}else{
	  console.info("LayerOS.prototype.showError - 2044");
		this.toolbar.drawNotLogged();
	}	
	
};


LayerOS.prototype.setNotLoggedData = function(){
	
	var tmp = this.currentLayer;
	
	this.removeCurrentLayer();
		
	if ( tmp._id > 0 ){
		this.setCurrentLayer(tmp);
	}	
	
	this.currentUser = null;
	this.currentIdentity = null;
	this.toolbar.drawNotLogged();
	
};

/**
 * setLoginData
 * <br />
 * Operations to perform after a successful login. User and identity will be set, and the currentLayer will be removed if so requested. Also, logged bar will be drawn. 
 * <br />
 * 
 * Created on 21/07/2009
 * 
 * @memberOf LayerOS
 * @function
 * 
 * @author	"Pablo Casado"
 * @version	2.0
 *
 * @param {User} data User object to be set.
 * @param {boolean} removeCurrentLayer Whether to remove the current layer or not.
 * 
 */
LayerOS.prototype.setLoginData = function(data, removeCurrentLayer){
	this.currentUser = data;
	
	if (removeCurrentLayer){
		this.removeCurrentLayer();
		this.toolbar._permalink = false;
	}
	
	this.currentIdentity = this.currentUser.getMainIdentity();
	console.info("this.currentUser");
	console.info(this.currentUser);
	console.info("this.currentIdentity");
	console.info(this.currentIdentity);
	LayersOSToolbar.prototype._alreadyRendered = false;
	this.toolbar.drawLogged();

};


/*
 * setGetLayersOverThisPageData
 * @param {LayerIterator} data A
 */
LayerOS.prototype.setGetLayersOverThisPageData = function(data){	
	this.toolbar.populateLayersOverThisPage(data);
};


LayerOS.prototype.setGetMyLayersOverThisPageData = function(data){
	this.toolbar.populateMyLayersOverThisPage(data);
};

LayerOS.prototype.setGetLayersByMyFriendsData = function(data){
	this.toolbar.populateLayersByMyFriends(data);
};

LayerOS.prototype.setGetLayersOverThisPageByMyFriendsData = function(data){
	this.toolbar.populateFriendsLayersOverThisPage(data);
};

LayerOS.prototype.setLikedLayer = function(){
	this.toolbar.setLikedLayer();
};

LayerOS.prototype.setReportedLayer = function(){
	this.toolbar.setReportedLayer();
};

LayerOS.prototype.setCommentLayer = function(){
	this.toolbar.setCommentedLayer();
};

LayerOS.prototype.setSendEmail = function(){
	this.toolbar.setSendEmail();
};


LayerOS.prototype.setLayer = function(data){
	this.setCurrentLayer(data);
	this.toolbar.loadBgPage2();
};

LayerOS.prototype.setSavedLayer = function(layer){	
	this.setCurrentLayer(layer);	
	this.toolbar.layerPublished();
};

LayerOS.prototype.setGetNotFriendsLayersOverThisPageData = function(data){
	this.toolbar.populateOtherLayersOverThisPage(data);
};

