(function($) {

var elements;
var options = {};

$.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:300,
		small_height:300,
		big_width:1000,
		big_height:1000
	};
	
	options = $.extend(defaults, params);
	
	//calculate correct positioning...
	options.a_width  = (options.big_width  - options.small_width)  * 2 + options.small_width;
	options.a_height = (options.big_height - options.small_height) * 2 + options.small_height;
	
	options.a_top  = options.big_height - options.small_height;
	options.a_left = options.big_width  - options.small_width;
	
	elements = this;
	
	return this.each(function() {
		
		//caching...
		var $container = $(this);
		var $link = $container.find('a');
		var $image = $link.find('img');
		
		//reading some parameters...
		$image.data('small_path', $image.attr('src'));
		$image.data('big_path', $link.attr('href'));
			
		/*
			INITIALIZATION...
		*/
		$link.css({
			display:'block',
			position:'absolute',
			width: options.a_width   + 'px',
			height: options.a_height + 'px',
			top:  '-' + options.a_top  + 'px',
			left: '-' + options.a_left + 'px'
		});
		
		/* Removing default link behaviour... */
		$link.click(function() { return false; });
		
		$image.css({
			display:'block',
			position:'absolute',
			width:  options.small_width + 'px',
			height: options.small_height + 'px',
			top: options.a_top + 'px',
			left: options.a_left + 'px'
		});
		
		/*
			HANDLE EVENTS...
		*/

		$image.mouseup(function() {
			
			if (!$image.data('dragging')) {
				if ($image.data('zoomed')) {
					zoomOut();
				} else {
					zoomIn();
				}
			}
		});

		/*
			METHODS...
		*/
		function zoomOut() {
			$image.animate({
				width:options.small_width + 'px',
				height:options.small_height + 'px',
				top:  options.a_top  + 'px',
				left: options.a_left + 'px'
			}, options.duration, swapWithSmall);
			$image.data('zoomed', false);
		}
		
		function zoomIn() {
			$image.animate({
				width:options.big_width + 'px',
				height:options.big_height + 'px',
				top:  options.a_top / 2  + 'px',
				left: options.a_left / 2 + 'px'
			}, options.duration, swapWithBig);
			$image.data('zoomed', true);
		}
		
		function swapWithBig() {
			$image.attr('src', $image.data('big_path'));
			$image.draggable({
				containment: 'parent',
				start: handleStartDrag,
				stop: handleStopDrag
			});
			$image.draggable('enable');
		}
		
		function swapWithSmall() {
			$image.attr('src', $image.data('small_path'));
			$image.draggable('disable');
		}
		
		function handleStartDrag() {
			$image.data('dragging', true);
		}
		
		function handleStopDrag() {
			$image.data('dragging', false);
		}
	});
}

$.fn.zoom.reset = function() {
	$(elements).each(function() {
		$(this).find('img').css({
			width:  options.small_width + 'px',
			height: options.small_height + 'px',
			top:    options.a_top  + 'px',
			left:   options.a_left + 'px'
		});
		$(this).find('img').data('zoomed', false);
		$(this).find('img').data('big_path', $(this).find('a').attr('href'));
		$(this).find('img').data('small_path', $(this).find('img').attr('src'));
	});
}

$.fn.zoom.zoomAll = function() {
	$(elements).each(function() {
		$(this).find('img').trigger('mouseup');
	});
}

})(jQuery)
