/************************************************
* Library                                       *
* Copyright (C) 1998-2005 Salvador Arnal Julián *
*************************************************/


// Para reconocer los navegadores

var A=document.all		// IE
var L=document.layers		// NS
var I=document.getElementById 	// MZ
var px=(I?'px':'');		// En Mozilla hay que añadir 'px' al final de las unidades en las hojas de estilo

// MEJORAS DEL OBJETO DATE

Date.monthDays=new Array(31,28,31,30,31,30,31,31,30,31,30,31);
Date.monthNames=new Array('January','February','March','April','May','June','July','August','September','October','November','December');
Date.monthShortNames=new Array('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec')
Date.dayNames=new Array('Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday')
Date.dayShortNames=new Array('Mon','Tue','Wed','Thu','Fri','Sat','Sun')

Date.S=1000;
Date.M=60*Date.S;
Date.H=60*Date.M;
Date.D=24*Date.H;
Date.W=7*Date.D;


// Recupera el numero de dias que tiene un mes, si no se le pasa el mes o el año coje los del propio objeto.
Date.prototype.getMonthDays=function (m,y){
	if(m==null){
		m=this.getMonth();
	}
	if(y==null){
		y=this.getFullYear();
	}
	return m==1&&0==y%4&&!(0==y%100&&0!=y%400)?29:Date.monthDays[m];
}

// Recupera el numero de dias que tiene un mes, si no se le pasa el mes o el año coje los de hoy.
Date.getMonthDays=function (m,y){
	return new Date().getMonthDays(m,y);
}

// Recupera el nombre corto de un mes, si no se le pasa, coje el del objeto.
Date.prototype.monthShortName=function(m){
	if(m==null){
		m=this.getMonth();
	}
	return Date.monthShortNames[m];
}

// Recupera el nombre de un mes, si no se le pasa, coje el del objeto.
Date.prototype.monthName=function(m){
	if(m==null){
		m=this.getMonth();
	}
	return Date.monthNames[m];
}

// Recupera el nombre corto de un dia, si no se le pasa, coje el del objeto.
Date.prototype.dayShortName=function(d){
	if(d==null){
		d=this.getDay();
	}
	return Date.dayShortNames[d];
}

// Recupera el nombre de un dia, si no se le pasa, coje el del objeto.
Date.prototype.dayName=function(d){
	if(d==null){
		d=this.getDay();
	}
	return Date.dayNames[d];
}

// Añade un dia a la fecha.
Date.prototype.addDay=function(){
	if(this.getDate()==this.getMonthDays()){
		if(this.getMonth()==11){
			this.setFullYear(this.getFullYear()+1);
			this.setMonth(0);
			this.setDate(1);
		}else{
			this.setDate(1);
			this.setMonth(this.getMonth()+1);
		}
	}else{
		this.setDate(this.getDate()+1);
	}
}

// Añade n dias a la fecha.
Date.prototype.addDays=function(n){
	n+=this.getDate();
	this.setDate(1);
	for(;n>this.getMonthDays();n-=this.getMonthDays()){
		this.addMonth();
	}
	this.setDate(n);	
}


// Añade un mes a la fecha.
Date.prototype.addMonth=function(){
	if(this.getMonth()==11){
		this.setFullYear(this.getFullYear()+1);
		this.setMonth(0);
	}else{
		this.setMonth(this.getMonth()+1);
	}
}

// Resta un dia a la fecha.
Date.prototype.subDay=function(){
	if(this.getDate()==1){
		if(this.getMonth()==0){
			this.setFullYear(this.getFullYear()-1);
			this.setMonth(11);
			this.setDate(31);
		}else{
			this.setMonth(this.getMonth()-1);
			this.setDate(this.getMonthDays());
		}
	}else{
		this.setDate(this.getDate()-1);
	}
}

Date.restaFechas=function(f1,f2){
	var i;
	var n;
	if(f1<f2){
		fa=f1;
		fb=f2;
		n=1;
	}else{
		fa=f2;
		fb=f1;
		n=-1;
	}
	for(i=0;fa<fb;fa.addDay()){
		i++;
	}
	return i*n;	
	
	
}

