/* Visual Navigation - Rob Garrison - v2.2 - 2011-04-10 */
/* Modified by Akin Bilgic for use on CGGallery.com */

(function($){
$.visualNav = function(el, options){

	var base = this;
	base.$el = $(el);
	base.$el.data("visualNav", base);
	base.win = window;
	base.$win = $(base.win);
	base.$doc = $(document);
	var scrollElement = 'html, body';
	$('html, body').each(function(){
		var initScrollTop = $(this).attr('scrollTop');
		$(this).attr('scrollTop', initScrollTop + 1);
		if ($(this).attr('scrollTop') === initScrollTop + 1) {
			scrollElement = this.nodeName.toLowerCase();
			$(this).attr('scrollTop', initScrollTop);
			return false;
		}
	});
	base.$body = $(scrollElement);

	base.init = function(){
		base.options = $.extend({},$.visualNav.defaultOptions, options);
		base.content = $('.' + base.options.contentClass);
		base.leftMargin = parseInt( base.content.css('margin-left'), 10);
		base.rightMargin = parseInt( base.content.css('margin-right'), 10);

		if (!$.isFunction($.easing[base.options.easing[0]])) { base.options.easing = ['swing', 'swing']; }

		base.$body.bind('scroll mousedown DOMMouseScroll mousewheel keyup', function(e){
			if ( e.which > 0 || e.type === 'mousedown' || e.type === 'mousewheel' ){
				base.$body.stop();
			}
		});

		var links = base.options.selectedAppliedTo + (base.options.selectedAppliedTo === base.options.link ? '' : ' ' + base.options.link);
		base.$el.find(links)
			.add( $('.' + base.options.contentLinks) )
			.click(function(){
				var att = (this.tagName === "A") ? 'href' : base.options.targetAttr;
				base.animate($(this).attr(att));
				return false;
			});
		base.$win
			.scroll(function(){ base.findLocation(); })
			.resize(function(){ base.findLocation(); });
	};

	base.animate = function(sel){
		if (sel !== '#' && $(sel).length) {
			var $sel = $(sel).eq(0).closest('.' + base.options.contentClass);
			base.$body.stop().animate({
				scrollLeft : Math.min( $sel.offset().left, base.$doc.width() - base.$win.width() ) - base.leftMargin,
				scrollTop  : Math.min( $sel.offset().top, base.$doc.height() - base.$win.height() )
			},{
				queue         : false,
				duration      : base.options.animationTime,
				specialEasing : {
					scrollLeft  : base.options.easing[0] || 'swing',
					scrollTop   : base.options.easing[1] || base.options.easing[0] || 'swing'
				},
				complete      : function(){
					if (base.options.useHash) { base.win.location.hash = $sel[0].id; }
				}
			});
		}
	};

	base.findLocation = function(){
		var tar, locLeft, locTop, sel, elBottom, elHeight, elWidth, elRight,
		winWidth = base.$win.width(),
		winLeft = base.$win.scrollLeft(),
		winTop = base.$win.scrollTop(),
		winRight = winLeft + winWidth,
		winBottom = winTop + base.$win.height(),
		docHeight = base.$doc.height(),
		el = base.$el.find(base.options.selectedAppliedTo).removeClass(base.options.inViewClass);
		if (base.options.fitContent) {
			base.content.width( winWidth - base.leftMargin - base.rightMargin );
		}
		base.$el.find(base.options.link).each(function(i){
			sel = $(this).attr(base.options.targetAttr);
			tar = (sel === "#" || sel.length <= 1) ? '' : $(sel);
			if (tar.length) {
				locTop = Math.ceil(tar.offset().top);
				locLeft = Math.ceil(tar.offset().left);
				elHeight = tar.outerHeight();
				elBottom = locTop + elHeight + base.options.bottomMargin;
				elWidth = tar.outerWidth();
				elRight = locLeft + elWidth;
				// in view class
				if ( locTop < winBottom && ( locTop + elHeight - base.options.bottomMargin > winTop || elBottom > winBottom ) &&
				locLeft < winRight && ( locLeft + elWidth - base.options.bottomMargin > winLeft || elRight > winRight ) ) {
					el.eq(i).addClass(base.options.inViewClass);
				}
			}
		});
		sel = ( winBottom + base.options.bottomMargin >= docHeight ) ? ':last' : ':first';
		el.removeClass(base.options.selectedClass);
		el.filter('.' + base.options.inViewClass + sel).addClass(base.options.selectedClass);
	};

	base.init();
	if (base.options.useHash && base.win.location.hash) {
		setTimeout(function(){
			base.animate(base.win.location.hash);
		}, base.options.animationTime/2);
	}
	base.findLocation();
};

$.visualNav.defaultOptions = {
	link              : 'a',
	targetAttr        : 'href',
	selectedAppliedTo : 'a',
	contentClass      : 'section',
	contentLinks      : 'visualNav',
	useHash           : false,

	inViewClass       : 'inview',
	selectedClass     : 'active',

	bottomMargin      : 100,
	fitContent        : false,

	animationTime     : 700,
	easing            : [ 'swing', 'swing' ]
};

$.fn.visualNav = function(options){
	return this.each(function(){
		var nav = $(this).data('visualNav');
		if (typeof options === "string" && /^(#|\.)/.test(options)) {
			nav.animate(options);
		}
		if (nav) { return; }
		(new $.visualNav(this, options));
	});
};

$.fn.getvisualNav = function(){
	return this.data("visualNav");
};

})(jQuery);

$(document).ready(function(){
	$('#menu').visualNav();
});
