// manage favorites
$(document).ready(function(){
	$(".add2fav").live("click", function(){
		var span = $(this);
		var id = $(this).attr('id').split('_')[1];
		$.ajax({
			cache: false,
			url: 'managefav.php',
			type: "POST", 
			data: 'task=add&propid='+id,
			success: function(ret, xhr) {
				ret = parseInt(ret);
				if (ret == -1) {
					alert('You have to login first');
				}
				else if (ret == -2) {
					alert('Sorry, your request could not be processed');
				}
				else {
					$("#myfav").html(ret);
					$(span).removeClass('add2fav');
					$(span).addClass('remfav');
					$(span).html('Remove favorite');
				}	
			},
			error: function(rText, sText, xhr){	
				alert('Error \n rText = '+rText+'\n sText = '+sText);
			}
		});
	});
	
	$(".remfav").live("click", function(){
		var span = $(this);
		var id = $(this).attr('id').split('_')[1];
		$.ajax({
			cache: false,
			url: 'managefav.php',
			type: "POST", 
			data: 'task=del&propid='+id,
			success: function(ret, xhr) {
				ret = parseInt(ret);
				if (ret == -1) {
					alert('You have to login first');
				}
				else if (ret == -2) {
					alert('Sorry, your request could not be processed');
				}
				else {
					$("#myfav").html(ret);
					$(span).removeClass('remfav');
					$(span).addClass('add2fav'); 
					$(span).html('Add to favorites');
				}	
			},
			error: function(rText, sText, xhr){	
				alert('Error \n rText = '+rText+'\n sText = '+sText);
			}
		});
	});
});
