/* ------------------------------------------------------------------------
	s3Slider

	Update By: Mircho Mirev -> http://momche.net
	heavily modded to provide syncronized animation

	Developped By: Boban Karišik -> http://www.serie3.info/
        CSS Help: Mészáros Róbert -> http://www.perspectived.com/
	Version: 1.0

	Copyright: Feel free to redistribute the script/modify it, as
			   long as you leave my infos at the top.
------------------------------------------------------------------------- */


(function($){

    $.fn.s3Slider = function(vars) {

        var element     = this;
        var timeOut     = (vars.timeOut != undefined) ? vars.timeOut : 4000;
		var textOut		= (vars.textOut != undefined) ? vars.textOut : Math.round( timeOut/6 );
		var transitionTime = (vars.transitionTime != undefined) ? vars.transitionTime : Math.round( timeOut/6 );
        var current     = null;
        var timeOutFn   = null;
        var faderStat   = true;
        var mOver       = false;

		var elId = element[0].id;
        var items       = $( "#" + elId + "Content ." + elId + "Image:not(.clear)" );
		var itemsLoaded = 0;

		var lastZIndex = 0;
	    var maxZIndex	= 1000 + items.length;

        items.each( function(i) {

            $( this ).mouseover(function() {
               mOver = true;
            });

            $( this ).mouseout(function() {
                mOver   = false;
                crossFadeElement(true);
            });
			
			//wait for all items (images) to be loaded
			$( 'img', this ).each( function( img )
			{
				if( $( this ).width() > 0 && $( this ).height() > 0 )
				{
					itemsLoaded++;
					if( itemsLoaded == items.length )
					{
						makeSliderCrossFade();
					}
				}
				else
				{
					$( this ).bind( 'load', function(  )
					{
						itemsLoaded++;
						if( itemsLoaded == items.length )
						{
							//alert( itemsLoaded );
							//alert( 'I Start...' );
							makeSliderCrossFade();
						}
					} );
				}
			} );

			$( this ).css( 'zIndex', maxZIndex - i ).css( 'position', 'absolute' );

		});

		function crossFadeElement()
		{
			//if we got just out of mouseOut make the transition twice as fast
			clearTimeout( timeOutFn );
			var thisTimeOut = mOver ? ( timeOut / 2 ) : timeOut;
			if( items.length > 0 ) {
				timeOutFn = setTimeout( makeSliderCrossFade, thisTimeOut );
			} else {
				debug( "Poof ... " );
			}
		}

		//hides a composite animation
		function hideItem( item, callback )
		{
			callback = callback || function(){};
			
			//we have 2 animations
			var iters = 2;
			
			var theCallback = function()
			{
				iters--;
				if( iters == 0 )
				{
					callback();
				}
			}
			
			var span, spanHeight;
			span = $( 'span', item );
			if( span.length > 0 )
			{
				spanHeight = span.outerHeight();

				$( 'span.top', item ).animate(
					{
						top: -spanHeight
					},
					{
						duration: textOut,
						complete: theCallback
					}
				);

				$( 'span.bottom', item ).animate(
					{
						bottom: -spanHeight
					},
					{
						duration: textOut,
						complete: theCallback
					}
				);

			}

			$( item ).fadeOut( transitionTime, theCallback );
		}
		
		//shows an item
		function showItem( item, callback )
		{
			callback = callback || function(){};

			var iters = 2;
			
			var theCallback = function()
			{
				iters--;
				if( iters == 0 )
				{
					callback();
				}
			}

			var span = $( 'span', item );
			if( span )
			{
				$( 'span.top', item ).animate(
				   {
					  top: 0
				   },
				   {
					  duration: textOut,
					  complete: theCallback
				   }
				);

				$( 'span.bottom', item ).animate(
				   {
					  bottom: 0
				   },
				   {
					  duration: textOut,
					  complete: theCallback
				   }
				);
			}

			$( item ).fadeIn( transitionTime, theCallback );
		}
		

        function makeSliderCrossFade() 
		{
			
			if( !mOver ) {

			    if( current !== null )
				{
					hideItem( current, function(){ } );
					
					current = $( current ).next( 'li' );
					if( current.length == 0 )
					{
						current = $( items[ 0 ] );
					}

				}

				current = ( current != null ) ? current : items[ 0 ];

				showItem( current, function()
				{
					if( !mOver )
					{
					  crossFadeElement();
					}				
				} );
			}
        }

        //makeSliderCrossFade();

		function debug( message )
		{
			if( typeof window.console != 'undefined'  && window.console.log )
			{
				window.console.log( message );
			}
		}

    };

})(jQuery);

