﻿$(function() {
		   
    $.fn.fadeInHandler = function(speed, callback) {
		if ($.browser.msie && $.browser.version < 9) {
			$(this).show();
		} else {
			$(this).fadeIn();
		}
	};
		   
	var autoScroll = false;
	
	function moveSide() {
		var bH = $(window).height();
		var sH = $("#Nav").height();
		var wT = $(window).scrollTop();
		var offset = $("#Nav").position();
		var sO = offset.top;
		var tolerance = 10;
		
		if ((sO + sH + tolerance) > (wT + bH)) {
			var pos = wT + bH - sH - tolerance;
			if (pos > 751) {
    			$('#Nav').stop().animate({ top: pos + "px" }, { queue: false, duration: 500, easing: 'easeInOutSine' });
			}
		}
		
		if ((sO - tolerance) < wT) {
			var pos = wT + tolerance;
			$('#Nav').stop().animate({ top: pos + "px" }, { queue: false, duration: 500, easing: 'easeInOutSine' });
		}	
	}
	
	moveSide();
	
	$(window).scroll(function() {
		if (autoScroll == false) {
			moveSide();
		}
	});
	
	$("#Nav a").click(function() {
		autoScroll = true;
		var linkClass = $(this).attr("class");
		if ($(this).hasClass("top")) {
			var pos = 751;
		} else if ($(this).hasClass("about")) {
			var offset = $("a#" + linkClass).offset();
			var pos = offset.top - 15;
		} else if ($(this).hasClass("products")) {
			var offset = $("a#" + linkClass).offset();
			var pos = offset.top - 15;
		} else if ($(this).hasClass("history")) {
			var offset = $("a#" + linkClass).offset();
			var pos = offset.top - 15;
		} else if ($(this).hasClass("order")) {
			var offset = $("a#" + linkClass).offset();
			var pos = offset.top - 15;
		}
		
		if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
		  var $target = $(this.hash);
		  $target = $target.length && $target || $('[name=' + this.hash.slice(1) +']');
		  if ($target.length) {
			var targetOffset = $target.offset().top - 25;
			$('html,body').animate({scrollTop: targetOffset}, {duration: 500 , easing: 'easeInOutSine', complete: function() {
				$('#Nav').animate({ top: pos + "px" }, 
            { queue: false, duration: 500, easing: 'easeInOutSine', complete: function() {
				autoScroll = false;
			}});
            }});
		   return false;
		  }
		}
	});

});

