/* ONLOAD & ONRESIZE */
window.onload = function() { 
setBlackContenido();
} // onload
window.onresize = function() {
setBlackContenido(); 
} // onresize


/* ---------------------------------
    FUNCIONES
-------------------------------------*/

/* FUNCIÓN (modifica dinámicamente la altura de #black_wrapper y #contenido) */
function setBlackContenido() {
var alturaDisponible = $("body").height();
$("#contenido").css("height",alturaDisponible - 200 + "px");
$("#content_giros,#content_explorar,#content_busqueda").css("height",alturaDisponible - 330 + "px");
} // function

/* FUNCIÓN (cierra el menú) */
function cerrar() {
	// Dejamos "default"
	$("#div_giros a").removeClass("selected"); // <-- Deseleccionamos todas las letras (GIROS COMERCIALES)
	$("#pestanas").children().addClass("amarillo"); //  <-- Agregar "amarillo" (a los 3)
	$("#pestanas").children(":first").removeClass("amarillo"); // <-- Quitar "amarillo" (al primero)
    $("#pestanas").children(":first").addClass("blanco"); // <-- Agregar "blanco" (al primero)
	$("#div_explorar,#div_busqueda").css("display","none"); // <-- Ocultamos contenido
	$("#div_giros").css("display","block"); // Mostramos contenido (GIROS COMERCIALES)
	    // Cerrar
    	$('#black').fadeOut('def');
		mostrarPublicidad();
		return false;
}

// FUNCIÓN ("desatontamiento" para IE6, IE7), sin embargo, aplica para todos
// EN DESHUSO (BORRAR SI NO SE PRESENTA NUEVAMENTE EL PROBLEMA)
function iefix() { 
altura = $("#content_giros").height();
$("#content_giros").css("height",(altura + 1)+"px");
setBlackContenido();
clearInterval(intervalo); 
}


/* GIROS COMERCIALES */
/* FILTRAR GIROS */
function filtrarGiros(e) {
	var str = escape(document.getElementById('filtrar').value); // <-- Obtenemos el valor
    if(str != "") {
    $.ajax({
		   type: "GET",
		   url: path + "ajax/ajax_filtrar.php?filtrar=" + str + "&path=" + path,
		   beforeSend: function() {
			   	$("#content_giros").removeClass();
	            $("#content_giros").addClass("loading");
		   }, // function
		   success: function(data,statusText) {
			   $("#content_giros").removeClass("loading");
			   $("#content_giros_inner").html(data); 
		   } // function
		   }); // ajax
	} // if
} // function

/* BÚSQUEDA RÁPIDA */
// Creamos el intervalo (basado en ajax_suggest.js)
function crearIntervalo2() {
	   intervalo2 = window.setTimeout("busquedaRapida()",400);
} // function

function busquedaRapida() {
	var b =  escape($("#busqueda_rapida").attr("value"));
    if(b != "") {
    $.ajax({
		   type: "GET",
		   url: path + "ajax/busqueda-rapida.php?b=" + b + "&path=" + path,
		   beforeSend: function() {
	            $("#busqueda_rapida").addClass("loading");
		   }, // function
		   success: function(data,statusText) {
			   $("#busqueda_rapida").removeClass("loading");
			   $("#busqueda_resultados").html(data); 
		   } // function
		   }); // ajax
	} // if
} //function



