/*
 * Visual Navigation (visualNav) v2.2 minified
 * https://github.com/Mottie/visualNav/wiki
 *
 * Copyright (c) 2011 Rob Garrison (aka Mottie & Fudgey)
 * Dual licensed under the MIT and GPL licenses.
 *
 */
(function(b) {
	b.visualNav = function(d, f) {
		var a = this;
		var tempObj;
		a.$el = b(d);
		a.$el.data("visualNav", a);
		a.win = window;
		a.$win = b(a.win);
		a.$doc = b(document);
		var h = "html, body";
		b("html, body").each(function() {
			var a = b(this).attr("scrollTop");
			b(this).attr("scrollTop", a + 1);
			if (b(this).attr("scrollTop") === a + 1) return h = this.nodeName.toLowerCase(), b(this).attr("scrollTop", a), !1
		});
		a.$body = b(h);
		a.init = function() {
			a.options = b.extend({}, b.visualNav.defaultOptions, f);
			a.content = b("." + a.options.contentClass);
			a.leftMargin = parseInt(a.content.css("margin-left"), 10);
			a.rightMargin = parseInt(a.content.css("margin-right"), 10);
			if (!b.isFunction(b.easing[a.options.easing[0]])) a.options.easing = ["swing", "swing"];
			a.$body.bind("scroll mousedown DOMMouseScroll mousewheel keyup", function(b) {
				(b.which > 0 || b.type === "mousedown" || b.type === "mousewheel") && a.$body.stop()
			});
			a.$el.find(a.options.selectedAppliedTo + (a.options.selectedAppliedTo === a.options.link ? "" : " " + a.options.link)).add(b("." + a.options.contentLinks)).click(function() {
				var c = this.tagName === "A" ? "href" : a.options.targetAttr;
				a.animate(b(this).attr(c));
				return !1
			});
			if (a.$el.find('.active').length != 0) a.$el.find('ul').css({backgroundPositionX: (a.$el.find('.active').offset().left - b(d).offset().left + (a.$el.find('.active').width() / 2) - 5 ) + 'px'});
			else a.$el.find('ul').css({backgroundPositionX: (a.$el.find('ul li:first-child').offset().left - b(d).offset().left + (a.$el.find('ul li:first-child').width() / 2) - 5 ) + 'px'});
			
			a.$win.scroll(function() {
				a.findLocation()
			}).resize(function() {
				a.findLocation()
			})
		};
		a.animate = function(c) {
			if (c !== "#" && b(c).length) {
				var e = b(c).eq(0).closest("." + a.options.contentClass);
				a.$body.stop().animate({
					//scrollLeft: Math.min(e.offset().left, a.$doc.width() - a.$win.width()) - a.leftMargin,
					scrollTop: Math.min(e.offset().top, a.$doc.height() - a.$win.height()) - a.options.topPadding
				}, {
					queue: !1,
					duration: a.options.animationTime,
					specialEasing: {
						scrollLeft: a.options.easing[0] || "swing",
						scrollTop: a.options.easing[1] || a.options.easing[0] || "swing"
					},
					complete: function() {
						if (a.options.useHash) a.win.location.hash = e[0].id
					}
				})
			}
		};
		a.findLocation = function() {
			var c, e, d, g, f, j, k, h, i = a.$win.width(),
				n = a.$win.scrollLeft(),
				o = a.$win.scrollTop(),
				p = n + i,
				l = o + a.$win.height(),
				q = a.$doc.height(),
				m = a.$el.find(a.options.selectedAppliedTo).removeClass(a.options.inViewClass);
			a.options.fitContent && a.content.width(i - a.leftMargin - a.rightMargin);
			a.$el.find(a.options.link).each(function(i) {
				g = b(this).attr(a.options.targetAttr);
				c = g === "#" || g.length <= 1 ? "" : b(g);
				c.length && (d = Math.ceil(c.offset().top), e = Math.ceil(c.offset().left), j = c.outerHeight(), f = d + j + a.options.bottomMargin, k = c.outerWidth(), h = e + k, d < l && (d + j - a.options.bottomMargin > o || f > l) && e < p && (e + k - a.options.bottomMargin > n || h > p) && m.eq(i).addClass(a.options.inViewClass))
			});
			g = l + a.options.bottomMargin >= q ? ":last" : ":first";
			m.removeClass(a.options.selectedClass);
			m.filter("." + a.options.inViewClass + g).addClass(a.options.selectedClass);
			
			if (m.filter("." + a.options.inViewClass + g) != null) {
				if (tempObj != m.filter("." + a.options.inViewClass + g)) {
			   		tempObj = m.filter("." + a.options.inViewClass + g);
			   		a.$el.find('ul').stop().animate({backgroundPositionX: (tempObj.offset().left - a.$el.find('ul').offset().left + (tempObj.width() / 2) - 5 ) + 'px'});
				}
			}
		};
		a.init();
		a.options.useHash && a.win.location.hash && setTimeout(function() {
			a.animate(a.win.location.hash)
		}, a.options.animationTime / 2);
		a.findLocation()
	};
	b.visualNav.defaultOptions = {
		link: "a",
		targetAttr: "href",
		selectedAppliedTo: "li",
		contentClass: "content",
		contentLinks: "visualNav",
		useHash: !0,
		inViewClass: "inView",
		selectedClass: "selected",
		bottomMargin: 100,
		fitContent: !1,
		topPadding: 0,
		animationTime: 1200,
		easing: ["swing", "swing"]
	};
	b.fn.visualNav = function(d) {
		return this.each(function() {
			var f = b(this).data("visualNav");
			typeof d === "string" && /^(#|\.)/.test(d) && f.animate(d);
			f || new b.visualNav(this, d)
		})
	};
	b.fn.getvisualNav = function() {
		return this.data("visualNav")
	}
})(jQuery);

