var NavAnimation = (function() {
	var isSetup = false, items, i = 0, elem, doneCallback, stripes, arrows, stripeWidths = [], 
		selectedIndex, isAnimated;
	
	function setup() {
		items = $('.nav');
		items.css('opacity', 0.0).show();
		stripes = $('.stripe');
		
		stripes.each(function(i) {
			stripeWidths.push(parseInt($(this).width()));
			$(this).css('width', '0px');
		});
		
		arrows = $('.arrow');
		
		items
			.mouseover(function() {
				var index = items.index(this);
				
				if (index != selectedIndex) {
					animateOn(index, false);
				}
			})
			.mouseout(toggleOff)
			.click(function() {
				GLOBAL.toggle(1);
				select(items.index(this));
			});
		
		isSetup = true;
		return isSetup;
	}
	
	function showItems() {		
		if (i < items.length) {
			items.eq(i).stop().animate({
				opacity	: 1.0
			}, 'fast', function() {
				i++;
				showItems();
			});
		} else {
			doneCallback();
		}
	}
	
	function go(callback) {
		doneCallback = callback;
		
		if (setup()) {
			showItems();
		}
	}
	
	function toggleOff() {
		stripes.each(function(i) {
			if (i !== selectedIndex) {
				elem = $(this);
				
				elem.stop().css('width', '0px');
				elem.parent().find('.arrow').hide();				
			} 
		});		
	}
	
	function showArrow(node) {		
		node.parent().find('.arrow').show();
	}
	
	function animateOn(index, doesShowArrow) {
		var stripe = items.eq(index).find('.stripe');
		
		if (parseInt(stripe.width(), 10) > 0) {
			stripe.show();
			stripe.css('width', stripeWidths[index] + 'px');
			if (doesShowArrow) {
				showArrow($(this));
			}			
		} else {
			stripe.stop().animate({
					width: stripeWidths[index] + 'px'
				}, 'medium', function() {
					if (doesShowArrow) {
						showArrow($(this));
					}
				}
			);			
		}		
		
		isAnimated = index;
	}
	
	function select(index) {
		items.each(function(i) {
			if (i !== index) {
				stripes.eq(i).css('width', '0px');
				stripes.eq(i).hide();
				arrows.eq(i).hide();				
			}
		});
		selectedIndex = index;
		
		stripes.eq(index).stop();
		animateOn(index, true);
		
		PhotoAnimation.loadThumbs(selectedIndex);
	}
	
	var self = {};
	self.go = go;
	self.select = select;
	
	return self;
})();