var caresp = Array('<','>','!','#','$','%','¨','&','*','(',')','+','=','{','}','[',']','?',';',':',',','|','"','~','^','´','`','¨','æ','Æ','ø','£','Ø','ƒ','ª','º','¿','®','½','¼','ß','µ','þ','ý','Ý','\'','\\','/',' ','á', 'à', 'ã', 'â', 'ä','Á', 'À', 'Ã', 'Â', 'Ä','é', 'è','É', 'È','í', 'ì','Í', 'Ì','ó', 'ò', 'ö','õ', 'ô','Ó', 'Ò', 'Ö', 'Õ', 'Ô','ú', 'ù', 'ü','Ú','Ù', 'Ü','ç','Ç','ñ','Ñ','§','¬','³','²','¹','°');
var carsfim = Array('.','@','_','-');
function in_array (needle, haystack, argStrict) {
    var key = '',
        strict = !! argStrict;
    if (strict) {
        for (key in haystack) {
            if (haystack[key] === needle) {
                return true;
            }
        }
    } else {
        for (key in haystack) {
            if (haystack[key] == needle) {
                return true;
            }
        }
    }
     return false;
}
function aniload(a,b,c,d){
	if(d == undefined){
		var divload = '#imomeio';
	}else{
		var divload = d;
	}
	$(divload).animate({height:'0px'},500);
	$('#loader').fadeIn(250);
	setTimeout(function (){
		$(divload+' div').html('');
		$('#loader').fadeIn(250);
		if($.isFunction(b)){
			function nb(){
				$('#loader').fadeOut(250);
				$(divload).animate({height:$(divload).children().eq(0).innerHeight()+'px'},500);
				b();
			}
		}else{
			nb = b;
			function nc(){
				$('#loader').fadeOut(250);
				$(divload).animate({height:$(divload).children().eq(0).innerHeight()+'px'},500);
				if($.isFunction(c)){
					c();
				}
			}
		}
		$(divload+' div').load(a,nb,nc);
	},500);
}
function doc(div){
	$('#iframeupload').animate({height:'0px'},500);
	setTimeout(function (){
		if(parseInt($(div).css('height').replace('px','')) > 0){
			$(div).animate({height:'0px'},500);
		}else{
			$(div).animate({height:$(div+' div').innerHeight()+'px'},500);
		}
	},500);
}
$('form').live('submit',function (){	
	var t = this;
	var id = $(t).attr('id');
	var atributos = $(t).serialize();
	var r = 0;
	var target = $(t).attr('target');
	if(target == undefined){
		var target = '#imomeio';
	}else{
		var target = target;
	}
	$('#'+id+' [obg]').each(function (){
		if(r != 2){
			if($(this).val() == ''){
				$(this).focus();
				alert('Preencha o campo "'+$(this).attr('obg')+'"!');
				r = 2;
			}
		}
	});
	$('#'+id+' [type="radio"][obg],#'+id+' [type="checkbox"][obg]').each(function (){
		if(r != 2){
			var name = $(this).attr('name');
			var obg = $(this).attr('obg');
			if($('#'+id+' [name="'+name+'"]:checked').length == 0){
				$(this).focus();
				alert('Escola uma opição em "'+obg+'"');
				r = 2;				
			}
		}
	});
	$('#'+id+' [accept]').each(function (){
		var at = this;
		if(r != 2){
			var accepts = $(at).attr('accept').split('|');
			var val = $(at).val();
			var ext = $(at).val().split('.');
			ext = ext[ext.length-1];
			if(!in_array(ext,accepts) && val != ''){
				$(at).focus();
				alert('Extensão Inválida');
				r = 2;				
			}
		}
	});
	$('#'+id+' .email').each(function (){
		if(r != 2){
			var val = $(this).val();
			var cars = 0;
			for(i=0;i<val.length;i++){
				if(in_array(val[i],caresp)){
					cars = 2;
				}
			}
			if(val == ''){
				// Não da alert
			}else if(val.indexOf('@') == -1 || val.indexOf('.') == -1 || val.indexOf('@.') != -1 || val.length < 6 || in_array(val.substr(-1),carsfim)){
				$(this).focus();
				alert('Email Invalido!');
				r = 2;				
			}else if(cars == 2){
				$(this).focus();
				alert('Email com caracteres invalidos!');
				r = 2;				
			}
		}
	});
	if(r == 2){
		return false;
	}
	if($(t).attr('onsubmit') != '' && $(t).attr('onsubmit') != undefined){
		eval($(t).attr('onsubmit'));
	}
	if($('#'+id+' input[type="file"]').length > 0){
		$(t).attr('enctype','multipart/form-data');
		$(t).attr('target','iframeupload');
		if($('#iframeupload').length == 0){
			$('<iframe id="iframeupload" name="iframeupload" src="" allowtransparency="1" scrolling="no" frameborder="0" width="0" height="0"></iframe>').insertBefore($(t).parent().parent());
		}
		$('#iframeupload').find('body').html('');
		$('#loader').fadeIn(250);
		var testetime = setInterval(function (){
			var h = $('#iframeupload').contents().find('body').html();
			if(h != ''){
				clearInterval(testetime);
				$('#loader').fadeOut(250);
				$(target).animate({height:'0px'},500);
				setTimeout(function (){
					$(target+' div').html(h);
					$(target).animate({height:$(target+' div').innerHeight()+'px'},500);
				},500);
			}
		},100);
		return true;
	}
	$(target).animate({height:'0px'},500);
	$('#loader').fadeIn(250);
	setTimeout(function (){
		$(target+' div').html('');
		var action = $(t).attr('action');
		var method = $(t).attr('method');
		$.ajax({
			url: action,
			type: method,
			data: atributos,
			dataType: 'html',
			success: function (data){
				$('#loader').fadeOut(250);
				$(target+' div').html(data);
				$(target).animate({height:$(target+' div').innerHeight()+'px'},500);
			}
		});
	},500);
	return false;
	alert('');
});
/**************************/
/**		Busca			**/
/************************/

