window.addEvent('domready',function(){
	new RotarImagenes();
	new MenuIntermedio();
	new MenuFooter();
	new Slide();
	new Botones();
	new SlidePatrocinadores();
});

var RotarImagenes = new Class({
	initialize: function(){
		this.imagenes 	= $('div-header-rotacion').getElements('img');
		this.total 			= this.imagenes.length;
		this.index 			= 0;
		this.recorrido 	= false;
		this.animando		= false;
		
		$('div-header-rotacion').set('tween',{duration:300});
		
		this.der = new Element('div',{
			'class':'div-rotar-der',
			styles:{
				opacity:0
			}
		}).inject(document.body);
		
		this.izq = new Element('div',{
			'class':'div-rotar-izq',
			styles:{
				opacity:0
			}
		}).inject(document.body);
		
		this.izq.set('tween',{duration:300});
		this.der.set('tween',{duration:300});
		
		$('div-header-rotacion').addEvent('mouseover',function(){
			var pox = new coordenadas($('div-header-rotacion'));
			this.izq.setStyles({
				left: pox.x + 'px',
				top: (pox.y + 49) + 'px'
			});
			this.der.setStyles({
				left: (pox.x + 197) + 'px',
				top: (pox.y + 49) + 'px'
			});
			this.izq.tween('opacity',1);
			this.der.tween('opacity',1);
		}.bind(this));
		
		$('div-header-rotacion').addEvent('mouseout',function(){
			this.izq.tween('opacity',0);
			this.der.tween('opacity',0);
		}.bind(this));
		
		this.izq.addEvent('mouseover',function(){
			this.izq.tween('opacity',1);
			this.der.tween('opacity',1);
		}.bind(this));
		
		this.der.addEvent('mouseover',function(){
			this.izq.tween('opacity',1);
			this.der.tween('opacity',1);
		}.bind(this));
		
		this.izq.addEvent('mouseout',function(){
			this.izq.tween('opacity',0);
			this.der.tween('opacity',0);
		}.bind(this));
		
		this.der.addEvent('mouseout',function(){
			this.izq.tween('opacity',0);
			this.der.tween('opacity',0);
		}.bind(this));
		
		this.izq.addEvent('click',function(){
			if(this.animando == false){
				this.animando 	= true;
				this.recorrido 	= true;
				if(this.index == 0){
					this.index = this.total - 1;
				}
				else{
					this.index--;
				}
				$('div-header-rotacion').tween('opacity',0);
				(function(){
					$('div-header-rotacion').setStyle('background-image','url(' + this.imagenes[this.index].getProperty('src') + ')');
					$('div-header-rotacion').tween('opacity',1);
					(function(){this.animando = false;}.bind(this)).delay(300);
				}.bind(this)).delay(300);
			}
		}.bind(this));
		
		this.der.addEvent('click',function(){
			if(this.animando == false){
				this.animando 	= true;
				this.recorrido 	= true;
				if(this.index == this.total - 1){
					this.index = 0;
				}
				else{
					this.index++;
				}
				$('div-header-rotacion').tween('opacity',0);
				(function(){
					$('div-header-rotacion').setStyle('background-image','url('+this.imagenes[this.index].getProperty('src')+')');
					$('div-header-rotacion').tween('opacity',1);
					(function(){this.animando = false;}.bind(this)).delay(300);
				}.bind(this)).delay(300);
			}
		}.bind(this));		
		
		this.funcion = function(){
			if(this.recorrido == false){
				if(this.index == this.total - 1){
					this.index = 0;
				}
				else{
					this.index++;
				}
				$('div-header-rotacion').tween('opacity',0);
				(function(){
					$('div-header-rotacion').setStyle('background-image','url('+this.imagenes[this.index].getProperty('src')+')');
					$('div-header-rotacion').tween('opacity',1);
					(function(){this.funcion();}.bind(this)).delay(6000);
				}.bind(this)).delay(300);
			}
			else{
				this.recorrido = false;
				(function(){this.funcion();}.bind(this)).delay(6000);
			}
		}.bind(this);
		(function(){this.funcion();}.bind(this)).delay(5000);
	}
});