/* ---------------------------------
    READY
-------------------------------------*/
$('document').ready(function() {	 

							 
/* MOSTRAR (Al darle click a una letra desde el INDEX) */
 $('#indice a').click(function(event) {
	$('#black').fadeIn('def', function() { // <-- Degredado (0% a 100%)
		if($.browser.msie) { // <-- Sólo IE
		 this.style.removeAttribute('filter'); 
		} // if
	}); 
    solicitar_subCategorias($(this).html()); // <-- Solicitamos sub-categorías
	setBlackContenido(); // <-- Ajuste de altura
	$(".nav a:contains(" + $(this).html() + ")").addClass("selected"); // <-- Seleccionamos la letra seleccionada en el INDEX
	event.preventDefault();
 }); // function
 
 
 /* MOSTRAR (Al darle click al botón "EXPLORAR DIRECTORIO") */
 $('#botones #btn_explorar2').click(function(event) {
	$('#black').fadeIn('def', function() { // <-- Degredado (0% a 100%)
		if($.browser.msie) { // <-- Sólo IE
		 this.style.removeAttribute('filter'); 
		} // if
	}); 
    solicitar_subCategorias("A"); // <-- Solicitamos sub-categorías "A"
	$(".nav a:contains(" + "A" + ")").addClass("selected"); // <-- Seleccionamos la "A"
	// Quitar colores
	$("#div_explorar").css("display","block");
	$("#div_giros").css("display","none");
	$("#div_busqueda").css("display","none");
	// Pestañas
	$("#btn_explorar").removeClass(); $("#btn_explorar").addClass("blanco");
	$("#btn_giros").removeClass(); $("#btn_giros").addClass("amarillo");
	$("#btn_busqueda").removeClass(); $("#btn_busqueda").addClass("amarillo");
	setBlackContenido(); // <-- Ajuste de altura
	event.preventDefault();
 }); // function
 
 
 /* MOSTRAR (Al darle click al botón "BUSQUEDA RAPIDA") */
 $('#botones #btn_busqueda2').click(function(event) {
	$('#black').fadeIn('def', function() { // <-- Degredado (0% a 100%)
		if($.browser.msie) { // <-- Sólo IE
		 this.style.removeAttribute('filter'); 
		} // if
	}); 
    solicitar_subCategorias("A"); // <-- Solicitamos sub-categorías "A"
	$(".nav a:contains(" + "A" + ")").addClass("selected"); // <-- Seleccionamos la "A"
	// Quitar colores
	$("#div_busqueda").css("display","block");
	$("#div_giros").css("display","none");
	$("#div_explorar").css("display","none");
	// Pestañas
	$("#btn_busqueda").removeClass(); $("#btn_busqueda").addClass("blanco");
	$("#btn_explorar").removeClass(); $("#btn_explorar").addClass("amarillo");
	$("#btn_giros").removeClass(); $("#btn_giros").addClass("amarillo");
	
	setBlackContenido(); // <-- Ajuste de altura
	event.preventDefault();
 }); // function
 
 
/* PESTAÑAS (Al darle clic a una pestaña, "GIROS COMERCIALES", "EXPLORAR DIRECTORIO", "BÚSQUEDA ABIERTA" */
$("#pestanas a").click(function(event) {
   // Si el botón es AMARILLO
   if($(this).parent().attr("class") != "blanco") {
	   $("#pestanas div").addClass("amarillo"); // <-- Pasamos todos a "amarillo"
	   $(this).parent().addClass("blanco"); // <-- Agregamos "blanco" al seleccionado.
	   $(this).parent().removeClass("amarillo"); // <-- Quitamos "amarillo" al seleccionado.
	   $(this).blur();
	   
	   // Mostrando y ocultando los contenidos
	   btn = $(this).parent().attr("id");
	   if(btn == "btn_giros") {
		   $("#contenido").children().css("display","none");
		   $("#div_giros").css("display","block");
	   } // if
	   
	   if(btn == "btn_explorar") {
		   $("#contenido").children().css("display","none");
		   $("#div_explorar").css("display","block");
	   } // if
	   
	   if(btn == "btn_busqueda") {
		   $("#contenido").children().css("display","none");
		   $("#div_busqueda").css("display","block");
	   } // if
	   
	   
   } //if
    event.preventDefault();
}); // function

/* GIROS COMERCIALES */
/* CAMBIO DE LETRA (Al darle clic a una letra (ej. A, B, C, D, E, F...etc) */
$(".nav a").click(function(event) {
	solicitar_subCategorias($(this).html()); // <-- Solicitamos sub-categorias
	$(this).siblings().removeClass("selected"); // <-- Quitamos letras amarillas
	$(this).addClass("selected"); // <-- Ponemos la letra a amarillo
	$(this).blur();
	event.preventDefault();
}); // function
							 
							 
/* EXPLORAR DIRECTORIO */
// Al darle clic a una CATEGORIA
$(".categoria a").click(function(event) {
								 
   // OPC. 1 - Primera ves que se abre (<a></a> NO tiene hermanos)
   if($(this).siblings().length == 0)  { 
   id = $(this).attr("class"); // <-- Obtenemos ID de la categoría (<a href="#" class="$id">)
   var a = $(this); // <-- El link <a href="#" class="$id">Abarrotes</a>
   a.parent().addClass("loading"); // <-- Cambiamos imagen [+] => "LOADING"
	 $.ajax({  // <-- Petición AJAX
			  type:"GET", 
			  url: path + "ajax/explorar-directorio.php?idcat=" + id,
			  success:function(data) {
				 a.after(data); // <-- Insertamos la información
	             a.parent().addClass("clic"); // <-- Cambiamos imagen [+] => [-]
				 a.parent().removeClass("loading");
				 a.siblings().slideDown("fast"); // <-- Mostramos
			  } // function success
			  }); // AJAX
	$(this).parent().addClass("abierto"); // <-- Agregamos una clase "abierto" (ya no será necesario volver a cargar la información)
   } 
   // OPC. 2 - Ya se había abierto (sólo mostramos)
   else if($(this).parent().attr("class") == "categoria abierto")  {
	 $(this).parent().addClass("clic"); // <-- Cambiamos imagen [+] => [-]
	 $(this).siblings().slideDown("fast"); // <-- Mostramos						 
	// OPC. 3 - Cerramos
	} else {
	 $(this).siblings().slideUp("fast");
	 $(this).parent().removeClass("clic"); // <-- Cambiamos imagen [-] => [+]
   } // else

   $(this).blur();
   event.preventDefault();  
   
}); // click


// Al darle clic a una SUBCATEGORÍA (con "live()" para captar elementos creados dinámicamente)
$(".subcategoria > a").live("click",function(event) {

   // OPC. 1 - Primera ves que se abre (<a></a> NO tiene hermanos)
   if($(this).siblings().length == 0)  { 
   id = $(this).attr("class"); // <-- Obtenemos ID de la subcategoría (<a href="#" class="$id">)
   var a = $(this); // <-- El link <a href="#" class="$id">Abarrotes</a>
   a.parent().addClass("loading"); // <-- Cambiamos imagen [+] => "LOADING"
	 $.ajax({  // <-- Petición AJAX
			  type:"GET", 
			  url: path + "ajax/explorar-directorio.php?idsubcat=" + id + "&path=" + path,
			  success:function(data) {
				 a.after(data); // <-- Insertamos la información
	             a.parent().addClass("clic"); // <-- Cambiamos imagen [+] => [-]
				 a.parent().removeClass("loading");
				 a.siblings().slideDown("fast"); // <-- Mostramos
			  } // function success
			  }); // AJAX
	$(this).parent().addClass("abierto"); // <-- Agregamos una clase "abierto" (ya no será necesario volver a cargar la información)
   } 
   // OPC. 2 - Ya se había abierto (sólo mostramos)
   else if($(this).parent().attr("class") == "subcategoria abierto")  {
	 $(this).parent().addClass("clic"); // <-- Cambiamos imagen [+] => [-]
	 $(this).siblings().slideDown("fast"); // <-- Mostramos						 
	// OPC. 3 - Cerramos
	} else {
	 $(this).siblings().slideUp("fast");
	 $(this).parent().removeClass("clic"); // <-- Cambiamos imagen [-] => [+]
   } // else

   $(this).blur();
   event.preventDefault();  

});

	 
}); // ready