// Convierte una fecha a formato 'dd/mm/yyyy'.
Date.prototype.toStr=function(){
	var aux='';
	aux+=('0'+this.getDate()).right(2);
	aux+='/';
	aux+=('0'+(this.getMonth()+1)).right(2);
	aux+='/';
	aux+=this.getFullYear();
	return aux;
}

Date.prototype.toId=function(){
	var aux='';
	aux+=this.getFullYear();
	aux+=('0'+(this.getMonth()+1)).right(2);
	aux+=('0'+this.getDate()).right(2);
	return aux;
}


// Convierte una fecha a formato 'nnn, MMM d'.
Date.prototype.toMsg=function(){
	var aux='';
	var n=this.getDay();
	aux+=Date.dayShortNames[(n==0)?6:n-1];
	aux+=', ';
	aux+=Date.monthShortNames[this.getMonth()];
	aux+=' ';
	aux+=this.getDate();
	return aux;
}


// MEJORAS DEL NAVEGADOR

// Para que el MZ tenga la funcion insertAdjacentElement() the IE.
if(typeof HTMLElement!="undefined"&&!HTMLElement.prototype.insertAdjacentElement){
	HTMLElement.prototype.insertAdjacentElement = function(where,parsedNode){
		switch (where){
			case 'beforeBegin':
				this.parentNode.insertBefore(parsedNode,this);
				break;
			case 'afterBegin':
				this.insertBefore(parsedNode,this.firstChild);
				break;
			case 'beforeEnd':
				this.appendChild(parsedNode);
				break;
			case 'afterEnd':
				if(this.nextSibling){
					this.parentNode.insertBefore(parsedNode,this.nextSibling);
				}else{
					this.parentNode.appendChild(parsedNode);
				}
				break;
		}
	}
	HTMLElement.prototype.insertAdjacentHTML=function(where,htmlStr){
		var r=this.ownerDocument.createRange();
		r.setStartBefore(this);
		var parsedHTML=r.createContextualFragment(htmlStr);
		this.insertAdjacentElement(where,parsedHTML);
	}
	HTMLElement.prototype.insertAdjacentText=function(where,txtStr){
		var parsedText = document.createTextNode(txtStr);
		this.insertAdjacentElement(where,parsedText);
	}
}

// Obtiene un objeto con el id que se le pasa.
function getLyr(N){
        return (I?document.getElementById(N):(A?document.all[N]:(L?document.layers[N]:null)));
}

// Obtiene la hoja de estilos del objeto que se le pasa.
function getCss(N){
	return (L?N:N.style);
}

// Obtiene las coordenadas absolutas del objeto que se le pasa.
function getPos(e){
	if(e){
		var id = /^div$/i.test(e.tagName);
		var l=(id&&e.scrollLeft)?e.scrollLeft:0;
		var t=(id&&e.scrollTop)?e.scrollTop:0;
		var p={x:e.offsetLeft-l,y:e.offsetTop-t};
		var aux=getPos(e.offsetParent);
		p.x+=aux.x;
		p.y+=aux.y;
		return p;
	}else{
		return {x:0,y:0};
	}
}

// Elimina un evento del documento.
function delEvent(e,f){
	if(this.detachEvent){
		eval('document.detachEvent("on'+e+'",'+f+');');
	}else if(this.removeEventListener){
		eval('window.removeEventListener("'+e+'",'+f+',true);');
	}else{
		eval('window["on'+e+'"]=null;');
	}
}

// Añade un evento al documento.
function addEvent(e,f){
	if(document.attachEvent){
		eval('document.attachEvent("on'+e+'",'+f+');');
	}else if(window.addEventListener){
		eval('window.addEventListener("'+e+'",'+f+',true);');
	}else{
		eval('window["on'+e+'"]='+f+';');
	}

}

// Para almacenar una Cookie.
function setCookie(name, value, expires, path, domain, secure){
	document.cookie=name+"="+escape(value)+"; expires="+((expires)?expires.toGMTString():"12/12/2012")+((path)?"; path="+path:"")+((domain)?"; domain="+domain:"")+((secure)?"; secure":"");
}

