(function($) { 
	$.fn.imagepulser = function(config) { 
		var opts = $.extend({
				width: '320', 
				height: '240', 
				item_class: 'imagepulser-open', 
				padding_top: 0, 
				padding_left: 0, 
				close_counter: 0
		}, config);
		
		$('img',this).each(function() {
			var ow = $(this).width();
			var oh = $(this).height();
			
			$(this).attr('imagepulse-width', ow );
			$(this).attr('imagepulse-height', oh );

			//Display image properties
			var h, w, t, l;
			
			//Mouseover for cloned() image
			var mouseover;
			
			//Absolutely positioned item
			var item;
			
			/**
			*	Grab dimensions of that & set to local h, w, t, l variables
			*	This is checked every time open is called, in case the page contents have moved
			*/
			function gather_dimensions(that) {
				h = $(that).height(); 
				w = $(that).width(); 
				t = $(that).offset().top; 
				l = $(that).offset().left; 
			}
			
			/**
			*	Clone the displayed element, hide it & position it over the 'real' element
			*	open should be called quickly after this, otherwise the offsets may change
			*/
			function attach(that) {
				var item = $(that).clone();
				$(item).css({
					height:	h, 
					width:  w,
					top:    t,
					left:   l,
					position: 'absolute'
				}).appendTo('body').hide();
				return item;
			}
			
			/**
			*	Remove the cloned item
			*/
			function detach(that) {
				return true;
				$(that).remove(); 
			}
			
			/**
			*	Animate the opening of the cloned item
			*/
			function open(item) {
				
				//Close any other items
				$('.' + opts.item_class).remove(); 
			
				$(item).css({'z-index' : '2000'}).show();
				$(item).addClass("hover").stop() 
					.animate({
						//top: -1 * ( ($(item).height() - 0) /2 ),  
						//left: -1 * ( ($(item).width() - 0) /2 ),
						paddingTop: opts.padding_top,
						paddingBottom: opts.padding_top,
						
						paddingLeft: opts.padding_left, 
						paddingRight: opts.padding_left,
						
						top:  t - ( (h)/2 ) - opts.padding_top,
						left: l - ( (w)/2 ) - opts.padding_left,
						
						/*bottom: t + ( (h)/2 ),
						right: l + ( (w)/2 ),*/
						width: opts.width, 
						height: opts.height
					}, 400, 'easeInQuad', function() {
						
					}).addClass(opts.item_class);
					mouseover = true;
			}
			
			/**
			*	Animate the closing of the item & detach when finished
			*/
			function close(item) { 
				if (mouseover == true)
					return false;
				
				$('.ui-effects-wrapper:empty').remove();				
				opts.close_counter++; 
				if ( opts.close_counter > 50 ) {
					return random_closer(item);
				}
				
				/*
				$(item).css({'z-index' : '0'});
				$(item).effect('blind').dequeue();
				
				return;
				*/
				
				$(item).css({'z-index' : '0'}).hide();
				$(item).removeClass("hover").stop()  
					.animate({
						top: t,
						left: l,
						padding: 0,
						width: $(item).attr('imagepulse-width'), 
						height: $(item).attr('imagepulse-height') 
						
					}, 400, 'easeOutQuad', function() {
						$(this).remove(); 
					}).removeClass(opts.item_class);
				 
				return true;
			}
			
			
			/**
			*	Main display image mouse over/out
			*/	
			//$(this).hover(function() {
			$(this).hoverIntent(function() {
					//window.alert('here');
					gather_dimensions(this); 
					item = attach(this); 
					open(item);

					/**
					*	Cloned image hover over
					*	When the cloned image resizes, the mouseover event fires for the displayed element
					*	We ignore the displayed element's mouseover if we're still on the cloned element in here. 
					*/
					$(item).hover(function() {
							mouseover = true; 
						}, function() {
							mouseover = false;
							var ret = close(this);
					}); 
					
				} , function() {
					if (mouseover == true)
						return false;
					
					var ret = close(item); 
					
			});
			

			function select_rand(array) {
				var i = Math.floor( Math.random() * array.length ) ;
				return array[i]; 
				
			}
			function random_closer(that) {
				var effects = [ "blind" , "fold", "puff", "explode", "clip", "scale", "drop" ]; 
				var e = select_rand(effects);
				var o = {}; 
				switch(e) {
					case 'blind':
						o.direction = select_rand( new Array('horizontal', 'vertical') );
						break; 
					case 'fold':
						o = select_rand( new Array( true, false ) );
						break; 
					case 'clip':
						o.direction = select_rand( new Array('horizontal', 'vertical') );
						break; 
					
				}
				$(item).css({'z-index' : '0'});
				
				$(item).hide(e, o, 400).dequeue();
				//$(item).remove();
				return true;
				
			}


			
		}); //End imagepulser
			

	}
})(jQuery); 


