
$(document).ready(function() {
    $('#banner').hover (
		function (){
			$(this).addClass('banner_show');
		},
		function (){
			$(this).removeClass('banner_show');
		}
	)
	
	//MENU
	$('#btn_1').hover (
		function (){
			$(this).addClass('mn_1_hover');
		},
		function (){
			$(this).removeClass('mn_1_hover');
		}
	)
	
	$('#btn_2').hover (
		function (){
			$(this).addClass('mn_2_hover');
		},
		function (){
			$(this).removeClass('mn_2_hover');
		}
	)
	
	$('#btn_3').hover (
		function (){
			$(this).addClass('mn_3_hover');
		},
		function (){
			$(this).removeClass('mn_3_hover');
		}
	)
	
	$('#btn_4').hover (
		function (){
			$(this).addClass('mn_4_hover');
		},
		function (){
			$(this).removeClass('mn_4_hover');
		}
	)
	
	$('#btn_5').hover (
		function (){
			$(this).addClass('mn_5_hover');
		},
		function (){
			$(this).removeClass('mn_5_hover');
		}
	)
});

// JavaScript Document
function buscaCep(){
	window.open('http://www.correios.com.br/servicos/cep/cep_default.cfm','Correios');
}
function str_replace (search, replace, subject, count) {
    var i = 0, j = 0, temp = '', repl = '', sl = 0, fl = 0,
            f = [].concat(search),
            r = [].concat(replace),
            s = subject,
            ra = r instanceof Array, sa = s instanceof Array;
    s = [].concat(s);
    if (count) {
        this.window[count] = 0;
    }
 
    for (i=0, sl=s.length; i < sl; i++) {
        if (s[i] === '') {
            continue;
        }
        for (j=0, fl=f.length; j < fl; j++) {
            temp = s[i]+'';
            repl = ra ? (r[j] !== undefined ? r[j] : '') : r[0];
            s[i] = (temp).split(f[j]).join(repl);
            if (count && s[i] !== temp) {
                this.window[count] += (temp.length-s[i].length)/f[j].length;}
        }
    }
    return sa ? s : s[0];
}
function substr_count (haystack, needle, offset, length) {
    var pos = 0, cnt = 0;
 
    haystack += '';
    needle += '';
    if (isNaN(offset)) {offset = 0;}
    if (isNaN(length)) {length = 0;}
    offset--;
 
    while ((offset = haystack.indexOf(needle, offset+1)) != -1){
        if (length > 0 && (offset+needle.length) > length){
            return false;
        } else{
            cnt++;
        }
    }
 
    return cnt;
}


function getCondominio(){
	if($("#carac_condominio").attr('disabled')==''){
		$("#carac_condominio").attr('disabled', 'none');
	}else{
		$("#carac_condominio").attr('disabled', '');
	}
	
}
function getURL() {
	if($.trim($("#mvl").val()) != ""){
		
		url = window.location
		url = url.toString()
		url = url.split("/")
		url = url[0]+'/'+url[1]+'/'+url[2]+'/'+url[3]+'/'+url[4]+'/'+url[5]+'/'+url[6]
		location=( url+'/'+$("#mvl").val())		 
	};
}
function getURLAss() {
	if($.trim($("#mvl").val()) != ""){
		url = window.location
		url = url.toString()
		url = url.split("/")
		url = url[0]+'/'+url[1]+'/'+url[2]+'/'+url[3]+'/'+url[4]
		location=( url+'/'+$("#mvl").val())		 
	};
}

