(function($) {
	$.fn.Header = function(options) {
		options = $.extend( {
			
			defaults: {
				log: false
			},
			core: {
				wrapper: $(this),
				ItemHolder: null,
				Items: new Array(),
				currentActive: 0,
				NavLeft: null,
				NavRight: null,
				Busy: false,
				TimeOutID: -1
			},
			
			ItemHolder: 'Slider',
			ItemIndentifier: 'Item',
			navLeftID: 'Left',
			navRightID: 'Right',
			Width: 904,
			TimeOut: 4000,
			AnimationTime: 500,
			AutoMove: true
			
		}, options );
		
		init();
		
		function init() {
			
			if (typeof(window["console"]) == "undefined") {
				options.defaults.log = false;
			}
			
			if ( options.defaults.log )
				console.info( 'Initialising Header for DIV with ID "' + options.core.wrapper.attr('id') + '"' );
			
			options.core.ItemHolder = $( '#' + options.core.wrapper.attr( 'id' ) + ' .' + options.ItemHolder );
			options.core.Items = $( options.core.ItemHolder ).find( '.' + options.ItemIndentifier );
			options.core.NavLeft = $( options.core.wrapper ).find( '#' + options.navLeftID );
			options.core.NavRight = $( options.core.wrapper ).find( '#' + options.navRightID );
			
			setPositions();
			setNavigation();
			
			if ( options.core.Items.length > 0 ) {
			
				if ( options.defaults.log )
					console.info( 'There are ' + options.core.Items.length + ' item(s) in this header' );
				
				if ( options.AutoMove === true ){
					setTimer();
					start();
				}
				
			} else {
				if ( options.defaults.log )
					console.info( 'No items in this header' );
			}
			
		};
		
		function setPositions() {
			var Left = 0;
			$(options.core.Items).each(function() {
				$(this).css( 'left', Left + 'px' );
				Left += options.Width;
			});
		};
		
		function move( Item ) {
			if ( options.AutoMove === true ) {
				stop();
			}
			options.core.Busy = true;

			var NewPos = Item * options.Width;
			
			$( options.core.ItemHolder ).animate({
				scrollLeft: NewPos + 'px' 
			}, {
				duration: options.AnimationTime,
				specialEasing: {
					width:  'easeOutBounce'
				},
				complete: function() {
					options.core.Busy = false;
					if ( options.AutoMove === true ) {
						start();
					}
				},
				queue: false
			});
			
			options.core.currentActive = Item;
		};
		
		function setNavigation() {
			
			if ( options.core.Items.length > 1 ) {
				$(options.core.NavLeft).css( 'display', 'block'  );
				$(options.core.NavRight).css( 'display', 'block'  );
			} else {
				return false;
			}
			
			$(options.core.NavLeft).click(function(ev){
				ev.preventDefault();	
				if ( options.core.Busy === true ) { 
					return false;
				}
				setPrevious();
			});
			
			$(options.core.NavRight).click(function(ev){
				ev.preventDefault();
				if ( options.core.Busy === true ) { 
					return false;
				}
				setNext();
			});
		};
		
		function setNext() {
					
			var Next = options.core.currentActive + 1; 
			if ( Next > ( options.core.Items.length - 1 ) ) {
				Next = 0;
			}
			
			if ( options.defaults.log )
				console.info( 'Move to item ' + ( Next + 1 ) );
			
			move( Next );
		}
		
		function setPrevious() {
			
			var Prev = options.core.currentActive - 1; 
			if ( Prev < 0  ) {
				Prev = options.core.Items.length - 1;
			}
			
			if ( options.defaults.log )
				console.info( 'Move to item ' + ( Prev + 1 ) );
			
			move( Prev );
		}
		
		function start() {
			
			if ( options.core.Busy === true )
				return false;
			
			options.core.TimeOutID = window.setTimeout( function() { setNext(); }, options.TimeOut );
			if ( options.defaults.log )
				console.info( 'Timer set with ID ' + options.core.TimeOutID );
		};
		
		function stop() {
			if (options.core.TimeOutID) { 
				clearTimeout( options.core.TimeOutID );
				if ( options.defaults.log )
					console.info( 'Cleared timeout from ID ' + options.core.TimeOutID );
				options.core.TimeOutID = null;
			}
		};
		
		function setTimer() {
			if ( options.AutoMove === true ) {
				
				if ( options.defaults.log )
					console.info( 'Timer was set to ' + options.TimeOut + 'ms' );
				
				$(options.core.ItemHolder).mouseleave(function(){
					if ( options.defaults.log )
						console.info( 'Mouseleave' );
					start();
				}).mouseenter(function(){
					if ( options.defaults.log )
						console.info( 'Mouseenter' );
					stop();
				});
			} else {
				if ( options.defaults.log )
					console.info( 'There was not timer set!' );
			}
		};
		
	};
})(jQuery);
