/********
Favorite
Usage:		new Favorite(mgid, collectionId, variableName, RefernceToTheFavoriteElementInHtml);
Example:	new Favorite('mgid:tw:user:flux.com:B1680000000068B10001000068B1', '87', 'somerandomname', this);
** it needs the reference to the Favorite element in html for DOM/style manipulation.
********/
var Favorite = function (id, varName, favoriteObj)
{
	this.ADD_DATASERVICE_URL		= "/sitewide/dataservices/content/add_to_favorites.jhtml";
	this.REMOVE_DATASERVICE_URL		= "/sitewide/dataservices/content/remove_from_favorites.jhtml";		
	this.MESSAGE = 
	{
		REMOVE_MESSAGE:
		{
			success:	"Quitado de Favoritos.",
			failure:	"Error al quitar de Favoritos.",
			inProgress:	"Aguarda por favor..."
		},
		
		ADD_MESSAGE:
		{
			success:	"Agregado a Favoritos.",
			failure:	"Error al agregar a Favoritos.",
			inProgress:	"Aguarda por favor..."
		}
		
	};
	
	this.id				= id;
	this.varName		= varName;							//this is variable name of the object which constructed a new instance of the Favorite class. Hacky -- but necessary.	
	this.favoriteObj	= favoriteObj;						//favorite button	
	this.parentObj		= favoriteObj.parentNode;			//parent obj of favorite button
	this.parentObjHtml	= favoriteObj.parentNode.innerHTML;
	
	this.Action			= 
	{
		
		ajaxMesssage:	null,
		favObjPtr:		null,
		resultObj:		null,
		
		add: function(favObjPtr)
		{
			var actionType		= "add";
			
			this.favObjPtr			= favObjPtr;
			this.ajaxMessage	= this.favObjPtr.MESSAGE.ADD_MESSAGE;	
			this.processFavoriteAction(actionType);
		},
		
		remove: function(favObjPtr)
		{
			var actionType		= "remove";
			
			this.favObjPtr			= favObjPtr;
			this.ajaxMessage	= this.favObjPtr.MESSAGE.REMOVE_MESSAGE;	
			this.processFavoriteAction(actionType);
		},
		
		createDataserviceUrl: function(actionType)
		{
			var url = null;
			
			switch(actionType)
			{
				case 'add': url = this.favObjPtr.ADD_DATASERVICE_URL+"?id="+this.favObjPtr.id; break;
				case 'remove': url = this.favObjPtr.REMOVE_DATASERVICE_URL+"?id="+this.favObjPtr.id; break;			
			}
			return url;
		},
	
		processFavoriteAction: function(actionType){
			var dsUrl		= this.createDataserviceUrl(actionType);
			var actionObjPtr	= this;	//a pointer to the original Action Object itself.			
			var ajaxReq		= new Ajax.Request
			(
				dsUrl,
				{
					method:'get',
					onSuccess: function(transport)
					{
						var response = transport.responseText || "no response text";
						
						actionObjPtr.resultObj = actionObjPtr.parseXML(response);
						
						switch(actionObjPtr.resultObj.result)
						{
							case "fail": 
								actionObjPtr.processAjaxFailure();
								break;
							case "success": 
								actionObjPtr.processAjaxSuccess();
								break;
						}
					},
					
					onLoading: function(){
						actionObjPtr.processAjaxInProgress();
					},
					
					onFailure: function(){						
						actionObjPtr.processAjaxFailure();
					}
				}
			);		
		
		},
		
		processAjaxSuccess: function(){				
			this.favObjPtr.parentObj.style.background = "#ffffcc";			
			this.favObjPtr.parentObj.innerHTML = this.ajaxMessage.success;
		},
		
		processAjaxFailure: function(){
			this.favObjPtr.parentObj.style.background = "#00ffff";				
			this.favObjPtr.parentObj.innerHTML = this.ajaxMessage.failure + "\n" + this.favObjPtr.Action.resultObj.message;
		},	
		
		processAjaxInProgress: function(){
			this.favObjPtr.parentObj.style.background = "#ff0000";
			this.favObjPtr.parentObj.innerHTML = this.ajaxMessage.inProgress;
		},

		parseXML:	function(xml)
		{
			var resultObject ={};			
			try
			{//try IE first				
				xmlObj=new ActiveXObject("Microsoft.XMLDOM");
				xmlObj.loadXML(xml)
				
			}
			catch(e)
			{
				try
				{//try Mozilla, Firefox, Opera, etc.
					xmlObj = (new DOMParser()).parseFromString(xml, "text/xml");
				}
				catch(e)
				{					
					//alert(e.message);
					return;
				}
			}			
			
			resultObject.result		= xmlObj.getElementsByTagName("result")[0].childNodes[0].nodeValue;
			resultObject.message	= xmlObj.getElementsByTagName("message")[0].childNodes[0].nodeValue;
			
			return resultObject;			
		}


	}
}

Favorite.prototype.addFavorite = function()
{		
	this.Action.add(this);
}

Favorite.prototype.removeFavorite = function()
{		
	this.Action.remove(this);
}

Favorite.prototype.resetFavoriteButton = function(){
	this.parentObj.style.background = "#fff";	
	this.parentObj.innerHTML = this.parentObjHtml;
}