// Para recuperar una Cookie.
function getCookie(name) {
	var dc=document.cookie;
	name+="=";
	var begin=dc.indexOf("; "+name);
	if(begin==-1){
		begin=dc.indexOf(name);
		if(begin!=0){
			return null;
		}
	}else{
		begin+=2;
	}
	var end=dc.indexOf(";",begin);
	if(end==-1){
		end=dc.length;
	}
	return unescape(dc.substring(begin+name.length,end));
}

// Recupera el tamaño del documento.

function getDocumentSize(w,h){
	return {w:typeof(window.innerWidth)=='number'?window.innerWidth:document.documentElement&&(document.documentElement.clientWidth||document.documentElement.clientHeight)?document.documentElement.clientWidth:document.body&&(document.body.clientWidth||document.body.clientHeight)?document.body.clientWidth:w,h:typeof(window.innerWidth)=='number'?window.innerHeight:document.documentElement&&(document.documentElement.clientWidth||document.documentElement.clientHeight)?document.documentElement.clientHeight:document.body&&(document.body.clientWidth||document.body.clientHeight)?document.body.clientHeight:h};
}

// Transforma un numero para ser utilizado como media en css.
function toPx(n,unit){
	if(unit=="px"){
		n=parseInt(n);
	}
	if(unit==null){
		unit=px;
	}
	return n+(n==0?"":unit);
}



// MEJORAS DEL OBJETO STRING

// Obtiene los ultimos n caracteres del string.
String.prototype.right=function(n){
	return this.substring(this.length-n);
};


// OBJETO Lyr
function Lyr(N){
	this.ID=N;
	this.lyr=getLyr(N);
	this.css=getCss(this.lyr);
	
	this.readSize();
	this.readPos();
	
	this.clip={x1:0,y1:0,x2:this.size.w,y2:this.size.h}


	this.oldPos={x:this.pos.x,y:this.pos.y};
	this.oldSize={w:this.size.w,h:this.size.h};
	this.oldClip=this.css.clip;

	this.retardo=20;
	
	this.pt=null;
	this.ct=null;
	this.st=null;
	
	this.obj=N+"Lyr";
	eval(this.obj+"=this");
	return this;
}

// Mueve el objeto a su posicion original.
Lyr.prototype.clearPos=function(){
	this.pos.x=this.oldPos.x;	
	this.pos.y=this.oldPos.y;	
	this.updatePos();
}

// PRIVATE. Actualiza la posicion de un objeto.
Lyr.prototype.updatePos=function(){
	this.css.left=toPx(this.pos.x);
	this.css.top=toPx(this.pos.y);
	this.pos2={x:this.pos.x+this.size.w,y:this.pos.y+this.size.h};
}

Lyr.prototype.readPos=function(){
	this.pos=getPos(this.lyr);
	this.pos2={x:this.pos.x+this.size.w,y:this.pos.y+this.size.h};
}


// Situa el objeto en una posicion.
Lyr.prototype.moveTo=function(xPos,yPos){
	this.pos.x=xPos;
	this.pos.y=yPos;
	this.updatePos();
}

// Desplaza el objeto.
Lyr.prototype.moveBy=function(xInc,yInc){
	this.pos.x+=xInc;
	this.pos.y+=yInc;
	this.updatePos();
}

// PRIVATE. Mueve el objeto.
Lyr.prototype.slide=function(iter,xInc,yInc,step,xEnd,yEnd,fix){
	if(iter<step){
		this.moveBy(xInc,yInc);
		iter++;
		if(fix){
			xInc=(xEnd-this.pos.x)/(step-iter);
			yInc=(yEnd-this.pos.y)/(step-iter);
		}
		this.pt=setTimeout(this.obj+".slide("+iter+","+xInc+","+yInc+","+step+","+xEnd+","+yEnd+","+fix+")",this.retardo);
	}else{
		if(fix){
			this.moveTo(xEnd,yEnd)
		}
	}
}

// Mueve el objeto en n pasos.
Lyr.prototype.slideBy=function(xInc,yInc,step,fix){
	this.slide(0,xInc/step,yInc/step,step,this.pos.x+xInc,this.pos.y+yInc,fix==null?true:fix);
}

// Mueve el objeto a una posicion en n pasos.
Lyr.prototype.slideTo=function(xPos,yPos,step,fix){
	this.slide(0,(xPos-this.pos.x)/step,(yPos-this.pos.y)/step,step,xPos,yPos,fix==null?true:fix);
}

