// JavaScript Document

var mydate=new Date()
var year=mydate.getYear()
if (year < 1000)
year+=1900
var day=mydate.getDay()
var month=mydate.getMonth()
var daym=mydate.getDate()
if (daym<10)
daym="0"+daym
var dayarray=new Array("Domingo","Lunes","Martes","Miercoles","Jueves","Viernes","Sabado")
var montharray=new Array("Enero","Febrero","Marzo","Abril","Mayo","Junio","Julio","Agosto","Septiembre","Octubre","Noviembre","Diciembre")
document.write(""+dayarray[day]+" "+daym+" de "+montharray[month]+" de "+year+"")

//////////////////////////////////////////////////////////////////////////////////////////
//CLASE PARA LA CAJA DE LOGIN 
//Espero que no se enfade nadie por haber  "tomado prestado" este script par nuestra página web, que 
//de cualquier modo no tiene ánimo de lucro. 
//Gracias a los desarrolladores de desarrolloweb
//////////////////////////////////////////////////////////////////////////////////////////
var CajaLoginUp = new Class({
	estado: "nologin",
	
	initialize: function(contenedorLogin){
		this.contenedorLogin = $(contenedorLogin);
		this.contenedorLogin.set('tween', {duration: 500});
		this.cajaLoading = new Element("div", {'class': 'loading'});
		this.cajaLoading.set("html","<img src='' >");
		this.cajaLoading.inject(this.contenedorLogin);
		this.cajaLogin  = null;
	},
	expande: function(){
		this.contenedorLogin.tween("height", "215px");
	},
	contrae: function(){
		//this.cajaLogin.set("styles", {'display': 'none'});
		this.contenedorLogin.tween("height", "0px");
		this.estado = "nologin";
	},
	muestraLoading: function(){
		this.cajaLoading.set("styles", {'display': 'block'});
	},
	muestraLogin: function(){
		this.cajaLoading.set("styles", {'display': 'none'});
		this.cajaLogin.set("styles", {'display': 'block', 'opacity': 0});
		this.cajaLogin.fade(1);
		this.estado = "completo";
	},
	ocultaLogin: function(){
		//this.cajaLogin.fade(0);
		//this.cajaLogin.set.delay(500,this, ["styles", {'display': 'none'}]);
		this.cajaLogin.set("styles", {'display': 'none'});
	},
	


accion: function(){
		if (this.estado=="nologin"){
			this.estado="cargando";
			//lo primero es ver si no tenemos el cajaLogin cargado
			if (!$defined(this.cajaLogin)){
				var esteObjeto = this;
				//es que no tenemos la caja de login cargada
				//la voy a cargar x ajax!
				var nuevoRequest = new Request({
					method: 'get',
					url: '/formularios-ajax/login.php?origen=' + url_origen,
					onRequest: function() { 
						esteObjeto.expande();
						esteObjeto.muestraLoading.delay(500,esteObjeto);
					},
					onSuccess: function(texto, xmlrespuesta){ 
						esteObjeto.cajaLogin = new Element('div', {id: 'cajalogin'});
						esteObjeto.cajaLogin.set('html',texto);
						esteObjeto.cajaLogin.inject(esteObjeto.contenedorLogin);
						esteObjeto.muestraLogin.delay(500, esteObjeto);
					},
					onFailure: function(){alert('Error!');}
				}).send();
				
			}else{
				//es que la caja de login ya la tenemos cargada
				this.expande();
				this.muestraLogin.delay(500, this);
			}
		}else{
			if(this.estado =="completo"){
				this.ocultaLogin();
				this.contrae();
			}
		}
	}
});

