/* 
	Core: MooTools 1.11
*/
var reproducir;
var volumen;

var metodoDeCarga = (window.ie ? 'load' : 'domready');
window.addEvent(metodoDeCarga, function(){
	/*
		Reproductor de Loops
	*/
	var autoplay = Cookie.get('autoplay') || 'true';
	var volumen_ = Cookie.get('volumen_') || 1.00;
	//new Element('span').setStyles({'position':'absolute','z-index':9}).setHTML(autoplay+'<br>'+volumen_).injectTop(document.body);
	var pista = 'track'+$random(1,3)+'.mp3';
	var so = new SWFObject('public/flash/reproductor.simple.v2011.2.swf', 'reproductor', '148', '19', '8', '#dd2e19');
		so.addParam('wmode','transparent');
		so.addParam('base','public/flash');
		so.addParam('allowscriptaccess','sameDomain');
		so.addVariable('pista',pista);
		so.addVariable('autoplay',autoplay);
		so.addVariable('volumen_',volumen_);
		so.write('reproductor_');
	reproducir = function(onoff){
		Cookie.set('autoplay', onoff,{path:'/'});
	}
	volumen = function(nivel){
		Cookie.set('volumen_', nivel,{path:'/'});
	}
	/*
		Animar Banners
	*/
	var banners = $('banners');
	if(banners && banners.getChildren().length > 1){
		var bft = null;
		var fxBanners = function(banner){
			try {
				if(!banner) banner = banners.getFirst();
				if(!banner) return false;
				var cnfg = {wait:false, duration:500, transition: Fx.Transitions.Circ.easeOut};
				switch($random(1,3)){
					case 1:
						banner.setOpacity(0).injectInside(banner.getParent());
						banner.effect('opacity',cnfg).start(0,1);
						break;
					case 2:
						banner.setStyle('left',880).injectInside(banner.getParent());
						banner.effect('left',cnfg).start(880,0);
						break;
					case 3:
						banner.setStyle('top',-280).injectInside(banner.getParent());
						banner.effect('top',cnfg).start(-280,0);
						break;
				}
			} catch(err) {
				bft = $clear(bft);
				return false;
			}
		}
		bft = fxBanners.periodical(6000);
	}
	/*
		Animar Galeria
		transition: Fx.Transitions.Circ.easeOut
	*/
	var fnGaleria = function(fotografias){
		var luft = null;
		var fxFotografias = function(fotografia){
			try {
				if(!fotografia) fotografia = fotografias.getFirst();
				if(!fotografia) return false;
				fotografia.setOpacity(0).injectInside(fotografia.getParent());
				fotografia.effect('opacity',{wait:false, duration:500}).start(0,1);
			} catch(err) {
				luft = $clear(luft);
				return false;
			}
		}
		var fotoindice = new Element('div',{'class':'fotoindice'}).injectAfter(fotografias);
		var imgs = fotografias.getElements('img');
		imgs.each(function(img){
			new Element('span').addEvent('click',function(){
				luft = $clear(luft);
				fxFotografias(img);
			}).setStyles({
				'background':'url('+img.src.replace('public/','public/minimi.php?')+',45,25,1) center center no-repeat'
			}).injectInside(fotoindice);
		});
		if(imgs.length>1) luft = fxFotografias.periodical($random(3000,3500));
		else return false;
	}
	/*
		Procedimiento: Ver Artículos
	*/
	var spanEUM = new Element('span').setStyles({
		'background':'url(public/pics/cargando.gif) center center no-repeat',
		'display':'block', 'height':408
	});
	$$('#loUltimoNoticias .fotografias').each(fnGaleria);
	$$('#loUltimoMenu a').addEvent('click',function(e){
		var e = new Event(e).stop();
		var titulo = this.getText();
		var noticia = $$('#loUltimoNoticias .titulo').filter(function(strong){
			return (strong.getText() == titulo);
		});
		if(noticia.length > 0) return noticia[0].getParent().injectTop('loUltimoNoticias');
		else noticia = new Element('div',{'class':'noticia'}).injectTop('loUltimoNoticias');
		spanEUM.clone().injectInside(noticia);
		var dirweb = this.href.replace(/[^a-z0-9\/\:\.\_\-\ ]/gi,'');
		new Ajax(dirweb,{
			data: 'ajax=true',
			update: noticia, onComplete: function(){
				fnGaleria(noticia.getElement('.fotografias'));
			}
		}).request();
	});
	$$('#empresaMenu a, #instalacionMenu a').addEvent('click',function(e){
		var e = new Event(e).stop();
		var titulo = this.getText();
		var cntndr = $('empresaContenido') || $('instalacionContenido');
		var noticia = cntndr.getElements('.titulo').filter(function(strong){
			return (strong.getText() == titulo);
		});
		if(noticia && noticia.length>0) return false;
		else {
			cntndr.empty();
			noticia = new Element('div',{'class':'noticia'}).injectTop(cntndr);
		}
		spanEUM.clone().injectInside(noticia);
		var dirweb = this.href.replace(/[^a-z0-9\/\:\.\_\-\ ]/gi,'');
		new Ajax(dirweb,{
			update: noticia, onComplete: function(){
				fnGaleria(noticia.getElement('.fotografias'));
			}
		}).request();
	});
	/*
		Procedimiento: Catalogo (Menú y Consulta de Elementos)
	*/
	var catalogo = $('rentaCatalogo') || $('mageventCatalogo');
	if(catalogo){
		var subcatalogo =  $('rentaSubcatalogo') || $('mageventSubcatalogo');
		var as = catalogo.getElements('a');
			as.addEvent('click',function(e){ 
				var e = new Event(e).stop();
			});
		var divs = subcatalogo.getElements('div');
		var acordeonRTCA = new Accordion(as,divs,{
			opacity:true, wait:false, alwaysHide:true, 
			onActive:function(toggler){
				$(toggler).addClass('activo');
			},
			onBackground:function(toggler){
				$(toggler).removeClass('activo');
			}
		});
	}
	$$('#rentaSubcatalogo a, #mageventSubcatalogo a').addEvent('click',function(e){
		var e = new Event(e).stop();
		var titulo = this.getText();
		var cntndr = $('rentaContenido') || $('mageventContenido');
		var ficha = cntndr.getElements('.codigo').filter(function(strong){
			return (strong.getText() == titulo);
		});
		if(ficha && ficha.length>0) return false;
		else {
			var activo = this.getParent().getParent().getElement('.activo');
			if(activo) activo.removeClass('activo');
			this.addClass('activo');
			cntndr.empty();
			ficha = new Element('div',{'id':'vistaDeCatalogo'}).injectInside(cntndr);
		}
		spanEUM.clone().injectInside(ficha);
		var dirweb = this.href.replace(/[^a-z0-9\/\:\.\_\-\ ]/gi,'');
		new Ajax(dirweb,{
			update: ficha, onComplete: function(){
				//fnGaleria(ficha.getElement('.fotografias'));
			}
		}).request();
	});
	/*
		Procedimiento: Contacto
	*/
	if($('formaDeContacto')){
		$$('#contactoDivision a.quien').filter(function(a){
			return (a.getText().clean().toLowerCase().test('ubicación') == false);
		}).each(function(a){
			a.addEvent('click',function(e){
				var e = new Event(e).stop();
				var titulo = this.getText().clean();
				var activo = this.getParent().getElement('.activo');
				var para = this.getNext().getElement('a[href^=mailto:]');
				if(activo) activo.removeClass('activo');
				this.addClass('activo');
				$('formaDeContacto').getElement('strong').setHTML(titulo);
				$('formaDeContacto').getElement('input[name=para]').value = (para ? para.innerHTML : null);
			});
		});
		$('formaDeContacto').getElement('form').addEvent('submit',function(e){
			var e = new Event(e).stop();
			var frm = this;
			var ok = true;
			if(frm.getElement('input[name=nombre]').value == '') ok = false;
			if(frm.getElement('input[name=email]').value == '') ok = false;
			if(frm.getElement('input[name=telefono]').value == '') ok = false;
			if(frm.getElement('textarea[name=mensaje]').value == '') ok = false;
			if(!ok) return alert('Faltan datos.\nVerifique su información e Intente de Nuevo');
			var msk = new Element('div').setStyles({
				'background':'#000 url(public/pics/cargando.gif) center center no-repeat', 'opacity':0.90,
				'position':'absolute', 'top':0, 'left':0, 'right':0, 'bottom':0, 'z-index':1
			}).injectInside('formaDeContacto');
			var rcp = new Element('div').setStyles({
				'margin':'10% 12.5%'
			}).injectInside(msk);
			frm.send({
				update: rcp, onComplete: function(){
					msk.setStyle('background','#000');
					new Element('span').setStyles({
						'display':'block',
						'background':'#202020', 'color':'#fff', 'border':'1px solid #101010',
						'width':128,'height':32,'line-height':32,'text-align':'center', 
						'cursor':'pointer','margin':'20px auto 0px'
					}).addEvent('click',function(){
						msk.remove();
						frm.reset();
					}).setHTML('&laquo; volver al formulario').injectInside(msk);
				}
			});
		});
	}
});