// PRIVATE. Actualiza el clip del objeto.
Lyr.prototype.updateClip=function(){
	if(this.clip.x1>this.clip.x2){
		this.clip.x1=(this.clip.x1+this.clip.x2)/2;
		this.clip.x2=this.clip.x1;
	}
	if(this.clip.y1>this.clip.y2){
		this.clip.y1=(this.clip.y1+this.clip.y2)/2;
		this.clip.y2=this.clip.y1;
	}
	if(L){
		this.css.clip.left=parseInt(this.clip.x1);
		this.css.clip.top=parseInt(this.clip.y1);
		this.css.clip.right=parseInt(this.clip.x2);
		this.css.clip.bottom=parseInt(this.clip.y2);
	}else{
		this.css.clip="rect("+toPx(this.clip.y1,"px")+" "+toPx(this.clip.x2,"px")+" "+toPx(this.clip.y2,"px")+" "+toPx(this.clip.x1,"px")+")";
	}
}

// Elimina el clip del objeto.
Lyr.prototype.clearClip=function(){
	if(!A){
		this.css.clip="";
	}
	this.css.clip=this.oldClip;
//	this.clip={x1:0,y1:0,x2:this.size.w,y2:this.size.h};
//	if(A){
//		this.updateClip();
//	}
}

// Cambia el clip del objeto.
Lyr.prototype.clipTo=function(x1Clip,y1Clip,x2Clip,y2Clip){
	this.clip.x1=x1Clip;
	this.clip.y1=y1Clip;
	this.clip.x2=x2Clip;
	this.clip.y2=y2Clip;
	this.updateClip();
}


// Cambia el clip del objeto.
Lyr.prototype.clipBy=function(x1Inc,y1Inc,x2Inc,y2Inc){
	this.clip.x1+=x1Inc;
	this.clip.y1+=y1Inc;
	this.clip.x2+=x2Inc;
	this.clip.y2+=y2Inc;
	this.updateClip();
}

// PRIVATE. Clipea el objeto.
Lyr.prototype.slideClip=function(iter,x1Inc,y1Inc,x2Inc,y2Inc,step,x1End,y1End,x2End,y2End,fix){
	if(iter<step){
		this.clipBy(x1Inc,y1Inc,x2Inc,y2Inc);
		iter++;
		if(fix){
			x1Inc=(x1End-this.clip.x1)/(step-iter);
			y1Inc=(y1End-this.clip.y1)/(step-iter);
			x2Inc=(x2End-this.clip.x2)/(step-iter);
			y2Inc=(y2End-this.clip.y2)/(step-iter);
		}
		this.ct=setTimeout(this.obj+".slideClip("+iter+","+x1Inc+","+y1Inc+","+x2Inc+","+y2Inc+","+step+","+x1End+","+y1End+","+x2End+","+y2End+","+fix+")",this.retardo);
	}else{
		if(fix){
			this.clipTo(x1End,y1End,x2End,y2End);
		}
	}
}

// Mueve el objeto en n pasos.
Lyr.prototype.slideClipBy=function(x1Inc,y1Inc,x2Inc,y2Inc,step,fix){
	this.slideClip(0,x1Inc/step,y1Inc/step,x2Inc/step,y2Inc/step,step,this.clip.x1+x1Inc,this.clip.y1+y1Inc,this.clip.x2+x2Inc,this.clip.y2+y2Inc,fix==null?true:fix);
}

// Mueve el objeto a una posicion en n pasos.
Lyr.prototype.slideClipTo=function(x1Clip,y1Clip,x2Clip,y2Clip,step,fix){
	this.slideClip(0,(x1Clip-this.clip.x1)/step,(y1Clip-this.clip.y1)/step,(x2Clip-this.clip.x2)/step,(y2Clip-this.clip.y2)/step,step,x1Clip,y1Clip,x2Clip,y2Clip,fix==null?true:fix);
}

// Restaura el tamaño original del objeto.
Lyr.prototype.clearSize=function(){
	this.size.w=this.oldSize.w;	
	this.size.h=this.oldSize.h;	
	this.updateSize();
}

