/* plugin name: zoombox */

(function($) {
//
// plugin definition
//
$.fn.zoombox = function(options) {

	// build main options before element iteration
	var opts = $.extend({}, $.fn.zoombox.defaults, options);
	// iterate and reformat each matched element
	return this.each(function() {
			var $this = $(this);
			var $img = $this.find('img');

			$(window).load(function(){
				var $boxW = $this.width();
				var $boxH = $this.height();
				var $imgW = $img.width();
				var $imgH = $img.height();
				var $imgW = $img.width();
				var link = $this.find('a').attr('href');

				//allargo il link all'intera immagine
				if(link != undefined) {
					$img.wrap('<a href="'+link+'" />');
				}

				//posiziono l'immagine
				$img.css({
					position: 'absolute',
					top: ($boxH - $imgH)/2,
					left: ($boxW - $imgW)/2
				}).data({
					top: ($boxH - $imgH)/2,
					left: ($boxW - $imgW)/2,
					width: $imgW
				});

				//imposto l'animazione
				$this.css({
						position: 'relative'
					})
					.hover(
						function(){
							var $this = $(this);
							var $image = $this.find('img');
							var targetWidth = $boxW;
							$image.stop().animate({
								top: 0,
								left: 0,
								width: targetWidth
							});
						},
						function(){
							var $this = $(this);
							var $image = $this.find('img');
							$image.stop().animate({
								top: $image.data('top'),
								left: $image.data('left'),
								width: $image.data('width')
							});
						}
					);
				});
		});
}

//
// plugin settings
//

$.fn.zoombox.defaults = {};

})(jQuery);
