(function($) {

	/**
	* @param options
	*/
	$.fn.mainnav = function(options) {

		var settings = {
			active: ".active",
			count: 0,
			$items: null,
			selector: "#mainnav",
			itemSelector: "li",
			$slider: null,
			speed: 500
		};

		var options = $.extend(settings, options);

		return this.each(function(i, e) {
			return new MainNav(e, options);
		});
	};

	/**
	* 
	*/
	function MainNav(ele, settings) {
		this.e = ele;
		this.$e = $(ele);
		this.settings = settings;

		init.call(this);

		registerEvents.call(this);
	}

	/**
	* 
	*/
	function init() {
		var obj = this;

		// Get the items
		obj.settings.$items = $(obj.settings.selector + " " + obj.settings.itemSelector);
		obj.settings.count = obj.settings.$items.length;

		// Create the slider
		obj.$e.prepend("<div class='slider'><div class='slider-inner'></div></div>");

		obj.settings.$slider = obj.$e.find(".slider").first();
	}

	/**
	* 
	*/
	function registerEvents() {
		var obj = this;

		// Add firing events


		var config = {
			over: function() {
				var ele = $(this);

				var active = obj.$e.children(obj.settings.active);

				// All items are indexed based on 1. NOT 0.
				var data = {
					newIndex: ele.prevAll().length
				};

				ele.trigger("mainnav:slide", data);
			},
			interval: 50,
			timeout: 250,
			out: function() {
				// empty
			}
		};
		obj.$e.children(obj.settings.itemSelector).hoverIntent(config);

		// Bind events
		$(document).bind("carousel:slide", function(event, data) {
			slide.call(obj, event, data);
		});
		$(document).bind("mainnav:slide", function(event, data) {
			slide.call(obj, event, data);
		});
	}

	/**
	* All items are indexed based on 1. NOT 0.
	*/
	function slide(event, data) {
		var obj = this;

		// Remove the active class
		$(obj.settings.selector + " " + obj.settings.active).each(function(i, e) {
			$(e).removeClass("active");
		});
		$(obj.settings.selector + " " + obj.settings.active + " a").each(function(i, e) {
			$(e).removeClass("active");
		});

		// Clear animation queue
		obj.settings.$slider.clearQueue();

		if (data.direction == "next" && data.newIndex < 1) {
			// Next was clicked when on last item. Temove the slider and
			// reposition to the start.
			var animateLeft = obj.settings.$slider.position().left + obj.settings.$slider.outerWidth();
			obj.settings.$slider.animate({
				left: animateLeft + "px",
				width: 0
			}, obj.settings.speed);

			obj.settings.$slider.animate({
				left: 0,
				width: 0
			}, 1);
			return;
		} else if (data.direction == "prev" && data.newIndex == obj.settings.count) {
			// Previous was called when slider is off screen. Move the slider to
			// the right of the last item before animating it on screen

			var item = $(obj.settings.$items[data.newIndex - 1]);

			var animateLeft = item.position().left + item.outerWidth();

			obj.settings.$slider.animate({
				left: animateLeft + "px",
				width: 0
			}, 1);
		} else if (data.newIndex < 1) {
			obj.settings.$slider.animate({
				left: 0,
				width: 0
			}, obj.settings.speed);
			return;
		}

		var item = $(obj.settings.$items[data.newIndex - 1]);

		// Add the active class
		item.addClass("active");
		item.find("a").addClass("active");

		// Subtract 4 from the left to compensate for the border
		// Add 8 to width to compensate for the padding
		obj.settings.$slider.animate({
			left: item.position().left - 4 + "px",
			width: item.outerWidth() + 8 + "px"
		}, obj.settings.speed);
	}
})(jQuery);