function getEndereco() {
	$('#cep_msg').html('Buscando...');
	if($.trim($("#cep").val()) != ""){
		$.getScript("http://cep.republicavirtual.com.br/web_cep.php?formato=javascript&cep="+$("#cep").val(), 
		function(){
			if(resultadoCEP["resultado"]){
				$('#cep_msg').html('');
				$("#rua").val(unescape(resultadoCEP["tipo_logradouro"])+" "+unescape(resultadoCEP["logradouro"]));
				$("#bairro").val(unescape(resultadoCEP["bairro"]));
				$("#cidade").val(unescape(resultadoCEP["cidade"]));
				$("#estado option[value="+unescape(resultadoCEP["uf"])+"]").attr("selected","selected");
			}else{
				$('#cep_msg').html('<span id="error">Endereço não encontrado.</span>');
			}
		});                             
	}                       
}
function getEndereco2() {
	$('#cep_msg2').html('Buscando...');
	if($.trim($("#cep_imovel").val()) != ""){
		$.getScript("http://cep.republicavirtual.com.br/web_cep.php?formato=javascript&cep="+$("#cep_imovel").val(), 
		function(){
			if(resultadoCEP["resultado"]){
				$('#cep_msg2').html('');
				$("#rua_imovel").val(unescape(resultadoCEP["tipo_logradouro"])+" "+unescape(resultadoCEP["logradouro"]));
				$("#bairro_imovel").val(unescape(resultadoCEP["bairro"]));
				$("#cidade_imovel").val(unescape(resultadoCEP["cidade"]));
				//$("#estado_imovel").val(unescape(resultadoCEP["uf"]));
				$("#estado_imovel option[value="+unescape(resultadoCEP['uf'])+"]").attr("selected","selected");
			}else{
				$('#cep_msg2').html('<span id="error">Endereço não encontrado.</span>');
			}
		});                             
	}                       
}

function getCaracteristica() {
	$('#caracteristica_msg').html('Buscando...');
	if($.trim($("#mvl").val()) != ""){
		$("#editar_caracteristica").find("INPUT").each(function(){
			this.checked = false;
		});
		$.getScript("../public/ajax_caracteristica.php?mvl="+$("#mvl").val(), 
		function(){
			$('#caracteristica_msg').html('');
			$("#editar_caracteristica").find("INPUT[type='checkbox']").each(function(){
				this.checked = false;
			})
			
			if(caracteristicas_condominio['total']==0){
				$("#checks_condominio").fadeIn()
			}else{
				$("#checks_condominio").fadeIn()
				for(i=0; i<caracteristicas_condominio['total']; i++){
					$("#editar_caracteristica").find("INPUT[value='"+ caracteristicas_condominio[i] +"']").each(function(){
						this.checked = true;
					})
				}
			}
			$("#checks_imovel").fadeIn()
			for(i=0; i<caracteristicas_imovel['total']; i++){
				$("#editar_caracteristica").find("INPUT[value='"+ caracteristicas_imovel[i] +"']").each(function(){
					this.checked = true;
				})
			}
			$("#checks_utensilio").fadeIn()
			for(i=0; i<caracteristicas_imovel_utensilio['total']; i++){
				$("#editar_caracteristica").find("INPUT[value='"+ caracteristicas_imovel_utensilio[i] +"']").each(function(){
					this.checked = true;
				})
			}
			$("#checks_instalacao").fadeIn()
			for(i=0; i<caracteristicas_imovel_instalacao['total']; i++){
				$("#editar_caracteristica").find("INPUT[value='"+ caracteristicas_imovel_instalacao[i] +"']").each(function(){
					this.checked = true;
				})
			}
		});                             
	}                       
}

function getCartao(){
	$('#bandeira').attr("disabled", "")
	$('#numero_cartao').attr("disabled", "")
	$('#nome_cartao').attr("disabled", "")
	$('#validade_cartao').attr("disabled", "")
	$('#codigo_cartao').attr("disabled", "")
	$('#parcela_cartao').attr("disabled", "")
}
function getFotos() {
	$('#fotos_msg').html('Buscando...');
	if($.trim($("#mvl").val()) != ""){
		$.getScript("../public/ajax_fotos.php?mvl="+$("#mvl").val(), 
		function(){
			if(fotos['total']==0){
				var tabela='';
				$("#fotos_msg").fadeIn()
				$("#fotos_msg").html('Nenhuma foto enviada.')
				$("#campos_fotos").hide()
				document.getElementById("lista_fotos").innerHTML = tabela;
			}else{
				$("#fotos_msg").hide()
				$("#campos_fotos").fadeIn()
				var tabela='';
				var a=0;
				for(i=0; i<(fotos['total']); i++){
					if(a==5){ 
						tabela += '</div>'
						a=0; 
					}else{
						a++;
						
					}
					if(a==1){ tabela+='<div style="clear:both">'}
					tabela += '<table border="0" cellspacing="0" cellpadding="1" bgcolor="#666666" align="left" style="margin: 10px;">'
					tabela += '<tr><td>';
					tabela += '<img src="../img/imovel/thumb_'+fotos[i]+'" border="0" class="foto_imovel" />';
					tabela += '</td></tr>';
					tabela += '<tr><td>';
					tabela += '<a href="javascript:getRemoveFoto(\''+fotos[i]+'\')"><img src="../img/btn_remover.gif" border="0" /></a>';
					tabela += '</td></tr>';
					tabela += '</table>';
										
					document.getElementById("lista_fotos").innerHTML = tabela;
				}	
			}
		});                             
	}                       
}