Lyr.prototype.readSize=function(){
	this.size={w:this.lyr.offsetWidth,h:this.lyr.offsetHeight};
}

// PRIVATE. Actualiza el tamaño del objeto.
Lyr.prototype.updateSize=function(){
	if(this.css.clip==""){
		this.clip={x1:0,y1:0,x2:this.size.w,y2:this.size.h};
	}
	if(this.size.w<0){
		this.size.w=0;	
	}
	if(this.size.h<0){
		this.size.h=0;	
	}

	this.css.width=toPx(this.size.w);
	this.css.height=toPx(this.size.h);
}

// Cambia el tamaño del objeto.
Lyr.prototype.resizeTo=function(wSize,hSize){
	this.size.w=wSize;
	this.size.h=hSize;
	this.updateSize();
}

// Incrementa el tamaño del objeto.
Lyr.prototype.resizeBy=function(wInc,hInc){
	this.size.w+=wInc;
	this.size.h+=hInc;
	this.updateSize();
}

// PRIVATE. Resiza el objeto.
Lyr.prototype.slideSize=function(iter,wInc,hInc,step,wEnd,hEnd,fix){
	if(iter<step){
		this.resizeBy(wInc,hInc);
		iter++;
		if(fix){
			wInc=(wEnd-this.size.w)/(step-iter);
			hInc=(hEnd-this.size.h)/(step-iter);
		}
		this.st=setTimeout(this.obj+".slideSize("+iter+","+wInc+","+hInc+","+step+","+wEnd+","+hEnd+","+fix+")",this.retardo);
	}else{
		if(fix){
			this.resizeTo(wEnd,hEnd)
		}
	}
}

// Incrementa el tamaño en n pasos.
Lyr.prototype.slideSizeBy=function(wInc,hInc,step,fix){
	this.slideSize(0,wInc/step,hInc/step,step,this.size.w+wInc,this.size.h+hInc,fix==null?true:fix);
}

// Cambia el tamaño en n pasos.
Lyr.prototype.slideSizeTo=function(wSize,hSize,step,fix){
	this.slideSize(0,(wSize-this.size.w)/step,(hSize-this.size.h)/step,step,wSize,hSize,fix==null?true:fix);
}

// Establece/Recupera el estado de la visibilidad.
Lyr.prototype.visible=function(v){
	if(v==null){
		return (this.css.visibility!=(L?"hide":"hidden")||this.css.visibility==null);
	}else{
		v?this.show():this.hide();
	}
}

// Oculta el control.
Lyr.prototype.hide=function(){
	this.css.visibility=L?"hide":"hidden";
}

// Muestra el control.
Lyr.prototype.show=function(){
	this.css.visibility=L?"show":"visible";
}

// Cambia el estado de la visibilidad.
Lyr.prototype.swapVisibility=function(){
	this.visible()?this.hide():this.show();
}

// Empieza a arrastrar con el raton el objeto.
Lyr.prototype.startDrag=function(){
	Dragger.mouse.x=0;
	Dragger.mouse.y=0;
	Dragger.current=this;
	addEvent("selectstart",'Dragger.dumb');
	addEvent("mousemove",'Dragger.drag');
}

// Deja de arrastar con el raton el objeto.
Lyr.prototype.stopDrag=function(){
	if(this==Dragger.current){
		delEvent("mousemove",'Dragger.drag');
		delEvent("selectstart",'Dragger.dumb');
		Dragger.current=null;
		Dragger.mouse.x=0;
		Dragger.mouse.y=0;
	}
}

function Dragger(){
	return this;
}

Dragger.current=null;
Dragger.mouse={x:0,y:0};

// Arrasta el raton.
Dragger.drag=function(e){
	var x=A?event.clientX + document.body.scrollLeft:e.pageX;
	var y=A?event.clientY + document.body.scrollTop:e.pageY;
	if(Dragger.mouse.x==0&&Dragger.mouse.y==0){
		Dragger.mouse.x=x-Dragger.current.pos.x;
		Dragger.mouse.y=y-Dragger.current.pos.y;
	}
	Dragger.current.moveTo(x-Dragger.mouse.x,y-Dragger.mouse.y);
}

Dragger.dumb=function(e){
	return false;
}