/*Image Bubbles Effect (April 4th, 2010)
* This notice must stay intact for usage 
* Author: Dynamic Drive at http://www.dynamicdrive.com/
* Visit http://www.dynamicdrive.com/ for full source code
*/

jQuery.fn.imgbubbles=function(options){
	var $j=jQuery
	
	var setting=$j.extend({}, {factor:2, speed:'fast'}, options) //merge options w/ default settings
	return this.each(function(){ //return jQuery obj
		var $jbubblewrap=$j(this)
		var $jimgs=$jbubblewrap.find('li img')
		$jimgs.each(function(){
			var $jimg=$j(this)
			var $jparentli=$jimg.offsetParent()
			var od={width:this.offsetWidth, height:this.offsetHeight} //original dimensions of image
			var nd={width:od.width*setting.factor, height:od.height*setting.factor} //enlarged dimensions of image
			var ncoords=[-(nd.width-od.width)/2, -(nd.height-od.height)/2] //coords to move enlarged image to
			$jimg.data("specs", { //cache image specs
				od: od,
				nd: nd,
				ncoords: ncoords
			})
			if ($jimg.attr('alt')){ //if tooltip for image defined
				var $jtip=$j('<div class="tooltip" style="z-index:1001" />').html($jimg.attr('alt')).css('visibility', 'hidden').appendTo($jimg.offsetParent())
				var tipd={width:$jtip.outerWidth(), height:$jtip.outerHeight()} //tip dimensions
				$jtip.data("specs", {
					d: tipd,
					ocoords: [ncoords[0]-tipd.width/2+nd.width/2, ncoords[1]-tipd.height/2], //resting tip coords
					ncoords: [-tipd.width/2+od.width/2, -tipd.height] //tip coords to animate to
				})
				$jimg.data('$jtip', $jtip)
			}
		})

		$jbubblewrap.mouseover(function(e){
			if (e.target.tagName=="IMG"){
				var $jimg=$j(e.target), $jtip=$jimg.data('$jtip')
				var imgspecs=$jimg.data('specs')
				var ncoords=imgspecs.ncoords
				var od=imgspecs.od
				var nd=imgspecs.nd
				$jimg.stop().css('zIndex', 1000).animate({left:ncoords[0], top:ncoords[1], width:nd.width, height:nd.height}, setting.speed) //animate image
				if ($jimg.attr('alt')){
					var tipspecs=$jtip.data("specs")
					$jtip.css({zIndex:1000, visibility:'visible', left:-tipspecs.d.width/2+od.width/2, top:-tipspecs.d.height})
						.animate({left:ncoords[0]-tipspecs.d.width/2+nd.width/2, top:ncoords[1]-tipspecs.d.height}, setting.speed) //animate tip			
				}
			}
		})
	
		$jbubblewrap.mouseout(function(e){
			if (e.target.tagName=="IMG"){
				var $jimg=$j(e.target), $jtip=$jimg.data('$jtip')
				var imgspecs=$jimg.data('specs')
				var od=imgspecs.od
				var nd=imgspecs.nd
				$jimg.stop().css('zIndex', 999).animate({left:0, top:0, width:od.width, height:od.height}, setting.speed) //animate image
				if ($jimg.attr('alt')){
					var tipspecs=$jtip.data("specs")
					$jtip.css({zIndex:999, visibility:'hidden'}) //hide tip
				}
			}
		})
	
	})
}
