var _Notas_js_ = true;
function mostrarNotas(tipo_nota, checkVi){
	if( typeof(tipo_nota) == 'undefined' ){
		tipo_nota = null;
	}
	if( typeof(checkVi) == 'undefined' ){
		checkVi = true;
	}

	if (checkVi) {
		var objNotas = getObjetoNotas('norma_texto', undefined, 1);
		
		var notas_pie = objNotas["notas_al_pie"];
		var notas_bcn = objNotas["notas_bcn"];
		var notas_orig = objNotas["notas_originales"];
		
		var total_pie = notas_pie.length;
		var total_bcn = notas_bcn.length;
		var total_orig = notas_orig.length;
		var total_total = total_pie + total_bcn + total_orig;
		if (total_total <= 0) {
			return false;
		}
	}
	if (!tipo_nota) {
		var aux = "";
		try{
			$('ops_ver_notas_bcn').checked;
		}catch(e){
			aux = "2";
		}
		var ops_ver_notas_bcn 		 = $('ops_ver_notas_bcn'+aux).checked;
		var ops_ver_notas_al_pie 	 = $('ops_ver_notas_al_pie'+aux).checked;
		var ops_ver_notas_originales = $('ops_ver_notas_originales'+aux).checked;
		
		return (ops_ver_notas_bcn || ops_ver_notas_al_pie || ops_ver_notas_originales);

	}else{
		return $('ops_ver_notas_'+tipo_nota).checked;
	}
}

function isset(val){
	if(typeof(val)=='undefined'){
		return false;
	}
	return true;
}

function getObjetoNotas(idDivContenedor, siempre, limite){
	var objNotas 		= new Object();
	var notas_bcn 		= new Array();
	var notas_al_pie 	= new Array();
	var notas_originales= new Array();
	
	if(typeof(siempre)=='undefined'){
		siempre = false;
	}
	if(typeof(limite)=='undefined'){
		limite = false;
	}
	
	if ($('ops_ver_notas_bcn').checked || siempre) {
		notas_bcn 			= getElementsByName_iefix('div', 'notas_bcn', idDivContenedor, limite);
	}
	if ($('ops_ver_notas_al_pie').checked || siempre) {
		notas_al_pie 		= getElementsByName_iefix('div', 'notas_al_pie', idDivContenedor, limite);
	}
	if ($('ops_ver_notas_originales').checked || siempre) {
		notas_originales = getElementsByName_iefix('div', 'notas_originales', idDivContenedor, limite);
	}
	
	objNotas["notas_al_pie"]	= notas_al_pie;
	objNotas["notas_bcn"] 		= notas_bcn;
	objNotas["notas_originales"]= notas_originales;
	
	return objNotas;
}

