/**
 * @author gavinwilliams
 */

var LL = LL || {};

LL.Header = (function($){
	
	var View = {
		$highlight: '',
		$current: '',
		$navigation: '',
		enhance: function enhance() {
			if(this.$current.length){
				this._setHighlightWidth();
				this._setHighlightPosition();
				this._showHighlight();
			}
		},
		_getCurrent: function _getCurrent(){
			return this.$navigation.find('.current');
		},
		_appendHighlight: function _appendHighlight(){
			this.$navigation.append('<span class="highlight"></span>');
		},
		_setHighlightWidth: function _setHighlightWidth(){
			this.$highlight.width(this.$current.width() - 1);
		},
		_setHighlightPosition: function _setHighlightPosition(){
			var left = this.$current.position();
			this.$highlight.css('left', (left.left - 1));
		},
		_showHighlight: function _showHighlight(){
			this.$highlight.css('visibility', 'visible');
		},
		init: function init(){
			this.$navigation = $('#primaryNav');
			this.$current = this._getCurrent();
			
			this._appendHighlight();
			
			this.$highlight = this.$navigation.find('span.highlight');
			
			this.enhance();
		}
	},
	Carousel = {
		$banners: '',
		interval: '',
		enhance: function enhance(){
			this.$banners.find('li:not(:first-child)').css('display', 'none');
			this.$banners.find('li').find('span').css('opacity', 0);
			this.$banners.find('li:first-child').find('span').animate({bottom: '0em', opacity: 0.8}, 'slow');
			this.setInt();
		},
		next: function next(){
			var self = LL.Header.getCarousel(), $next = self.$banners.find('li:visible').next('li'), $first = self.$banners.find('li:first-child'), $current = self.$banners.find('li:visible');
			
			if ($next.length) {
				$current.find('span').animate({bottom: '-2.5em', opacity: 0}, 'slow', function(){
					$next.fadeIn(function(){
						$(this).find('span').animate({bottom: 0, opacity: 0.8}, 'slow');
						$current.css('display', 'none');
					});
				});

			} else {
				$current.find('span').animate({bottom: '-2.5em', opacity: 0}, 'slow', function(){
					$first.css('display', 'block');
					$current.fadeOut(function(){
						$first.find('span').animate({bottom: 0, opacity: 0.8}, 'slow');
					});
				});
			}
		},
		setInt: function setInt(){
			var self = this;
			this.interval = setInterval(self.next, 10000);
		},
		init: function init(){
			this.$banners = $('#header-html ul.banners');
			this.enhance();
		}
	}
	
	return {
		init: function init(){
			View.init();
			Carousel.init();
		},
		getCarousel: function(){
			return Carousel;
		}
	}
	
})(jQuery);