/**
 * Scroller for abtvmedia
 * 
 * @author Dominik "Kurrija" Schöner - darkhavens.de
 * @version 0.2.1-20100912
 */

(function($) {	
	
	var Scroller = function( container, options )
	{
		var dhs = this;
		this.scrollTimer = 0;
		this.container = container;
		this.currFirst = 0;
		this.scrollTarget = 0;
		var items = $(container).children(options.itemClass).wrap('<div style="position:absolute;display:none"/>').parent();
		var itemWidth = $(container).width() / options.itemCount;
		
		$(items).css('width',itemWidth+'px');		
		$(container).css( { position:'relative', overflow:'hidden', height:options.height } );
		
		/*
		 * Initialize buttons and attach events
		 */
		$(options.buttonPrev).css( { display: 'inline', visibility: 'visible'} );
		$(options.buttonNext).css( { display: 'inline', visibility: 'visible'} );
		
		$(options.buttonPrev).bind( 'mouseup', dhs, function(event){
			event.data.doScrollBy(-1);
			return false;
		} );
		$(options.buttonNext).bind( 'mouseup', dhs, function(event){															
			event.data.doScrollBy(1);
			return false;
		} );
		
		for( i = 0; i < items.length; i++ ) {
			if( i < options.itemCount ) {
				$(items[i]).css( { display:'block',top:'0px',left:($(container).width()+i*itemWidth)+'px' } );
				$(items[i]).animate( { left:(i*itemWidth)+'px' }, 1000 );
			}
			else
				$(items[i]).delay( 1000 );
		}
		$(items[0]).queue( function(next){ dhs.autoScroll(); next(); } );

		/*
		 * Disable selections within the scroller
		 */
		$(container).bind( 'mousedown', this, function(event){	
			event.data.container.focus();
			return false;
		} );
		/* IE fix */
		container.onselectstart = function () { return false; };
				
		/*
		 * Stop auto-scroll on mouseover
		 */
		$(container).bind( 'mouseover', this, function( event ) {
			clearTimeout( dhs.scrollTimer );
		});
		/*
		 * Restore auto-scroll on mouseout
		 */
		$(container).bind('mouseout',this,function(event){
			dhs.autoScroll();
		});

		this.doScroll = function()
		{
			if( dhs.scrollTarget == 0 ) {
				dhs.autoScroll();
				return;
			}
			else if( dhs.scrollTarget < 0 ) {
				var spawnItem = dhs.currFirst-1;
				if( spawnItem < 0 )
					spawnItem += items.length;
				$(items[spawnItem]).queue( function(next){ $(items[spawnItem]).css( { display:'block',top:'0px',left:(itemWidth*(-1))+'px' } ); next(); } );
				for( i = 0; i < items.length; i++ ) {
					if( i <= options.itemCount )
						$(items[(spawnItem+i)%items.length]).animate( { left:'+='+itemWidth+'px' }, 1000 );
					else
						$(items[(spawnItem+i)%items.length]).delay( 1000 );
				}
				dhs.currFirst = spawnItem;
				dhs.scrollTarget++;
			}
			else {
				var spawnItem = (dhs.currFirst+options.itemCount)%items.length;
				$(items[spawnItem]).queue( function(next){ $(items[spawnItem]).css( { display:'block',top:'0px',left:$(container).width()+'px' } ); next(); } );
				for( i = 0; i < items.length; i++ ) {
					if( i <= options.itemCount )
						$(items[(dhs.currFirst+i)%items.length]).animate( { left:'-='+itemWidth+'px' }, 1000 );
					else
						$(items[(dhs.currFirst+i)%items.length]).delay( 1000 );
				}
				dhs.currFirst++;
				dhs.currFirst %= items.length;
				dhs.scrollTarget--;
			}
			this.doScroll();
		};

		/*
		 * Scroll by abs(dir) elements to the left for dir < 0 and to the right for dir > 0
		 */
		this.doScrollBy = function(dir)
		{	
			clearInterval( dhs.scrollTimer );
			dhs.scrollTarget += dir;
			dhs.scrollTarget %= items.length;
			dhs.doScroll();
		};
		
		this.autoScroll = function()
		{			
			if ( options.autoScroll != 0 )
			{
				dhs.scrollTimer = setTimeout( function(){ dhs.doScrollBy( options.autoScroll ); }, options.autoScrollDelay );
			}
		};
	}; 
	
	$.fn.dhScroller = function(options) {
		this.each( function() {			
			options = $.extend({}, {
				autoScrollDelay:10000,
				autoScroll:1,
				useMouseWheel:true,
				itemCount:4,
				itemClass:'.dhscroller',
				height:'70px'
			},options );	
			$(this).data('dhscroller', new Scroller( this, options ) );
		});				
		return this;
	};

})(jQuery);