Notas = function(idDivContenedor, arrNotas, tipo_nota){
	if (typeof(arrNotas) != 'undefined') {
		this.objNotas = new Object();
		this.objNotas['notas_al_pie'] = new Array();
		this.objNotas['notas_bcn'] = new Array();
		this.objNotas['notas_originales'] = new Array();
		
		this.objNotas[tipo_nota] = arrNotas;
		
	}
	else 
		if (typeof(idDivContenedor) != 'undefined') {
			this.objNotas = getObjetoNotas(idDivContenedor);
		}
		else {
			this.objNotas = new Object();
			this.objNotas["notas_al_pie"] = new Array();
			this.objNotas["notas_bcn"] = new Array();
			this.objNotas["notas_originales"] = new Array();
		}
	
	this.notas_pie = this.objNotas["notas_al_pie"];
	this.notas_bcn = this.objNotas["notas_bcn"];
	this.notas_orig = this.objNotas["notas_originales"];
	
	this.total_pie = this.notas_pie.length;
	this.total_bcn = this.notas_bcn.length;
	this.total_orig = this.notas_orig.length;
	
	this.estado = "inicial";
	this.cargando = false;
	
	this.ultimaPos_pie = 0;
	this.ultimaPos_bcn = 0;
	this.ultimaPos_orig = 0;
	
	this.intervalNumber = -1;
	
	this.notasPorVez = getValorParametro(__notas_por_vez__);
	
	this.top_nota_anterior = 0;
	this.alto_nota_anterior = 0;
	
	this.total_iteaciones = 0;
	this.total_total = this.total_pie + this.total_bcn + this.total_orig;
	
	this.objMensaje  = $('mensaje_carga_nota');
	this.soloDisplay_pie  = false;
	this.soloDisplay_bcn  = false;
	this.soloDisplay_orig = false;
	this.ejecutaOnEnd  	  = "void(0);"
	
	this.ajustaAnchos = function(){
		document.getElementById('norma_texto_sin_notas').style.display = 'none';
		document.getElementById('norma_texto').style.display = 'block';
		
		var pres_texto = getElementsByName_iefix('pre', 'pre_texto', "norma_texto");
		var divs_textos = getElementsByName_iefix('div', 'texto', "norma_texto");
		var span_textos = getElementsByName_iefix('span', 'texto', "norma_texto");
		
		for (var i = 0; i < pres_texto.length; i++) {
			pres_texto[i].style.width = '100%';
		}
		for (var i = 0; i < divs_textos.length; i++) {
			divs_textos[i].style.width = width_texto;
		}
		for (var i = 0; i < span_textos.length; i++) {
			span_textos[i].style.width = width_texto;
		}
		if (detectar_browser()) {// => cualquier excepto IE
			var style_width = '98%';
		}
		else {//=> CASO IE
			var winW = 630, winH = 460;
			if (parseInt(navigator.appVersion) > 3) {
				if (navigator.appName.indexOf("Microsoft") != -1) {
					winW = document.body.offsetWidth;
				}
			}
			var ancho = winW - 300;
			var style_width = ancho + 'px';
		}
		document.getElementById('div_general').style.width = style_width;
		parent.ajustarTam('norma','iframeContenido_norma');
	};
	
	this.procesa = function(){
		if (this.cargando || arbolTOC.estado != "terminado" || !mostro_busqueda) {
			return false;
		}
		if (!mostrarNotas()) {
			this.terminar();
		}

		//detenerMoverMenuLateral();

		this.estado = "procesando";
		
		var iteraciones_efectuadas = 0;
		
		this.cargando = true;
		
		if (this.total_pie > 0 && mostrarNotas('al_pie')) {
			//Notas Tipo Al Pie
			if (this.ultimaPos_pie == 0) {
				this.top_nota_anterior = 0;
				this.alto_nota_anterior = 0;
			}
			for (var i = this.ultimaPos_pie; i < this.total_pie; i++) {
				iteraciones_efectuadas++;
				this.total_iteaciones++;
				
				this.ultimaPos_pie++;
				try {
					this.notas_pie[i].style.display = "";
					if (!this.soloDisplay_pie) {
						var res = this.ejecutarOrdenamientoNota(this.notas_pie[i], this.top_nota_anterior, this.alto_nota_anterior, 'pie');
					}else{
						var res = [this.notas_pie[i].style.top,this.notas_pie[i].offsetHeight]
					}
				} 
				catch (e) {
					alert(e);
				}
				this.top_nota_anterior = res[0];
				this.alto_nota_anterior = res[1];
				
				if (this.debeSalir(iteraciones_efectuadas)) {
					parent.ajustarTam('norma','iframeContenido_norma');
					this.continuar();
					return;
				}
			}
		}

		
		if (this.total_orig > 0 && mostrarNotas('originales')) {
			//Notas Tipo ORIGINALES
			if (this.ultimaPos_orig == 0) {
				this.top_nota_anterior = 0;
				this.alto_nota_anterior = 0;
			}
			for (var i = this.ultimaPos_orig; i < this.total_orig; i++) {
				iteraciones_efectuadas++;
				this.total_iteaciones++;
				
				this.ultimaPos_orig++;
				
				try {
					this.notas_orig[i].style.display = "";
					if (!this.soloDisplay_orig) {
						var res = this.ejecutarOrdenamientoNota(this.notas_orig[i], this.top_nota_anterior, this.alto_nota_anterior, 'orig');
					}else{
						var res = [this.notas_orig[i].style.top,this.notas_orig[i].offsetHeight]
					}
				} 
				catch (e) {
					alert(e);
				}
				this.top_nota_anterior = res[0];
				this.alto_nota_anterior = res[1];
				
				if (this.debeSalir(iteraciones_efectuadas)) {
					this.continuar();
					return;
				}
			}
		}
		
		
		if (this.total_bcn > 0 && mostrarNotas('bcn')) {
			//Notas Tipo BCN
			if (this.ultimaPos_bcn == 0) {
				this.top_nota_anterior = 0;
				this.alto_nota_anterior = 0;
			}
			for (var i = this.ultimaPos_bcn; i < this.total_bcn; i++) {
				iteraciones_efectuadas++;
				this.total_iteaciones++;
				
				this.ultimaPos_bcn++;
				try {
					this.notas_bcn[i].style.display = "";
					if (!this.soloDisplay_bcn) {
						var res = this.ejecutarOrdenamientoNota(this.notas_bcn[i], this.top_nota_anterior, this.alto_nota_anterior, 'bcn');
					}else{
						var res = [this.notas_bcn[i].style.top,this.notas_bcn[i].offsetHeight]
					}
				} 
				catch (e) {
					alert(e);
				}
				this.top_nota_anterior = res[0];
				this.alto_nota_anterior = res[1];
				
				if (this.debeSalir(iteraciones_efectuadas)) {
					this.continuar();
					return;
				}
			}
		}
		
		
		this.terminar();
		return;
	};
	this.continuar = function(){
		this.cargando = false;
		this.mostrarProgeso();
	};
	this.mostrarProgeso = function(){
		if (this.total_total > 0) {
			var porc = Math.round(this.total_iteaciones * 100 / this.total_total);
		}
		else {
			var porc = 100;
		}
		
		if (!this.objMensaje) {
			this.objMensaje = $('mensaje_carga_nota');
		}

		this.objMensaje.innerHTML = "<span style='background-color:#FFFFCC;border:1px solid #ff9933;'><b>Cargando notas: " + porc + "%</b></span>";
		this.objMensaje.style.display = "";
	};
	
	this.terminarExterno = function(){
		this.cargando = false;
		this.estado = "terminado";
		clearInterval(this.intervalNumber);
		this.intervalNumber = -1;
		if (!this.objMensaje) {
			this.objMensaje = $('mensaje_carga_nota');
		}
		//this.objMensaje.style.display = "none";
	};
	
	this.hayNotasVisibles = function(){
		var object    		= getObjetoNotas('norma_texto', true, 1);
		var notas_bcn 		= object["notas_bcn"]; 
		var notas_al_pie 	= object["notas_al_pie"]; 
		var notas_originales= object["notas_originales"];
		
		if(isset(notas_bcn[0])){
			var nota1 = notas_bcn[0].style.display;
		}else{
			var nota1 = "none";
		}
		if(isset(notas_al_pie[0])){
			var nota2 = notas_al_pie[0].style.display;
		}else{
			var nota2 = "none";
		}
		if(isset(notas_originales[0])){
			var nota3 = notas_originales[0].style.display;
		}else{
			var nota3 = "none";
		}
		
		if(nota1 == "none" && nota2 == "none" && nota3 == "none"){
			return false;
		}else{
			return true;
		}
	};
	
	this.terminar = function(){
		this.cargando = false;
		this.estado = "terminado";
		clearInterval(this.intervalNumber);
		this.intervalNumber = -1;
		if (!this.objMensaje) {
			this.objMensaje = $('mensaje_carga_nota');
		}
		this.objMensaje.style.display = "none";
		if (this.total_iteaciones == 0) {
			modifica_ancho_etiqueta_pre(true);
		}
		else 
			if (!this.hayNotasVisibles()) {
				modifica_ancho_etiqueta_pre(true);
			}
			else {
				parent.ajustarTam('norma', 'iframeContenido_norma');
			}
		
		try{
			eval(this.ejecutaOnEnd);
		}catch(e){}
	};
	this.debeSalir = function(iteraciones_efectuadas){
		if (iteraciones_efectuadas >= this.notasPorVez) {
			//reiniciarMoverMenuLateral();
			return true;
		}
		else 
			if (this.total_iteaciones >= this.total_total) {
				//reiniciarMoverMenuLateral();
				return this.terminar();
			}
		
		return false;
	};
	
	this.muestra = function(display){
		if (this.cargando || arbolTOC.estado != "terminado") {
			if(arbolTOC.estado != "terminado"){
				this.estado = "procesando";
			}
			return false;
		}

		this.cargando = true;
		this.estado = "procesando";
		
		if (typeof(display) == 'undefined') {
			display = "";
		}else{
			if (!mostrarNotas()) {
				this.terminar();
				return;
			}
		}
		
		var iteraciones_efectuadas = 0;

		//Notas Tipo Al Pie
		this.top_nota_anterior = 0;
		this.alto_nota_anterior = 0;
		for (var i = this.ultimaPos_pie; i < this.total_pie; i++) {
			iteraciones_efectuadas++;
			this.total_iteaciones++;
			
			this.ultimaPos_pie++;
			try {
				this.notas_pie[i].style.display = display;
			} 
			catch (e) {
				alert(e);
			}
			
			if (this.debeSalir(iteraciones_efectuadas)) {
				this.continuar();
				return;
			}
		}
		
		//Notas Tipo Originales
		for (var i = this.ultimaPos_orig; i < this.total_orig; i++) {
			iteraciones_efectuadas++;
			this.total_iteaciones++;
			
			this.ultimaPos_orig++;
			
			try {
				this.notas_orig[i].style.display = display;
			} 
			catch (e) {
				alert(e);
			}
			
			if (this.debeSalir(iteraciones_efectuadas)) {
				this.continuar();
				return;
			}
		}
		
		//Notas Tipo BCN
		for (var i = this.ultimaPos_bcn; i < this.total_bcn; i++) {
			iteraciones_efectuadas++;
			this.total_iteaciones++;
			
			this.ultimaPos_bcn++;
			try {
				this.notas_bcn[i].style.display = display;
			} 
			catch (e) {
				alert(e);
			}
			
			if (this.debeSalir(iteraciones_efectuadas)) {
				this.continuar();
				return;
			}
		}
				
		this.terminar();
		return;
	};
	
	this.oculta = function(){
		this.muestra("none");
	};
	this.ejecutarOrdenamientoNota = function(nota, top_nota_anterior, alto_nota_anterior, tipo_nota){
		var espacio_entre_pre_y_nota = 25;
		
		var top_orig  = nota.style.top;
		var left_orig = nota.style.left;
		
		var id = nota.getAttribute("id_de_cuerpo")
		if (top_nota_anterior == 0) {
			var pre = document.getElementById("pre_" + id);
			var pos_pre = findPos(pre);//[left, top]
			var top_pre = pos_pre[1];
			top_nota_anterior = top_pre;
		}
		
		var id = nota.getAttribute("id_de_cuerpo");
		var correlativo_nota = nota.getAttribute("correlativo_nota");
		var ancla_nota = document.getElementById(correlativo_nota);
		var pre = document.getElementById("pre_" + id);
		var posicion_pre = findPos(pre);//[left,top]
		var posicion_ancla = findPos(ancla_nota);//[left,top]
		try {
			var ancho_pre = pre.offsetWidth;
		} 
		catch (e) {
			//Si entra aca es porque no entro a tratarCasoSaltosDeLineaPorNotas
			ultima_parte_tratada = null;
			modifica_ancho_etiqueta_pre(false);
			return false;
		}
		if (detectar_browser()) {// => cualquier excepto IE
			var top_nota = posicion_ancla[1] - 1;
		}
		else {//=> CASO IE
			var top_nota = posicion_ancla[1] + 10;
		}
		
		var left_nota = posicion_pre[0] + ancho_pre + espacio_entre_pre_y_nota;
		
		
		if (top_nota_anterior + alto_nota_anterior > top_nota) {
			top_nota = top_nota_anterior + alto_nota_anterior + 0;
		}
		
		if (top_nota <= 0) {
			return [0, 0]
		}
		nota.style.top = top_nota + "px";
		nota.style.left = left_nota + "px";
		
		var att_tipo_nota = nota.getAttribute("tipo");
		if (att_tipo_nota == 'nota_al_pie'){
			nota.parentNode.insertBefore(document.createElement('br'),nota);
		}
		
		if(nota.style.top == top_orig && nota.style.left == left_orig){
			switch(tipo_nota){
				//case 'bcn' :this.soloDisplay_bcn  = true;break;
				case 'pie' :this.soloDisplay_pie  = true;break;
				//case 'orig':this.soloDisplay_orig = true;break;
			}
		}else{
			switch(tipo_nota){
				case 'bcn' :this.soloDisplay_bcn  = false;break;
				case 'pie' :this.soloDisplay_pie  = false;break;
				case 'orig':this.soloDisplay_orig = false;break;
			}
		}
		
		top_nota_anterior = top_nota;
		alto_nota_anterior = nota.offsetHeight;
		
		return [top_nota_anterior, alto_nota_anterior];
	};
	
}


ClaseNotasM = new Notas();
ClaseNotas  = new Notas();
