/*
Copyright 2011 Erick Martínez Sámano
This file is part of PHP Quick Calendar E.
*/
var smallReq = new Class({
	Implements: [Events],
	initialize: function(container,target){
		if (typeof(target)=='undefined') {target=container;}
		this.xhrqe=new Request.HTML({
		onRequest: function(){
				target.fade('hide');
				container.addClass("QCEloader");
			},
		onComplete: function(){
				container.removeClass.delay(20,container,"QCEloader");
			},
		onFailure: function(xhr){if (xhr.status==404){
							target.innerHTML="<span class='QCEErr'>Documento no encontrado</span>"+target.innerHTML;
						} else {
							target.innerHTML="<span class='QCEErr'>Hubo un error al procesar su petición</span>"+target.innerHTML;
						}target.fade('show');
			},
		onSuccess: function(tree,elements,html,js){
				target.set('html', html);
				this.js=js;
				target.tween('opacity',1);
				if (Browser.Engine.trident){container.setStyle('zoom',1);}
			},
		evalScripts:false,
		chain:'ignore'
		});
		target.set('tween',{duration:500,onStart:this.ejecutar.bind(this)});
	},
	send: function(url){
		this.xhrqe.options.url=url;
		this.xhrqe.send();
	},
	ejecutar: function(){
		$exec(this.xhrqe.js);
	}
});
var smallShow = new Class({
	Implements: [Options, Events],
	options:{
		espacio:50,
		duracion:500,
		fps:50
	},
	initialize: function(prev,next,close,padre,target,options){
		this.setOptions(options);
		this.padre=padre;
		this.padreTween=new Fx.Tween(padre,{property:'opacity', duration:2000,transition:'quint:out',link:'cancel',onComplete: (function(){
			if (!this.padre.getStyle('opacity').toInt()){this.padre.setStyle('display','none');}
							}).bind(this)
				});
		this.padre.addEvent('mouseleave',this.ocultar.bind(this));
		this.padre.addEvent('mouseenter',this.mostrar.bind(this));
		close.addEvent('click',(function(){
			this.padre.setStyles({'display':'none','opacity':0});
		}).bind(this));
		close.setStyle('display','block');
		this.scan(prev,next,target);
	},
	scan: function(prev,next,target){
		this.children=target.getChildren('div');
		this.last=this.children.length-1;
		if (this.last<2){return;}
			prev.addEvent('click',this.back.bind(this));
			next.addEvent('click',this.forw.bind(this));
			this.tween= new Fx.Tween(target,{property:'left', duration:this.options.duracion,transition:'quint:out',fps:this.options.fps});
			this.prev=prev;
			this.next=next;
			this.left=this.children[0].getSize().x;
			this.onefx=false;
			this.target=target;
			this.visibleChild=0;
	},
	forw: function(){
		if (this.onefx){return;}
		this.onefx=true;
		this.children[this.visibleChild+2].setStyle('display','block');
		this.tween.start(0,-(this.left + this.options.espacio)).chain((function(){
		this.children[this.visibleChild].setStyle('display','none');
		this.children[(this.visibleChild+1)].setStyle('display','none');
		this.tween.set(0);
		this.onefx=false;
		this.visibleChild+=2;
		this.prev.setStyle('visibility','visible');
		if (this.last==this.visibleChild) {this.next.setStyle('visibility','hidden');}
		}).bind(this));
		
	},
	back: function(){
		if (this.onefx){return;}
		this.onefx=true;
		this.children[(this.visibleChild-1)].setStyle('display','block');
		this.tween.set(-this.options.espacio);
		this.children[(this.visibleChild-2)].setStyle('display','block');
		this.tween.set(-(this.options.espacio+this.left));
		this.tween.start(0).chain((function(){
		this.next.setStyle('visibility','visible');
		this.onefx=false;
		this.children[this.visibleChild].setStyle('display','none');
		this.visibleChild-=2;
		if (this.visibleChild==0) {this.prev.setStyle('visibility','hidden');}
		}).bind(this));		
	},
	ocultar: function(){
		this.timer=this.padreTween.start.delay(10000,this.padreTween,0);
	},
	mostrar: function(){
		$clear(this.timer);
		this.padreTween.start(1);
	}
});

window.addEvent('domready',function(){
	smallQCEreq= new smallReq(document.id('QCEsmall'),document.id('QCEsmalltrgt'));
	scanQCEHead();
	smallQCEreqB= new smallReq(document.id('smallQCEtrgt'),document.id('smallSmooth'));
	scanQCEBody();
});

function peticion(a){
		var ref=a.retrieve('Ahref');
		smallQCEreq.send(ref);
}
function peticion2(a,target){
		var ref=a.retrieve('Ahref');
 		if (typeof(smallQCETween)!="undefined")
		{
		$clear(smallQCETween.timer);
		smallQCETween.padreTween.cancel();
		}
		smallQCEreqB.send(ref);
}
function scanQCEHead(){
	var Arrhead=$$('#QCEsmall thead a');
	for (i=4;i--;){
		var a = Arrhead[i];
		var ref=a.href;
		var target= document.id('smallQCEtrgt');
		var target1= document.id('smallSmooth');
		(ref.indexOf('?')+1)? c='&': c='?';
		ref+=c+'view=small&format=raw';
		a.store('Ahref',ref);
		a.addEvent('click',function(event){
			event.stop();target.setStyle('display','none');target1.setStyle('opacity',0);peticion(this);});
	}
}
function scanQCEBody(){
	var ArrBody=$$('.small tr>td>a');
	var n = ArrBody.length;
	if (n==0) {return;}
	for (i=n;i--;){
		var a=ArrBody[i];
		var ref=a.href;
		var target=document.id('smallQCEtrgt');
		(ref.indexOf('?')+1)? c='&': c='?';
		ref+=c+'view=small&format=raw&p=desc';
		a.store('Ahref',ref);
		a.addEvent('click',function(event){
			event.stop();
			target.setStyle('display','block');
			if (target.getStyle('opacity')==0){target.setStyle('opacity',1);}
			peticion2(this);
		});
	}
}


