	var tid;
	var lens;

	function chk(c,inc)
	{
		return (c == '+') ? inc : inc*-1;
	}

	function is()
	{
		return (Math.floor(Math.random() * 2) == 1);
	}	

	function rand(i)
	{
		return Math.floor(Math.random() * i);
	}

	function getMouseXY(e)
	{
		if (!e) var e = window.event;
		if (e.pageX)
			return { x: e.pageX, y: e.pageY };
		else if (e.clientX)
			return { x: e.clientX, y: e.clientY };
		return { x: 0, y: 0 };
	}

	function wander()
	{	
		var lens = document.getElementById('lens');
		if (lens.mouse == null)
		{
			with (lens)
			{
				pos = { x: pos.x += chk(course.x, inc.x), y: pos.y += chk(course.y, inc.y) };
				if (pos.x < bounds.l) {
					course.x = "+";
                    inc.x = Math.floor(Math.random() * 4);
                    inc.y = 3-inc.x;
                    if ((inc.y == 0) && (pos.y > 40)) {
                      inc.y = 1;
                      }
                    }
				else if (pos.x + dim.w > bounds.r) {
					course.x = "-";
                    inc.x = Math.floor(Math.random() * 4);
                    inc.y = 3-inc.x;
                    if ((inc.y == 0) && (pos.y > 40)) {
                      inc.y = 1;
                      }
                    }
				if (pos.y < bounds.t){
					course.y = "+";
                    inc.x = Math.floor(Math.random() * 4);
                    inc.y = 3-inc.x;
                    if ((inc.y == 0) && (pos.y > 40)) {
                      inc.y = 1;
                      }
                    }
				else if (pos.y + dim.h  > bounds.b) {
					course.y = "-";			
                    inc.x = Math.floor(Math.random() * 4);
                    inc.y = 3-inc.x;
                    if ((inc.y == 0) && (pos.y > 40)) {
                      inc.y = 1;
                      }
                    }
			}
			lens.position();
			tid = setTimeout("wander()", 90);
		}
	}
	
	function position()
	{
		with (this)
		{
			if (pos.x < bounds.l)
				pos.x = bounds.l;
			else if (pos.x + dim.w > bounds.r)
				pos.x = bounds.r - dim.w;
			if (pos.y < bounds.t)
				pos.y = bounds.t;
			else if (pos.y + dim.h > bounds.b)
				pos.y = bounds.b - dim.h;
			for (c in clips)
			{
				var t = clips[c].pos.y + pos.y;
				var b = t + clips[c].dim.h;
				var l = clips[c].pos.x + pos.x;
				var r = l + clips[c].dim.w;
				clips[c].style.clip = 'rect(' + t + 'px, ' + r + 'px, ' + b + 'px, ' + l + 'px)';
			}	
			magnifier.style.top = magnifier.pos.y + pos.y + 'px';
			magnifier.style.left = magnifier.pos.x + pos.x + 'px';
		}
	}
	
	function addclip(id, pos, dim)
	{
		var img = document.getElementById(id);
		img.pos = pos;
		img.dim = dim;
		return img;
	}
	
	function initimage(id, lens)
	{
		var img = document.getElementById(id);
		img.lens = lens;
		img.onmouseover = function(e) { this.lens.start(getMouseXY(e)); }		
		img.onmousemove = function(e) { this.lens.move(getMouseXY(e)); }	
		img.onmouseout = function(e) {	this.lens.stop(); }	
	}
	
	function init()
	{
		lens = document.getElementById('lens');
		var base = document.getElementById('base');
		lens.position = position;
		lens.start = function(pos)
		{
			window.clearTimeout(tid);
			this.mouse = pos;
			//this.style.cursor = "hand";
		}
		lens.move = function(pos)
		{
			if (this.mouse)
			{
				this.pos.x += pos.x - this.mouse.x;
				this.pos.y += pos.y - this.mouse.y;
				this.mouse = pos;
				this.position();		
			}
		}
		lens.stop = function(e)
		{	
			if (this.mouse)
			{
				this.mouse = null;
				wander();
			}
		}

		lens.pos = { x: rand(base.offsetWidth), y: rand(base.offsetHeight) };
		lens.dim = { w: 60, h: 50 };
		lens.bounds = { t: 0, r: base.offsetWidth + 15, b: base.offsetHeight, l: 0 };
		lens.course = { x: (is()) ? '-' : '+', y: (is()) ? '-' : '+' };
lens.inc = { x: 1 , y: 1 }
		lens.clips = new Array();
		lens.clips[0] = addclip('clip1', { x: 12, y: 3 }, { w: 20, h: 4 });
		lens.clips[1] = addclip('clip2', { x: 8, y: 7 }, { w: 29, h: 4 });
		lens.clips[2] = addclip('clip3', { x: 4, y: 11 }, { w: 37, h: 4 });
		lens.clips[3] = addclip('clip4', { x: 2, y: 15 }, { w: 42, h: 4 });
		lens.clips[4] = addclip('clip5', { x: 0, y: 19 }, { w: 44, h: 4 });
		lens.clips[5] = addclip('clip6', { x: 2, y: 23 }, { w: 42, h: 4 });
		lens.clips[6] = addclip('clip7', { x: 4, y: 27 }, { w: 37, h: 4 });
		lens.clips[7] = addclip('clip8', { x: 6, y: 31 }, { w: 33, h: 4 });
		lens.clips[8] = addclip('clip9', { x: 8, y: 35 }, { w: 28, h: 4 });
		lens.clips[9] = addclip('clip10', { x: 11, y: 39 }, { w: 20, h: 3 });

		lens.magnifier = document.getElementById('magnifier');
		lens.magnifier.pos = { x: 0, y: 0 }

		initimage('glass', lens);
		initimage('handle1', lens);
		initimage('handle2', lens);			
		wander();
        externalLinks();
	}
window.onload = init;
