/*
 * zoom images
 * author: ghost1k
 * date: 21.04.2010
 * dreamki.com
 */
;(function($) {
    $.fn.zoomable = function(options) {
	var defaults = {
	    zoomInText: 'Click to enlarge',
	    zoomOutText: 'Click to minimize',
	    excludeSrc: [
		'/images/wish-image.png',
		'/images/nophotoman.gif',
		'/images/nophotowoman.gif'
	    ],
	    width: 252,
	    height: 191,
	    zoomOutOnBodyClick: true
	};
	var opts = jQuery.extend(defaults, options);

	// contains helper methods
	function Zoomable (obj) {

	    var self = this;
	    var obj = obj || $('.zoomable:first');
	    var zIndex = obj.css('z-index');

	    self.getDimensions = function(src) {
		var img = new Image();
		img.src = src;
		img.width = (img.width == 0) ? opts.width : img.width;
		img.height = (img.height == 0) ? opts.height : img.height;
		return img;
	    };

	    self.zoomOut = function(zoomOutDimensions) {
		// zoom out
		obj.animate({
		    width: zoomOutDimensions.width,
		    height: zoomOutDimensions.height
		}, 'normal', 'linear', function() {
		    obj.css('z-index', zIndex);
		});
		obj.attr('title', opts.zoomInText);
	    };

	    self.zoomIn = function(zoomInDimensions, zoomOutDimensions) {
		// zoom in
		obj.css('z-index', 2000);
		obj.animate({
		    width: zoomInDimensions.width,
		    height: zoomInDimensions.height
		});
		obj.attr('title', opts.zoomOutText);

		if (opts.zoomOutOnBodyClick) {
		    $('body').bind('mousedown', function(event) {
			// when clicked on body in will cause zoom-out
			var $this = $(this);
			if (obj.width() == zoomInDimensions.width && obj.height() == zoomInDimensions.height) {
			    self.zoomOut(zoomOutDimensions);
			    $this.unbind('mousedown');
			}
		    });
		}
	    };
	    
	}
	
	// Iterate each element
	return this.find('img:first').each(function() {
	    var self = $(this);
	    var zm = new Zoomable(self);

	    // continue each loop if the image is default
	    if (jQuery.inArray(self.attr('src'), opts.excludeSrc) >= 0)
		return;

	    // scaledWidth/scaledHeight are scaled image dimensions: width/height
	    var scaledWidth = parseInt(self.width());
	    var scaledHeight = parseInt(self.height());
	    
	    self.attr('title', opts.zoomInText);
	    
	    self.bind('click', function() {

		scaledWidth = (scaledWidth == 0) ? parseInt(self.css('width')) : scaledWidth;
		scaledHeight = (scaledHeight == 0) ? parseInt(self.css('height')) : scaledHeight;
		
		// get full-size image dimensions
		var dimensions = zm.getDimensions(self.attr('src'));

		// currentWidth/currentHeight are current image dimensions: width/height
		var currentWidth = parseInt(self.width());
		var currentHeight = parseInt(self.height());
//alert(currentWidth + '===' + scaledWidth + '|' + currentHeight + '===' + scaledHeight);
		if (currentWidth === scaledWidth && currentHeight === scaledHeight) {
		    // previous clicked image
		    zm.zoomIn({
			width: dimensions.width,
			height: dimensions.height
		    },
		    {
			width: scaledWidth,
			height: scaledHeight
		    });
		} else {
		    zm.zoomOut({
			width: scaledWidth,
			height: scaledHeight
		    });
		}
	    });
	});
    }
})(jQuery);
