﻿Element.addMethods({
	tipoBusquedaClase: Constantes.tiposBusqueda.busquedaPorClase,
	tipoBusquedaId: Constantes.tiposBusqueda.busquedaPorId,
	tipoBusquedaEtiqueta: Constantes.tiposBusqueda.busquedaPorEtiqueta,
	claseDeshabilitadorCampos: Clases.campos.deshabilitador,
	claseDisabled: Clases.campos.disabled,
	detieneEvento: function (ev) { with(this) {
		if (ev) 
			Event.stop(ev);
		return false;
	}},
	devuelveElementoAccionado: function (ev, etiqueta) { with(this) {
		var elementoAccionado;
		if (ev) {
			elementoAccionado = Event.element(ev);
			if (elementoAccionado.nodeName != etiqueta) elementoAccionado = buscaElementoSuperior(elementoAccionado, etiqueta,  Adesis.Constantes.tiposBusqueda.busquedaPorEtiqueta);
		}
		return elementoAccionado;
	}},
	buscaElementoSuperior: function (elementoActual, parametroBusqueda, tipoBusqueda) { with(this) {
		var contador = 1;
		for (i = 0; i < contador; i++){
			if(elementoActual){
				elementoActual = elementoActual.parentNode;
				if (seHaEncontradoElElementoBuscado(elementoActual, parametroBusqueda, tipoBusqueda)){
					return elementoActual;
					break;
				}
				else
					contador++;
			}
			else
				contador++;
			
		}
	}},
	seHaEncontradoElElementoBuscado: function (elemento, parametroBusqueda, tipoBusqueda) { with(this) {
		switch(tipoBusqueda){
			case tipoBusquedaClase:
				return $(elemento).hasClassName(parametroBusqueda);
				break;
			case tipoBusquedaId:
				return elemento.id == parametroBusqueda;
				break;
			case tipoBusquedaEtiqueta:
				return elemento.tagName == parametroBusqueda;
				break;
		}
	}},
	toggleaCampos: function (campo,campos) { with(this) {
		var deshabilitar;
		if (hayQueHabilitarCampos(campo))
			deshabilitar = false;
		else
			deshabilitar = true;
		campos.each( function (campo) {
			modificaAtributoDisabled(campo, deshabilitar);
			modificaClaseDeshabilitar(campo,deshabilitar);
		},this);


	}},
	modificaAtributoDisabled: function (campo,opcionDeshabilitar) { with(this) {
		campo.writeAttribute("disabled", opcionDeshabilitar);
	}},	
	modificaClaseDeshabilitar: function (campo,hayQueDeshabilitar) { with(this) {
		if (hayQueDeshabilitar)
			campo.addClassName(claseDisabled)
		else
			campo.removeClassName(claseDisabled)
	}},
	hayQueHabilitarCampos: function (campo) { with(this) {
		return (campo.checked && !campo.hasClassName(claseDeshabilitadorCampos));
	}},
	Y: function () { with(this) {
		return this;
	}}
});





if(typeof Adesis == "undefined") Adesis = {}
if(typeof Adesis.Utilidades == "undefined") Adesis.Comportamientos = {};
Adesis.Utilidades = Object.extend(Adesis.Comportamientos, {
	loadTemplates: function(list){
		var templates = {}
		$H(list).each(function(element){
			templates[element[0]]  = OI.Base.loadTemplate(element[1]);
		})
		return templates;
	},
	loadTemplate: function(id){
		var templateText = "";
		 if($(id))
			templateText = this.trimTemplate($(id).innerHTML);
		else
			this.notFoundTemplates.push(id);
		return new Template(templateText);
	},
	trimTemplate: function(templateContent){
		return templateContent
			.replace(/\<\!\[CDATA\[((\r\n)(\t*)\r\n)*?/gi, '')
			.replace(/((\r\n)(\t*)\r\n)*?\]\]\>/gi, '')
			.replace(/^\s+|\s+$/gi,'');
	},
	notFoundTemplates: []
});




