var Ajx={
	Recibido : 0,
	Enviado : 0,
	Bt : undefined,
	Contar : function( mas ){
		if(mas == undefined)
			this.Recibido++;
		else
			this.Enviado++;
		if(this.Recibido >= this.Enviado) 
			this.Bt.setStyle( 'display', 'none' ); 
		else 
			this.Bt.setStyle( 'display', 'block' );
	}
}
var embed;
function GenerarPagEmbed()
{
	$$('div.paginat a').addEvent('click',function(e){
		e.stop();
		CambioEmbed( this.href.split('#')[1] );
	});
}
function Comentar(id,t){
  var j=$('ccoment'),cmt=j.getElement('textarea').value;
  if(cmt.length<2) return false;
  j.getElement('a').setStyle('display','none');
  j.getElement('textarea').disabled='disabled';
  var myHTMLRequest = new Request.HTML({
    url: Base + 'socios/comentar/' + id + '/' + t,
    evalScripts: true,
    onSuccess:function(html) {
      Ajx.Contar(1);
      j.getElement('textarea').value='Su comentario fue enviado correctamente. A la brevedad será activado.'
    }}
  ).post({'cmt':cmt});
  Ajx.Contar();
  return false;
}

function CambioEmbed( ee )
{
	$$('div.paginat').getElement('a.ea'+embed).removeClass('sel');
	$('embed'+embed).setStyle( 'display', 'none' ); 
	$$('div.paginat').getElement('a.ea'+ee).addClass('sel');
	$('embed'+ee).setStyle( 'display', 'block' ); 
	embed = ee
}
var lvl3 = {

	obj : undefined,
	hoja : undefined,
	fondo : undefined,
	minh : undefined,
	
	mostrando :false,
	
	proximo : '',
	
	init:function( dir ){
		Ajx.Contar(1);
		this.proximo = '';
		this.obj = $('dvgal');
		if( this.obj == undefined )
		{
			this.obj = new Element('div', {
				'id': 'dvgal'
			});
			this.hoja = new Element('div', {
				'class': 'hoja'
			});
			this.fondo = new Element('div', {
				'class': 'fondo'
			});
			this.minh = new Element('div', {
				'class': 'minh'
			});
			document.getElement('body').adopt(this.obj);
			this.obj.adopt(this.hoja);
			this.obj.adopt(this.fondo);
			this.obj.adopt(this.minh);
		}else{
			this.hoja = this.obj.getElement('div.hoja'); 
			this.fondo = this.obj.getElement('div.fondo');
			this.minh = this.obj.getElement('div.minh');
		}
		wAuto = this.hoja.getStyle('width');
		if( this.obj != undefined && this.mostrando )
		{
			this.ocultar();
			this.proximo = dir
			return;
		}
		
		var thees = this;
		var fn = function(a,b,c) 
		{
			if (c != undefined && Browser.Engine.webkit )
			{
				thees.hoja.innerHTML = c;
			}
			Ajx.Contar();
			thees.hoja.getElement('div.close').addEvent('click',function(e){
				e.stop();
				thees.ocultar();
			});
			thees.fondo.addEvent('click',function(e){
				e.stop();
				thees.ocultar();
			});
			thees.hoja.getElements('div.contenido a').each(function(el){
        el.set('target','_blank')
      });
			thees.hoja.getElements('div.next').addEvent('click', thees.evtcl );
			thees.hoja.getElements('div.prev').addEvent('click', thees.evtcl );
			thees.mostrar();
		};
		if( dir )
		{
			if(!Browser.Engine.webkit) 
				new Request.HTML( {evalScripts: true, update:thees.hoja, url : dir , onComplete : fn } ).get();
			else
				new Request.HTML( {evalScripts: false, url : dir , onComplete : fn } ).get();
		}
		else
			fn();		
	},
	
	mostrar: function(){
		embed = 0;
		GenerarPagEmbed();
		this.mostrando=true;
		this.obj.morph = new Fx.Morph(this.hoja,{duration:500});	
		this.hoja.setStyle('opacity',0);
		this.minh.setStyle('height', window.getScrollHeight() + 100 );
		this.fondo.setStyle('opacity',0.2);
		this.obj.setStyle('display','block');
		this.obj.morph.start({
			opacity: [0,1],
			width: ['700px',wAuto]
		});
	},
	
	ocultar: function(){
		var thes = this;
		this.mostrando=false;
		this.obj.morph.cancel();
		this.obj.morph.start({width:'700px', opacity:0}).chain(function(){
			thes.obj.setStyle('display','none');
			thes.hoja.setStyle('width',wAuto);
			thes.dir = '';
			if( thes.proximo ){
				thes.init(thes.proximo)
			}
		});
	},
	
	evtcl: function(e){
		e.stop();
		var a = this.getElement('a').href.split('/');
		/*switch( a[4] )
		{
			case 'setas':
				if( a[5] != 'leer' ){
					dir = this.getElement('a').href;
					document.body.scrollTo(0);
				}
			break;
			case 'footer':
				dir = this.getElement('a').href;
				document.body.scrollTo(0);
			break;
			default:
				sc = a[a.length-3], id = a[a.length-1];
				dir = Base + sc + "/leer/" + id;
		}*/	
		dir = this.getElement('a').href;
		switch( a[4] )
		{
			case 'footer':
				document.body.scrollTo(0);
			break
		}
		if( dir != lvl3.dir )
			lvl3.init( dir );
	}
	
}

window.addEvent('domready', function() {
	Ajx.Bt = $('ajax');
	document.getElements('div.lkgal').addEvent('click', lvl3.evtcl );
	if( $('dvgal') != undefined )
		lvl3.init()
});