var MenuIntermedio = new Class({
	initialize:function(){
		var tabla = '<table align="center"><tr>';
		var link = '';
		$('menu-intermedio').getChildren('li').each(function(li, index){
			var class1 = 'opc-menu';
			var class2 = 'a-opc-menu';
			li.getProperty('class').split(' ').each(function(clase){
				if(clase == 'current_page_item' || clase == 'current-menu-item'){
					class1 = 'opc-menu-activo';
					class2 = 'a-opc-menu-activo';
				}
			});
			link = '<a class="' + class2 + '" href = "' + li.getElement('a').getProperty('href') + '">' + li.getElement('a').getProperty('text').toUpperCase() + '</a>';
			tabla = tabla + '<td style="vertical-align:middle;"><div class="' + class1 + ' opc-menu-accion" id="menu-opc-' + index + '">' + link + '</div></td>';
			if(li.getElement('ul.sub-menu')){
				var submenus = li.getElement('ul.sub-menu').getChildren('li');
				if(submenus.length > 0){
					var div = new Element('div',{
						id: 'sub-menu-opc-' + index,
						'class':'div-submenu'
					}).inject(document.body);
					
					submenus.each(function(submenu, index2){
						if(index2 > 0){
							var bg = new Element('div',{
								'class':'div-submenu-bg'
							}).inject(div);
							
							var renglon = new Element('div',{
								'class':'div-submenu-renglon'
							}).inject(bg);
						}
						else{
							var renglon = new Element('div',{
								'class':'div-submenu-renglon'
							}).inject(div);
						}
						
						link = '<a class="a-submenu-renglon" href = "' + submenu.getElement('a').getProperty('href') + '">' + submenu.getElement('a').getProperty('text') + '</a>';
						renglon.set('html',link);
					}.bind(this));
				}
			}
		});
		tabla = tabla + '</tr></table>';
		$('menu-inferior').set('html',tabla);
		
		$$('div.opc-menu-accion').each(function(div){
			div.addEvent('mouseover',function(){
				this.setStyle('background-color','#6F292B');
			});
			div.addEvent('mouseout',function(){
				this.setStyle('background','none');
			});
		});
		
		$$('div.opc-menu-accion').each(function(div){
			div.addEvent('mouseover',function(){
				if($('sub-'+this.getProperty('id'))){
					var pos = new coordenadas(this);
					$('sub-'+this.getProperty('id')).setStyles({
						display:'block',
						left: pos.x + 'px',
						top: (pos.y + 36) + 'px'
					});
				}
			});
			div.addEvent('mouseout',function(){
				if($('sub-'+this.getProperty('id'))){
					$('sub-'+this.getProperty('id')).setStyle('display','none');
				}
			});
			
			div.getElement('a').addEvent('mouseover',function(){
				if($('sub-'+this.getProperty('id'))){
					var pos = new coordenadas(this);
					$('sub-'+this.getProperty('id')).setStyles({
						display:'block',
						left: pos.x + 'px',
						top: (pos.y + 36) + 'px'
					});
				}
			});
			div.getElement('a').addEvent('mouseout',function(){
				if($('sub-'+this.getProperty('id'))){
					$('sub-'+this.getProperty('id')).setStyle('display','none');
				}
			});
			
			$$('div.div-submenu').addEvent('mouseover',function(){
				this.setStyle('display','block');
			});
			$$('div.div-submenu').addEvent('mouseout',function(){
				this.setStyle('display','none');
			});
			
			$$('div.div-submenu-renglon').addEvent('mouseover',function(){
				this.setStyle('background-color','#6F292B');
			});
			
			$$('div.div-submenu-renglon').addEvent('mouseout',function(){
				this.setStyle('background-color','#372001');
			});
		});
	}
});


