/**
	Smooth Ticker
	@author Eric King
	@date 2008-11-25
*/
jQuery.fn.smoothTicker = function( settings ){

	settings = jQuery.extend( {
		speed: 100,
		pixelPerSec: 1,
		startOffset: 0,
		direction: 'left',
		showStatus: false
	}, settings );
	
	var dump = function( obj, return_val, delim ){
		return_val = return_val || false;
		delim = delim || "\n";
		var txt = [];
		for( var i in obj ){
			txt[ txt.length ] = ( i + " = " + obj[ i ] );
		}
		txt = txt.join( delim );
		if( return_val == true )return txt;
		else alert( txt );
	}
	
	var totalWidths = function(){
		var items_width = 0;
		this.each( function(){
			items_width += $( this ).outerWidth( true );
		} );
		return items_width;
	}

	var status = function( elm, data ){
		if( settings.showStatus ){
			if( 'undefined' == typeof data )
				data = settings;
			$('.status', elm.parentNode.parentNode ).text( dump( data, true, ', ' ) );
		}
	}

    var stop_ticker = function( elm ){
        elm.pause = true;
		clearInterval( elm.timer );
		status( elm );
    };

    var start_ticker = function( elm ){
		elm.pause = false;
		elm.timer = setInterval( function(){ move_ticker( elm ) }, settings.speed );
		status( elm );
    };

	var move_ticker = function( elm ){
		if( elm.pause ) return;
		elm.pause = true;
		var _elm = $( elm );
		var xpos = parseInt( _elm.css( settings.direction ) );
		var abs_xpos = Math.abs( xpos );
		var w = parseInt( _elm.css('width') );

		var info = {dir: settings.direction, xpos: xpos, w: w }

		status( elm, info );
		
		_elm.css( settings.direction, ( xpos - settings.pixelPerSec ) + 'px' );

		var _list_item = null;

		if( settings.direction == 'left' ){
			_list_item = $('li:first-child', _elm );
		} else {
			_list_item = $('li:last-child', _elm );
		}

		var li_w = _list_item.outerWidth( true );

		if( xpos < ( 0 - li_w ) ){
		
			var new_x = xpos + li_w;

			if( settings.direction == 'left' ){

				_list_item.appendTo( _elm );

				_elm.css( settings.direction , new_x );

			} else {
			

/*				if( confirm( xpos +' < '+ ( 0 - li_w ) ) ){
					stop_ticker( elm );
					return;
				}*/

				_list_item.prependTo( _elm );
				
				_elm.css( settings.direction , new_x );

			}
		}

		elm.pause = false;

	}

	return this.each( function(){

		var list = $(this).wrap('<div class="smooth-ticker-wrapper"></div>');

		var items = $('li', this);

		if( items.length == 0 )return;

		if( settings.direction == 'left' || settings.direction == 'right' ){
			list.css('textAlign', settings.direction );
		}
		
		
		var items_width = totalWidths.call( items );

		var target_width = $( items[0] ).outerWidth( true ) + list.innerWidth();

		/**
			Clone the list items so that the wrap around effect works properly.
		*/
/*		for( var w = items_width ; items_width < target_width ; items_width += w ){
			items.clone().appendTo( list );
		}*/
		
		for( ; items_width < target_width ; items_width += totalWidths.call( items.clone().appendTo( list ) ) );
	
		// Force the width so the line items don't wrap.
		list.width( items_width * 1.05 );// FF3 Mac issue...

		// Force the smooth-ticker-wrapper height since the absolute positioning changes things later.
		$( this.parentNode ).height( list.height() );

		var status = $('.status', this.parentNode.parentNode );
		if( settings.showStatus ){
			status.css( { top: ( list.height() + 2 ) + 'px' } );
		} else {
			status.remove();
		}
		
		
		var params = {};
			params['position'] = 'absolute';
			params[ settings.direction ] = settings.startOffset;

		list.css( params );

		start_ticker( this );

	} ).hover( function(){ stop_ticker( this ); }, function(){ start_ticker( this ); } );

};