window.addEvent('domready', function(){
	if($defined($('enlacelogin'))){
		LoginPrincipal = new CajaLoginUp("clogin");
		$('enlacelogin').addEvent('click', function(evento){
			LoginPrincipal.accion();
		});
	}
});
//CLASE DE OPCIONES PRINCIPALES DEL NAVEGADOR
var OpcionPrincipal = new Class({
	initialize: function(texto, url, indice, navegadorDondeEstoy, claseEspecial){
		this.texto = texto;
		this.url = url;
		this.indice = indice;
		this.navegadorDondeEstoy = navegadorDondeEstoy;
		this.claseEspecial = claseEspecial;
		this.enlace = new Element("a", {href: this.url});
		this.enlace.set("html",this.texto); 
		this.enlace.addEvent("mouseenter",function(){
			this.navegadorDondeEstoy.ocultaSubmenus();
			this.navegadorDondeEstoy.muestraSubmenu(this.indice);
		}.bind(this));
		this.enlace.addEvent("mouseleave",function(){
			this.navegadorDondeEstoy.ocultaSubmenuRetardo(this.indice);
		}.bind(this));
	},
	
	dameCodigo: function(){
		return "<li class='" + this.claseEspecial + "'><a href='" + this.url + "'>" + this.texto + "</a></li>";
	},
	dameEnlace: function(){
		return this.enlace;
	},
	
	dameElementoLista: function(){
		var elementoLista = new Element("li",{'class': this.claseEspecial});
		this.enlace.inject(elementoLista);
		return elementoLista;
	}
});
////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////
//CLASE DE UNA ÚNICA OPCIÓN SECUNDARIA
var OpcionSecundaria = new Class({
	initialize: function(texto, url){
		this.texto = texto;
		this.url = url;
	},
	
	dameCodigo: function(){
		return "<div class='opcionessubnavegador'><a href='" + this.url + "'>" + this.texto + "</a></div>";
	}
});
////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////
//CLASE DE UN GRUPO DE OPCIONES SECUNDARIAS
var SubNavegador = new Class({
	initialize: function(arrayComponentes,indice, navegadorPresente, desplazamientoSubmenu){
		this.navegadorDondeEstoy = navegadorPresente;
		this.indice = indice;
		this.desplazamientoSubmenu = desplazamientoSubmenu;
		this.elementos = new Array();
		var i
		for (i=0; i<arrayComponentes[0].length; i++){
			this.elementos.include(new OpcionSecundaria(arrayComponentes[0][i], arrayComponentes[1][i]));
		}
		
		this.elementoGenerado  = new Element('div', {
			id: 'subnavegador' + this.indice, 'class': 'subnavegador'
		});
		var subNavmd = new Element ('div', {'class': 'subnavegadormd'});
		var subNavbl = new Element ('div', {'class': 'subnavegadorbl'});
		var subNavup = new Element ('div', {'class': 'subnavegadorup'});
		var subNavdw = new Element ('div', {'class': 'subnavegadordw'});
		subNavbl.inject(subNavmd);
		subNavup.inject(this.elementoGenerado);
		subNavmd.inject(this.elementoGenerado);
		subNavdw.inject(this.elementoGenerado);
		
		//meto los eventos para los div donde están los subnavegadores
		this.elementoGenerado.addEvent("mouseenter", function(){
			this.navegadorDondeEstoy.anulaRetardo();
		}.bind(this));
		this.elementoGenerado.addEvent("mouseleave", function(){
			this.navegadorDondeEstoy.ocultaSubmenuRetardo(this.indice);
		}.bind(this));
		
		var navegador = "";
		this.elementos.each(function (item, index){
			navegador += item.dameCodigo();
		});
		
		subNavbl.set("html",navegador);
	},
	
	ocultate: function(){
		this.elementoGenerado.setStyle("display", "none");
		this.navegadorDondeEstoy.opcionesPrincipales[this.indice].enlace.set("styles", {"color": "#000000", "background-color": "transparent", "border": "0px"});
	},
	
	muestrate: function(){
		if (this.elementos.length > 0){
			this.elementoGenerado.setStyle("display", "block");
		}
	},
	
	dameElemento: function(){
		return this.elementoGenerado;
	},
	
	recolocate: function(posicionEnlace, sizeEnlace){
		this.elementoGenerado.setStyle("top", posicionEnlace.y + sizeEnlace.y);
		this.elementoGenerado.setStyle("left", posicionEnlace.x - this.desplazamientoSubmenu);
	}
});
var MiPopup = new Class({
	initialize: function(miHtml,ancho,alto,titulo){
		this.titulo=titulo;
		this.tamanoBody = window.getScrollSize();
		this.posScroll = window.getScroll();
		this.espacioDisponibleVentana = window.getSize();
		this.capaSombra = new Element("div", {'id': 'capasombra', 'style': 'width: ' + this.tamanoBody.x + 'px; height: ' + this.tamanoBody.y + 'px; ' });
		this.capaSombra.inject(document.body);
		var myFx = new Fx.Tween(this.capaSombra,{'duration': 300});
		myFx.start('opacity',0,0.8);
		
		this.contenido = new Element("div", {'id': 'contenidopopup'});
		this.contenido.set('html', "<div class=cuerpotextopopup>" + miHtml + "</div>");
		var titulo = new Element("div", {'id': 'titulopopup'});
		titulo.set('html', this.titulo);
		var cerrar = new Element("div", {'id': 'cerrarpopup'});
		cerrar.addEvent('click', function(){
			this.cerrar();
		}.bind(this));
		cerrar.inject(titulo,'top');
		titulo.inject(this.contenido,'top');
				
		this.capaPopup = new Element("div", {'id': 'capapopup', 'style': 'margin-left:-' + ancho/2 +'px; top:' + (this.posScroll.y + (this.espacioDisponibleVentana.y/2) - (alto/2)-15) +'px'});
		this.capaPopup.inject(this.capaSombra,'after');
		
		var myFx2 = new Fx.Tween(this.capaPopup,{'duration': 700});
		myFx2.start('width',4,ancho);
		myFx2.addEvent('complete', function(){
			var myFx3 = new Fx.Tween(this.capaPopup,{'duration': 700});
			myFx3.start('height',4,alto+30);
			myFx3.addEvent('complete', function(){
				this.contenido.inject(this.capaPopup);
				this.contenido.setStyle('opacity', 0);
				this.contenido.setStyle('display', 'block');
				var myFx4 = new Fx.Tween(this.contenido,{'duration': 600});
				myFx4.start('opacity',0,1);
			}.bind(this));
		}.bind(this));
		
		this.capaSombra.addEvent('click', function(){
				this.cerrar();
			}.bind(this)
		);
	},
	
	cerrar: function(){
		var myFx = new Fx.Tween(this.capaPopup,{'duration': 500});
		myFx.start('opacity',1,0);
		myFx.addEvent('complete', function(){
			var myFx2 = new Fx.Tween(this.capaSombra,{'duration': 500});
			myFx2.start('opacity',0.8,0);
			myFx.addEvent('complete', function(){
				this.capaSombra.destroy();
				this.capaPopup.destroy();
			}.bind(this));
		}.bind(this));
	}
});