var MenuFooter = new Class({
	initialize: function(){
		var tabla = '<table align="center"><tr>';
		var link = '';
		$('menu-footer').getElements('li').each(function(li, index){
			if(index != 0){
				tabla = tabla + '<td style="vertical-align:middle;"><span class="span-opc-menu-footer">|</span></td>';
			}
			var nombre_class = 'a-opc-menu-footer';
			li.getProperty('class').split(' ').each(function(clase){
				if(clase == 'current_page_item' || clase == 'current-menu-item'){
					nombre_class = 'a-opc-menu-footer-hover';
				}
			});
			link = '<a class="' + nombre_class + '" href = "' + li.getElement('a').getProperty('href') + '">' + li.getElement('a').getProperty('text').toUpperCase() + '</a>';
			tabla = tabla + '<td style="vertical-align:middle;"><div class="opc-menu-footer">' + link + '</div></td>';
		});
		tabla = tabla + '</tr></table>';
		$('contenedor-footer').set('html',tabla);
	}
});

var Slide = new Class({
	initialize: function(){
		if(!$('slide-imagenes')){return false;}
		this.imagenes = $$('img.img-slide');
		if(this.imagenes.length == 0){return false;}
		$('contenedor-slide-imagenes').setStyles({
			backgroundImage:		'url(' + this.imagenes[0].getProperty('src') + ')',
			backgroundPosition:	'0 0',
			backgroundRepeat:		'no-repeat'
		});
		if(this.imagenes.length == 1){return false;}
		
		$('contenedor-slide-imagenes').set('tween',{duration:300});
		$('slide-imagenes').set('tween',{duration:300});
		
		this.apuntador 	= 0;
		this.contador		= this.imagenes.length - 1;
		this.pos				= new coordenadas($('slide-imagenes'));
		this.animacion	= false;
		
		this.izq = new Element('div',{
			'class':	'div-slide-izquierda',
			styles:{
				left: this.pos.x + 'px',
				top:	(this.pos.y + 144)+ 'px',
				opacity:0
			}
		}).inject(document.body).set('tween',{duration:150});
		
		this.der = new Element('div',{
			'class':	'div-slide-derecha',
			styles:{
				left: (this.pos.x + 503) + 'px',
				top:	(this.pos.y + 144)+ 'px',
				opacity:0
			}
		}).inject(document.body).set('tween',{duration:150});
		
		this.funDesplegar = function(){
			//alert(this.apuntador + '|' + this.contador);
			if(this.apuntador > 0){
				this.izq.tween('opacity',1);
			}
			if(this.apuntador < this.contador){
				this.der.tween('opacity',1);
			}
		}.bind(this);
		
		this.funOcultar = function(){
			this.izq.tween('opacity',0);
			this.der.tween('opacity',0);
		}.bind(this);
		
		$('slide-imagenes').addEvent('mouseover',this.funDesplegar);
		$('slide-imagenes').addEvent('mouseout',this.funOcultar);
		this.izq.addEvent('mouseover',this.funDesplegar);
		this.der.addEvent('mouseover',this.funDesplegar);
		this.izq.addEvent('mouseout',this.funOcultar);
		this.der.addEvent('mouseout',this.funOcultar);
		
		this.der.addEvent('click',function(){
			if(this.animacion == false){
				this.animacion = true;
				
				$('contenedor-slide-imagenes').setStyles({
					backgroundImage:		'url(' + this.imagenes[this.apuntador].getProperty('src') + ')',
					backgroundPosition:	'0 0',
					backgroundRepeat:		'no-repeat'
				});
				
				$('slide-imagenes').setStyles({
					backgroundImage:		'url(' + this.imagenes[this.apuntador + 1].getProperty('src') + ')',
					backgroundPosition:	'550px 0',
					backgroundRepeat:		'no-repeat'
				});
				
				$('contenedor-slide-imagenes').tween('background-position','-550px');
				$('slide-imagenes').tween('background-position','0px');
				
				if(this.apuntador == 0){
					this.izq.tween('opacity',1);
				}
				
				this.apuntador++;
				
				if(this.apuntador == this.contador){
					this.der.tween('opacity',0);
				}
				
				(function(){this.animacion = false;}.bind(this)).delay(305);
			}
		}.bind(this));
		
		this.izq.addEvent('click',function(){
			if(this.animacion == false){
				this.animacion = true;
				
				$('contenedor-slide-imagenes').setStyles({
					backgroundImage:		'url(' + this.imagenes[this.apuntador].getProperty('src') + ')',
					backgroundPosition:	'0 0',
					backgroundRepeat:		'no-repeat'
				});
				
				$('slide-imagenes').setStyles({
					backgroundImage:		'url(' + this.imagenes[this.apuntador - 1].getProperty('src') + ')',
					backgroundPosition:	'-550px 0',
					backgroundRepeat:		'no-repeat'
				});
				
				$('contenedor-slide-imagenes').tween('background-position','550px');
				$('slide-imagenes').tween('background-position','0px');
				
				if(this.apuntador == this.contador){
					this.der.tween('opacity',1);
				}
				
				this.apuntador--;
				
				if(this.apuntador == 0){
					this.izq.tween('opacity',0);
				}
					
				(function(){this.animacion = false;}.bind(this)).delay(305);
			}
		}.bind(this));
		
		$('contenedor-slide-imagenes').addEvent('click',function(){
			document.location.href=this.imagenes[this.apuntador].getParent('a').getProperty('href');
		}.bind(this));
	}
});

