﻿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>";
		if ((esUnEnlaceAbreVentanaNueva(enlace)) ||(esUnEnlaceAbrePdf(enlace)) || (esUnEnlaceAbreDoc(enlace)))
			enlace.target = "blank";
		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;
	}}
};
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;
	}}
};

Adesis.Utilidades.ModificarHTMLIframes = Class.create();
Adesis.Utilidades.ModificarHTMLIframes.prototype = {
	initialize: function (controladorGeneral, contenedor) { with(this) {
		inizializaVariables(contenedor).Y().modificaHTMLContenedor();
	}},
	inizializaVariables: function (contenedor) {with(this) {
		this.contenedor = contenedor;
		return this;
	}},
	generaHTMLIframe: function () {with(this) {
		if (esIframeMapaGoogle())
			generaMapaGoogle();
		else
			contenedor.innerHTML = Constantes.htmlIframeLogin;
	}},	
	esIframeMapaGoogle: function () {with(this) {
		return (contenedor.hasClassName(Clases.mapaGoogle))
	}},	
	modificaHTMLContenedor: function () {with(this) {
		generaHTMLIframe();
	}},
	generaMapaGoogle: function () {with(this) {
		if (GBrowserIsCompatible()) {
			redimensionaCapaIFrameMapa().Y().creoEstanciaMapaGoogle().Y().creoIconoSituacion().Y().anyadoCoordenadasMapa().Y().anyadoControles();
		}
	}},
	creoEstanciaMapaGoogle: function () {with(this) {
		this.map = new GMap2(contenedor);  
		//map.setMapType(G_SATELLITE_MAP);
		return this;
	}},
	creoIconoSituacion: function () {with(this) {
		//DEFINO EL ICONO
		function addtag(point, address) {
			var marker = new GMarker(point);
			GEvent.addListener(marker, "click", function() {
			marker.openInfoWindowHtml(address); } );
			return marker;
        }
		var point = new GLatLng(41.4899, 2.0742);
		var address = '<p><b>COSALUD S.A</b> <a href="http://www.grupocatalanaoccidente.com" target="blank" title="Abre en ventana nueva">Grupo Catalana Occidente<span class="indentado"> Abre en ventana nueva</span></a></p><p> C/Francesc Viñas, 1.08174 SANT CUGAT DEL VALLÉS (BARCELONA)</p>';
        var marker = addtag(point, address);
		map.addOverlay(marker);
		marker.openInfoWindowHtml(address);
		return this;
	}},
	anyadoCoordenadasMapa: function () {with(this) {
		map.setCenter(new GLatLng(41.4900, 2.0742), 15);
		return this;
	}},
	anyadoControles: function () {with(this) {
		map.addControl(new GMapTypeControl());
        map.addControl(new GLargeMapControl());
        map.addControl(new GScaleControl());
        map.addControl(new GOverviewMapControl());
		return this;
	}},
	redimensionaCapaIFrameMapa: function () {with(this) {
		contenedor.setStyle({
				  width: '550px',
				  height: '350px'
			});
		return this;
	}},
	Y: function () {with(this) {
		return this;
	}}
};
Adesis.Utilidades.menuDesplegable = Class.create();
Adesis.Utilidades.menuDesplegable.prototype = {
	initialize: function (controladorGeneral, contenedor) { with(this) {
		inizializaVariables(contenedor).Y().insertaComportamiento().enOpcionesMenu($$(Selectores.menuDesplegable.opcionesMenuDesplegable));
	}},
	inizializaVariables: function (controladorGeneral, contenedor) {
		this.controladorGeneral = controladorGeneral;
		this.contenedor = contenedor;
		return this;
	},	
	accionaSubmenu: function (control) {with(this) {
		ocultaSubmenuActivo(control).Y().reseteaEnlaceActivo(control).Y().activaEnlace(control).Y().muestraSubmenu(control);
		return this;
	}},	
	reseteaEnlaceActivo: function (control) {with(this) {
		$$(Selectores.menuDesplegable.opcionActivo).each( function (opcionActiva) {
			if (esUnaOpcionDelMismoSubmenu(opcionActiva, control)){
				opcionActiva.removeClassName(Clases.generales.activo);	
			}
			
		},this);
		return this;
	}},	
	esUnaOpcionDelMismoSubmenu: function (opcionActiva, control) {with(this) {
		return(Element.up(opcionActiva, "ul") == control.up("ul"));
	}},	
	activaEnlace: function (control) {with(this) {
		Element.addClassName(control,Clases.generales.activo);
		return this;
	}},	
	ocultaSubmenuActivo: function (control) {with(this) {
		$$(Selectores.menuDesplegable.submenuActivo).each( function (opcionActiva) {
			if (!esElSubmenuDeLaOpcionClicada(opcionActiva, control)){
				opcionActiva.style.display = "none";	
			}
		},this);
		return this;
		return this;
	}},	
	esElSubmenuDeLaOpcionClicada: function (opcionActiva, control) {with(this) {
		return (Element.up(control,"ul") == opcionActiva);
	}},	
	muestraSubmenu: function (control) {with(this) {
		$$(Selectores.menuDesplegable.submenuActivo).each( function (opcionActiva) {
			if (esUnSubmenuDePrimerNivel(opcionActiva))
				opcionActiva.style.display = "block";
		},this);
		return this;
	}},
	esUnSubmenuDePrimerNivel: function (opcionActiva) {with(this) {
		return (opcionActiva.up("li").hasClassName("activo"))
	}},	
	insertaComportamiento: function () {with(this) {
		return controladorGeneral.insertaComportamiento(this);
	}},
	Y: function () {with(this) {
		return this;
	}}
};
