/*
 * tinyLightbox "balupton edition" animation extension 2.0 - Plugin for jQuery
 * inspired by http://www.balupton.com/sandbox/jquery_lightbox/
 * 
 * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
 * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
 *
 * Depends:
 *  jquery.js
 *	jquery.tinyLightbox.js
 * 
 *
 *  Copyright (c) 2008 Oleg Slobodskoi (ajaxsoft.de)
 */
;(function($) {

	$.tinyLightbox.animation.balupton = function()
	{
			var self = this;
			
			this.show = function ( callback)
			{
				self.$overlay.animate({opacity:'show' }, self.d.speed, function(){
					self.change(callback);
				});			
			};
			
			this.prepare = function ( callback )
			{
			
				self.$bar.animate({height:0, bottom:0, opacity:'hide'}, self.d.speed, function(){
					self.$bar.hide().height('auto');
					self.$labelClose.hide();
					self.$image.fadeOut(self.d.speed, function(){
						callback && callback();					
					});
				});
			
			};
			
			this.change = function ( callback )
			{

				var imageWidth = self.imageWidth< self.d.minWidth ? self.d.minWidth : self.imageWidth;

				var left= self.doc.width/2- imageWidth/2,
					vertAnimation = false,
					horAnimation = false
				;
				
				if ( self.imageHeight != self.$tinyLightbox.innerHeight() )
					vertAnimation = true;
				if (imageWidth != self.$tinyLightbox.innerWidth())
					horAnimation = true;

				var animation = {
					top: $(document).scrollTop()+self.top
				};
				
				if (vertAnimation)
					animation.height = self.imageHeight+self.paddingTop*2;
				if (horAnimation)
				{
					animation.width = imageWidth;
					animation.left = left;
				};	
				
				self.$tinyLightbox.animate(animation, showImage);

				function showImage (callback)
				{
					self.$image.fadeIn(self.d.speed, function(){
						self.barHeight = self.$bar.height();				
						self.$bar.height(0).width(self.$tinyLightbox.innerWidth());
						self.$labelClose.show();
						self.$bar.animate({height: self.barHeight,bottom: self.barHeight*-1, opacity: 'show'}, self.d.speed, function(){
							callback && callback();		
						});
					
					});
				};
			};
			
			this.close = function ()
			{
				self.$tinyLightbox.fadeOut(self.d.speed/2,function(){
					self.$overlay.fadeOut(self.d.speed/2,function(){
						self.$tinyLightbox.remove();
						self.$overlay.remove();
					});	
				});		
			
			
			};
			
			this.limit = function ( callback )
			{
				shake(4, self.d.speed/2, 40, '+', 0);
				function shake (times, speed, distance, dir, timesNow)
				{
					timesNow++;
					dir = dir=='+' ? '-' : '+';
					self.$tinyLightbox.animate({left: dir+'='+distance}, speed, function(){
						timesNow<times && shake(times, speed, distance, dir, timesNow);
						
					});
				};
			};
			
		
	};				

})( jQuery );