/*
Copyright 2011 Erick Martínez Sámano
This file is part of PHP Quick Calendar E. GNU/GPL3
*/
var transAlerts= new Class({
	Implements:Options,
	options:{
		en:{
			DOC_NOT_FOUND:"Document not found",
			T_W_E_P_R:"There was an error processing the request",
			P_Try_A_L:"Please try again later"
		},
		es:{
			DOC_NOT_FOUND:"Documento no encontrado",
			T_W_E_P_R:"Hubo un error al procesar su petición",
			P_Try_A_L:"Por favor, intente de nuevo más tarde"
		},
		nl:{
			DOC_NOT_FOUND:"Document niet gevonden",
			T_W_E_P_R:"Er is een fout opgetreden bij het verwerken van de aanvraag",
			P_Try_A_L:"Probeert u het later nog eens"
		}
	},
	getMsgs: function(){
		ltag=document.getElement('html').lang.substr(0,2);
		(typeof(this.options[ltag])=="undefined")?ltag="en":'';
		return this.options[ltag];
	}
});
var smallReq = new Class({
	Implements: [Events,Options],
	initialize: function(container,target){
		if (typeof(target)=='undefined') {target=container;}
		target.set('tween',{duration:500,onStart:this.ejecutar.bind(this,target)});
		if (Browser.ie){container.setStyle('zoom',1);}
		this.xhrqe=new Request.HTML({
		onRequest: function(){
				(this.uerr)? this.uerr.setStyle('display','none'):'';
				(this.err404)? this.err404.setStyle('display','none'):'';
				(this.seErr)? this.seErr.setStyle('display','none'):'';
				target.fade('hide');
				container.addClass("QCEloader");
			},
		onComplete: function(){
				container.removeClass.delay(20,container,"QCEloader");
				target.fade('show');
			},
		onFailure: function(xhr){if (xhr.status==404){
						if (!this.err404){
							this.err404=new Element('p.QCEErr#'+container.id+'9',{html:this.alerts.DOC_NOT_FOUND}); container.grab(this.err404,'top');} (this.uerr)? this.uerr.setStyle('display','none'):''; this.err404.setStyle('display','block');
						} else {
						if (!this.uerr){
							this.uerr=new Element('p.QCEErr#'+container.id+'8',{html:this.alerts.T_W_E_P_R});container.grab(this.uerr,'top');} (this.err404)? this.err404.setStyle('display','none'):'';this.uerr.setStyle('display','block');
						}
						container.fireEvent('QCEAjErr',target);
			},
		onSuccess: function(tree,elements,html,js){
				if (this.getHeader('pqce')=='t'){
					target.set('html', html);
					this.js=js;
					target.tween('opacity',1);
				}else{  if(!this.seErr){
					this.seErr=new Element('p.QCEErr#'+container.id+'7',{html:this.alerts.P_Try_A_L});
					container.grab(this.seErr,'top');}
					container.fireEvent('QCEAjErr',target);
					this.seErr.setStyle('display','block');
				}
			},
		evalScripts:false,
		chain:'ignore'
		});
	var al= new transAlerts();
	this.xhrqe.alerts=al.getMsgs();
	al=null;
	},
	send: function(url,more){
		this.xhrqe.options.url=url;
		this.xhrqe.send(more);
	},
	ejecutar: function(target){
//		Browser.exec(this.xhrqe.js);
		target.fireEvent('QCEAjSucc');
	}
});
var smallShow = new Class({
	Implements: [Options, Events],
	options:{
		espacio:50,
		duracion:300,
		fps:24
	},
	initialize: function(prev,next,close,padre,options){
		this.ie= (Browser.ie)? 'quad:out':'quint:out';
		this.setOptions(options);
		this.padre=padre;
		this.prev=prev;
		this.next=next;
		this.padreTween=new Fx.Tween(padre,{property:'opacity', duration:2000,transition:this.ie,link:'cancel',onComplete: (function(){
			if (!this.padre.getStyle('opacity').toInt()){this.padre.setStyle('display','none');this.prev.setStyle('visibility','hidden');}
							}).bind(this)
				});
		this.padre.addEvent('mouseleave',this.ocultar.bind(this));
		this.padre.addEvent('mouseenter',this.mostrar.bind(this));
		close.addEvent('click',function(){
			padre.setStyles({'display':'none','opacity':0});
			prev.setStyle('visibility','hidden');
			next.setStyle('visibility','hidden');
		});
		prev.addEvent('click',this.back.bind(this));
		next.addEvent('click',this.forw.bind(this));

	},
	scan: function(target){
		this.children=target.getChildren('div');
		this.last=this.children.length-1;
		if (this.last<2){return;}
			this.next.setStyle('visibility','visible');
			this.tween= new Fx.Tween(target,{property:'left', duration:this.options.duracion,transition:this.ie,fps:this.options.fps});
			this.left=this.children[0].getSize().x;
			this.onefx=false;
			this.target=target;
			this.visibleChild=0;
	},
	forw: function(){
		if (this.last<2){return;}
		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.prev.setStyle('visibility','visible');
		this.visibleChild+=2;
		this.onefx=false;
		if (this.last==this.visibleChild) {this.next.setStyle('visibility','hidden');}
		}).bind(this));
		
	},
	back: function(){
		if (this.last<2){return;}
		if (this.onefx){return;}
		this.onefx=true;
		this.children[(this.visibleChild-1)].setStyle('display','block');
		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.children[this.visibleChild].setStyle('display','none');
		this.visibleChild-=2;
		this.onefx=false;
		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(){
		clearTimeout(this.timer);
		this.padreTween.start(1);
	}
});

window.addEvent('domready',function(){
	var el=document.id("QCEsmalltrgt"),target= document.id('smallQCEtrgt'),target1= document.id('smallSmooth');
	smallQCEreq= new smallReq(document.id('QCEsmall'),el);
	smallQCETween= new smallShow(document.id("smallQCEa"),document.id("smallQCEs"),document.id("smallQCEc"),target,{duracion:1500,fps:40});
	el.addEvent('click:relay(.Apqcejx)',function(e){
		e.stop();
		target.setStyle('display','none');target1.setStyle('opacity',0);
		pqceHideArr();
		smallQCEreq.send(this.href,'view=small&format=raw');
	});
	smallQCEreqB= new smallReq(target,target1);
	el.addEvent('click:relay(.pQCEsmall)',function(e){
		e.stop();
		pqceHideArr();
		target.setStyles({'display':'block','visibility':'visible','opacity':1});
		clearTimeout(smallQCETween.timer);
		smallQCETween.padreTween.cancel();
		smallQCEreqB.send(this.href,'view=small&format=raw&p=desc');
	});
	target.addEvent('QCEAjErr',function(el){
		el.setStyle('visibility','hidden');
		pqceHideArr();
	});
	target1.addEvent('QCEAjSucc',function(){
		smallQCETween.scan(document.id("smallQCEconMainFix"));
	});
});
function pqceHideArr(){
	document.id("smallQCEa").setStyle('visibility','hidden');
	document.id("smallQCEs").setStyle('visibility','hidden');
}

