var S = {
	
	init: function() {
		S.HorzBar.run($('#horz-bar'));
	},
	
	HorzBar: {
		
		/**/
		_settings: {
			tab: {
				current: 0
			},
			counter: {
				prPage: 4,
				total: 0,
				current: 1
			}
		},
		
		/**/
		run: function(selector) {
			S.HorzBar.initTabs($('.horz-bar-middle', selector));
			S.HorzBar.setCounter($('.horz-bar-middle', selector))
			S.HorzBar.setNavigation($('.horz-bar-middle', selector));
			S.HorzBar.showNodes(1, false);
		},
		
		/* Runs the tabs
		*/
		initTabs: function(selector) {
			selector.tabs({
				fx: { opacity: 'toggle' },
				select: function(event, ui) {
					S.HorzBar._settings.tab.current = ui.index;
					S.HorzBar._settings.counter.current = 1;
					S.HorzBar.setCounter(selector);
					S.HorzBar.showNodes(1, false);
				}
			});
		},
		
		/* Binds the navigation
		*/
		setNavigation: function(selector) {
			var $next = $('.horz-bar-navigation .horz-bar-navigation-next', selector);
			var $prev = $('.horz-bar-navigation .horz-bar-navigation-prev', selector);
			
			$next.bind('click', function() {
				S.HorzBar.Event.next();
				return false;
			});
			
			$prev.bind('click', function() {
				S.HorzBar.Event.prev();
				return false;
			});
		},
		
		/* Sets the counter in navigation 
		*/
		setCounter: function(selector) {
			// Select the counter display
			var $counter = $('.horz-bar-navigation .horz-bar-navigation-counter', selector);
			// Get the current selected tab
			var $tab = S.HorzBar._settings.tab.current;
			// Count how many nodes is inside the current tab
			var $nodes = $('.horz-bar-middle .ui-tabs-panel:eq(' + $tab + ') .horz-bar-node').size();
			
			// Current page
			var $current = S.HorzBar._settings.counter.current;
			// now do a little math and count the total
			var $total = S.HorzBar._settings.counter.total = Math.ceil($nodes / S.HorzBar._settings.counter.prPage);
			
			// add the result
			$counter.html($current + '/' + $total);
		},
		
		showNodes: function(page, animate) {
			
			// Get the current selected tab
			var $tab = S.HorzBar._settings.tab.current;
			// calculate which nodes to show next
			var $above = (page * S.HorzBar._settings.counter.prPage);
			var $under = (page - 1) * S.HorzBar._settings.counter.prPage;
			var $nodes = $('.horz-bar-middle .ui-tabs-panel:eq(' + $tab + ') .horz-bar-node');
			
			var $total = $nodes.size();
			
			// fetch the ones we want to keep
			var $keep = $nodes.slice($under, $above);
			
			$keep.filter(':last').addClass('last');
			
			
			if(animate) {
				$nodes.filter('.shown').fadeOut(300, function() {
					$nodes.removeClass('shown');
					$keep.fadeIn(300).addClass('shown');
				});
			} else {
				$nodes.hide();
				$keep.show();
				$keep.addClass('shown');
			}
		},
		
		/* Events
		*/
		Event: {
			next: function() {
				if(S.HorzBar._settings.counter.current == S.HorzBar._settings.counter.total) {
					/* no more to do */
				} else {
					S.HorzBar._settings.counter.current++;
					S.HorzBar.setCounter($('#horz-bar .horz-bar-middle'));
					S.HorzBar.showNodes(S.HorzBar._settings.counter.current, true);
				}
			},
			prev: function() {
				if(S.HorzBar._settings.counter.current == 1) {
					/* no more to do */
				} else {
					S.HorzBar._settings.counter.current--;
					S.HorzBar.setCounter($('#horz-bar .horz-bar-middle'));
					S.HorzBar.showNodes(S.HorzBar._settings.counter.current, true);
				}
			}
		}
	}
	
};

jQuery(document).ready(function($) {
	S.init();
});