var Botones = new Class({
	initialize: function(){
		$$('input').each(function(boton){
			if(boton.getProperty('type') == 'submit' || boton.getProperty('type') == 'button'){
				boton.setProperty('class','boton-01');
				boton.addEvent('mouseover',function(){
					this.setProperty('class','boton-01-hover');
				});
				boton.addEvent('mouseout',function(){
					this.setProperty('class','boton-01');
				});
			}
		});
	}
});

var coordenadas = new Class({
	initialize: function(elemento){
		if(elemento){
			var posX = 0, posY = 0;
			while (elemento.offsetParent) {posX += elemento.offsetLeft; posY += elemento.offsetTop; elemento = elemento.offsetParent;}
			this.x = posX, this.y = posY;
		}
		else{ this.x = 0,	this.y = 0;}
	}
});

var SlidePatrocinadores = new Class({
	initialize: function(){
		if(!$('div__cont__patrocinadores')){return false;}
		this.imagenes = $('div__cont__patrocinadores').getElements('img');
		if(this.imagenes.length == 0){return false;}
		this.index = 0;
		this.total = this.imagenes.length;
		$('div__cont__patrocinadores').setStyles({
			backgroundImage:		'url(' + this.imagenes[this.index].getProperty('src') + ')',
			backgroundPosition: 'center center',
			backgroundRepeat: 	'no-repeat'
		});
		if(this.imagenes.length == 1){return false;}
		
		$('div__cont__patrocinadores').set('tween',{duration:300});
		
		this.funRotar = function(){
			if(this.index == this.total - 1){
				this.index = 0;
			}
			else{
				this.index++;
			}
			$('div__cont__patrocinadores').tween('opacity',0);
			(function(){
				$('div__cont__patrocinadores').setStyle('background-image','url(' + this.imagenes[this.index].getProperty('src') + ')');
				$('div__cont__patrocinadores').tween('opacity',1);
				(function(){this.funRotar()}.bind(this)).delay(6000);
			}.bind(this)).delay(300);
		}.bind(this);
		
		(function(){this.funRotar()}.bind(this)).delay(6000);
	}
});
