/*
 * jQuery HoverPulse Plugin by M. Alsup
 * Examples and docs at: http://malsup.com/jquery/hoverpulse/
 * Dual licensed under the MIT and GPL
 * Requires: jQuery v1.2.6 or later
 * @version: 1.01  26-FEB-2009
 */

(function($) {

$.fn.hoverpulse = function(options) {
    // in 1.3+ we can fix mistakes with the ready state
    if (this.length == 0) {
        if (!$.isReady && this.selector) {
            var s = this.selector, c = this.context;
            $(function() {
                $(s,c).hoverpulse(options);
            });
        }
        return this;
    }    
    
	var opts = $.extend({}, $.fn.hoverpulse.defaults, options);

	// parent must be relatively positioned
	this.parent().css({ position: 'relative' });
	// pulsing element must be absolutely positioned
	this.css({ position: 'absolute', top: 0, left: 0 });

	this.each(function() {
		var $this = $(this);
		var w = $this.width(), h = $this.height();
		$this.data('hoverpulse.size', { w: parseInt(w), h: parseInt(h) });
	});

	// bind hover event for behavior
	return this.click(
		// hover over
		function() {
			
			var $this = $(this);
			
			if($this.width() <= 30 && $this.height() <= 30){
			
				
				$this.parent().css('z-index', opts.zIndexActive);
				
				//alert($(this).attr('rel'));
				//return false;
				
				var im = new Image();			
				im.onload = function(){
					im.onload = null;
					nw = 30;
					nh = im.height*nw/im.width;
					if(nh > 30){
						nh = 30;
						nw = nh*im.width/im.height;
					}
					nw = nh = 30;
					$this.attr('rel', $this.attr('src'));
					$this.css('width', nw);
					$this.css('height', nh);
					$this.attr('src', im.src);	
					
					var size = $this.data('hoverpulse.size');
					var w = size.w, h = size.h;
					$this.stop().animate({ 
						top:  ('-'+(im.height/2-15)+'px'), 
						left: ('-'+(im.width/2-15)+'px'), 
						height: (im.height+2)+'px', 
						width:	(im.width+2)+'px' 
					}, opts.speed);
				
				}
				im.src = $this.attr('rel');
				return false;	
				
			}else{
				
				var size = $this.data('hoverpulse.size');
				var w = size.w, h = size.h;
				
				$this.stop().animate({ 
					top:  0, 
					left: 0, 
					height: (30+'px'), 
					width:	(30+'px') 
				}, opts.speed, function() {
					$this.parent().css('z-index', opts.zIndexNormal);
					var old_s = $this.attr('src');
					$this.attr('src', $this.attr('rel'));
					$this.attr('rel', old_s);
					$this.css('width', 30);
					$this.css('height', 30);
				});
				
			}
		}
		/* hover out
		function() {
			var $this = $(this);
			var size = $this.data('hoverpulse.size');
			var w = size.w, h = size.h;
			
			$this.stop().animate({ 
				top:  0, 
				left: 0, 
				height: (30+'px'), 
				width:	(30+'px') 
			}, opts.speed, function() {
				$this.parent().css('z-index', opts.zIndexNormal);
				var old_s = $this.attr('src');
				$this.attr('src', $this.attr('rel'));
				$this.attr('rel', old_s);
				$this.css('width', 30);
				$this.css('height', 30);
			});
		}
		*/
	);
};

$.fn.hoverpulse.defaults = {
	size:  20,
	speed: 200,
	zIndexActive: 100,
	zIndexNormal: 1
};

})(jQuery);