Adesis.Utilidades.ModificarEnlacesConAccion = Class.create();
Adesis.Utilidades.ModificarEnlacesConAccion.prototype = {
	initialize: function (controlador) { with(this) {
		inizializaVariables(controlador).Y().modificaEnlaces();
		//new Lightbox();
	}},
	inizializaVariables: function (controlador) {
		this.selectores = Selectores.enlaces;
		this.clases = Clases.enlaces;
		this.textosComplementarios = TextosEstaticos.enlacesConAccion;
		this.controladorGeneral = controlador;
		return this;
	},
	modificaEnlaces: function () { with(this) {
		$$(Selectores.enlaces.enlaceConAccion).each ( function (enlace) {
			generaTextoComplementario(enlace).Y().modificaHTML(enlace).Y().modificaTitle(enlace).Y().insertaComportamiento().enEnlaceConAccion(enlace)
		},this);
		return this;
	}},
	modificaHTML: function (enlace) {with(this) {
		enlace.innerHTML += "<span class='indentado'> ."+textoComplementario+"</span>";
		return this;
	}},	
	generaTextoComplementario: function (enlace) {with(this) {
		if (esUnEnlaceAbreVentanaNueva(enlace))
			this.textoComplementario = textosComplementarios.abreVentanaNueva;
		else if (esUnEnlaceAbrePdf(enlace))
			this.textoComplementario = textosComplementarios.abrePdf;
		else if (esUnEnlaceAbreDoc(enlace))
			this.textoComplementario = textosComplementarios.abreDoc;
		else if (esUnEnlaceAbreMail(enlace))
			this.textoComplementario = textosComplementarios.abreMail;
		else if (esUnEnlaceLightbox(enlace))
			this.textoComplementario = textosComplementarios.lightbox;
		return this;
	}},
	esUnEnlaceAbreVentanaNueva: function (enlace) {with(this) {
		return (enlace.hasClassName(clases.abreVentanaNueva));
	}},
	esUnEnlaceAbrePdf: function (enlace) {with(this) {
		return (enlace.hasClassName(clases.abrePdf));
	}},
	esUnEnlaceAbreDoc: function (enlace) {with(this) {
		return (enlace.hasClassName(clases.abreDoc));
	}},
	esUnEnlaceAbreMail: function (enlace) {with(this) {
		return (enlace.hasClassName(clases.abreMail));
	}},	
	esUnEnlaceLightbox: function (enlace) {with(this) {
		return (enlace.hasClassName(clases.lightbox));
	}},
	modificaTitle: function (enlace) {with(this) {
		enlace.title += textoComplementario;
		return this;
	}},
	insertaComportamiento: function () {with(this) {
		return controladorGeneral.insertaComportamiento(this);
	}},
	accionaEnlace: function (enlace) {with(this) {
		if ((esUnEnlaceAbreVentanaNueva(enlace)) ||(esUnEnlaceAbrePdf(enlace)) || (esUnEnlaceAbreDoc(enlace)))
			abreEnVentanaNueva(enlace);

		return this;

	}},
	abreEnVentanaNueva: function (enlace) { with(this) {
		var ventana = window.open(enlace.href, '_blank');
		if (ventana && !ventana.closed) { 
			// si efectivamente hemos logrado abrirla 
			// la ponemos en foco 
			ventana.focus(); 
		}
		return this;
	}},
	Y: function () {with(this) {
		return this;
	}}
};

