$(document).ready(function(){
	function vardump( a ){
		aux = "";
		if( typeof a == "object" ){
			var espaco = ( typeof arguments[1] != "undefined" )?arguments[1]:"..";
			if( typeof a.nodeType != "undefined"){
				return "[NODE] = ( '"+ a.innerHTML.substr( 0, 15 ) +"...' )\n";
			}
			aux += typeof a + " {\n";
			for( i in a ){
				aux += espaco + "["+i+"]" + " = " + vardump( a[i], espaco + ".." );
			}
			aux += espaco + "}\n";
		}else{
			aux += typeof a + " '" + a + "'\n"
		}
		return aux;
	}
	function checkEmail(toCheck)
	{
		if (/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/.test(toCheck))
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	$(".cmp_off").livequery('click', function(){
		var id = $(this).attr("id_cmp");
		$.ajax({
			type: "post",
			url: "manage_cmp.php",
			data: "id="+id+"&action=add",
			success: function(msg){

				if(msg == "true")
				{
					$("img[id_cmp="+id+"]").attr("src", "slices/favoritos_cor.gif");
					$("img[id_cmp="+id+"]").removeClass("cmp_off");
					$("img[id_cmp="+id+"]").addClass("cmp_on");
				}
				else
				{
					alert(msg);
				}
			}
		})
	})
	$(".cmp_on").livequery('click', function(){
		var id = $(this).attr("id_cmp");
		$.ajax({
			type: "post",
			url: "manage_cmp.php",
			data: "id="+id+"&action=del",
			success: function(msg){
				if(msg == "true")
				{
					$("img[id_cmp="+id+"]").attr("src", "slices/favoritos_pb.gif");
					$("img[id_cmp="+id+"]").removeClass("cmp_on");
					$("img[id_cmp="+id+"]").addClass("cmp_off");
				}
				else
				{
					alert(msg);
				}
			}
		})
	})
	$(".delete_cmp").livequery('click', function(){
		var id = $(this).attr("id_imo");
		$.ajax({
			type: "post",
			url: "manage_cmp.php",
			data: "id="+id+"&action=del",
			success: function(msg){
				if(msg == "true")
				{
					$("#"+id).fadeOut("300");
				}
				else
				{
					alert(msg);
				}
			}
		})
	})
	$(".cmp_off_detalhes").livequery('click', function(){
		var id = $(this).attr("id_imo");
		$.ajax({
			type: "post",
			url: "manage_cmp.php",
			data: "id="+id+"&action=add",
			success: function(msg){
				if(msg == "true")
				{
					$("img[id_imo="+id+"]").attr("src", "detalhes_imovel/adicionar_favoritos.jpg");
					$("img[id_imo="+id+"]").removeClass("cmp_off_detalhes");
					$("img[id_imo="+id+"]").addClass("cmp_on_detalhes");
				}
			}
		})
	})
	$(".cmp_on_detalhes").livequery('click', function(){
		var id = $(this).attr("id_imo");
		$.ajax({
			type: "post",
			url: "manage_cmp.php",
			data: "id="+id+"&action=del",
			success: function(msg){
				if(msg == "true")
				{
					$("img[id_imo="+id+"]").attr("src", "detalhes_imovel/adicionar_favoritos_off.jpg");
					$("img[id_imo="+id+"]").removeClass("cmp_on_detalhes");
					$("img[id_imo="+id+"]").addClass("cmp_off_detalhes");
				}
			}
		})
	})
})
