/* This notice must be untouched at all times.

animimg.js    v. 1.0
The latest version is available at http://www.walterzorn.de/

Copyright (c) 2002 Walter Zorn. All rights reserved.
Created 19. 9. 2002 by Walter Zorn <walter@kreuzotter.de>
Last modified: 21. 9. 2002

Image animation script,
moving a foreground image in front of a static background image,
and clipping off areas of the foreground which otherwise
would overflow the borders of the background.
Optional: accelerating or decelerating cycle speed.

This software is provided "as is", without a warranty of any kind.
*/


var ACCELERATE = 'aC',
	DECELERATE = 'dA',
	NOACCEL = 'nA',
	DELAY = 0;


function CATCH_ERR()
{
	return true;
}
window.onerror = CATCH_ERR;


function AIClass(t)
{
	(t = this).d = document;
	t.n = navigator.userAgent.toLowerCase();
	t.ie = (t.n.indexOf('msie')!=-1 && t.d.all && (t.d.compatMode || t.d.body) && !window.opera)? true : false;
	t.n4 = t.d.layers? true : false;
	t.n6 = (t.n.indexOf('gecko')!=-1 || t.n.indexOf('konqueror')!=-1)? true : false;
	t.op = window.opera? true : false;
	t.htm = this.n4? '<div style="position:absolute;"><\/div>\n' : '';
	t.img = new Array();
	
	t.Int = new Function('arg',
		'return (arg==null)? 0 : (typeof arg!=\'number\')? parseInt(arg) : arg;'
	);
	
	t.write = new Function('ai.d.write(ai.htm);'+
		'if (ai.op) ai.init();'
	);
	
	t.init = new Function('re',
		'var z = 0, i; while (z<ai.img.length)'+
		'{'+
			'i = ai.img[z++];'+
			'if (re==1) i.getImg();'+
			'else i.div = i.getDiv();'+
			'i.spacer();'+
			'i.setCont();'+
			'i.setDef();'+
			'if (re!=1) i.run();'+
		'}'
	);
	
	t.opResiz = new Function(
		'var z = 0, i; while (z<ai.img.length)'+
		'{'+
			'var bgx = (i = ai.img[z++]).bgx, bgy = i.bgy;'+
			'i.getImg();'+
			'if (i.bgx!=bgx || i.bgy!=bgy) location.reload();'+
		'}'
	);
}
var ai = new AIClass();


