// Dynamic.js (c)2000-2004 Nina Cording

var layersInMotion = 0;

function BrowserCheck() {
	var b = navigator.appName;
	var ua = navigator.userAgent;
	if (b=="Netscape") this.b = "ns";
	else if (b=="Microsoft Internet Explorer") this.b = "ie";
	else this.b = b;
	if (ua.indexOf("Opera")>=0) this.opera=true; else this.opera = false;
	this.version = navigator.appVersion;
	this.v = parseInt(this.version);
	this.ns = (this.b=="ns" && this.v>=4);
	this.ns4 = (this.b=="ns" && this.v==4);
	this.ns5 = (this.b=="ns" && this.v==5);
	this.ns6 = (ua.indexOf('Netscape6')>=0);

	this.ie = (this.b=="ie" && this.v>=4);
	this.ie4 = (this.version.indexOf('MSIE 4')>0);
	this.ie5 = (this.version.indexOf('MSIE 5')>0);

	if (document.layers) this.mode = 0;
		else if (document.all) this.mode = 1;
		else if (document.getElementById) this.mode = 2;
		else this.mode = -1;
}
bc = new BrowserCheck();

function Dynamic(id) {
	this.frame = self;
	this.id = id;
	this.obj = id + "Dynamic";
	if (bc.mode == 0) {	// layers
			this.css = document.layers[id];
			this.elm = this.event = this.css;
			this.doc = this.css.document;
			this.w = this.css.clip.width;
			this.h = this.css.clip.height;
			this.x = parseInt(this.css.left);
			this.y = parseInt(this.css.top);
	} else if (bc.mode ==2) { // getElementById
			this.elm = document.getElementById(id);
			this.css = this.elm.style;
			this.doc = document;
			this.w = parseInt(this.elm.offsetWidth);
			this.h = parseInt(this.elm.offsetHeight);
			this.x = parseInt(this.elm.offsetLeft);
			this.y = parseInt(this.elm.offsetTop);
			/*
			var key;
			var str = "";
			for (key in this.elm) {
				//str += "elm."+key+" = typeOf: "+typeof(this.elm[key])+"\n";
				if (typeof(this.elm[key])=="number")
				  str += "elm."+key+" = "+this.elm[key]+"\n";
			}
			alert (str);
			*/
			//alert ("w,h('"+this.id+"')="+this.w+","+this.h);
		} else if (bc.mode == 1) {
		this.elm = this.event = this.frame.document.all[id];
		this.css = this.frame.document.all[id].style;
		this.doc = document;
		this.x = this.elm.offsetLeft;
		this.y = this.elm.offsetTop;
		//this.w = (bc.ie4)? this.css.pixelWidth : this.elm.offsetWidth;
		//this.h = (bc.ie4)? this.css.pixelHeight : this.elm.offsetHeight;
		this.w = this.elm.offsetWidth;
		this.h = this.elm.offsetHeight;
	}
	this.to = null;
	eval(this.obj + "=this");
}
function DynamicGetWidth() {
	if (bc.mode == 0) return this.css.clip.width;
	  else if (bc.mode == 2) {
		return this.elm.offsetWidth;
	} else if (bc.mode == 1) {
		return (bc.ie4)? this.css.pixelWidth : this.elm.offsetWidth;
	} else return 0;
}
function DynamicGetHeight() {
	if (bc.mode == 0) return this.css.clip.height;
		else if (bc.mode == 2) {
		return this.elm.offsetHeight;
	} else if (bc.mode == 1) {
		return (bc.ie4)? this.css.pixelHeight : this.elm.offsetHeight;
	} else return 0;
}
function DynamicMove(x,y) {
	if (x!=null) {
		this.x = x;
		if (bc.mode!=1) this.css.left = this.x;
		      else this.css.pixelLeft = this.x;
	}
	if (y!=null) {
		this.y = y;
		if (bc.mode!=1) this.css.top = this.y;
		      else this.css.pixelTop = this.y;
	}
}
function DynamicMoveBy(x,y) {
	this.move(this.x+x,this.y+y);
}
function DynamicShow() {
	this.css.visibility = (bc.mode==0) ? "show" : "visible";
}
function DynamicHide() {
	this.css.visibility = (bc.mode==0) ? "hide" : "hidden";
}
function DynamicClip(x0,y0,x1,y1) {
	if (bc.mode == 0) {
		this.css.clip.top = y0;
		this.css.clip.right = x1;
		this.css.clip.bottom = y1;
		this.css.clip.left = x0;
	} else
		this.css.clip =
		"rect("+
		y0+"px "+
		x1+"px "+
		y1+"px "+
		x0+"px)";
}
function DynamicMotion(endx,endy,inc,speed) {
	if (this.inMotion) return;
	layersInMotion++;
	if (!inc) inc = 10;
	if (!speed) speed = 20;
	var distx = endx-this.x;
	var disty = endy-this.y;
	var num = Math.sqrt(Math.pow(distx,2) + Math.pow(disty,2))/inc;
	if (num==0) {layersInMotion--;return;}
	var dx = distx/num;
	var dy = disty/num;
	this.inMotion = true;
	this.doMotion(dx,dy,endx,endy,num,1,speed);
}
function DynamicDoMotion(dx,dy,endx,endy,num,i,speed) {
	if (!this.inMotion) return;
	if (i++ < num) {
		this.moveBy(dx,dy);
		if (this.inMotion) setTimeout(this.obj+".doMotion("+dx+","+dy+","+endx+","+endy+","+num+","+i+","+speed+")",speed);
	}
	else {
		this.inMotion = false;
		layersInMotion--;
		this.move(endx,endy);
	}
}
function DynamicSlowMotion(endx,endy,factor,speed) {
	if (this.inMotion) clearTimeout(this.to);
	this.inMotion = true;
	this.doSlowMotion(endx,endy,factor,speed);
}
function DynamicDoSlowMotion(endx,endy,factor,speed) {
	if ((Math.round(this.x)==endx) && (Math.round(this.y)==endy)) {
		this.inMotion=false;
		this.move(endx,endy);
		return;
	}
	this.x += (endx-this.x)*factor;
	this.y += (endy-this.y)*factor;
	this.move(this.x,this.y);
	if (!speed) speed=20;
	this.to = setTimeout(this.obj+'.doSlowMotion('+endx+','+endy+','+factor+','+speed+');',speed);
}
function DynamicReWrite(html) {
	if (bc.mode==0) {
		this.doc.open()
		this.doc.write(html)
		this.doc.close()
	}
	else if (bc.mode==1) {
		this.event.innerHTML = html
	} else if (bc.mode==2) {
		this.elm.innerHTML = html;
	}
}
function DynamicClippedMove(x,y,x0,y0,x1,y1) {
    var clipX0,clipY0,clipX1,clipY1;
    if ((x0!=null) && (x<x0)) {
	    clipX0 = x0-x;
	  } else clipX0=0;
	if ((y0!=null) && (y<y0)) {
	    clipY0 = y0-y;
	  } else clipY0=0;
	if ((x1!=null) && (this.w+x>x1)) {
	    clipX1 = x1-x;
	  } else clipX1 = this.w;
	if ((y1!=null) && (this.h+y>y1)) {
	    clipY1 = y1-y;
	  } else clipY1 = this.h;
	if ((clipY1<=clipY0) || (clipX1<=clipX0)) {this.hide();return;} else this.show();
	this.clip(clipX0,clipY0,clipX1,clipY1);
	this.move(x,y);
}
function DynamicSetBackground(src) {
	if (bc.mode == 0)
		this.css.background.src = src;
	else
		this.css.backgroundImage = 'url('+src+')';
}
function DynamicChangeImage(name,src) {
    if (bc.mode == 0)
		this.doc.images[name].src = src;
	else
		document.images[name].src = src;
}
Dynamic.prototype.setBackground = DynamicSetBackground;
Dynamic.prototype.reWrite = DynamicReWrite
Dynamic.prototype.move= DynamicMove;
Dynamic.prototype.moveBy = DynamicMoveBy;
Dynamic.prototype.show = DynamicShow;
Dynamic.prototype.hide = DynamicHide;
Dynamic.prototype.motion = DynamicMotion;
Dynamic.prototype.doMotion = DynamicDoMotion;
Dynamic.prototype.clip = DynamicClip;
Dynamic.prototype.clippedMove = DynamicClippedMove;
Dynamic.prototype.changeImage = DynamicChangeImage;
Dynamic.prototype.getWidth = DynamicGetWidth;
Dynamic.prototype.getHeight = DynamicGetHeight;
Dynamic.prototype.slowMotion = DynamicSlowMotion;
Dynamic.prototype.doSlowMotion = DynamicDoSlowMotion;
