﻿var AjaxEnv=0,AjaxRec=0,mMenu,ajax;
var agregarOpcion=function(el,pl,l){
	if(l) pl.getChildren().dispose();
	el.each(function(c){
		new Element('option',{'html': c.nombre,'value':c.link}).inject(pl)
	});
	if(pl.length>1) pl.disabled=false; else pl.disabled=true;
},
AjaxAccion=function(mas){
	if(mas==1) AjaxRec++;
	if(mas==0) AjaxEnv++;
	if(AjaxRec>=AjaxEnv) ajax.setStyle('display','none'); else ajax.setStyle('display','block');
},
PintarTitulos=function(){
	document.getElements('.titulogrl').each(function(c){
		if(!c.hasClass('cv')){
			var temp=c.innerHTML.split(" ");
			temp[0]=temp[0].fontcolor('#9BB824');
			c.innerHTML=temp.join(" ");
			c.addClass('cv');
		}
	});
},
ocultarSub=function(s){
	s.m.removeClass('ssel');
	s.morph({ 
		'opacity' : 0,
		'top' : ['0px','40px']
	});
},
mostrarSub=function(s){
	s.setStyle('z-index','10');
	s.m.addClass('ssel');
	s.morph({ 
		'opacity' : 1,
		'top' : ['-40px','0px']
	});
},
GenerarSubmenu=function(){
	var menu=$('menu').getChildren(),submenu=$('submenu').getChildren()[0].getChildren();
  $$('div.imagenb3 div.titular').each(function(el){
    el.setStyle('opacity',.8)
  });
	menu.each(function(el, i){
		if(!el.hasClass('sel')){
			el.set('morph', {link : 'cancel',duration:1000}); 
			submenu[i].m=el;
			el.addEvents({ 
				'mouseenter': function() {
					this.addClass('ssel');
				}, 
				/*'click': function() { 
					if(mMenu!=undefined&&mMenu!=submenu[i]) ocultarSub(mMenu)
					mostrarSub(submenu[i])
					mMenu=submenu[i];
					return false;
				},*/
				'mouseleave' : function() { 
					if(mMenu==undefined||mMenu!=submenu[i])	this.removeClass('ssel');
				}
			});
		}else{
			el.addEvents({ 
				'mouseenter': function() {
					if(mMenu!=undefined&&!mMenu.m.hasClass('sel')){
						ocultarSub(mMenu)
						mMenu=undefined;
					}
				}
			})		
		}
	});
},
Elemento={
	HojaActual:0,
	Nombre:'Elemento',
	AgregarValor:function(n,v,l){
		if(v)	new Element('strong',{'html': n}).inject(new Element('h3',{'html':v}).inject(l),'top');
	},
	ToolTip:function(){},
	ToogleToolTip:function(o){
		var tt=$('tooltip'),myEffect=new Fx.Morph(tt, {duration: 300, transition: Fx.Transitions.Sine.easeOut});
		tt.style.display='block';
		myEffect.cancel();
		if(o) myEffect.start({'opacity': 1}); else myEffect.start({'opacity': 0});
	},
	MostrarTCnt:function(el){
		var tt=$('tooltip'),cnt=tt.getElement('#toolcnt');
		cnt.getChildren().each(function(g){
			if(g==el) g.style.display='block'; else g.style.display='none';
		});
		this.ToogleToolTip(1);
	},
	Colorear:function(el){
		var thees=this;
		document.getElements('.recbuscar').each(function(c){
			c.addEvent('mouseenter',function(){
				$clear(this.cerrar)
				this.abrir=setTimeout(el+".ToolTip("+this.id.split('dir')[1]+")", 200);
				this.style.background='#e7ffe2';
				this.getElements('.imagenb2')[0].style.background='#e7ffe2';
			});	
			c.addEvent('mouseleave',function(){
				$clear(this.abrir)
				this.cerrar=setTimeout(el+".ToogleToolTip(0)", 0);
				this.style.background='#FFF';
				this.getElements('.imagenb2')[0].style.background='#FFF';
			});
			c.addEvent('mousemove',function(event){
				var size=window.getSize(),scroll=window.getScroll(),tt=$('tooltip');
				var tip={x:tt.offsetWidth,y:tt.offsetHeight},props={x:'left',y:'top'};
				var over=16;
				for(var z in props){
					var pos=event.page[z]+over;
					if((pos+tip[z]-scroll[z])>size[z]) pos=event.page[z]-over-tip[z];
					tt.setStyle(props[z],pos);
				}
			});
		});
	},
	ComentarElemento:function(id,t){
		var j=$$('.dejarcmt'),cmt=j.getElement('textarea')[0].value;
		if(cmt.length<2) return false;
		j.getElement('a')[0].setStyle('display','none');
		j.getElement('textarea')[0].disabled='disabled';
		var myHTMLRequest = new Request.HTML({
			url:'/json-comentarios/',
			evalScripts: true,
			onSuccess:function(html) {
				AjaxAccion(1);
				j.getElement('textarea')[0].value='Su comentario fue enviado correctamente. A la brevedad será activado.'
			}}
		).get({'id':id,'cmt':cmt,'t':t});
		AjaxAccion(0)
	},
	AleatoriaToolTip:function(tira){
		var thees=this;
		if(tira==undefined) tira=$('tiraImg'); else tira=$(tira);
		tira.getElements('a').each(function(c){
			c.addEvent('mouseenter',function(){
				$clear(this.cerrar)
				this.abrir=setTimeout(thees.Nombre+".ToolTip("+this.id.split('ale')[1]+")", 200);
			});	
			c.addEvent('mouseleave',function(){
				$clear(this.abrir)
				this.cerrar=setTimeout(thees.Nombre+".ToogleToolTip(0)", 0);
			});
			c.addEvent('mousemove',function(event){
				var size=window.getSize(),scroll=window.getScroll(),tt=$('tooltip');
				var tip={x:tt.offsetWidth,y:tt.offsetHeight},props={x:'left',y:'top'};
				var over=16;
				for(var z in props){
					var pos=event.page[z]+over;
					if((pos+tip[z]-scroll[z])>size[z]) pos=event.page[z]-over-tip[z];
					tt.setStyle(props[z],pos);
				}
			});
		});
	}
}
var directorio=$merge(Elemento,{
	Nombre:'directorio',
	ToolTip:function(id){
		var el=$('dira'+id),tt=$('tooltip'),cnt=tt.getElement('#toolcnt');
		if(el==undefined){
			el=new Element('div',{'class': 'tooltipdir','id':'dira'+id}).inject(cnt,'top'),thees=this;
			new Request.JSON({url: "/json-directorio.elemento/", onComplete: function(c){
				AjaxAccion(1);
				c=c.resultado[0];
				new Element('strong',{'html': c.nombre}).inject(new Element('h2').inject(el));
				var tmp=new Element('div',{'class':'titulobsq'}).inject(el);
				thees.AgregarValor('Categoria',c.categoria,tmp)
				thees.AgregarValor('Sinopsis',c.sinopsis,tmp)
				thees.AgregarValor('Población',c.poblacion,tmp)
				thees.AgregarValor('Provincia',c.provincia,tmp)
				thees.AgregarValor('País',c.pais,tmp)
				thees.MostrarTCnt(el)
			}}).get({'id':id});
			AjaxAccion(0)
		}else this.MostrarTCnt(el)
	},
	CargarElemento:function(id){
		var myHTMLRequest = new Request.HTML({
			url:'/json-directorio.galeria/',
			evalScripts: true,
			onSuccess:function(html) {
				AjaxAccion(1);
				document.getElement('.hojan').set('text', '');
				document.getElement('.hojan').adopt(html);
				Galeria.mostrar();
			}}
		).get({'id':id});
		AjaxAccion(0)
	}
});
var revista=$merge(Elemento,{
	Nombre:'revista',
	ToolTip:function(id){
		var el=$('dira'+id),tt=$('tooltip'),cnt=tt.getElement('#toolcnt');
		if(el==undefined){
			el=new Element('div',{'class': 'tooltipdir','id':'dira'+id}).inject(cnt,'top'),thees=this;
			new Request.JSON({url: "/json-articulos.elemento/", onComplete: function(c){
				AjaxAccion(1);
				c=c.resultado[0];
				new Element('strong',{'html': c.titulo}).inject(new Element('h2').inject(el));
				var tmp=new Element('div',{'class':'titulobsq'}).inject(el);
				thees.AgregarValor('Categoria',c.categoria,tmp)
				thees.AgregarValor('Subcategoria',c.subcategoria,tmp)
				thees.AgregarValor('Autor',c.autor,tmp)
				thees.AgregarValor('Lugar',c.lugar,tmp)
				thees.AgregarValor('Fecha de Creación',c.fecha,tmp)
				thees.MostrarTCnt(el)
			}}).get({'id':id});
			AjaxAccion(0)
		}else this.MostrarTCnt(el)
	},
	CargarElemento:function(id){
		var myHTMLRequest = new Request.HTML({
			url:'/json-articulos.galeria/',
			evalScripts: true,
			onSuccess:function(html) {
				AjaxAccion(1);
				document.getElement('.hojan').set('text', '');
				document.getElement('.hojan').adopt(html);
				Galeria.mostrar();
			}}
		).get({'id':id});
		AjaxAccion(0)
	}
});
var productos=$merge(Elemento,{
	Nombre:'productos',
		ToolTip:function(id){
		var el=$('dira'+id),tt=$('tooltip'),cnt=tt.getElement('#toolcnt');
		if(el==undefined){
			el=new Element('div',{'class': 'tooltipdir','id':'dira'+id}).inject(cnt,'top'),thees=this;
			new Request.JSON({url: "/json-directorio.elemento/", onComplete: function(c){
				AjaxAccion(1);
				c=c.resultado[0];
				new Element('strong',{'html': c.nombre}).inject(new Element('h2').inject(el));
				var tmp=new Element('div',{'class':'titulobsq'}).inject(el);
				thees.AgregarValor('Categoria',c.categoria,tmp)
				thees.AgregarValor('Subcategoria',c.subcategoria,tmp)
				thees.AgregarValor('Sinopsis',c.sinopsis,tmp)
				thees.MostrarTCnt(el)
			}}).get({'id':id,'s':'productos'});
			AjaxAccion(0)
		}else this.MostrarTCnt(el)
	},
	CargarElemento:function(id){
		var myHTMLRequest = new Request.HTML({
			url:'/json-directorio.galeria/',
			evalScripts: true,
			onSuccess:function(html) {
				AjaxAccion(1);
				document.getElement('.hojan').set('text', '');
				document.getElement('.hojan').adopt(html);
				Galeria.mostrar();
			}}
		).get({'id':id});
		AjaxAccion(0)
	}
});
var planta=$merge(Elemento,{
	Nombre:'planta',
	ToolTip:function(id){
		var el=$('dira'+id),tt=$('tooltip'),cnt=tt.getElement('#toolcnt');
		if(el==undefined){
			el=new Element('div',{'class': 'tooltipdir','id':'dira'+id}).inject(cnt,'top'),thees=this;
			new Request.JSON({url: "/json-plantas.elemento/", onComplete: function(c){
				AjaxAccion(1);
				c=c.resultado[0];
				new Element('strong',{'html': c.titulo}).inject(new Element('h2').inject(el));
				var tmp=new Element('div',{'class':'titulobsq'}).inject(el);
				thees.AgregarValor('Tipo de Planta',c.tipo,tmp)
				thees.AgregarValor('Nombre Cientifico',c.cientifico,tmp)
				thees.AgregarValor('Familia',c.familia,tmp)
				thees.AgregarValor('Altura',c.altura,tmp)
				thees.AgregarValor('Clima',c.clima,tmp)
				thees.AgregarValor('Floración',c.floracion,tmp)
				thees.MostrarTCnt(el)
			}}).get({'id':id});
			AjaxAccion(0)
		}else this.MostrarTCnt(el)
	},
	CargarElemento:function(id){
		var myHTMLRequest = new Request.HTML({
			url:'/json-plantas.galeria/',
			evalScripts: true,
			onSuccess:function(html) {
				AjaxAccion(1);
				document.getElement('.hojan').set('text', '');
				document.getElement('.hojan').adopt(html);
				Galeria.mostrar();
			}}
		).get({'id':id});
		AjaxAccion(0)
	}
});

