// ----------------------------
// Pixbox
// ----------------------------
// Recolocamiento de capas
// Visor de imágenes de móviles 
// Desplegables dinámicos
// Carga de páginas dinámicas (jQuery adress)
// ----------------------------

var url_marcas = "/BackOffice/Front/GetMarcas/";
var url_modelos = "/BackOffice/Front/GetModelos/";
var url_imagenes = "/BackOffice/Front/GetImagenes";

// external interface para AS3
function doResize(height){
	if (arguments.length == 1) resizer.resizeHeight(true, height);
	else resizer.resizeWidth(true); 
}

// variable para saber si ya hay resultados del apartado "móviles"
var hayResultados = false
		
$(document).ready(function(){	

	resizer = new Resizer($("#seccion"), 436, 778, 265, 500);
	carrousel = new Carrousel($("#moviles"), 120, 6);
	
	// carga de páginas dinámicas
	setCargadorContenidos();
	
	// carga de desplegables con AJAX
	
	var resultado = "";;
	var campoResultado = $("#buscar_resultados");
	
	function setCombosChange(){
		$("#selectmarca").css("opacity", ".5");
		$("#selectmodelo").css("opacity", ".5");
		
		$("#soporte").change(setComboMarcas).change();
		$("#marca").change(setComboModelos);
		$("#buscar").click(doSearch);
	}
	
	function setComboMarcas() {
		var soporteValue = $("#soporte").val();
		var combo = $("#marca");
		if (soporteValue == ""){
			if (!hayResultados){
				resultado = "Elige el <strong>soporte</strong>, después la <strong>marca</strong> y el <strong>modelo</strong>.";
				campoResultado.html(resultado);
			}
			$("#selectmarca").html("marca");
			$("#selectmarca").fadeTo('normal', .5);		
			combo.attr("disabled", true);
			combo.emptySelect();
			setComboModelos();
		} else {
			combo.attr("disabled", false);					
			$.getJSON(
				//"ajax/getMarcas.php",
				//{ soporte: soporteValue },
				url_marcas+soporteValue,
				{}, 
				function(data){
					$("#selectmarca").fadeTo('normal', 1);	
					combo.loadSelect(data);
					setComboModelos();
					if (!hayResultados){
						resultado = "Elige la <strong>marca</strong> y <strong>modelo</strong>, o búscalos todos.";
						campoResultado.html(resultado);
					}
				}
			);
		}
	}
	
	function setComboModelos(){
		var soporteValue = $("#soporte").val();
		var marcaValue = $("#marca").val();
		var combo = $("#modelo");
		if (soporteValue == "" || marcaValue == ""){
			if ((soporteValue != "") && (!hayResultados)){
				resultado = "Elige la <strong>marca</strong> y <strong>modelo</strong>, o búscalos todos.";
				campoResultado.html(resultado);
			}
			combo.attr("disabled", true);
			combo.emptySelect();
			$("#selectmodelo").html("modelo");
			$("#selectmodelo").fadeTo('normal', .5);
		} else {
			combo.attr("disabled", false);
			$.getJSON(
				//"ajax/getModelos.php", 
				//{ soporte: soporteValue, marca: marcaValue }, 
				url_modelos+soporteValue+"/"+marcaValue,
				{},
				function(data){
					$("#selectmodelo").fadeTo('normal', 1);
					$(combo).loadSelect(data);
					if (!hayResultados){
						resultado = "Elige el <strong>modelo</strong>, o búscalos todos.";
						campoResultado.html(resultado);
					}
				}
			);
		}
	}
	
	(function($) {
	
		$.fn.emptySelect = function() {
			return this.each(function(){
				if (this.tagName == "SELECT") this.options.length = 1;
			});
		}
		
		$.fn.loadSelect = function(optionsDataArray) {
			return this.emptySelect().each(function(){
				if (this.tagName == "SELECT"){
					var selectElement = this;
					$.each(optionsDataArray, function(index, optionData){
						var option = new Option(optionData.caption, optionData.value);
						
						if ($.browser.msie) selectElement.add(option);
						else selectElement.add(option, null);
					});
				}
			});
		}
		
	})(jQuery);
	
	function doSearch(){
		var soporteValue = $("#soporte").val();
		var marcaValue = $("#marca").val();
		var modeloValue = $("#modelo").val();
		
		var modeloValueOk = escape(modeloValue);
			modeloValueOk = modeloValueOk.replace("+", "%2B");
			modeloValueOk = modeloValueOk.replace("/", "%2F");

		var ruta = url_imagenes;
		if (soporteValue!="") ruta += "/"+soporteValue;
		if (marcaValue!="") ruta += "/"+marcaValue;
		if (modeloValue!="") ruta += "/"+modeloValueOk;
		
		$("#moviles .visor_columnas_contenido").load(
			//"ajax/getImagenes.php", 
			//{ soporte:soporteValue, marca:marcaValue, modelo:modeloValue }, 
			ruta,
			{},
			function(data){
				hayResultados = true;
				resizer.resizeHeight(true, 380);
				window.setTimeout(function(){
					carrousel.init();
					
					// calculamos los resultados obtenidos
					var numResultados = carrousel.numImg();
					var busqueda = '';
					if (soporteValue!='') busqueda += soporteValue+" ";
					if (marcaValue!='') busqueda += marcaValue+" ";
					if (modeloValue!='') busqueda += modeloValue+" ";
					if (busqueda=='') busqueda = "todo ";
					var result = (numResultados==1)? "resultado" : "resultados"
					resultado = '<strong>'+ numResultados +' '+result+'</strong> de la búsqueda <strong>" '+ busqueda +'"</strong>';
					campoResultado.html(resultado); 
				}, 200);
			}
		);
		
		
		return false;
	}
	
	window.setTimeout(setCombosChange, 1000);

});