//config
$float_speed=1000; //milliseconds
$float_easing="easeOutQuint";

//cache vars
$menu=$("#menu");
$menu_menu=$("#menu");

$(window).load(function() {
	menuPosition=$('#menu').position().top;
	FloatMenu();
	$menu.hover(
		function(){ //mouse over
			$menu_label.fadeTo($menu_fade_speed, 1);
			$menu_menu.fadeIn($menu_fade_speed);
		},
		function(){ //mouse out
			$menu_label.fadeTo($menu_fade_speed, $closed_menu_opacity);
			$menu_menu.fadeOut($menu_fade_speed);
		}
	);
});

$(window).scroll(function () { 
	FloatMenu();
});

function FloatMenu(){
	var scrollAmount=$(document).scrollTop();
	var newPosition=menuPosition+scrollAmount;
	if($(window).height()<$menu.height()+$menu_menu.height()){
		$menu.css("top",menuPosition);
	} else {
		$menu.stop().animate({top: newPosition}, $float_speed, $float_easing);
	}
}       
$(function() {
     function filterPath(string) {
                    return string
                        .replace(/^\//,'')
                        .replace(/(index|default).[a-zA-Z]{3,4}$/,'')
                        .replace(/\/$/,'');
                }
                var locationPath = filterPath(location.pathname);
                var scrollElem = scrollableElement('html', 'body');
                $('a[href*=#]').each(function() {
                        var thisPath = filterPath(this.pathname) || locationPath;
                        if (  locationPath == thisPath
                                && (location.hostname == this.hostname || !this.hostname)
                                && this.hash.replace(/#/,'') ) {
                                        var $target = $(this.hash), target = this.hash;
                                        if (target) {
                                                var targetOffset = $target.offset().top;
                                                $(this).click(function(event) {
                                                        event.preventDefault();
                                                        $(scrollElem).animate({scrollTop: targetOffset}, 1500, function() {
                                                                location.hash = target;
                                                        });
                                                });
                                        }
                        }
                });
                function scrollableElement(els) {
                        for (var i = 0, argLength = arguments.length; i <argLength; i++) {
                                var el = arguments[i],
                                $scrollElement = $(el);
                                if ($scrollElement.scrollTop()> 0) {
                                        return el;
                                } else {
                                        $scrollElement.scrollTop(1);
                                        var isScrollable = $scrollElement.scrollTop()> 0;
                                        $scrollElement.scrollTop(0);
                                        if (isScrollable) {
                                                return el;
                                        }
                                }
                        }
                        return [];
                }
        });