Adesis.Utilidades.ModificarPestanyasHome = Class.create();
Adesis.Utilidades.ModificarPestanyasHome.prototype = {
	initialize: function (controlador, contenedor) { with(this) {
		inizializaVariables(controlador, contenedor).Y().insertaEnlacesEnEncabezados().Y().redimensionaHeightContenedorPestanyas().ocultaContenidos().muestraContenido(contenedor.getElementsByClassName(clases.contenido)[0]).Y().marcaEncabezadoActivo($$(selectores.encabezado)[0]).modificaFormatoPresentacionPestanyas().Y().insertaComportamiento().enEnlacesPestanyas(contenedor, Selectores.pestanyasInteractivasHome.encabezado + " a");
	}},
	inizializaVariables: function (controlador, contenedor) {
		this.selectores = Selectores.pestanyasInteractivasHome;
		this.clases= Clases.pestanyasInteractivasHome;
		this.controladorGeneral = controlador;
		this.contenedor = contenedor;
		this.arrContenidosPestanyas = contenedor.getElementsBySelector(this.selectores.contenido);
		return this;
	},
	insertaEnlacesEnEncabezados: function () { with(this) {
		var nodoHref;
		$A($$(selectores.encabezado)).each( function (encabezado) {
			var textoEnlace = encabezado.getElementsByTagName("span")[0].innerHTML;
			encabezado.innerHTML = "";
			encabezado.appendChild(generaNodoHref(encabezado, textoEnlace));
			
		},this);
		return this;
	}},
	generaNodoHref: function (encabezado, textoEnlace) { with(this) {
		var nodo = document.createElement('A');
		nodo.setAttributeNode(generaAtributoHref(encabezado))
		var nodoSpan = document.createElement('SPAN');
		var nodoSpanInterior = document.createElement('strong');
		nodoSpanInterior.setAttributeNode(generaAtributoClassSpan())
		var textoNodo = document.createTextNode(textoEnlace);
		nodoSpan.appendChild(textoNodo);
		nodoSpanInterior.appendChild(nodoSpan); 
		nodo.appendChild(nodoSpanInterior); 
		return nodo;
	}},
	generaAtributoClassSpan: function () { with(this) {
		var newAttrClass = document.createAttribute("class");
		newAttrClass.nodeValue = "interior";
		return newAttrClass;
	}},
	generaAtributoHref: function (encabezado) { with(this) {
		var newAttrHref = document.createAttribute("href");
		newAttrHref.nodeValue = "#";
		return newAttrHref;
	}},
	ocultaContenidos: function () { with(this) {
		$A(contenedor.getElementsByClassName(clases.contenido)).each( function (contenido) {
			contenido.hide();
		},this);
		return this;
	}},		
	muestraContenido: function (content) { with(this) {
		this.contenido = content;
		contenido.show();
		return this;
	}},	
	ocultaContenidoYaActivo: function () { with(this) {
		contenido.hide();
		return this;
	}},	
	marcaEncabezadoActivo: function (encabezado) { with(this) {
		this.lista = Element.buscaElementoSuperior(encabezado, "LI", TiposBusqueda.busquedaPorEtiqueta);
		Element.addClassName(lista,Clases.generales.activo);
		return this;
	}},	
	desmarcaEncabezadoYaActivo: function () { with(this) {
		Element.removeClassName(lista,Clases.generales.activo);
		return this;
	}},
	modificaFormatoPresentacionPestanyas: function () { with(this) {
		contenedor.addClassName(clases.contenedorConFormatoPresentacion);
		return this;
	}},
	insertaComportamiento: function () {with(this) {
		return controladorGeneral.insertaComportamiento(this);
	}},	
	accionaPestanya: function (enlace) {with(this) {
		var elementoLista = Element.buscaElementoSuperior(enlace, "LI", TiposBusqueda.busquedaPorEtiqueta);
		if (document.all)
			var content = Element.getElementsByClassName(elementoLista,clases.contenido)[0];
		else
			var content = Element.buscaElementoSuperior(enlace, "LI", TiposBusqueda.busquedaPorEtiqueta).getElementsByClassName(clases.contenido)[0];
		desmarcaEncabezadoYaActivo().Y().marcaEncabezadoActivo(enlace).ocultaContenidoYaActivo().Y().muestraContenido(content);
		return this;
	}},
	redimensionaHeightContenedorPestanyas: function () {with(this) {
		calcularHeightMayorDeTodosLosContenidos().Y().asignarHeightMayorAlRestoDeLosContenidos().Y().asignaHeightMayorAlContenedorPestanyas();
		return this;
	}},
	calcularHeightMayorDeTodosLosContenidos: function () {with(this) {
		this.heightMayorDeTodosLosContenidos = 0;
		arrContenidosPestanyas.each( function (contenido) {
			if (contenido.clientHeight > heightMayorDeTodosLosContenidos)
				heightMayorDeTodosLosContenidos = contenido.clientHeight;
		},this);
		return this;
	}},	
	asignarHeightMayorAlRestoDeLosContenidos: function () {with(this) {
		arrContenidosPestanyas.each( function (contenido) {
			contenido.style.height = heightMayorDeTodosLosContenidos + "px";
		},this);
		return this;
	}},	
	asignaHeightMayorAlContenedorPestanyas: function () {with(this) {
		Element.buscaElementoSuperior(contenedor, "DIV", TiposBusqueda.busquedaPorEtiqueta).style.height = heightMayorDeTodosLosContenidos + 80 + "px";
		return this;
	}},	
	Y: function () {with(this) {
		return this;
	}}
}

