
if (typeof(vb) == 'undefined') {
	window.vb = {};
}

vb.lastMouse = { x: 0, y: 0 };
vb.currentOverlay = 0;

// thanks, quirksmode!
vb.windowDimensions = function() {
	var x,y;
	if (self.innerHeight) { 
		// all except Explorer
		x = self.innerWidth;
		y = self.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientHeight) {
		// Explorer 6 Strict Mode
		x = document.documentElement.clientWidth;
		y = document.documentElement.clientHeight;
	}
	else if (document.body) { 
		// other Explorers
		x = document.body.clientWidth;
		y = document.body.clientHeight;
	}
	return { x: x, y: y };
}

vb.scrollingOffset = function() {
	var x,y;
	if (self.pageYOffset) { // all except Explorer
		x = self.pageXOffset;
		y = self.pageYOffset;
	}
	else if (document.documentElement && document.documentElement.scrollTop) {
		// Explorer 6 Strict
		x = document.documentElement.scrollLeft;
		y = document.documentElement.scrollTop;
	}
	else if (document.body) { // all other Explorers
		x = document.body.scrollLeft;
		y = document.body.scrollTop;
	}
	return { x: x, y: y };
}

vb.showOverlay = function(link, overlay) {
	if (vb.currentOverlay && vb.currentOverlay != overlay) {
		vb.hideOverlay(vb.currentOverlay);
	}
	var jOverlay = jQuery(overlay);
	var offset = vb.scrollingOffset();
	var w = vb.windowDimensions();
	jOverlay.css('left', (w.x / 2) - 287 + offset.x);
	jOverlay.css('top', (vb.lastMouse.y - 100));
	jOverlay.addClass('visible');
	vb.currentOverlay = overlay;
	jOverlay.hover(
		function() {
			this.gotMouse = true;
		},
		function() {
			this.gotMouse = false;
			vb.tryToHideOverlay(link, overlay);
		}
	);
}

vb.tryToHideOverlay = function(link, overlay) {
	setTimeout(function(){
		if (!link.gotMouse && !overlay.gotMouse) {
			vb.hideOverlay(overlay);
		}
	}, 300);
}

vb.hideOverlay = function(overlay) {
	vb.currentOverlay = 0;
	jQuery(overlay).removeClass('visible');
	jQuery(overlay).unbind();
}

if (typeof(jQuery) != 'undefined') {
	jQuery(document).ready(function(){
		var link = jQuery('a.vbw_hoverlink');
		link.hover(
			function() {
				var id = this.id;
				id = id.substring('vbw_hoverlink-'.length);
				this.gotMouse = true;
				var overlay = jQuery('#vbw_overlay-' + id).get();
				vb.showOverlay(this, overlay[0]);
			},
			function() {
				var id = this.id;
				this.gotMouse = false;
				id = id.substring('vbw_hoverlink-'.length);
				var overlay = jQuery('#vbw_overlay-' + id).get();
				vb.tryToHideOverlay(this, overlay[0]);
			}
		);
		jQuery(document).mousemove(function(e) {
			vb.lastMouse = { x: e.pageX, y: e.pageY }
		})
	});
}

