
	//Activa o desactiva la opcion de impresion a PDF, basandose en el humero de articulos seleccionados
	function evaluarImpresion ()
	{
		num_articulo = document.getElementById ('numero_elementos').innerHTML;
		if (num_articulo < 1) {
			document.getElementById ("impresion_no").style.display = '';
			document.getElementById ("impresion_si").style.display = 'none';
		}
		else {
			document.getElementById ("impresion_no").style.display = 'none';
			document.getElementById ("impresion_si").style.display = '';
		}
	}

	function cleanElementos ()
	{
		dojo.fadeOut({node: 'productos',duration: 500}).play ();
		setTimeout ("document.getElementById ('productos').innerHTML = ''", 500);
	}
	
	function addElemento (html)
	{
		document.getElementById ('productos').innerHTML += html;
		if (typeof (widgets_anteriores) !== 'undefined') {
			for (var i = 0; i < widgets_anteriores.length; i++) {
				widgets_anteriores[i].destroy ();
			}
		} else {
			//--
		}
		
		widgets_anteriores = dojo.parser.parse(document.getElementById ('productos'));
		evaluarImpresion();
	}

	function getHTMLElemento (elemento)
	{
		if (elemento.checked) {
		    var checked = 'checked="checked"';
		} else {
			var checked = '';
		}

		var template_url = base_path + '/' +  elemento.template;
		
		elemento = prepareElemento (elemento);
		
		html_elemento = new EJS({ url: template_url }).render (elemento);
		
		return html_elemento;
	}

	function actualizaNumeroElementos (count)
	{
		document.getElementById ('numero_elementos').innerHTML = count;
		document.getElementById ("sin_elementos").style.display = 'none';
		evaluarImpresion();
	}

	function showSeccion (param)
	{
		history_js.push ('showSeccion ('+json_param+')');
		document.getElementById ('history_js_back').style.display = 'none';
		tree.showSeccion (param).addCallback (actualizarElementos);
	}

	function actualizarElementos (elementos)
	{	
		window.scrollTo (0,0);
		seccion	  = elementos [0];
		
		preproducto = elementos[1][0];
		producto = preproducto.producto;
		updateHTMLSeccion(seccion, producto);
		elementos = elementos [1];
		var fadeout = dojo.fadeOut({node: 'productos',duration: 1200});
		var fadein  = dojo.fadeIn ({node: 'productos',duration: 350});
		dojo.connect(fadeout, 'onEnd', function () { 
			document.getElementById ('productos').innerHTML = '';
			addElemento (html);
			fadein.play ();
		 });
		 
		fadeout.play ();
		var html = '';
		window.elementos = new Array ();
		for (var i = 0; i < elementos.length; i++) {
			if (i % 4 == 0) {
				html += '<div class="clear"></div>';
			}
			html += getHTMLElemento (elementos [i]);
			window.elementos[elementos[i].id_sku] = elementos[i];
		}
		html += '<div class="clear"></div>';
	}
	
	function updateHTMLSeccion (seccion, producto)
	{
		var ruta = false;
		
		//RID:4890
		//Modificamos la ruta para obtener las imagenes de las marcas a la carpeta de baja_resolucion
		
		for (var i = 0; i < seccion.imagenes.length; i++) {
			if (seccion.imagenes [i].principal == "1") {
				ruta = seccion.imagenes [i].ruta_pdf;
				break;
			}
		}
		
		if (ruta) {
			document.getElementById('logotipo_seccion').innerHTML = '<img src="'+base_path+ruta+'" alt="'+seccion.tabla+'" title="'+seccion.tabla+'" />';
		} else {
			document.getElementById('logotipo_seccion').innerHTML = '';
		}

		if (seccion.registro && seccion.registro.aprende_mas) {
			window.registro_aprende_mas = seccion.registro;
			document.getElementById('seccion_aprende_mas').innerHTML = '<a class="iconAprendeMas" href="javascript:void(0);" onclick="showAprendeMas();history_js.push(\'showAprendeMas()\');" title="Aprende m&aacute;s sobre el producto '+seccion.registro.nombre+'">Aprende m&aacute;s&nbsp;</a>';
		} else {
			document.getElementById('seccion_aprende_mas').innerHTML = '';
		}

		//Si está en la sección de Familias...
		if (seccion.tabla == "marca" && seccion.arbol_leyenda.Producto) {
			window.registro_aprende_mas = producto;
			document.getElementById('seccion_aprende_mas').innerHTML = '<a class="iconAprendeMas" href="javascript:void(0);" onclick="showAprendeMas();history_js.push(\'showAprendeMas()\');" title="Aprende m&aacute;s sobre el producto '+producto.nombre+'">Aprende m&aacute;s&nbsp;</a>';
		}		
		
		if (typeof (seccion.arbol_leyenda) 		  !== 'undefined' &&
		    typeof (seccion.arbol_leyenda.length)  == 'undefined'
		   ) {
			var nodos_ruta = new Array ();
			
			for (var i in seccion.arbol_leyenda) {
				//Interfaces graficas indican que cuando es Marca no se dice que es marca
				if (i !== 'Marca') {
					//Sobreescribimos en cada vuelta para solo tener el ultimo
					var leyenda = i + ' ' + seccion.arbol_leyenda [i];
				} else {
					var leyenda = seccion.arbol_leyenda [i];
				}
				nodos_ruta.push (seccion.arbol_leyenda [i]);
			}
			
			updateRuta (nodos_ruta);
			
			document.getElementById('leyenda_seccion').innerHTML = '<h2>' + leyenda + '</h2>';
		} else {
			document.getElementById('leyenda_seccion').innerHTML = '<h2>Seleccione una categor&iacute;a de la izquierda</h2>';
		}
	}
	
	/**
	 * Muestra el aprende más cargado en window.registro_aprende_mas
	 */
	function showAprendeMas ()
	{
		var template_url = base_path + '/views/specialized/aprende_mas.jhtml';
		
		var html = new EJS({ url: template_url }).render (window.registro_aprende_mas);
		
		document.getElementById ('logotipo_seccion').innerHTML = '';
		document.getElementById ('productos').innerHTML 	   = html;
	}
	
	function showDetalle (id_sku)
	{
	    // Ticket   #23717 - Scroll en "Ver detalles" de un producto
	    window.scrollTo (0,0);
	    
		var fadeout = dojo.fadeOut({node: 'productos',duration: 1200});
		var fadein  = dojo.fadeIn ({node: 'productos',duration: 350});
		dojo.connect(fadeout, 'onEnd', function () { 
			document.getElementById ('productos').innerHTML = '';
			addElemento (html);
			fadein.play ();
		 });
		 
		fadeout.play ();
		
		var elemento = window.elementos[id_sku];
		
		elemento ['ruta_ficha_pdf'] = base_url + '/productos/tree/product-detail/print/1/print-pdf/1/id/' + id_sku;
		
		var template_url = base_path + '/' +  elemento.template_detalle;
		elemento = prepareElemento (elemento);
		
		var html = new EJS({ url: template_url }).render (elemento);
	}
	
	function prepareElemento ( elemento )
	{
		if ( elemento.moneda == 'USD' ) {
			elemento.moneda_nombre_comun = 'd&oacute;lares americanos';
		} else if ( elemento.moneda == 'EUR' ) {
			elemento.moneda_nombre_comun = 'euros';
		} else {
			elemento.moneda_nombre_comun = 'pesos mexicanos';
		}
		
		return elemento;
	}