var keytime;
$('#buscar_prod').live('keydown',function(e){
	clearTimeout(keytime);
	var code = (e.keyCode ? e.keyCode : e.which);
	if($('#buscar_prod').val() == ''){
		$('#tabela_conteudo_busca').html('');
		$('#focus').text('-1');
	}else{
		if(code != 40 && code != 38 && code != 13){
			keytime = setTimeout(function (){
				busca();
				$('#focus').text('-1');
			},500);
		}
	}
	if(code == 40){
		var j = $('#focus').text();
		var total = $('#tabela_conteudo_busca tr').length;
		
		j++;

		if(j == total){
			 j = 0;
		}
		
		$('#tabela_conteudo_busca tr').each(function (i){
			$(this).find('td').css('border-color',$(this).attr('bgcolor'));
		});
		$('#tabela_conteudo_busca tr').eq(j).find('td').css('border-color','#39C');
		$('#focus').text(j);
		$('#idfocus').text($('#tabela_conteudo_busca tr').eq(j).attr('id'));
	}

	if(code == 38){
		var j = $('#focus').text();
		var total = $('#tabela_conteudo_busca tr').length;

		if(j == -1){
		   j = parseInt(total)+1;
		}
		j--;
		$('#tabela_conteudo_busca tr').each(function (i){
			$(this).find('td').css('border-color',$(this).attr('bgcolor'));
		});
		$('#tabela_conteudo_busca tr').eq(j).find('td').css('border-color','#39C');
		$('#focus').text(j);
		$('#idfocus').text($('#tabela_conteudo_busca tr').eq(j).attr('id'));
	}
	if(code == 13){
		var j = $('#focus').text();
		if(j < 0){j=0;}
		aniload('area-imobiliaria_pesquisa.ajx',{conc:$('#tabela_conteudo_busca tr').eq(j).find('td').text()});
	}
});
$('.buscaprod td').live('click',function (){
	aniload('area-imobiliaria_pesquisa.ajx',{conc:$(this).text()});
})
function busca(){
    $.ajax({
        type: "post",
        dataType:'json',
        url:"area-imobiliaria_busca.ajx",
        data:"busca="+$('#buscar_prod').val(),
        timeout: 120000,
        cache:false,
        async:true,
        success:function(data){
            $('#tabela_conteudo_busca').html('');
            $.each(data.conteudo_busca,function(i,a){
              $('#tabela_conteudo_busca').append('<tr id="'+a.id_produto+'" bgcolor="'+a.bg+'" class="buscaprod"><td style="border-color:'+a.bg+';">'+a.produto+'</td></tr>');
            })
        },
    })
}