window.addEvent('domready', function() {	
	ajax=$('ajax');
	AjaxAccion();
	GenerarSubmenu();
	var tira=$('tiraImg');
	if(tira!=undefined){
		var pos=0;
		$('dvfi').addEvent('click',function(){
			var myEffect = new Fx.Morph(tira, {duration: 'short', transition: Fx.Transitions.Sine.easeOut});
			if(pos>0){
				pos--;
				nleft=(424*(-pos));	
				myEffect.start({'left': nleft+'px'});
			}
			FPosicion()
		});	
		$('dvfd').addEvent('click',function(){
			var myEffect = new Fx.Morph(tira, {duration: 'short', transition: Fx.Transitions.Sine.easeOut});
			if(pos<6){
				pos++;
				nleft=(424*(-pos));	
				myEffect.start({'left': nleft+'px'});
			}
			FPosicion()
		});	
		var FPosicion=function(){
			var i=$('dvfi'),d=$('dvfd')
			if(pos>3) d.addClass('nonev'); else d.removeClass('nonev');
			if(pos==0) i.addClass('nonev'); else i.removeClass('nonev');
		}
		$('tooltip').setStyle('display','block');
		$('tooltip').setStyle('opacity',0);
	}
	PintarTitulos()
});

var avisoLegal=function(){
	var opciones="toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=no, width=490, height=315, top=200, left=200";
	window.open("/recursos/otros/aviso.html","",opciones);
}


window.addEvent('scroll', function() {
	new Fx.Morph(ajax, {duration: 'short', transition: Fx.Transitions.Sine.easeOut})
	.start({'top':4+window.getScroll()['y']+'px'});
});