Adesis.Utilidades.ModificarHTMLMapaGoogle = Class.create();
Adesis.Utilidades.ModificarHTMLMapaGoogle.prototype = {
	initialize: function (controladorGeneral, contenedor) { with(this) {
		inizializaVariables(contenedor).Y().modificaHTMLContenedor();
	}},
	inizializaVariables: function (contenedor) {
		this.htmlMapa= Constantes.htmlMapaGoogle;
		this.contenedor = contenedor;
		return this;
	},
	modificaHTMLContenedor: function () {with(this) {
		contenedor.innerHTML = htmlMapa;
	}},
	Y: function () {with(this) {
		return this;
	}}
}

if(typeof Adesis.Utilidades.ARIA == "undefined") Adesis.Utilidades.ARIA = {};
Adesis.Utilidades.ARIA.AsignarRolesEstructuras = Class.create();
Adesis.Utilidades.ARIA.AsignarRolesEstructuras.prototype = {
	initialize: function (controlador) { with(this){
	inicializaVariables(controlador)
			.Y()
			.insertaRolesEnElementoEstructura();
	
	}},
	inicializaVariables:  function (controlador) { with(this) {
		this.controladorGeneral = controlador;
		this.selectoresAriaRolesEstructuras = Selectores.aria.roles.landmark;
		this.roles = Constantes.aria.roles.landmark;
		return this;
	}},
	insertaRolesEnElementoEstructura:  function () { with(this) {
		
		if (existeEstructura(selectoresAriaRolesEstructuras.banner))
			insertaRole(selectoresAriaRolesEstructuras.banner,roles.banner);
		if (existeEstructura(selectoresAriaRolesEstructuras.complementary))
			insertaRole(selectoresAriaRolesEstructuras.complementary, roles.complementary);
		if (existeEstructura(selectoresAriaRolesEstructuras.contentinfo))
			insertaRole(selectoresAriaRolesEstructuras.contentinfo, roles.contentinfo);
		if (existeEstructura(selectoresAriaRolesEstructuras.main))
			insertaRole(selectoresAriaRolesEstructuras.main, roles.main);
		if (existeEstructura(selectoresAriaRolesEstructuras.navigation))
			insertaRole(selectoresAriaRolesEstructuras.navigation, roles.navigation);
		if (existeEstructura(selectoresAriaRolesEstructuras.search))
			insertaRole(selectoresAriaRolesEstructuras.search, roles.search);
		return this;
	}},	
	existeEstructura: function (selector) { with(this) {
		return ($$(selector).length > 0);
	}},		
	insertaRole: function (selector, paramRole) { with(this) {
		$$(selector).each( function(elemento) {
			elemento.setAttribute("role",paramRole);
		},this);
	}},	
	Y: function () { with(this) {
		return this;
	}}
};
