jQuery.fn.extend({
	scrollTo : function(speed, easing,cbFunc){
    	return this.each(function() {
      		var targetOffset = $(this).offset().top||0;
      		$('HTML,BODY').animate({scrollTop: targetOffset}, speed, easing, cbFunc);
    	});
  	}
});
$(function(){//console.log('LOADING '+new Date().getTime());
	$('#menu>LI>UL').parent().hover(function(){
		$(this).addClass('hover');
		$(this).children('UL:first').stop(true,true).slideDown(200);		
	},function(){
		$(this).removeClass('hover');
		$(this).children('UL:first').stop(true).slideUp(200,function(){$(this).height('auto')});
	});
	var currentMenu=$('A.current').next('UL.subMenu').get(0);
	if(currentMenu){
		var isFixed=false;
		var content=document.getElementById('content');
		var newMenu=content.insertBefore(currentMenu.cloneNode(true),content.firstChild);
		var fixedPoint=$(content).offset().top;
		var unFixedPoint=$('#footer').offset().top-newMenu.offsetHeight-60;
		var page=location.pathname.split('/').reverse()[0];
		$('A[href*='+page+']').click(function(event){
			var urlHash=(this.hash=='#')?'':this.hash;//For Safari
			$(urlHash||'BODY').scrollTo(500,'',function(){
				if(urlHash==location.hash){
					return false;
				}
				if(urlHash.indexOf('#')<0){
					urlHash='#top';
				}
				location.hash=urlHash;
			});
			return false;
		});
		$(window).scroll(function(){
			if(isFixed){
				if($(this).scrollTop()<=fixedPoint){
					newMenu.style.position='absolute';
					isFixed=false;
				}
				/*if(isFixed&&$(this).scrollTop()>=unFixedPoint){
					newMenu.style.position='absolute';
					newMenu.style.top=unFixedPoint+'px';
					isFixed=false;
				}*/
			}else{
				if($(this).scrollTop()>=fixedPoint){
					newMenu.style.position = 'fixed';
					isFixed=true;
				}	
			}
		});
	}
	$('UL.subMenu>LI').hover(function(){
		$(this).addClass('hover');
	},function(){
		$(this).removeClass('hover');
	});
	if(location.hash){
		$(location.hash).scrollTo(500);
	}
});