function getFotos2(mvl) {
	$("#fotos_msg").fadeIn()
	$('#fotos_msg').html('Buscando...');
	if(mvl != ""){
		$.getScript("../public/ajax_fotos.php?mvl="+mvl, 
		function(){
			if(fotos['total']==0){
				var tabela='';
				$("#fotos_msg").fadeIn()
				$("#fotos_msg").html('Nenhuma foto enviada.')
				$("#campos_fotos").hide()
				document.getElementById("lista_fotos").innerHTML = tabela;
			}else{
				$("#fotos_msg").hide()
				$("#campos_fotos").fadeIn()
				var tabela='';
				var a=0;
				for(i=0; i<(fotos['total']); i++){
					if(a==5){ 
						tabela += '</div>'
						a=0; 
					}else{
						a++;
					}
					if(a==1){ tabela+='<div style="clear:both">'}
					tabela += '<table border="0" cellspacing="0" cellpadding="1" bgcolor="#666666" align="left" style="margin: 10px;">'
					tabela += '<tr><td>';
					tabela += '<img src="../img/imovel/thumb_'+fotos[i]+'" border="0" class="foto_imovel" />';
					tabela += '</td></tr>';
					tabela += '<tr><td>';
					tabela += '<a href="javascript:getRemoveFoto2(\''+fotos[i]+'\', '+mvl+')"><img src="../img/btn_remover.gif" border="0" /></a>';
					tabela += '</td></tr>';
					tabela += '</table>'
					
					
					document.getElementById("lista_fotos").innerHTML = tabela;
				}	
			}
		});                             
	}                       
}
function getRemoveFoto2(fto, mvl) {
	$("#fotos_msg").fadeIn()
	$('#fotos_msg').html('Removendo...');
	if(fto != ""){
		//alert('1')
		$.getScript("../public/ajax_fotos_delete.php?fto="+fto, 
		function(){
			//alert(foto['status'])
			//if(foto['status']=='ok'){
				//alert('3')
				return getFotos2(mvl);
			//}
		});                             
	}                       
}

function getRemoveFoto(fto) {
	$("#fotos_msg").fadeIn()
	$('#fotos_msg').html('Removendo...');
	if(fto != ""){
		$.getScript("../public/ajax_fotos_delete.php?fto="+fto, 
		function(){
			if(foto['status']=='ok'){
				return getFotos();
			}
		});                             
	}                       
}


function getCod(){
	if($.trim($("#codigo").val()) != ""){
		url = window.location
		url = url.toString()
		url = url.split("/")
		url = url[0]+'/'+url[1]+'/'+url[2]
		location=( url+'/imovel/'+$("#codigo").val())		 
	};
}
function getExcluirPreco(mvl) {
	if(mvl != ""){
		$.getScript("../../public/ajax_excluir_preco.php?mvl="+mvl, 
		function(){
			if(data['status']=='OK'){
				url = window.location
				location=( url )	
			}
		});                             
	}                       
}
function getDisponivel(){
	$('#disponivel_msg').html('Limpando...');
	if($.trim($("#mvl").val()) != ""){
		$.getScript("../public/ajax_disponivel.php?mvl="+$("#mvl").val(), 
		function(){
			if(datas['result']=='OK'){
				getURL();
			}
		})
	}
}
function getExcluirFav(imv){
	if(imv != ""){
		$.getScript("../public/ajax_excluir_favorito.php?mvl="+imv, 
		function(){
			if(datas['result']=='OK'){
				getURL();
			}
		})
	}
}


