//
// MundiCapas
// (c) 1999-2002
//     Fernando Sengáriz Martínez de Goñi
//     Dpto. Diseño
//     Mundivía, S.A.
//     [01-02-1999]
//

function MundiBrowser () {
	this.ver    = navigator.appVersion;
	this.agent  = navigator.userAgent;
	this.DOM    = document.getElementById?1:0;
	this.opera5 = this.agent.indexOf("Opera 5")>-1;
	this.ie5    = (this.ver.indexOf("MSIE 5")>-1 && this.DOM && !this.opera5)?1:0; 
	this.ie6    = (this.ver.indexOf("MSIE 6")>-1 && this.DOM && !this.opera5)?1:0;
	this.ie4    = (document.all && !this.DOM && !this.opera5)?1:0;
	this.ie     = this.ie4 || this.ie5 || this.ie6;
	this.mac    = this.agent.indexOf("Mac")>-1;
	this.ns6    = (this.DOM && parseInt(this.ver) >= 5) ?1:0; 
	this.ns4    = (document.layers && !this.DOM)?1:0;
	this.ns     = this.ns4 || this.ns6;
	this.bw     = (this.ie6 || this.ie5 || this.ie4 || this.ns4 || this.ns6 || this.opera5);
	return this;
}

function MundiCapa (capa) {
	this.browser = new MundiBrowser();
	if (this.browser.ns4) {
		this.estilo = document.layers[capa];
		this.x = this.estilo.left;
		this.y = this.estilo.top;
	}
	if (this.browser.ie4) {
		this.estilo = document.all[capa].style;
		this.x = this.estilo.pixelLeft;
		this.y = this.estilo.pixelTop;
	}
	if (this.browser.DOM) {
		this.estilo = document.getElementById(capa).style;
		this.x = this.estilo.pixelLeft;
		this.y = this.estilo.pixelTop;
	}
	if (this.browser.ns4) this.elemento = this.estilo;
	if (this.browser.ie4) this.elemento = document.all[capa];
	if (this.browser.DOM) this.elemento = document.getElementById(capa);
	this.obj = capa + "MundiCapa";
	eval(this.obj + "=this");
	if (this.browser.ns4) this.texto = this.estilo.document;
	if (this.browser.ie4) this.texto = document.all[capa].elemento;
	if (this.browser.DOM) this.texto = document.getElementById(capa).document;
	this.capa = capa;
	if (this.browser.ns4) {
		this.w = this.estilo.clip.width;
		this.h = this.estilo.clip.height;
	}
	if (this.browser.ie) {
		this.w = this.estilo.pixelWidth;
		this.h = this.elemento.scrollHeight;
	}
	if (this.browser.DOM && !this.browser.ie) {
		this.w = this.estilo.pixelWidth;
		this.h = this.elemento.pixelHeight;
	}
	this.z = this.estilo.zIndex;
	this.scrollh = this.h;
	this.subeA = MundiCapaSubeA;
	this.ajustaA = MundiCapaAjustaA;
	this.mueveA = MundiCapaColocaEn;
	this.desplazaEn = MundiCapaDesplazaEn;
	this.muestra = MundiCapaMuestra;
	this.oculta = MundiCapaOculta;
	this.escribe = MundiCapaEscribe;
	this.setArrastraEn = MundiCapaArrastraEnInit;
	this.arrastraEn = MundiCapaArrastraEn;
	this.arrastra = MundiCapaArrastra;
	this.arrastraSuaveEn = MundiCapaArrastraSuaveEn;
	this.arrastraSuave = MundiCapaArrastraSuave;
	this.creaContenido = MundiCapaCreaContenido;
	this.cargaPagina = MundiCapaCargaPagina;
	this.clipA = MundiCapaClipA;
	this.agrandaEn = MundiCapaAgrandaEn;
	this.agranda = MundiCapaAgranda;
	this.creaCapaScroll = MundiCapaCreaCapaScroll;
	this.scrollArriba = MundiCapaScrollArriba;
	this.scrollAbajo = MundiCapaScrollAbajo;
	this.scrollNo = MundiCapaScrollNo;
}
function MundiCapaCreaContenido () {
	this.contenido = this.capa + "MundiContenido";
	if (this.browser.ns4) {
		//this.escribe("<layer id='"+this.capa+"Contenido"+"' WIDTH=175 HEIGHT=175 MARGINWIDTH=0 MARGINHEIGHT=0 SCROLLING='NO' FRAMEBORDER='NO'></layer>");
		//eval(this.contenido+"=document."+this.capa+".document."+this.capa+"Contenido");
		eval(this.contenido+"=this.elemento");
	}
	if (this.browser.ie4) {
		this.escribe("<iframe id='"+this.capa+"Contenido"+"' WIDTH="+this.w+" HEIGHT="+this.h+" MARGINWIDTH=0 MARGINHEIGHT=0 SCROLLING='NO' FRAMEBORDER='NO'></IFRAME>");
		eval(this.contenido+"=document.all."+this.capa+"Contenido");
	}
	if (this.browser.DOM) {
		this.escribe("<iframe id='"+this.capa+"Contenido"+"' WIDTH="+this.w+" HEIGHT="+this.h+" MARGINWIDTH=0 MARGINHEIGHT=0 SCROLLING='NO' FRAMEBORDER='NO'></IFRAME>");
		eval(this.contenido+"=document.all."+this.capa+"Contenido");
	}
}
function MundiCapaCargaPagina (url) {
	eval(this.contenido).src=url;
}
function MundiCapaEscribe (texto_html) {
	if (this.browser.ns4) {
		this.texto.open();
		this.texto.write(texto_html);
		this.texto.close();
	}
	if (this.browser.ie4) {
		this.texto.innerHTML = texto_html;
	}
	if (this.browser.DOM) {
		this.texto.innerHTML = texto_html;
	}
}
function MundiCapaSubeA (z) {
	if (z!=null) {
		this.z = z;
		this.estilo.zIndex = z;
	}
}
function MundiCapaAjustaA (new_w, new_h) {
	if (new_w!=null) {
		this.w = new_w;
		if (this.browser.ns4) this.estilo.clip.width = this.w;
		if (this.browser.ie || this.browser.DOM) this.estilo.pixelWidth = this.w;
	}
	if (new_h!=null) {
		this.h = new_h;
		if (this.browser.ns4) this.estilo.clip.height = this.h;
		if (this.browser.ie || this.browser.DOM) this.estilo.pixelHeight = this.h;
	}
}
function MundiCapaColocaEn(x,y) {
	if (x!=null) {
		this.x = x;
		this.estilo.left = this.x;
	}
	if (y!=null) {
		this.y = y;
		this.estilo.top = this.y;
	}
}
function MundiCapaDesplazaEn(x,y) {
	if (x==null) x = 0;
	if (y==null) y = 0;
	this.mueveA(this.x+x,this.y+y);
}
function MundiCapaMuestra() {
	this.estilo.visibility = (this.browser.ns4)? "show" : "visible";
}
function MundiCapaOculta() {
	this.estilo.visibility = (this.browser.ns4)? "hide" : "hidden";
}
function MundiCapaSubeA (z) {
	if (z!=null) {
		this.z = z;
		this.estilo.zIndex = z;
	}
}
function MundiCapaArrastraEnInit () {
	this.arrastraEn = MundiCapaArrastraEn;
	this.arrastra = MundiCapaArrastra;
}
function MundiCapaArrastraEn ( xdist, ydist, inc, velocidad, f_funcion) {
	if ( this.arrastrando ) return
	if (!inc) inc = 20;
	if (!velocidad) velocidad = 10;
	var xf = this.x + xdist;
	var yf = this.y + ydist;
	var num = Math.sqrt(Math.pow(xdist,2) + Math.pow(ydist,2))/inc;
	if (num==0) return;
	var dx = xdist/num;
	var dy = ydist/num;
	if (!f_funcion) f_funcion = null;
	this.arrastrando = true;
	this.arrastra( dx, dy, xf, yf, num, 1, velocidad, f_funcion );
}
function MundiCapaArrastra ( dx, dy, xf, yf, num, i, velocidad, f_funcion ) {
	if (i++ < num) {
		this.desplazaEn(dx,dy);
		setTimeout(this.obj+".arrastra("+dx+","+dy+","+xf+","+yf+","+num+","+i+","+velocidad+",\""+f_funcion+"\")",velocidad);
	}
	else {
		this.mueveA( xf, yf );
		this.arrastrando = false;
		eval( f_funcion );
	}
}
function MundiCapaArrastraSuaveEn ( xdist, ydist, inc, velocidad, f_funcion) {
	if ( this.arrastrando ) return
	if (!inc) inc = 20;
	if (!velocidad) velocidad = 10;
	xdir = (xdist>0) ? 1 : -1;
	ydir = (ydist>0) ? 1 : -1;
	var xf = this.x + xdist;
	var yf = this.y + ydist;
	var num = Math.sqrt(Math.pow(xdist,2) + Math.pow(ydist,2))/inc;
	if (num==0) return;
	var dx = xdist/num;
	var dy = ydist/num;
	sum_xinc = sum(dx*xdir);
	sum_yinc = sum(dy*ydir);	
	if (!f_funcion) f_funcion = null;
	this.arrastrando = true;
	this.arrastraSuave( dx, dy, xf, yf, num, 1, velocidad, f_funcion, xdist, xdir, ydist, ydir, sum_xinc, sum_yinc );
}
function MundiCapaArrastraSuave ( dx, dy, xf, yf, num, i, velocidad, f_funcion, xdist, xdir, ydist, ydir, sum_xinc, sum_yinc ) {
	if (dx!=0 && xdist*xdir<=sum_xinc) { dx-=1*xdir;}
	if (dy!=0 && ydist*ydir<=sum_yinc) { dy-=1*ydir;}
	xdist-=dx;
	ydist-=dy;
	if ((xdir<0 && this.x+dx<xf) || (xdir>0 && this.x+dx>xf))  dx = 0;
	if ((ydir<0 && this.y+dy<yf) || (ydir>0 && this.y+dy>yf))  dy = 0;
	if (dx*xdir>0 || dy*ydir>0) {
		this.desplazaEn(dx,dy);
		setTimeout(this.obj+".arrastraSuave("+dx+","+dy+","+xf+","+yf+","+num+","+i+","+velocidad+",\""+f_funcion+"\","+xdist+","+xdir+","+ydist+","+ydir+","+sum_xinc+","+sum_yinc+")",velocidad);
	}
	else {
		this.mueveA( xf, yf );
		this.arrastrando = false;
		eval( f_funcion );
	}
}
function sum (numero) {
 var i,resul;
 resul = numero;
 for (i=numero-1; i>1; i--) resul+=i;
 return(resul);
}
function MundiCapaClipInit(cT,cR,cB,cL) {
	if (this.browser.ie4) {
		if (arguments.length==4) this.clipTo(cT,cR,cB,cL)
		else this.clipTo(0,this.w,this.h,0)
	}
	if (this.browser.DOM) {
		if (arguments.length==4) this.clipTo(cT,cR,cB,cL)
		else this.clipTo(0,this.w,this.h,0)
	}
}
function MundiCapaClipA(t,r,b,l) {
	if (this.browser.ns4) {
		if (t!=null) this.estilo.clip.top = t;
		if (r!=null) this.estilo.clip.right = r;
		if (b!=null) this.estilo.clip.bottom = b;
		if (l!=null) this.estilo.clip.left = l;
	}
	if (this.browser.ie4) this.estilo.clip = "rect("+t+"px "+r+"px "+b+"px "+l+"px)";
	if (this.browser.DOM) this.estilo.clip = "rect("+t+"px "+r+"px "+b+"px "+l+"px)";
	this.clipA_t = t;
	this.clipA_r = r;
	this.clipA_b = b;
	this.clipA_l = l;
}
function MundiCapaAgrandaEn (ydist, inc, velocidad, f_funcion) {
	if ( this.agrandando ) return
	if (!inc) inc = 10;
	if (!velocidad) velocidad = 10;
	ainc = (inc>0) ? inc : inc*(-1);
	var veces = ydist/ainc;
	this.agrandando = true;
	this.agranda(veces, inc, velocidad, f_funcion);
}
function MundiCapaAgranda (veces, inc, velocidad, f_funcion) {
 	if (veces>0) {
		this.clipA( 0,this.clipA_r,this.clipA_b+inc,0 );
 		veces--;
		setTimeout(this.obj+".agranda("+veces+","+inc+","+velocidad+",\""+f_funcion+"\")",velocidad);
 	} else {
 		this.agrandando = false;
		eval( f_funcion );
	}
}
function MundiCapaCreaCapaScroll (capa) {
	this.capaScroll = new MundiCapa(capa);
	this.loop = true;
}
function MundiCapaScrollArriba (inc, velocidad) {
	if (!velocidad) velocidad = 10;
	if (this.capaScroll.y>(-1)*this.capaScroll.h+this.h) {
		this.capaScroll.desplazaEn(0,-inc);
		if (this.loop) setTimeout(this.obj+".scrollArriba("+inc+","+velocidad+")",velocidad);
		else this.loop = true;
	}
	else this.loop = false;
}
function MundiCapaScrollAbajo (inc, velocidad) {
	if (!velocidad) velocidad = 10;
	if (this.capaScroll.y<0) {
		this.capaScroll.desplazaEn(0,inc);
		if (this.loop) {
			//alert(this.obj+".ScrollAbajo("+inc+","+velocidad+")");
			setTimeout(this.obj+".scrollAbajo("+inc+","+velocidad+")",velocidad);
		}
		else this.loop = true;
	}
	else this.loop = false;
}
function MundiCapaScrollNo (inc, velocidad) {
	this.loop = !this.loop;
}

