/**
 * A function that lets you navigate trough objects. 
 * 
 * VERSION 1
 * 
 * @author Sybren Dotinga, JDI internet professionals
 * @version 0.1
 */
(function($) {
	$.fn.Navigator = function(options) {		
		options = $.extend( {
			
			defaults: {
				log: false,
				wrapper: $(this)
			},
			
			Navigator : {
				Holder: null,
				Items: null,
				HowMany: 0,
				Active: 0,
				TimeOutID: null,
				Busy: false
			},
			
			Settings : {
				NavigationID: 'Navigation',
				NavigationItemClass: 'Item',
				ItemHolderID: 'Navigator',
				ItemHolderClass: 'Item',
				Speed: 350,
				TimeOut: 1000
			},
			
			Navigation : {
				Holder: null,
				Items: null
			}
			
		}, options );
			
		init();
		
		function init() {
			
			if (typeof(window["console"]) == "undefined") {
				options.defaults.log = false;
			}
						
			//Navigator
			options.Navigator.Holder = $(options.defaults.wrapper).find('#' + options.Settings.ItemHolderID);
			options.Navigator.Items = $(options.Navigator.Holder).find('.' + options.Settings.ItemHolderClass);
			options.Navigator.HowMany = options.Navigator.Items.length;
			
			//Navigation
			options.Navigation.Holder = $(options.defaults.wrapper).find('#' + options.Settings.NavigationID );
			options.Navigation.Items = $(options.Navigation.Holder).find('.' + options.Settings.NavigationItemClass );
			
			if (options.defaults.log)
				console.info('Items in this navigator: ' + options.Navigator.HowMany );
			
			create();
			
			if (options.defaults.log)
				console.info('Actieve item: ' + options.Navigator.Active );
			
			start();
			
		};
		
		function create() {
			makeNavigation();
			setOpacity();
			setTimers();
		};	
		
		function start() {
			options.Navigator.TimeOutID = window.setTimeout( function() { activate( getNext() ) }, options.Settings.TimeOut );
		};
		
		function stop() {
			if (options.Navigator.TimeOutID) { 
				clearTimeout( options.Navigator.TimeOutID );
				options.Navigator.TimeOutID = null;
			}
		};
		
		function setTimers() {
			if ( options.Navigator.HowMany > 1 ) {
				$(options.Navigator.Items).each(function() {
					$(this).mouseleave(function(){
						if ( options.defaults.log )
							console.info( 'Mouseleave' );
						start();
					}).mouseenter(function(){
						if ( options.defaults.log )
							console.info( 'Mouseenter' );
						stop();
					});
				});
			}
		};
		
		function setOpacity() {
			var _Counter = 1;
			$(options.Navigator.Items).each(function(i){
				if ( _Counter == 1 ) {
					$(this).css( 'opacity', 1);
				} else {
					$(this).css( 'opacity', 0);
				}
				_Counter++;
			});
		}
		
		function makeNavigation() {
			
			$(options.Navigation.Items).each(function(i){
				$(this).click(function(){
					activate(i);
					return false;
				});
			});
			
			if (options.defaults.log)
				console.info('Made all items navigation clickable');
			
		};
		
		function activate( ItemID ) {

			if ( options.Navigator.Active != ItemID ) {				
				if ( options.Navigator.Busy === false ) {
					
					options.Navigator.Busy = true;
					window.clearTimeout(options.Navigator.TimeOutID);
				
					//Oude inactief maken
					deactivate();
					
					//Actief maken
					$(options.Navigator.Items[ItemID]).animate({
						opacity: 1
					}, {
						duration: options.Settings.Speed,
						complete: function() {
							$(this).removeClass('Inactive');	
							$(this).addClass('Active');
							options.Navigator.Active = ItemID;
							options.Navigator.Busy = false;
							$(options.Navigation.Items[ItemID]).removeClass('Inactive');	
							$(options.Navigation.Items[ItemID]).addClass('Active');
						}
					});
					start();
				}
			}
		};
		
		function deactivate() {
			$(options.Navigation.Items[options.Navigator.Active]).removeClass('Active');
			$(options.Navigator.Items[options.Navigator.Active]).animate({
				opacity: 0
			}, {
				duration: options.Settings.Speed,
				complete: function() {
					$(this).removeClass('Active');
				}
			});
		};
		
		function getNext() {
			
			if ( options.defaults.log )
				console.info('Current active: ' + options.Navigator.Active );
			
			var _Counter = 0;
			var _Found = false;
			
			$(options.Navigator.Items).each(function(){
				
				if ( _Counter == options.Navigator.Active ) {
					_Found = true;
				}
				_Counter++;
				
				if ( _Found == true ) {
					if ( options.defaults.log )
						console.info('Next active: ' + _Counter );
					return false;
				}
				
			});
			
			if ( _Found === false || _Counter >= options.Navigator.HowMany) {
				if ( options.defaults.log )
					console.info('Het was de laatste, overnieuw beginnen!');
				return '0';
			}
			
			return _Counter;
		}
	}
})(jQuery);
