/**
 * @namespace
 * Class LayersObject
 * <br />
 * This class models an LayersObject in a Layer
 * @class LayersObject
 * 
 *
 * @property {number} _id The identifier of the LayersObject
 * @property {String} _seed The identifier of the LayersObject in the DOM 
 * @property {number} _application The identifier of the application that
 * created the LayersObject
 * @property {String} _owner The owner of the LayersObject (Identity)
 * @property {String} _ownerImage The URL of the image of the owner of the
 * LayersObject
 * @property {String} _data The data of the LayersObject (CDATA) 
 * 
 * Created on 29/04/2009
 * @author "Pablo Casado (pablo.casado@layers.com)"
 * @version <1.0>
 *  
 **/
function LayersObject(){
	this._id = "";
	this._seed = "";
	this._application = "";
	this._owner = "";
	this._ownerImage = "";
	this._cdata = "";
	this._dirtyCdata = false;
	this._dirtyTitle = false;
	this._editable = false;
}


LayersObject.prototype._id;
LayersObject.prototype._seed;
LayersObject.prototype._application;
LayersObject.prototype._owner;
LayersObject.prototype._ownerImage;
LayersObject.prototype._title;
LayersObject.prototype._dirtyTitle;
LayersObject.prototype._cdata;
LayersObject.prototype._dirtyCdata;
LayersObject.prototype._editable;


/**
 * parse
 * 
 * This function returns an LayersObject from an XMLDocument returned by the server.
 * It parses the XML received and assign all the fields to the attributes of an
 * LayersObject.
 *
 * Created on 29/04/2009
 * @author "Pablo Casado (pablo.casado@layers.com)"
 * @version <version>
 * 
 * @memberOf LayersObject
 * @function
 * 
 * @param {mydata} XMLDocument An XMLDocument
 *
 * @return {LayersObject} An LayersObject object
 * 
 * @throws {Exception} If an error occurred when creating the object from XML.
 **/
LayersObject.prototype.parse = function(myXML){
	
	try{
		this._id = myXML.attributes.getNamedItem("oid").value;
	
		this._owner = myXML.getElementsByTagName("owner")[0].childNodes[0].nodeValue;
		this._ownerImage = this._owner;
	
		this._application = myXML.getElementsByTagName("app")[0].childNodes[0].nodeValue;
		this._title = myXML.getElementsByTagName("otitle")[0].childNodes[0].nodeValue;
	
		// Deal with FF 4k limit for XML nodeValues, and use noteText instead in case we are FF.
		var contentNode = myXML.getElementsByTagName("content")[0];
	
		if(typeof(contentNode.textContent) != "undefined") this._cdata = contentNode.textContent;  
		else this._cdata = contentNode.childNodes[0].nodeValue;   
		
		
		this._dirtyCdata = false;
		this._dirtyTitle = false;
	}catch(e){
		throw e;
	}
	
	return this;
}



/**
 * getId
 * 
 * This function returns the Identifier of an object
 *
 * Created on 05/05/2009
 * @author "Pablo Casado (pablo.casado@layers.com)"
 * @version <version>
 * 
 * @memberOf Object
 * @function
 *
 * @return {Number} The Id of the object
 * 
 **/
LayersObject.prototype.getId = function(){
	return this._id;
}


/**
 * setSeed
 * 
 * Sets the seed
 *
 * Created on 11/05/2009
 * @author "Francesc Navarro <francesc.navarro@layers.com>"
 * @version <version>
 * 
 * @memberOf LayersObject
 * @function
 *
 * @return
 * 
 **/
LayersObject.prototype.setSeed = function(seed){
	this._seed = seed;
}

/**
 * getSeed
 * 
 * Returns the seed
 *
 * Created on 11/05/2009
 * @author "Francesc Navarro <francesc.navarro@layers.com>"
 * @version <version>
 * 
 * @memberOf LayersObject
 * @function
 *
 * @return
 * 
 **/
LayersObject.prototype.getSeed = function(){
	return this._seed;
}


/**
 * getTitle
 * 
 * This function returns the Identifier of an LayersObject
 *
 * Created on 05/05/2009
 * @author "Pablo Casado (pablo.casado@layers.com)"
 * @version <version>
 * 
 * @memberOf LayersObject
 * @function
 *
 * @return {String} The title of the LayersObject
 * 
 **/
LayersObject.prototype.getTitle = function(){
	return this._title;
}


 

/**
 * setTitle
 * 
 * This function sets the title of an LayersObject
 *
 * Created on 05/05/2009
 * @author "Pablo Casado (pablo.casado@layers.com)"
 * @version <version>
 * 
 * @memberOf LayersObject
 * @function
 *
 * @param {String} aTitle The nmew title of the LayersObject
 * 
 **/
LayersObject.prototype.setTitle = function(aTitle){
	this._dirtyTitle = true;
	this._title = aTitle;
}




/**
 * setContent
 * 
 * This function sets the title of an LayersObject
 *
 * Created on 05/05/2009
 * @author "Pablo Casado (pablo.casado@layers.com)"
 * @version <version>
 * 
 * @memberOf LayersObject
 * @function
 *
 * @param {String} newContent The new contents of the LayersObject
 * 
 **/
LayersObject.prototype.setContent = function(newContent){
	this._owner = os.getCurrentIdentity();
	this._dirtyCdata = true;
	this._cdata = newContent;
}



/**
* getContent
* 
* This function returns the title of an LayersObject
*
* Created on 05/05/2009
* @author "Pablo Casado (pablo.casado@layers.com)"
* @version <version>
* 
* @memberOf LayersObject
* @function
* 
* @return {String} The title of the LayersObject
* 
**/
LayersObject.prototype.getContent = function(){
	return this._cdata;
}


/*
 * @param data Any string containing
 */
LayersObject.prototype.appendToCData = function(data){
	this._dirtyCdata = true;
	this._cdata = this._cdata + data;
}



/**
* toXML
* 
* This function returns a string representing a marshalled LayersObject
*
* Created on 05/05/2009
* @author "Pablo Casado (pablo.casado@layers.com)"
* @version <version>
* 
* @memberOf LayersObject
* @function
* 
* @return {String} The title of the LayersObject
* 
**/
LayersObject.prototype.toXML = function(){
	
	var objectXML = "";
		
	if( this._id != undefined && this._id != "" ){
		//Has Id			
		try{
			if( (this._cdata != "" || this._cdata != undefined) && this._dirtyCdata ){
				//contents != null --> update!
				objectXML = "<object objid='"+this._id+"'>";
				
				if(this._dirtyCdata){
					objectXML = objectXML + "<ocontent>"+this._cdata+"</ocontent>";
				}
			
				if(this._dirtyTitle){
					objectXML = objectXML + "<otitle>"+this._title+"</otitle>";
				}

				objectXML = objectXML + "</object>";						
			}else if( this._cdata == "" ){
				//contents = null --> delete!			
				objectXML = "<object objid='"+this._id+"' delete='true'>";
				objectXML = objectXML + "</object>";						
			}else{
				// Do nothing
			}
		}catch(e){}
	
	}else{		
		//No Id --> create object!
		objectXML = "<object app='"+this._application+"'>";
		objectXML = objectXML + "<otitle>"+this._title+"</otitle>";
		objectXML = objectXML + "<owner>"+this._owner._displayName+"</owner>";
		objectXML = objectXML + "<ocontent>"+this._cdata+"</ocontent>";
		objectXML = objectXML + "</object>";
	}

	return objectXML;
}

