/**
 * @author falvarez
 */
var agrupaciones = {
	carroceria: [0, 1, 2, 3],
	industria: [4,5,6,7,8,9,10,11,12,13,14,15,16],
	decoracion: [17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36]
}

var produccion ={
	media:[0,1],
	alta:[2,3,4,5]
}



$(document).ready(function(){
	inicializar($("#bombas_busqueda_aplicacion_sector"),$("#bombas_busqueda_aplicacion_produccion"));
	$("#bombas_busqueda_aplicacion_sector").change(
		function(i){
			productos(this.value);	
		});
	$("#bombas_busqueda_aplicacion_sector").focus(
		function(i){
			$(this).trigger("click");
		});
			
	$("#bombas_busqueda_aplicacion_produccion").change(
		function(i){
			sistemas(this.value);	
		});

	$("#bombas_busqueda_aplicacion_produccion").focus(
		function(i){
			$(this).trigger("click");
		});
	
		
	
});

function restore(e) {
	if (e.options[e.selectedIndex].disabled) {
		e.selectedIndex = window.select_current[e.id];
	}
}

function emulate(e) {
	for (var i=0, option; option = e.options[i]; i++) {
		if (option.disabled) {
			option.style.color = "graytext";
		}
		else {
			option.style.color = "menutext";
		}
	}
}

function inicializar(arg,arg02){
	
	arg.parent().find("select").attr("value",0);
	productos(arg.attr("value"));
	sistemas(arg02.attr("value"));
}


function productos(index){
	var productos =$("#bombas_busqueda_aplicacion_producto option");
	switch(index){
		case "0":
		    productos.parent().attr("value",0);
			ocultar([agrupaciones.industria,agrupaciones.decoracion],productos);
			mostrar([agrupaciones.carroceria],productos);
		break;
		case "1":
    		productos.parent().attr("value",4);
			ocultar([agrupaciones.carroceria,agrupaciones.decoracion],productos);
			mostrar([agrupaciones.industria],productos);
		break;
		case "2":
     		productos.parent().attr("value",17);
			ocultar([agrupaciones.carroceria,agrupaciones.industria],productos);
			mostrar([agrupaciones.decoracion],productos);
		break;
		
	}
}

function sistemas(index){
	var sistemas =$("#bombas_busqueda_aplicacion_sistema option");
	switch(index){
		case "0":
		    sistemas.parent().attr("value",0);
			ocultar([produccion.alta],sistemas);
			mostrar([produccion.media],sistemas);
		break;
		case "1":
    		sistemas.parent().attr("value",2);
			ocultar([produccion.media],sistemas);
			mostrar([produccion.alta],sistemas);
		break;		
	}
}






function ocultar(args,productos){
	for(x in args){
		ocultaProductos(args[x],productos);
	}
}

function mostrar(args,productos){
	for(x in args){
		mostrarProductos(args[x],productos);
	}
}

function ocultaProductos(args,productos){
	for(x in args){
		productos.eq(args[x]).attr("disabled",true);
	}
}

function mostrarProductos(args,productos){
	for(x in args){
		productos.eq(args[x]).attr("disabled",false);
	}
	if (document.getElementsByTagName) {
		var s = document.getElementsByTagName("select");

		if (s.length > 0) {
			window.select_current = new Array();

			for (var i=0, select; select = s[i]; i++) {
				select.onfocus = function(){ window.select_current[this.id] = this.selectedIndex; }
				select.onchange = function(){ restore(this); }
				emulate(select);
			}
		}
	}
}





