$(document).ready(function() {
    $("#tipoTransSelect").change(function () {
        var trans =  $(this).val();

        if (trans == "4ea1fc3b458b06" || trans == '4ea1fc5c6950c6') {
        	$('#precioSelect option').each(function(i, option){ $(option).remove(); });
        	var preciosrenta = new Array(" --- ", 
    				"0 - 5,000",
    				"5,000 - 10,000",
    				"10,000 - 15,000",
    				"15,000 - 20,000",
    				"20,000 - 30,000",
    				"30,000 en adelante");
	        $.each(preciosrenta, function(val, text) {
	        	$("#precioSelect").append(
		    		$("<option></option>").val(text).html(text)
		       	);
		    });
        } else if (trans == "4ea1fc2ae205f4" || trans == '4ea1fc4b9bb264') {
        	$('#precioSelect option').each(function(i, option){ $(option).remove(); });
        	var preciosventa = new Array(" --- ", 
    				"0 - 500,000", 
    				"500,000 - 1,000,000",
    				"1,000,000 - 1,500,000",
    				"1,500,000 - 2,000,000",
    				"2,000,000 - 2,500,000",
    				"2,500,000 - 3,000,000",
    				"3,000,000 - 3,500,000",
    				"3,500,000 - 4,000,000",
    				"4,000,000 - 5,000,000",
    				"5,000,000 - 6,000,000",
    				"6,000,000 en adelante");
        	$.each(preciosventa, function(val, text) {
        		$("#precioSelect").append(
                	$("<option></option>").val(text).html(text)
           		);
        	});
        } else {
        	$('#precioSelect option').each(function(i, option){ $(option).remove(); });
        	$("#precioSelect").append(
		    	$("<option></option>").val("").html(" --- ")
		    );
        }	 	
    });
	
	$("#selectOrder").change(function () {
		$("#orderForm").submit();
	});
	
	$("#submitbusquedaForm").click(function (){
		$("#busquedaForm").submit();
	});

    $("#estadoSelect").change(function () {
    	$('#zonaSelect option').each(function(i, option){ $(option).remove(); });
    	$.ajax({
        	type: "GET",
			url: "http://ambienteinmobiliario.com.mx/service?method=traerZona&zona_id&ciudad_id="+$(this).val(),		
	    	dataType: "xml",
			success: function(xml) {
    			ids = new Array();
    			i=0;
    			$(xml).find("zona_id").each(function(key,val) {
    				ids[i] = $(val).text();
    				i++;
            	});
            	nombres = new Array();
				j=0;
				$(xml).find("zona_nombre").each(function(key,val) {
					nombres[j] = $(val).text();
    				j++;
            	});
				
				var k=0;
				$("#zonaSelect").append(
					$("<option></option>").val("").html(" --- ")
				);
				while(k < ids.length) {
					$("#zonaSelect").append(
					   	$("<option></option>").val(ids[k]).html(nombres[k])
					);
					k++;
				}
			}
		});
    });
    
    $("#btnagregarfavorito").click(function (m){
    	m.preventDefault();
    	$.ajax({
    		type: "POST",
    		url: "/inmueble/favorito/",
    		data: "id="+$("#inmuebleidhidden").val(),
    		dataType: "text",
    		success: function(fav){
    		     if(fav == 1) {
    		    	 alert("Este inmueble se ha agregado a tus favoritos.")
					 location.reload();
    		     } else if (fav == 0) {
    		    	 alert("Este inmueble ya esta en tus favoritos.")
    		     }
    		}
    	});
    });
	
	$("#btnsubmitcompartir").click(function(){
		if($("#nombrefrom").val() == "" || $("#nombreto").val() == "" 
		|| $("#correofrom").val() == "" || $("#correoto").val() == ""
		|| $("#comentario").val() == "") {
			alert("Para poder compartir este inmueble, debes llenar todos los campos")
		} else {
			$.ajax({
				type: "POST",
				url: "/inmueble/compartir/",
				data: "nombrefrom="+$("#nombrefrom").val()+"&nombreto="+$("#nombreto").val()+"&correofrom="+$("#correofrom").val()+
					  "&correoto="+$("#correoto").val()+"&comentario="+$("#comentario").val()+
					  "&inmuebleid="+$("#inmuebleidhidden").val(),
				dataType: "text",
				success: function(response){
					 if(response == 1) {
						 alert("El correo se ha enviado correctamente.")
						 $("#enviarAmigo").dialog("close");
					 } else if (response == 0) {
						 alert("El correo no se pudo enviar, por favor revisa que los correos estén bien escritos.")
					 }
				}
			});
		}
	});
	
	$("#btnsubmitcontacto").click(function(){
		if($("#nombrefromcontacto").val() == "" || $("#correofromcontacto").val() == "" || $("#comentariocontacto").val() == "") {
			alert("Para poder contactar al vendedor de este inmueble, debes llenar todos los campos.")
		} else {
			$.ajax({
				type: "POST",
				url: "/inmueble/contacto/",
				data: "nombrefrom="+$("#nombrefromcontacto").val()+"&correofrom="+$("#correofromcontacto").val()+"&comentariocontacto="+$("#comentariocontacto").val()+"&correoto="+$("#correotocontacto").val()+"&nombreto="+$("#nombretocontacto").val(),
				dataType: "text",
				success: function(response){
					 if(response == 1) {
						 alert("El correo se ha enviado correctamente.")
						 $("#contacto").dialog("close");
					 } else if (response == 0) {
						 alert("El correo no se pudo enviar, por favor revisa que los correos estén bien escritos.")
					 }
				}
			});
		}
	});
	
	$("#btngooglemap").click(function(m){
		m.preventDefault();
		latlong = $("#latlonghidden").val();
		prueba = latlong.split(",");
		lat = prueba[0];
		long = prueba[1];
		$("#googlemap").dialog({
			height: 452,
			width: 621,
			resizable: false,
			modal: true,
			open : function (){
				if (GBrowserIsCompatible()) {
			      var map = new GMap2(document.getElementById("map_canvas"));
			      map.setCenter(new GLatLng(lat,long), 13);
				  var bounds = map.getBounds();
				  var southWest = bounds.getSouthWest();
				  var northEast = bounds.getNorthEast();
				  var lngSpan = northEast.lng() - southWest.lng();
				  var latSpan = northEast.lat() - southWest.lat();
				  var point = new GLatLng(lat,long);
			      map.addOverlay(new GMarker(point));
			      map.setUIToDefault();
			    }
			}
			
		});
	});

	$("#galeria").galleria({
        width: 480,
        height: 338,
        imageCrop: false
    });
	
	$("#btncontacto").click(function(m){
		m.preventDefault();
		$("#contacto").dialog({
			height: 238,
			width: 521,
			resizable: false,
			modal: true
		});
	});
	
	$("#btndudasycomentarios").click(function(m){
		m.preventDefault();
		$("#dudasycomentarios").dialog({
			height: 506,
			width: 521,
			resizable: false,
			modal: true
		});
	});
	
	$("#btnvideo").click(function(m){
		m.preventDefault();
		$("#video").dialog({
			height: 506,
			width: 521,
			resizable: false,
			modal: true
		});
	});
	
	$("#btnskype").click(function(m){
		m.preventDefault();
		$("#skype").dialog({
			height: 150,
			width: 296,
			resizable: false,
			modal: true
		});
	});
	
	$("#btnirfavoritos").click(function(m){
		m.preventDefault();
		$("#misfavoritos").dialog({
			height: 548,
			width: 701,
			resizable: true,
			modal: true
		});
	});
	
	$("#btnenviaramigo").click(function(m){
		m.preventDefault();
		$("#enviarAmigo").dialog({
			height: 285,
			width: 584,
			resizable: false,
			modal: true
		});
	});
});
