(function($) {
$.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:438,
		small_height:344,
		big_width:1500,
		big_height:1182
	};
	var options = $.extend(defaults, params);
	
	return this.each(function() {
		//caching...
		var $container = $(this);
		var $link = $container.find('a:not(#btn_zoom)');
		var $image = $link.find('img');
		
		//reading some parameters...
		var small_path = $image.attr('src');
		var big_path   = $link.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...
		*/
		$link.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... */
		$link.removeAttr('href');
		
		$image.css({
			display:'block',
			position:'absolute',
			width:  options.small_width + 'px',
			height: options.small_height + 'px',
			top: a_top + 'px',
			left: a_left + 'px'
		});
		
		/*
			HANDLE EVENTS...
		*/
		$image.mouseup(function() {
			if (!dragging) {
				if (zoomed) {
					zoomOut();					
				} else {
					zoomIn();					
				}
			}
		});
		
		$('a#btn_zoom').click(function(){
				zoomIn();
				return false;
		});
		
		/*
			METHODS...
		*/
		function zoomOut() {
			$image.animate({
				width:options.small_width + 'px',
				height:options.small_height + 'px',
				top:  a_top  + 'px',
				left: a_left + 'px'
			}, options.duration, swapWithSmall);
			zoomed = false;			
		}
		
		function zoomIn() {
			$image.animate({
				width:options.big_width + 'px',
				height:options.big_height + 'px',
				top:  a_top / 2  + 'px',
				left: a_left / 2 + 'px'
			}, options.duration, swapWithBig);
			zoomed = true;	
			$image.draggable('enable');
		}
		
		function swapWithBig() {
			$image.attr('src', big_path);
			$image.draggable({
				containment: 'parent',
				start: handleStartDrag,
				stop: handleStopDrag,
				cursor: 'move'
			});
			$('a#btn_zoom').hide();
		}
		
		function swapWithSmall() {
			$image.attr('src', small_path);
			$image.draggable('disable');
			$('a#btn_zoom').show();
		}
		
		function handleStartDrag() {
			dragging = true;
		}
		
		function handleStopDrag() {
			dragging = false;
		}
	});
}
})(jQuery)