(function($){
 $.fn.fbSlider = function(options) {

	var defaults = {
			sliderTimeout: 1000,
			width: 0,
			height: 0
	};
	
	var options = $.extend(defaults, options);
	var sliderBusy = 0;
    
	return this.each(function() {
		options.container = $(this);
		options.containerHeight = 0;
		options.containerWidth = 0;
		options.slideCount = 0;
		options.activeSlide = 0;
		options.imagesLoaded = 0;
		
		options.container.css('overflow', 'hidden');
		
		//console.log( options.width );
		
		if( options.width != 0 )
		{
			if( options.width >= 640 )
			{
				options.width = 640;
			}
		}
		
		$('#fb-slider-prev').click(function() {
			prevSlide();
		});
		
		$('#fb-slider-next').click(function() {
			nextSlide();
		});
		
		$("#fb-slider").touchwipe({
		     wipeLeft: function() { nextSlide(); event.preventDefault(); },
		     wipeRight: function() { prevSlide(); event.preventDefault(); },
		     /* wipeUp: function() { },
		     wipeDown: function() { }, */
		     min_move_x: 20,
		     min_move_y: 20,
		     preventDefaultEvents: false
		});
		
		$('#fb-slider').height( '0px' );
		$('#fb-slider').width( options.width * 10 );
		
		options.container.find('li').each( function() {
			options.slideCount++;
			$(this).width( options.width );
		});
		
		$.fancybox.resize();
		
		if( options.slideCount == 0 )
		{
			$('.fb-preview').hide();
		}
		
		setNavigation();
		//$(this).width( ( options.slideCount * options.container.parent().width() ) );

		//console.log('finding images...');
		options.container.find('img').each( function(){
			$(this).css('max-width', '100%').css('position','absolute').css('left','0px');
			//console.log('do setVAlign()');
			setVAlign( this );
		});
		
		function prevSlide( )
		{
			if( sliderBusy == 0 && options.activeSlide <= options.slideCount && ( options.activeSlide - 1 ) >= 0 )
			{
				sliderBusy = 1;
				options.activeSlide--;
				
				setNavigation();
				
				$( options.container ).animate({
					left: '+=' + options.width
				}, options.sliderTimeout, function() {
					sliderBusy = 0;
				});
			}
		}
		
		function nextSlide( )
		{
			if( sliderBusy == 0 && options.activeSlide < ( options.slideCount - 1 ) && ( options.activeSlide + 1 ) >= 0 )
			{
				sliderBusy = 1;
				options.activeSlide++;
				
				setNavigation();
				
				$( options.container ).animate({
					left: '-=' + options.width
				}, options.sliderTimeout, function() {
					sliderBusy = 0;
				});
			}
		}
		
		function setVAlign( obj )
		{
			//console.log( 'setVAlign ' + obj );
			
			if( $( obj ).height() <= 50 )
			{
				setTimeout( function(){ setVAlign( $( obj ) ) }, 150 );
				//console.log( $( obj ).height() );
			} else {
				var imgHeight = $( obj ).height();
				
				if( imgHeight > $('#fb-slider').height() ) {
					$('#fb-slider').height( imgHeight );
				}
				
				var imgNewHeight = $('.fb-preview').height();
				
				//console.log( obj );
				//console.log( imgNewHeight + '--' + imgHeight + '--' + ( imgNewHeight - imgHeight ) / 2 );
				
				$(obj).css('position','relative').css('margin-top', ( imgNewHeight - imgHeight ) / 2 );
				
				options.imagesLoaded++;
				
				//console.log( options.imagesLoaded + '/' + options.slideCount + ' h: ' + $( obj ).height() + ' src: ' + $(this).attr('src') );
				
				$.fancybox.resize();
				
				var i = 0;
				
				options.container.find('li').each( function(){
					$(this).css('position', 'absolute').css('left', $(this).width() * i );
					i++;
				});
				
					
				if( options.slideCount == options.imagesLoaded )
				{
					//console.log('Images loaded. Showing div.');
					
					options.container.find('li img').each( function(){
						setVAlign( $(this) );
					});
					
					$('#fancybox-content').show().css('visibility', 'visible').css('display', 'block');
					
					$.fancybox.resize();
				}
			}
		}
		
		function setNavigation()
		{
			//console.log( options.activeSlide +'---'+ ( options.slideCount - 1 ) );
			
			if( options.activeSlide == ( options.slideCount - 1 ) )
			{
				$('#fb-slider-next').fadeOut('slow');
			} else {
				$('#fb-slider-next').fadeIn('slow');
			}
			
			if( options.activeSlide == 0 )
			{
				$('#fb-slider-prev').fadeOut('slow');
			} else {
				$('#fb-slider-prev').fadeIn('slow');
			}
		}
	});
 };
})(jQuery);


(function ($) {
	$.event.special.load = {
		add: function (hollaback) {
			//console.log( this );
			if ( this.nodeType === 1 && this.tagName.toLowerCase() === 'img' && this.src !== '' ) {
				// Image is already complete, fire the hollaback (fixes browser issues were cached
				// images isn't triggering the load event)
				if ( this.complete || this.readyState === 4 ) {
					hollaback.handler.apply(this);
				}

				// Check if data URI images is supported, fire 'error' event if not
				else if ( this.readyState === 'uninitialized' && this.src.indexOf('data:') === 0 ) {
					$(this).trigger('error');
				}

				else {
					setTimeout( function(){ $(this).bind('load', hollaback.handler) }, 100 );
				}
			}
		}
	};
}(jQuery));