var TickerDW = new Class({
	indice: 0,
	contenedoresLlenos: 0,
	contenedorActual: 0,
	identificadorRetardo: null,
	
	//constructor
	//opcion classElemento (la class de css del elemento, por defecto "elementoticker")
	//opcion retardo (numero de milisegundos con el que configurar el retardo defecto: "3000")
	//opcion classContenedorElemento (la clase css del contenedor del elemento defecto "contelementoticker")
	//opcion numElementos (el numero de elementos qe se ven a la vez defecto 3)
	
	initialize: function(elementos, destino, opciones){
		this.ticker = $(destino);
		this.arrayElementos = elementos;
		if ($defined(opciones.classElemento)){
			this.classElemento = opciones.classElemento;
		}else{
			this.classElemento = "elementoticker";
		}
		if ($defined(opciones.classContenedorElemento)){
			this.classContenedorElemento = opciones.classContenedorElemento;
		}else{
			this.classContenedorElemento = "contelementoticker";
		}
		if ($defined(opciones.retardo)){
			this.retardo = opciones.retardo;
		}else{
			this.retardo = 3000;
		}
		if ($defined(opciones.numElementos)){
			this.numElementos = opciones.numElementos;
		}else{
			this.numElementos = 3;
		}
		this.contenedores = new Array();
		for (i=0; i < this.numElementos; i++){
			this.contenedores.include(new Element ("div", {
				"class": this.classContenedorElemento
			}));
			this.contenedores[i].inject(this.ticker);
		}
		
		if (this.arrayElementos.length > 0){
			this.rotaTextos();
		}
		
	},
	crearElemento: function(){
		var nuevoTitulo = new Element ("A", {
			"href": this.arrayElementos[this.indice].url,
			"html": this.arrayElementos[this.indice].titulo
		});
		var nuevoElemento = new Element ("div", {
			"class": this.classElemento
		});
		var nuevoTexto = new Element ("span", {
			"html": this.arrayElementos[this.indice].intro
		});
		nuevoTitulo.inject(nuevoElemento);
		nuevoTexto.inject(nuevoElemento);
		nuevoElemento.addEvent("mouseover", function(){
			$clear(this.identificadorRetardo);
		}.bind(this));
		nuevoElemento.addEvent("mouseout", function(){
			this.identificadorRetardo = this.rotaTextos.delay((this.retardo / 3).toInt(), this);
		}.bind(this));
		nuevoElemento.set("opacity", 0);
		return nuevoElemento;
	},
	
	muestraNoticia: function(){
		if (this.contenedoresLlenos < this.numElementos){
			var nuevoElemento = this.crearElemento();
			nuevoElemento.inject(this.contenedores[this.indice]);
			nuevoElemento.fade("in")
			this.contenedoresLlenos++;
			this.indice = (this.indice + 1) % this.arrayElementos.length;
		}else{
			var myFx1 = new Fx.Tween(this.contenedores[this.contenedorActual]);
			myFx1.start("height", 0);
			this.contenedores[this.contenedorActual].tween("margin-top", 0);
			this.contenedorEnCola.delay(800, this);
		}
	},
	
	contenedorEnCola: function(){
		this.contenedores[this.contenedorActual].destroy();
		this.contenedores[this.contenedorActual] = new Element ("div", {
			"class": this.classContenedorElemento
		});
		var nuevoElemento = this.crearElemento();
		nuevoElemento.inject(this.contenedores[this.contenedorActual]);
		this.contenedores[this.contenedorActual].inject(this.ticker);
		nuevoElemento.fade("in")
		this.contenedorActual = (this.contenedorActual + 1) % this.contenedores.length;
		this.indice = (this.indice + 1) % this.arrayElementos.length;
	},
	
	rotaTextos: function(){
		this.muestraNoticia();
		var actRetardo;
		if (this.contenedoresLlenos < this.numElementos){	
			actRetardo = (this.retardo/10).toInt();
		}else{
			actRetardo = this.retardo;
		}
		this.identificadorRetardo = this.rotaTextos.delay(actRetardo, this);
	}
});