/* --------------------------------------------
    FUNCIONES (Javascript Normal)
-----------------------------------------------*/

	// OCULTANDO PUBLICIDAD (directorio)
	function ocultarPublicidad() {
	if(document.getElementById("publicidad_bordes")) {
	var publicidad_bordes = document.getElementById("publicidad_bordes");
	 for(i=0;i<publicidad_bordes.childNodes.length;i++) {
	   if(publicidad_bordes.childNodes[i].nodeName == "DIV") {
		   publicidad_bordes.childNodes[i].style.display = "none";
	   } // if
	 } // for
	} // if
	if(document.getElementById("superior1")) { /* <-- PUBLICIDAD CINES */
	  document.getElementById("superior1").style.display = "none";
	  document.getElementById("superior2").style.display = "none";
	  document.getElementById("superior3").style.display = "none";
	  document.getElementById("inferior1").style.display = "none";
	  document.getElementById("inferior2").style.display = "none";
	  document.getElementById("inferior3").style.display = "none";
	}// if
$("#publicidad_xcosas").css("display","none");
	} // function
	
	// MOSTRANDO PUBLICIDAD (directorio)
	function mostrarPublicidad() {
	if(document.getElementById("publicidad_bordes")) {
	var publicidad_bordes = document.getElementById("publicidad_bordes");
	 for(i=0;i<publicidad_bordes.childNodes.length;i++) {
	   if(publicidad_bordes.childNodes[i].nodeName == "DIV") {
		   publicidad_bordes.childNodes[i].style.display = "block";
	   } // if
	 } // for
	} // if
	if(document.getElementById("superior1")) { /* <-- PUBLICIDAD CINES */
	  document.getElementById("superior1").style.display = "block";
	  document.getElementById("superior2").style.display = "block";
	  document.getElementById("superior3").style.display = "block";
	  document.getElementById("inferior1").style.display = "block";
	  document.getElementById("inferior2").style.display = "block";
	  document.getElementById("inferior3").style.display = "block";
	}// if
$("#publicidad_xcosas").css("display","block");
	} // function


// Función que solicita y cambia la seccion
function solicitar_subCategorias(sc) {
	
	// Ocultando la publicidad
	ocultarPublicidad();
	
    // Creamos nuestra referencia
    var XMLHttp = new newXMLHttpRequest();

    // URL a solicitar
   rand = Math.random(); // evitar caché
   url = path + "ajax/ajax_subcategorias.php?sc=" + sc + "&path=" + path + "&rand=" + rand;

   // Abrimos (inicializamos)
   XMLHttp.open("GET",url,true);

   // Evento "onreadystatechange"
   XMLHttp.onreadystatechange = procesarRespuesta;

   // Enviamos el pedido (GET)
   XMLHttp.send(null);



function procesarRespuesta() {
	
	if(XMLHttp.readyState == 4 && XMLHttp.status == 200) {
		
		// Obtenemos el documento (la respuesta)
		var respuesta = XMLHttp.responseText;
		
		// Agregamos la informacion
	    var contenido = document.getElementById('content_giros');
        var contenido_inner = document.getElementById('content_giros_inner');
		contenido_inner.innerHTML = respuesta;
		contenido.className = sc;
		
	} else {
		
		// Mostramos "Loading..."
        var contenido = document.getElementById('content_giros');
		var contenido_inner = document.getElementById('content_giros_inner');
        contenido_inner.innerHTML = "";
		contenido.className = "loading";
		
	} // else
} //function

} // function (solicitar seccion)
