function UserProfile()
{
	this.reqWidgetQueue= new Array();//queue with the widgets that made a request through this controller object
	var self=this;
	this.linkReff = null;
	this.lastUsedUserId = null;
	
	this.lastAction = null;
	
	/**
	 * @param The manager index of the request 
	 * @desc This method is a handler for the completition of one request
	 */
	this.handleResponse=function (managerIndex)
	{		
		var code = RequestManager.getInstance().getRequestObj (managerIndex).getResponseErrorCode();
		var content = RequestManager.getInstance().getRequestObj (managerIndex).getResponseContent();
		switch(code)
		{
			case 0 :
				PopupsWidget.getInstance().closeAllPopups();
				PopupsWidget.getInstance().createPopup("Succes!",content.msg,this.linkReff);
				switch(this.lastAction)
				{
					case "add_profile_comment" :
						_e("profile_comments").innerHTML += content.html;
					break;
					case "add_image_comment" :
						_e("image_comments").innerHTML += content.html;
					break;
				}
			break;
			case 1:
				PopupsWidget.getInstance().closeAllPopups();
				PopupsWidget.getInstance().createPopup("Eroare!",content,this.linkReff);
			break;
			//mesaj de la confirmare prietenie
			case 4 :
				PopupsWidget.getInstance().closeAllPopups();
				PopupsWidget.getInstance().createPopup("Succes!",content+"<br><a href='#' class='button' onclick='UserProfile.getInstance().removeUserBox("+this.lastUsedUserId+");PopupsWidget.getInstance().closeAllPopups();return false;'>ok</a>",this.linkReff);
			break;
			case 2:
			break;
			case 5:
				alert(content);
			break;
		}
	}

	this.addProfileComment = function(profile_id,inputReff)
	{
		this.linkReff = _e("profile_comment_link")
		var comment = inputReff.value;
		
		this.lastAction = "add_profile_comment";
	
		var rqid=RequestManager.getInstance().createRequest(self,Config.APP_URL+"dy.php?action=add_profile_comment&profile_id="+profile_id+"&comment="+comment);
		RequestManager.getInstance().startRequest(rqid);
		return rqid;
	} 	
	
	this.addImageComment = function(image_id,inputReff)
	{
		this.linkReff = _e("image_comment_link")
		var comment = inputReff.value;
		
		this.lastAction = "add_image_comment";
	
		var rqid=RequestManager.getInstance().createRequest(self,Config.APP_URL+"dy.php?action=add_image_comment&image_id="+image_id+"&comment="+comment);
		RequestManager.getInstance().startRequest(rqid);
		return rqid;
	} 	
	
	this.addToFriends = function(profile_id)
	{
		this.linkReff = _e("add_as_friend_link");
		var rqid=RequestManager.getInstance().createRequest(self,Config.APP_URL+"dy.php?action=add_friend&profile_id="+profile_id);
		RequestManager.getInstance().startRequest(rqid);
		return rqid;
	} 	
	
	this.confirmFriend = function(friend_id)
	{
		this.lastUsedUserId = friend_id;
		this.linkReff = _e("confirm_button_"+friend_id);
		var rqid=RequestManager.getInstance().createRequest(self,Config.APP_URL+"dy.php?action=confirm_friend&friend_id="+friend_id);
		RequestManager.getInstance().startRequest(rqid);
		return rqid;
	} 	
	
	
	/**
	 * @param the index of the request 
	 * @desc Remove widget from the queue
	 */
	this.removeWidget=function (managerIndex)
	{
		RequestManager.getInstance().removeRequest(managerIndex);		
		self.reqWidgetQueue[managerIndex]=null;
	}
	
	
	this.removeUserBox = function (user_id)
	{
		var user_box = _e("user_box_"+user_id);
		user_box.parentNode.removeChild(user_box);
	}
	
	this.removeProfileComment = function (comment_id)
	{
		var rqid=RequestManager.getInstance().createRequest(self,Config.APP_URL+"dy.php?action=remove_comment&comment_id="+comment_id);
		RequestManager.getInstance().startRequest(rqid);
		return rqid;
	}
}


UserProfile.__instance__=null;

UserProfile.getInstance=function()
{
	if(UserProfile.__instance__==null)
	{
		UserProfile.__instance__=new UserProfile();
	}
	return UserProfile.__instance__;
}
