(function($) {
    
	$.fn.limpia = function($desplegable, txt) {
		var $elem = $(this);
		if (txt != "") $elem.html(txt);
		$desplegable.find("option:not(:first)").remove();		
	};
	
    $.fn.resetea_familias = function($desplegable) {
		var $elem = $(this);
		$elem.limpia($desplegable, txt_seleccionafamilia);
		$desplegable.find("option:not(:first)").remove();
		$.ajax({
			url: URL_BASE + 'ajax_desplegable_familias.php',
			success: function(datos) {
				var obj = $.parseJSON(datos);
				if (obj.error == 0) {											
					var opciones = '';
					$.each(obj, function(key, value) {
						if (key != "error") {
							var $opcion = '<option value="' + key + '"';
							$opcion += '>' + value + '</option>';                                   
							$($opcion).appendTo($desplegable);                                    
						}
					});					
				}				
			}
		});
	};
	
    $.fn.resetea_fabricantes = function($desplegable) {
		var $elem = $(this);
		$elem.limpia($desplegable, txt_seleccionafabricante);
		$desplegable.find("option:not(:first)").remove();
		$.ajax({
			url: URL_BASE + 'ajax_desplegable_fabricantes.php',
			success: function(datos) {
				var obj = $.parseJSON(datos);
				if (obj.error == 0) {											
					var opciones = '';
					$.each(obj, function(key, value) {
						if (key != "error") {
							var $opcion = '<option value="' + key + '"';
							$opcion += '>' + value + '</option>';                                   
							$($opcion).appendTo($desplegable);                                    
						}
					});					
				}				
			}
		});
	};
	
    $.fn.resetea_subfamilias = function($desplegable) {
		var $elem = $(this);
		$elem.limpia($desplegable, txt_seleccionasubfamilia);
		$desplegable.find("option:not(:first)").remove();
	};
	
	$.fn.carga_fabricantes_familia = function($desplegable, familia, callback) {
		var $elem = $(this);
		$.ajax({
			url: URL_BASE + 'ajax_desplegable_fabricantes.php',
			data: "familia=" + familia,
			success: function(datos) {
				var obj = $.parseJSON(datos);
				if (obj.error == 0) {											
					var opciones = '';
					$.each(obj, function(key, value) {
						if (key != "error") {
							var $opcion = '<option value="' + key + '"';
							$opcion += '>' + value + '</option>';                                   
							$($opcion).appendTo($desplegable);                                    
						}
					});
					return callback.call(this);
				}				
			}
		});			
	}
	
	$.fn.carga_familias_fabricante = function($desplegable, fabricante, callback) {
		var $elem = $(this);
		$.ajax({
			url: URL_BASE + 'ajax_desplegable_familias.php',
			data: "fabricante=" + fabricante,
			success: function(datos) {
				var obj = $.parseJSON(datos);
				if (obj.error == 0) {											
					var opciones = '';
					$.each(obj, function(key, value) {
						if (key != "error") {
							var $opcion = '<option value="' + key + '"';
							$opcion += '>' + value + '</option>';                                   
							$($opcion).appendTo($desplegable);                                    
						}
					});
					return callback.call(this);
				}				
			}
		});			
	}

	$.fn.carga_subfamilias_familia_fabricante = function($desplegable, familia, fabricante) {
		var $elem = $(this);
		$.ajax({
			url: URL_BASE + 'ajax_desplegable_subfamilias.php',
			data: "familia=" + familia + "&fabricante=" + fabricante,
			success: function(datos) {
				var obj = $.parseJSON(datos);
				if (obj.error == 0) {											
					var opciones = '';
					$.each(obj, function(key, value) {
						if (key != "error") {
							var $opcion = '<option value="' + key + '"';
							$opcion += '>' + value + '</option>';                                   
							$($opcion).appendTo($desplegable);                                    
						}
					});					
				}				
			}
		});			
	}
	
	$.fn.carga_subfamilias_familia = function($desplegable, familia) {
		var $elem = $(this);
		$.ajax({
			url: URL_BASE + 'ajax_desplegable_subfamilias.php',
			data: "familia=" + familia,
			success: function(datos) {
				var obj = $.parseJSON(datos);
				if (obj.error == 0) {											
					var opciones = '';
					$.each(obj, function(key, value) {
						if (key != "error") {
							var $opcion = '<option value="' + key + '"';
							$opcion += '>' + value + '</option>';                                   
							$($opcion).appendTo($desplegable);                                    
						}
					});					
				}				
			}
		});			
	}

    $(document).ready(function() {
       
        // Desplegable fabricantes
		$(".select_fabricantes").change(function() {
			var $desplegable_fabricantes = $(this);
			var $desplegable_familias = $(".select_familias");
			var familia = $desplegable_familias.val();
			var familia_txt = $("#selectbusc_familia").html();
			var fabricante = $desplegable_fabricantes.val();
			var $desplegable_subfamilias = $(".select_subfamilias");
			var subfamilia = $desplegable_subfamilias.val();
			var subfamilia_txt = $("#selectbusc_subfamilia").html();
			$("#selectbusc_familia").limpia($desplegable_familias, "");
			$("#selectbusc_familia").carga_familias_fabricante($desplegable_familias, fabricante, function() {
				if (familia != 0) {
					$("#selectbusc_familia").html(familia_txt);
					$desplegable_familias.val(familia);
					$("#selectbusc_subfamilia").limpia($desplegable_subfamilias, txt_seleccionasubfamilia);
					$("#selectbusc_subfamilia").carga_subfamilias_familia_fabricante($desplegable_subfamilias, familia, fabricante);
					/*if (subfamilia != 0) {
						$("#selectbusc_subfamilia").html(subfamilia_txt);
						$desplegable_subfamilias.val(subfamilia);
					}*/
				}
			});
		});
		
		// Desplegable familias
		$(".select_familias").change(function() {
			var $desplegable_familias = $(this);
			var $desplegable_fabricantes = $(".select_fabricantes");
			var familia = $desplegable_familias.val();
			var fabricante = $desplegable_fabricantes.val();
			var fabricante_txt = $("#selectbusc_fabricante").html();
			var $desplegable_subfamilias = $(".select_subfamilias");
			var subfamilia = $desplegable_subfamilias.val();
			var subfamilia_txt = $("#selectbusc_subfamilia").html();			
			$("#selectbusc_fabricante").limpia($desplegable_fabricantes, "");
			$("#selectbusc_fabricante").carga_fabricantes_familia($desplegable_fabricantes, familia, function() {
				if (fabricante != 0) {				
					$("#selectbusc_fabricante").html(fabricante_txt);
					$desplegable_fabricantes.val(fabricante);
					$("#selectbusc_subfamilia").limpia($desplegable_subfamilias, txt_seleccionasubfamilia);			
					$("#selectbusc_subfamilia").carga_subfamilias_familia_fabricante($desplegable_subfamilias, familia, fabricante);
				} else {
					$("#selectbusc_subfamilia").limpia($desplegable_subfamilias, txt_seleccionasubfamilia);			
					$("#selectbusc_subfamilia").carga_subfamilias_familia($desplegable_subfamilias, familia);
				}
			});			
		});
        /*$(".select_fabricantes").change(function() {
            var desplegable_fabricantes = $(this);
            var fabricante = desplegable_fabricantes.val();
            var desplegable_familias = $(".select_familias");
			var familia = desplegable_familias.val();
			if (familia == 0) {
				desplegable_familias.find("option:not(:first)").remove();
				$("#selectbusc_familia").html(txt_seleccionafamilia);
				$("#selectbusc_subfamilia").html(txt_seleccionasubfamilia);
				//var modelo_actual = $(".modelo_actual").val();
				if (fabricante != "0") {
					$.ajax({
						url: URL_BASE + 'ajax_familias_fabricante.php',
						data: { fabricante: fabricante },
						success: function(datos) {
							var obj = $.parseJSON(datos);
							if (obj.error == 0) {
														
								var opciones = '';
								$.each(obj, function(key, value) {
									if (key != "error") {
										var opcion = '<option value="' + key + '"';
										//if (key == modelo_actual) opcion += ' selected="selected"';
										opcion += '>' + value + '</option>';                                   
										$(opcion).appendTo(desplegable_familias);                                    
									}
								});
								
							}
						}
					});
					
				} else {
					$.ajax({
						url: URL_BASE + 'ajax_familias.php',
						success: function(datos) {
							var obj = $.parseJSON(datos);
							if (obj.error == 0) {
														
								var opciones = '';
								$.each(obj, function(key, value) {
									if (key != "error") {
										var opcion = '<option value="' + key + '"';
										//if (key == modelo_actual) opcion += ' selected="selected"';
										opcion += '>' + value + '</option>';                                   
										$(opcion).appendTo(desplegable_familias);                                    
									}
								});
								
							}
						}
					});
				}
			}
        });
		
        // Desplegable familias
        $(".select_familias").change(function() {
            var desplegable_familias = $(this);
            var familia = desplegable_familias.val();
			var fabricante = $(".select_fabricantes").val();
            var desplegable_fabricantes = $(".select_fabricantes");
			var desplegable_subfamilias = $(".select_subfamilias");
			desplegable_fabricantes.find("option:not(:first)").remove();
			desplegable_subfamilias.find("option:not(:first)").remove();
			$("#selectbusc_subfamilia").html(txt_seleccionasubfamilia);
			$("#selectbusc_fabricante").html(txt_seleccionafabricante);
            //var modelo_actual = $(".modelo_actual").val();
            if (familia != "0") {
                $.ajax({
                    url: URL_BASE + 'ajax_subfamilias.php',
                    data: { familia: familia, fabricante: fabricante },
                    success: function(datos) {
                        var obj = $.parseJSON(datos);                        
                        if (obj.error == 0) {                                                    
                            var opciones = '';
                            $.each(obj, function(key, value) {
                                if (key != "error") {
                                    var opcion = '<option value="' + key + '"';
                                    //if (key == modelo_actual) opcion += ' selected="selected"';
                                    opcion += '>' + value + '</option>';                                   
                                    $(opcion).appendTo(desplegable_subfamilias);                                    
                                }
                            });
                            
                        }
                    }
                });
                $.ajax({
                    url: URL_BASE + 'ajax_fabricantes.php',
                    data: { familia: familia },
                    success: function(datos) {
                        var obj = $.parseJSON(datos);                        
                        if (obj.error == 0) {                                                    
                            var opciones = '';
                            $.each(obj, function(key, value) {
                                if (key != "error") {
                                    var opcion = '<option value="' + key + '"';
                                    //if (key == modelo_actual) opcion += ' selected="selected"';
                                    opcion += '>' + value + '</option>';                                   
                                    $(opcion).appendTo(desplegable_fabricantes);                                    
                                }
                            });
                            
                        }
                    }
                });				
            }
        });*/
        
        // Buscador
        var default_txt = txt_buscar + "...";
        //var error_txt = "Escribe un mínimo de 4 caracteres";
        $("#inputBuscar").focus(function() {
          if ($(this).val() == default_txt) $(this).val("");
          /*if ($(this).val() == error_txt) {
            $(this).css("color", "#6090C0").val($(this).attr("rel"));
            $(this).attr("rel", "");
          }*/
        }).blur(function() {
          if ($(this).val() == "") $(this).val(default_txt);
        });

		$('#inputBuscar').keypress(function(e) {
			if (e.which == 13) {
				e.preventDefault();
							$(".envia_buscador").focus().trigger("click");
			}
		});        

		$(".radio").live("click", function() {
			if ($(this).next(".busc_oferta").attr("id") == "busc_oferta_si") {
				$("#solo_ofertas").val("1");
			} else {
				$("#solo_ofertas").val("0");
			}
		});
        
        $(".envia_buscador").click(function(e) {
            e.preventDefault();

            /*if ($("#inputBuscar").val().replace(/ /g, '').length <= 3) {
                var msj = "Escribe un mínimo de 4 caracteres";
                $("#inputBuscar").attr("rel", $("#inputBuscar").val());
                $("#inputBuscar").val(msj).css("color", "red");                
                return false;
            }*/
            
                    if ($("#inputBuscar").val() == txt_buscar + "...") {
                        var buscar_val = "0";
                    } else {
                        var buscar_val = $("#inputBuscar").val();
                    }
                    var destino = URL_BASE + 'productos/buscar/' + buscar_val.replace("/", "|") + "/" +  $(".select_fabricantes").val() + "/" + $(".select_familias").val() + "/" + $(".select_subfamilias").val() + "/" + ($("#solo_ofertas").val() != "" ? $("#solo_ofertas").val() : 0);
                    document.location = destino;
            
            
        });

		$("#reset_buscador").click(function(e) {
			e.preventDefault();
			$("#inputBuscar").val(txt_buscar + "...");
			var $desplegable_fabricantes = $(".select_fabricantes");
			$("#selectbusc_fabricante").resetea_fabricantes($desplegable_fabricantes);
			var $desplegable_familias = $(".select_familias");
			$("#selectbusc_familia").resetea_familias($desplegable_familias);
			var $desplegable_subfamilias = $(".select_subfamilias");
			$("#selectbusc_subfamilia").resetea_subfamilias($desplegable_subfamilias);
			/*desplegable_familias.html(txt_seleccionafamilia);
			desplegable_familias.find("option:not(:first)").remove();
			$.ajax({
				url: URL_BASE + 'ajax_familias.php',
				success: function(datos) {
					var obj = $.parseJSON(datos);
					if (obj.error == 0) {
												
						var opciones = '';
						$.each(obj, function(key, value) {
							if (key != "error") {
								var opcion = '<option value="' + key + '"';
								//if (key == modelo_actual) opcion += ' selected="selected"';
								opcion += '>' + value + '</option>';                                   
								$(opcion).appendTo(desplegable_familias);                                    
							}
						});
						
					}
				}
			});
			
			$("#selectbusc_subfamilia").html(txt_seleccionasubfamilia).find("option:not(:first)").remove();
			$("#selectbusc_fabricante").html(txt_seleccionafabricante).find("option:not(:first)").remove();
			*/
		});
    });
    
})(jQuery);
