/**
 * @author mark priatel, iolog (mark@iolog.com)
 * @license commercial
 * 
 * Please contact the author for permission to use this javascript on your
 * website.
 * 
 */

	
(function($){

$.fn.hnews = function(cfg)
{
	var settings = {
		speed: 500,
		delay: 3000,
		itemOffsetX: 8,
		itemOffsetY: 8,
		startDelay:2000
	}
	
	jQuery.extend(settings,cfg);
	
	
	this.each( function(){
		
		/* generate a wrapping <div> container that we will place the existing UL into.
		 * this container is used to relatively position arrow widgets that can not be
		 * placed into the UL because the UL has overflow:hidden
		 */
		var ourUL = $(this);
		var ourC = $('<div>').addClass('hnews-scroll-container').css({position:'relative'});

		ourUL.css({position:'relative',overflow:'hidden'}).addClass('hnews-scroll-area');
		ourUL.hover( pause , resume );
				
		var items = [];
		var currentIdx = 0;
		var currentItem;
		var animateTimer;
		
		ourC.insertBefore(ourUL);
		ourC.append(ourUL);

		/* create arrow widgets */
		$('<div>').addClass('hnews-b-prev').appendTo(ourC).bind('click',[true],prevItem);
		$('<div>').addClass('hnews-b-next').appendTo(ourC).bind('click',[true],nextItem);		

		/* initialize all the news items and position them.  also add them to an internal
		 * array 'items' for easy index-based access.
		 */		
		$(this).children('li').each( function(i){
			$(this).css({   position:'absolute', top: settings.itemOffsetY + 'px', left: settings.itemOffsetX + 'px'});
			if (i > 0 ) $(this).hide();
			items.push( $(this) );
		});

		/* start with first item in the list.
		 * start the animation sequence.  
		 */
		currentItem = items[0];
		animateTimer = setTimeout( nextItem, settings.startDelay ); 

		function nextItem( notransition )
		{
			clearTimeout(animateTimer);
			var outboundItem = items[currentIdx];
			currentIdx++;
			if ( currentIdx == items.length ) currentIdx = 0;
			currentItem = items[currentIdx];		
			if (notransition) 
			{
				outboundItem.hide();
				currentItem.show();
			}
			else 	fadeTransition(currentItem, outboundItem, settings);
			animateTimer = setTimeout( nextItem, settings.delay );
		}
		
		function prevItem( notransition )
		{
			clearTimeout(animateTimer);
			outboundItem = items[currentIdx];
			currentIdx--;
			if ( currentIdx == -1 ) currentIdx = items.length-1;
			currentItem = items[currentIdx];		
			if (notransition) 
			{
				outboundItem.hide();
				currentItem.show();
			}
			else 	fadeTransition(currentItem, outboundItem, settings);

			animateTimer = setTimeout( nextItem, settings.delay );
			
		}
		
		
		function pause()
		{
			clearTimeout(animateTimer);
			ourUL.addClass('hnews-scroll-area-over');
		}
		
		function resume()
		{
			animateTimer = setTimeout( nextItem, settings.delay );
			ourUL.removeClass('hnews-scroll-area-over');			
		}
		
	})	
	

}	

})(jQuery);

function fadeTransition(inbound,outbound,cfg)
{
	outbound.fadeOut(cfg.speed,function(){
		inbound.fadeIn(cfg.speed,function(){
			if ( jQuery.browser.msie)	this.style.removeAttribute('filter');
		})});
}

function slideTransition(i,o,cfg)
{
	o.animate( {
		left: "-" + o.width() + "px"
	},cfg.speed,'linear', function(){
		i.css({position:'absolute', top: cfg.itemOffsetY + 'px', left: cfg.itemOffsetX + 'px'});				
		i.fadeIn(cfg.delay);				
	})
}