function AIImg(i, fg, iw, ih, xa, ya, xz, yz, n, t)
{
	if (!i) return;
	(t = this).w = iw;
	t.h = ih;
	t.xa = xa;
	t.ya = ya;
	t.xz = xz;
	t.yz = yz;
	t.n = n;
	t.id = i+"aid";
	t.htm = '';
	
	t.getDiv = new Function('id',
		'id = id || this.id;'+
		'return (ai.n4 && ai.d[id])? ai.d[id]'+
			': (ai.ie && (i = ai.d.all[id]))? i.style'+
			': (ai.d.getElementById && ai.d.getElementById(id))? ai.d.getElementById(id).style'+
			': null;'
	);
	
	t.run = new Function(
		'var t = this;'+
		't.clt = t.bgy-Math.round(t.y);'+
		't.clr = t.bgw+t.bgx-Math.round(t.x);'+
		't.clb = t.bgh+t.bgy-Math.round(t.y);'+
		't.cll = t.bgx-Math.round(t.x);'+
		'if (ai.n4)'+
		'{'+
			'if (t.done==0)'+
				't.div.clip.top = t.div.clip.right = t.div.clip.bottom = t.div.clip.left = 0;'+
			't.div.clip.top = t.clt;'+
			't.div.clip.right = t.clr;'+
			't.div.clip.bottom = t.clb;'+
			't.div.clip.left = t.cll;'+
		'}'+
		'else if (!t.cont)'+
		'{'+
			't.div.clip = "rect("'+
			'+t.clt+"px, "'+
			'+t.clr+"px, "'+
			'+t.clb+"px, "'+
			'+t.cll+"px)"'+
			';'+
		'}'+
		't.div.left = Math.round(t.x)+t.px;'+
		't.div.top = Math.round(t.y)+t.px;'+
		't.x += (t.xz-t.xa)/t.n;'+
		't.y += (t.yz-t.ya)/t.n;'+
		'var dt = (t.done==t.n)? t.delay'+
			': (t.acc==NOACCEL)? 0*t.done'+
			': (t.acc==ACCELERATE)? Math.round(1000*(1-Math.pow(t.done/t.n,1/10)))'+
			': Math.round(800*Math.pow(t.done/t.n,8));'+
		'window.setTimeout("ai.img["+t.index+"].run();", 80+dt);'+
		'if (t.done==t.n)'+
		'{'+
			'if (ai.op) ai.opResiz();'+
			't.setDef();'+
		'}'+
		'else t.done++;'
	);

	t.getImg = new Function('d','t',
		'(t = this).bgx = 0;'+
		't.bgy = 0;'+
		'var i = t.id.substring(0, t.id.length-3);'+
		'd = d || window.document;'+
		'if (d.layers)'+
		'{'+
			'if (d.images[i])'+
			'{'+
				't.bgx += d.images[i].x;'+
				't.bgy += d.images[i].y;'+
				'return d.images[i];'+
			'}'+
			'for (var z = 0; z<d.layers.length; z++)'+
			'{'+
				'var y = t.getImg(d.layers[z].document);'+
				'if(y)'+
				'{'+
					't.bgx += d.layers[z].left;'+
					't.bgy += d.layers[z].top;'+
					'return y;'+
				'}'+
			'}'+
			'return null;'+
		'}'+
		'else'+
		'{'+
			'var y = d.images[i] || null;'+
			'if (y && typeof y.offsetLeft!=\'undefined\' && typeof y.offsetParent!=\'undefined\')'+
			'{'+
				'var el = y;'+
				'while (el)'+
				'{'+
					't.bgx += ai.Int(el.offsetLeft);'+
					't.bgy += ai.Int(el.offsetTop);'+
					'el = el.offsetParent;'+
				'}'+
			'}'+
			'else y = null;'+
			'return y;'+
		'}'
	);

	t.setDef = new Function('t',
		'if (!(t = this).div) return;'+
		't.done = 0;'+
		't.x = t.xa+(t.cont? 0 : t.bgx);'+
		't.y = t.ya+(t.cont? 0 : t.bgy);'+
		'if (!t.cont) t.div.visibility = '+(ai.n4? '"show";' : '"visible";')
	);

	t.spacer = new Function('t',
		'if (!(t = this).div) return;'+
		't.getDiv(t.id+"spacer").left = (t.bgx+t.xa)+t.px;'+
		't.getDiv(t.id+"spacer").top = (t.bgy+t.ya)+t.px;'
	);

	t.setCont = new Function(
		'var t = this;'+
		't.cont = t.getDiv(t.id+"container");'+
		'if (!t.div || !t.cont) return;'+
		't.cont.left = t.bgx+t.px;'+
		't.cont.top = t.bgy+t.px;'
	);
	
	t.img = t.getImg();
	t.px = (ai.n4 || ai.op)? '' : 'px';
	if (!t.img) return;
	t.bgw = t.img.width;
	t.bgh = t.img.height;
	t.htm += '<div id="'+t.id+'spacer" style="position:absolute;'+
		'width:'+t.w+'px;height:'+t.h+'px;'+
		'"><\/div>\n'+

		((ai.n6 || ai.op)?
		('<div id="'+t.id+'container" style="position:absolute;'+
		'width:'+t.bgw+'px;height:'+t.bgh+'px;overflow:hidden;">\n')
		: '')+

		'<div id="'+t.id+'" '+
		'style="position:absolute;'+
		'visibility:'+(ai.n4? 'hide' : (ai.n6 || ai.op)? 'inherit' : 'hidden')+';">\n'+

		'<img src="'+fg+'" alt="" '+
		'width="'+t.w+'" '+
		'height="'+t.h+'">\n'+
		
		'<\/div>\n'+

		((ai.n6 || ai.op)? '<\/div>\n' : '');

}


function AIRESIZ(x)
{
	if (!ai) return;
	if (x==1)
	{
		if (ai.n4)
		{
			ai.d.iW = innerWidth;
			ai.d.iH = innerHeight;
			onresize = AIRESIZ;
		}
		else window.onresize = AIRESIZ;
	}
	else if (ai.n4 && (innerWidth!=ai.d.iW || innerHeight!=ai.d.iH))
		location.reload();
	else
		(ai.n.indexOf('msie 4')!=-1)? setTimeout('ai.init(1)', 50) : ai.init(1);
}
AIRESIZ(1);


function ANIMATE(id, bg, fg, iw, ih, xa, ya, xz, yz, n)
{
	var arg = ANIMATE.arguments;
	if (!(ai && ai.d[id] && (ai.ie || ai.n4 || ai.d.getElementById))) return;
	var i = new AIImg(id,fg,iw,ih,xa,ya,xz,yz,n);
	if (i.htm)
	{
		ai.img[i.index = ai.img.length] = i;
		ai.d[id].src = bg;
		ai.htm += i.htm;
		i.acc = NOACCEL;
		var z = 10; while (z<arg.length)
		{
			if (arg[z]==ACCELERATE) i.acc = ACCELERATE;
			if (arg[z++]==DECELERATE) i.acc = DECELERATE;
		}
		i.delay = DELAY;
		DELAY = 0;
	}
}


if (!ai.op) window.onload = ai.init;
