function addInFavorites(portfolio_id, user_id, label, me){
	
	$.post(
		'/portfolio/addfavorites',
		{portfolio_id:portfolio_id, user_id:user_id},
		function(data){
			if (data['success'] == true){
				$(me).unbind('click').attr('onclick','');
				var oldLabel = $(me).html();
				$(me).click(function(){
					delFromFavorites(portfolio_id, user_id, oldLabel, me);
					return false;
				});
				$(me).html(label);
				count = parseInt($('span.h__favs').html());
				$('span.h__favs').html(count + 1);
			}
		}, 'json'
	);
}

function delFromFavorites(portfolio_id, user_id, label, me){
	$.post(
		'/portfolio/delfavorites',
		{portfolio_id:portfolio_id, user_id:user_id},
		function(data){
			if (data['success']){
				$(me).unbind('click').attr('onclick','');
				var oldLabel = $(me).html();
				$(me).click(function(){
					addInFavorites(portfolio_id, user_id, oldLabel, me);
					return false;
				});
				$(me).html(label);
				count = parseInt($('span.h__favs').html());
				if (count){
					$('span.h__favs').html(count - 1);	
				}
				
			}
		}, 'json'
	);
}

function delFromFavoritesPage(portfolio_id, user_id, label, me){
	$.post(
			'/portfolio/delfavorites',
			{portfolio_id:portfolio_id, user_id:user_id},
			function(data){
				if (data['success']){
					count = parseInt($('span.h__favs').html());
					if (count){
						$('span.h__favs').html(count - 1);	
					}
					$(me).closest('.m__item').fadeOut(500, function(){$(this).remove()});
				}
			}, 'json'
		);
}
