jQuery.extend({

	zoom: {

		SHOW_DURATION: 250,
		SHOW_DELAY: 250,
		HIDE_DURATION: 250,

		clicked: false,
		showing: null,

		hide: function() {
			if (!jQuery.zoom.clicked) {
				jQuery('#zoom a').unbind('mouseout');
				jQuery('#zoom').animate({'opacity': 'hide'}, jQuery.zoom.HIDE_DURATION);
			}
			return false;
		},

		show: function(self) {
			var a  = jQuery(self),
				ah = a.attr('href'),
				at = '', //a.attr('alt'),
				i  = jQuery('img', self),
				is = i.attr('longdesc'),
				ia = '', //i.attr('alt'),
				it = '', //i.attr('title'),
				ip = jQuery.absolutePosition(i[0]),
				ipx = ip.x + (i.width() - 340) / 2,
				ipy = ip.y + (i.height() - 240) / 2;

			jQuery('#zoom')
				.html([
					'<a href="', (ah || ''), '" title="', (at || ''),
					'" onclick="javascript:jQuery.zoom.clicked=true;">',
					'<img style="width:340px;border:1px solid #B0B0B0;" ',
					'src="', (is || ''), '" alt="', (ia || ''),
					'" title="', (it || ''), '" border="0"/>',
					'</a>'
				].join(''))
				.css({
					'left': (ipx < 0 ? 0 : ipx) + 'px',
					'top': (ipy < 0 ? 0 : ipy) + 'px' })
				.animate({'opacity':'show'}, jQuery.zoom.SHOW_DURATION);

			jQuery('#zoom a').bind('mouseout', jQuery.zoom.hide);
		}
	},

	absolutePosition: function(el) {
		var r = { x: el.offsetLeft, y: el.offsetTop };
		if (el.offsetParent) {
			var tmp = jQuery.absolutePosition(el.offsetParent);
			r.x += tmp.x;
			r.y += tmp.y;
		}
		//alert("x="+r.x+" y="+r.y);
		
		return r;
	}
});

jQuery.fn.extend({

	zoomOver: function() {

		return this
			.bind('mouseover', function() {
				if (!jQuery.zoom.clicked) {
					clearTimeout(jQuery.zoom.showing);
					var self = this;
					jQuery.zoom.showing = setTimeout(
						function() { jQuery.zoom.show(self); },
						jQuery.zoom.SHOW_DELAY
					);
				}
			})
			.bind('mouseout', function() {
				clearTimeout(jQuery.zoom.showing);
			})
			.bind('click', function() {
				jQuery.zoom.clicked = true;
				clearTimeout(jQuery.zoom.showing);
			});

	}

});

$(document).ready(function() {
	$('a.zoom').zoomOver();
});

