function BBox(item) {
	// Returns a Bounding-Box object with the current l,t,r,b of element relative to the page.
	this.b = (this.t = item.offsetTop) + item.offsetHeight;
	this.r = (this.l = item.offsetLeft) + item.offsetWidth;
	var d;
	var op = item;
	// Here's the hard part -- we have to CLIP the bounding box to fit within its DOM lineage...
	while (op = op.offsetParent) {
		// Shift the box Vertically... - op.scrollTop
		if (d = op.offsetTop) {
			this.t += d;
			this.b += d;
			if (this.t < op.offsetTop) this.t = op.offsetTop;
		}
		// Then shift it Horizontally... - op.scrollLeft
		if (d = op.offsetLeft) {
			this.l += d;
			this.r += d;
			if (this.l < op.offsetLeft) this.l = op.offsetLeft;
		}
		if (!op.offsetParent) break;
		// And Clip Height...
		d = op.offsetTop + op.offsetHeight;
		if (this.b > d) this.b = d;
		// And Clip Width
		d = op.offsetLeft + op.offsetWidth;
		if (this.r > d) this.r = d;
	}
	this.h = this.b - this.t;
	this.w = this.r - this.l;
}

BBox.prototype.contains = function(pt) {
	return ((pt.pageX >= this.l) && (pt.pageX <= this.r) && (pt.pageY >= this.t) && (pt.pageY <= this.b));
}

BBox.prototype.apply = function(object) {
	with (object) {
		style.left = this.l + 'px';
		style.width = this.w + 'px';
		style.top = this.t + 'px';
		style.height = this.h + 'px';
	}
}

var w, h;

var FlyOver = {
	element: null,
	tRect: null,
	to: null,
	sTime: null,
	max_w: 640,
	max_h: 480,
	tgt: null,
	fRect: null,
	sf: 0,
	
	abort: function() {
		if (FlyOver.to) window.clearTimeout(FlyOver.to);
		FlyOver.to = 0;
	},
	
	startAnimation: function() {
		if (self.innerHeight) {
			w = self.innerWidth;
			h = self.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight) {
			w = document.documentElement.clientWidth;
			h = document.documentElement.clientHeight;
		} else if (document.body) {
			w = document.body.clientWidth;
			h = document.body.clientHeight;
		}
		
		var t = new Date().getTime();
		with (FlyOver) {
			// Set the final destination...
			var cx = (tRect.l + tRect.r) / 2;
			var cy = (tRect.t + tRect.b) / 2;
			fRect = new BBox(document.body);
			
			fRect.l = cx - (max_w / 2);
			if (fRect.l < 5) fRect.l = 5;
			if (fRect.l + max_w > w - 20) fRect.l = w - max_w - 20;
			fRect.r = fRect.l + max_w;
			fRect.w = max_w;
			
			fRect.t = cy - (max_h / 2);
			if (fRect.t < document.body.scrollTop + 5) fRect.t = document.body.scrollTop + 5;
			if (fRect.t + max_h > document.body.scrollTop + h - 5)
				fRect.t = document.body.scrollTop + h - max_h - 5;
			fRect.b = fRect.t + max_h;
			fRect.h = max_h;
			
			sf = 0.0;
			
			if (t < sTime) {
				abort();
				to = window.setTimeout("FlyOver.startAnimation()", sTime - t);
			} else {
				element.style.zIndex = 100;
				if (tgt) tgt.onmouseout = null;
				animate();
			}
		}
	},
	
	animate: function() {
		with (FlyOver) {
			abort();
			sf += 0.04;
			if (sf < 1.0) {
				to = window.setTimeout('FlyOver.animate()', 20);
			} else sf = 1.0;

			var isf = 1 - sf;
			
			r.w = (sf * fRect.w) + (isf * tRect.w);
			r.h = (sf * fRect.h) + (isf * tRect.h);
			r.l = (sf * fRect.l) + (isf * tRect.l);
			r.t = (sf * fRect.t) + (isf * tRect.t);
			// Update the display
			r.apply(element);
		}
	},
	
	show: function(target, picURL, linkURL) {
		with (FlyOver) {
			abort();
			if (!element) {
				var e = FlyOver.element = document.createElement('img');
				e.style.position = 'absolute';
				//e.style.border = '3px solid #09f';
				e.onmouseout = FlyOver.hide;
				e.onload = FlyOver.startAnimation;
				document.body.appendChild(e);
				document.body.onmousemove = FlyOver.move;
			}
			if (element) {
				element.style.zIndex = -10;
				tRect = new BBox(target);
				r = new BBox(target);
				r.apply(element);
				sTime = new Date().getTime() + 300;
				if (element.src != picURL) {
					element.src = picURL;
				} else to = window.setTimeout("FlyOver.startAnimation()", 300);
				if (linkURL) {
					element.onclick = function() {window.location = linkURL};
					element.style.cursor = "pointer";
				}
				element.style.display = 'block';
			}
		}
	},
	
	move: function(event) {
		// IE won't populate the event, so we have to do it ourselves.
		if (!event) event = window.event;
		if (!(event.pageX || event.pageY)) {
			// If missing (IE again, and others), add in the page-relative coordinates
			event.pageX = event.clientX + document.body.scrollLeft;
			event.pageY = event.clientY + document.body.scrollTop;
		};
		if (!FlyOver.tRect || !(FlyOver.tRect.contains(event))) FlyOver.hide();
	},
	
	hide: function() {
		FlyOver.abort();
		if (FlyOver.element) {
			FlyOver.element.style.display = 'none';
		}
	}
}

function doFlyOver() {
	if (this.href.match(/\/ebay\//)) {
		FlyOver.show(this, this.href.replace(/\/ebay\//, '/full/'), '{{!EBAY HREF}}');
	} else FlyOver.show(this, this.href);
}

function RegisterGallery(event) {
	var Gallery = document.getElementById('gallery');
	if (Gallery) {
		var links = Gallery.getElementsByTagName('a');
		for(var i = 0; i < links.length; i++) links[i].onmouseover = doFlyOver;
	}
}

window.onload = RegisterGallery;