/**
 * 
 */

var _navigationShowing = true;
var _navigationHeight = 0;
var _headerHeight = 0;

var _staticMouseTimeOut;
var _idleMouseThreshold = 7;
var _previousMousePageY = 0;
var _previousMousePageX = 0;


$(document).ready(function() {
	_navigationShowing = true;
	_navigationHeight = $('#navigation').height();
	_headerHeight = $('#header').height();
	if( navigator.userAgent.match(/Android/i) ||
		navigator.userAgent.match(/webOS/i) ||
		navigator.userAgent.match(/iPhone/i) ||
		navigator.userAgent.match(/iPod/i) ||
		navigator.userAgent.match(/iPad/i) ){
		// do nothing
	}
	else {
		$('body').mousemove(handleMouseMove);
	}	
});

function handleMouseMove(event) {
	var headerHeight = parseInt($('#navigation').css('height')) + parseInt($('#header').css('height'));
	if ( event.pageY < headerHeight ) {
		clearTimeout(_staticMouseTimeOut);
		showNavigation();
	} else {
		var vy = event.pageY - _previousMousePageY;
		var vx = event.pageX - _previousMousePageX;
		var distance = Math.sqrt(vx * vx + vy * vy);

		if ( distance  > _idleMouseThreshold ) {		
			clearTimeout(_staticMouseTimeOut);
			_staticMouseTimeOut = setTimeout('hideNavigation()',1300);
		}
		_previousMousePageY = event.pageY;
		_previousMousePageX = event.pageX;
	}
}

function showNavigation() {
	if ( _navigationShowing ) {
		return;
	}
	_navigationShowing = true;
	clearTimeout(_staticMouseTimeOut);
	
	
	$('#navigation').stop().animate({
		height: _navigationHeight
	}, 500);
	
	
	$('#navigationwrapper').stop().animate({
		'padding-top':15,
		opacity: 1
	}, 500,function() {
		if ( this.style.removeAttribute ) {
			this.style.removeAttribute("filter");
		}
	});
	
}

function hideNavigation() {
	if ( !_navigationShowing ) {
		return;
	}
	_navigationShowing = false;
	clearTimeout(_staticMouseTimeOut);
	
	$('#navigation').stop();
	$('#navigationwrapper').stop();
	$('#navigation').animate({
		height: 0
	}, 1000);
	$('#navigationwrapper').animate({
		'padding-top':2,
		opacity: 0
	}, 1000);
}

