$(document).ready(function(){
	var w = $('#menu-container').css({overflow:'hidden'}).css('width').replace(/[^\d]/g,'');
	var m = $('#menu').get(0);
	var currentArrowInterval;
	var currentArrowDir;
	var whileArrowDown = function() {
		var currPos = m.offsetLeft
		var inc = 5;
		var newPos = Math.max(w-m.offsetWidth,Math.min(0,currPos+currentArrowDir*inc));
		m.style.left = newPos+'px';
	}
	var arrowMouseDown = function(dir) {
		$('html').bind('mouseup', arrowMouseUp);
		currentArrowDir = dir;
		whileArrowDown();
		currentArrowInterval = setInterval(whileArrowDown, 20);
	}
	var arrowMouseUp = function(event){
		$('html').unbind('mouseup', arrowMouseUp);
		clearInterval(currentArrowInterval);
	};
	$('#scroll-left').css('display','block').bind('mousedown', function() {
		arrowMouseDown(1);
		return false;
	});
	$('#scroll-right').css('display','block').bind('mousedown', function() {
		arrowMouseDown(-1);
		return false;
	});
	$('#menu ul').each(function() {
		//alert(this.tagName + this.offsetWidth);
		this.orig_width = this.offsetWidth;
		if(!$(this).parent('li').hasClass('current')) {
			$(this).css({
				width:'1px',
				overflow:'hidden'
			})
		}
	});
	$('#menu li li').click(function(event){
		event.stopPropagation();
		this.style.overflow='hidden';
		$('#menu li li').removeClass('current');
		$(this).addClass('current');
		var a = $('a',this).get(0);
		a.blur();
		location.href = a.href;
	});
	$('#menu > li').each(function(i){		
		$(this).click(function(event) {
			event.stopPropagation();
			event.preventDefault();
			var jq = $('ul',this);
			var a = $('a',this).get(0);
			a.blur();
			var newW;
			if (jq.length) {
				$('#menu > li.current ul').animate({width:1},600,function(){$(this).parent('li').removeClass('current')});
				if (jq[0].offsetWidth > 1) {
					newW = 1;
					$(this).removeClass('current');
				} else {
					newW = jq[0].orig_width;
					$(this).addClass('current');
				}
				jq.animate({width:newW},600,function(){
					jq.css({overflow:'hidden'});
					if (newW != 1) {
						location.href = a.href;
					}
				});
			}
		});
	});
});
