(function(JQ) {
JQ.fn.zoom = function(params) {
	/*
	Zoom jQuery Plugin
	Copyright: (C) 2009 - Francesco Paggin
	Parameters:
		(int) duration: the duration of the zooming animaton
		(int) small_width, small_height: the dimensions of the small thumbnails
		(int) big_width, big_height: the dimensions of the zoomed images
	*/
	
	var defaults = {
		duration: 300,
		small_width:370,
		small_height:277,
		big_width:640,
		big_height:480
	}
	
	var options = JQ.extend(defaults, params);
	
	return this.each(function() {
		//caching...
		var JQcontainer = JQ(this);
		var JQlink = JQcontainer.find('a');
		var JQimage = JQlink.find('img');
		
		//reading some parameters...
		var small_path = JQimage.attr('src');
		var big_path   = JQlink.attr('href');
		
		//status variables...
		var dragging = false;
		var zoomed   = false;
		
		//calculate correct positioning...
		var a_width  = (options.big_width  - options.small_width)  * 2 + options.small_width;
		var a_height = (options.big_height - options.small_height) * 2 + options.small_height;
		
		var a_top  = options.big_height - options.small_height;
		var a_left = options.big_width  - options.small_width;
		
		/*
			INITIALIZATION...
		*/
		JQlink.css({
			display:'block',
			position:'absolute',
			width: a_width   + 'px',
			height: a_height + 'px',
			top:  '-' + a_top  + 'px',
			left: '-' + a_left + 'px'
		});
		
		/* Removing default link behaviour... */
		JQlink.removeAttr('href');
		
		JQimage.css({
			display:'block',
			position:'absolute',
			width:  options.small_width + 'px',
			height: options.small_height + 'px',
			top: '246px',
			left: a_left + 'px'
		});
		
		/*
			HANDLE EVENTS...
		*/
		JQimage.mouseup(function() {
			if (!dragging) {
				if (zoomed) {
					zoomOut();
				} else {
					zoomIn();
				}
			}
		});
		
		
		
		/*
			METHODS...
		*/
		function zoomOut() {
			JQimage.animate({
				width:options.small_width + 'px',
				height:options.small_height + 'px',
				top:  '246px',
				left: a_left + 'px'
			}, options.duration, swapWithSmall);
			zoomed = false;
		}
		
		function zoomIn() {
			JQimage.animate({
				width:options.big_width + 'px',
				height:options.big_height + 'px',
				top:  '138px',
				left: a_left / 2 + 'px'
			}, options.duration, swapWithBig);
			zoomed = true;
		}
		
		function swapWithBig() {
			JQimage.attr('src', big_path);
			JQimage.draggable({
				containment: 'parent',
				start: handleStartDrag,
				stop: handleStopDrag
			});
		}
		
		function swapWithSmall() {
			JQimage.attr('src', small_path);
			JQimage.draggable('disable');
		}
		
		function handleStartDrag() {
			dragging = true;
		}
		
		function handleStopDrag() {
			dragging = false;
		}
	});
}
})(jQuery)
 


