// JavaScript Document

/** Variables globales **/
var catSel_cantLst = 5;

function catSel_cargarSubCategorias(listado, principal) {
	var url;
	var idLstSig;

	$('#catSel_estado').html("Cargando...");
	idLstSig = listado.attr("id");
	if (principal)
		idLstSig = idLstSig.substring(idLstSig.length - 1);
	else
		idLstSig = parseInt(idLstSig.substring(idLstSig.length - 1)) + 1;

	if (listado && listado.val() >= 0)
		url = "ID=" + encodeURIComponent(listado.val()) + "&AucType=" + $('#catSel_AucType').val();
	else
		url = "ID=&AucType=" + $('#catSel_AucType').val();
		
	$.ajax({
		type: "GET",
		url: "APPickCategory2.asp",
		data: url,
   	dataType: "xml",
		success: function(xml) {
			var message = $(xml).find("categorias")[0];
			catSel_parseMessage(listado, message, idLstSig);
		}
	}); 

}

function catSel_parseMessage(listado, message, sigList) {
	if (message) {
		// Vaciamos las listas siguientes
		for (i = catSel_cantLst; i >= sigList; i--) {
			$('#catSel_cat' + i).children().remove();
			if (i >= 4) 
				$('#catSel_col' + i).css("display", "none");
		}
				
		var lst = $('#catSel_cat' + sigList);
		catSel_llenarLista(lst, message, sigList);
	}
	$('#catSel_estado').html("&nbsp;");
}


// Cargar elementos en la lista
function llenarLista() {
	var catID;
	var pathCategorias = "";
	for (i = 1; i <= catSel_cantLst; i++) {
		var listado = $('#catSel_cat' + i);

		if (listado && listado.val() > 0) {
			catID = listado.val();
			pathCategorias += "> " + listado.find(":selected").text();
		}
	}
	return {
		"catID": catID,
		"pathCategorias": pathCategorias
	};
}

function catSel_llenarLista(lst, message, sigList) {
	// recorro el xml llenando con las nuevas opciones:
	var longitudXML = $(message).find("item").length;

	if (longitudXML == 0) {
		//catSel_UltimaCat();
		$('#catSel_tblCatSel').css("display", "block");
		$('#catSel_btnSelCat').css("display", "block");
	}
	else {	
		//habilito el botón de selección si es en modo restringido y no seleccionó solo categoría principal
		if (($('#catSel_RestringirSel').val() != "N") 
			|| ($('#catSel_RestringirSel').val() == "N" 
			&& (lst.name == "catSel_cat1" 
			|| lst.name == "catSel_cat2"))) {
			$('#catSel_tblCatSel').css("display", "none");
			$('#catSel_btnSelCat').css("display", "none");
		}
		else {
			$('#catSel_tblCatSel').css("display", "block");
			$('#catSel_btnSelCat').css("display", "block");
		}

		$('#catSel_layCategorias').scrollTo(
			$('#catSel_col' + sigList).fadeIn("normal"), 1000);

		$(message).find("item").each(function(){
			$("<option>")
				.attr("value", $(this).attr("id"))
				.text($(this).text())
				.appendTo(lst);  
		});
	}

	var selected = llenarLista();
	$('#catSel_pathCatsSel').html(selected["pathCategorias"]);
	$('#catSel_CatID').val(selected["catID"]);
}

function catSel_UltimaCat() {
	$('#catSel_tblCatSel').css("display", "block");
	$('#catSel_btnSelCat').css("display", "block");
	var selected = llenarLista();
	$('#catSel_pathCatsSel').html(selected["pathCategorias"]);
	$('#catSel_CatID').val(selected["catID"]);
}

function catSel_Seleccionar() {
	$('#CatID').val($('#catSel_CatID').val());
	$('#CategoriaSeleccionada').html(
		"<div class='APNavTree'>" + $('#catSel_pathCatsSel').html() + "</div>");
	$('#btnSelCat').attr("src", 'images/btn_catcambiar.gif');
	$('#btnSelCat').attr("title", 'valTitleBotonPostSelect');
	catSel_DeshabilitarLayer();
}

function catSel_Close() {
	catSel_DeshabilitarLayer();
}

function catSel_DeshabilitarLayer() {
	$('#catSel_layCategorias').css("display", "none");
	$('#catSel_laySelCat').css("display", "none");
	//WCH.Discard('catSel_layCategorias');
	//WCH.Discard('catSel_laySelCat');
}

$(function() {
	// Cargo la primer lista:
	catSel_cargarSubCategorias($("#catSel_cat1"), true);
	if ($('#catSel_RestringirSel').val() == "N") {
		$('#catSel_tblCatSel').css("display", "block");
		$('#catSel_btnSelCat').css("display", "block");
	}
	
	$(".catSel_select").bind("change", function(e){
		catSel_cargarSubCategorias($(this), false);
	});
});