	var window_last_scrolltop = 0;
	var menu_last_position = 0;
	var window_scroll_direction = 'none';
	var menu_newtop = 0;
	var menu_oldtop = 0;
	
	var NEWMENU_current_status = 'closed';
	var NEWMENU_current_mode = 'top';
	var NEWMENU_current_permission = 1;
	

	
	var NEWMENU_BUTTON_top_width 	= '15%';
	var NEWMENU_BUTTON_top_height	= '11%';
	var NEWMENU_BUTTON_top_left		= '16%';
	var NEWMENU_BUTTON_top_right	= '0px';
	var NEWMENU_BUTTON_top_top		= '-100%';
	var NEWMENU_BUTTON_top_bottom	= '0px';

	var NEWMENU_BUTTON_bot_width	= '15%';
	var NEWMENU_BUTTON_bot_height	= '11%';
	var NEWMENU_BUTTON_bot_left		= '16%';
	var NEWMENU_BUTTON_bot_right	= '0px';
	var NEWMENU_BUTTON_bot_top		= '-10%';
	var NEWMENU_BUTTON_bot_bottom	= '0px';
	
	var NEWMENU_BUTTON_left_width	= '11%';
	var NEWMENU_BUTTON_left_height	= '15%';
	var NEWMENU_BUTTON_left_left	= '0px';
	var NEWMENU_BUTTON_left_right	= '0px';
	var NEWMENU_BUTTON_left_top		= '-85%';
	var NEWMENU_BUTTON_left_bottom	= '0px';
	
	var NEWMENU_BUTTON_right_width	= '11%';
	var NEWMENU_BUTTON_right_height	= '15%';
	var NEWMENU_BUTTON_right_left	= '89%';
	var NEWMENU_BUTTON_right_right	= '0px';
	var NEWMENU_BUTTON_right_top	= '-85%';
	var NEWMENU_BUTTON_right_bottom	= '0px';
	
	var NEWMENU_start = 0;
	
	$(document).ready(function()
	{
		NEWMENU_bind_objects();
		
		NEWMENU_show();
		
		if (NEWMENU_start == 0) {
	
		var fuori = $(window).width()+($(window).width() * 21.3 / 100);
		var maxnum1 = $(window).width() - ($(window).width() * 21.3 / 100)-1;
		var maxnum2 = $(window).height() + $(window).scrollTop() - ($(window).width() * 21.3 / 100)-1;
		
		var randomleft=Math.floor(Math.random()*maxnum1);
		var randomtop=Math.floor(Math.random()*maxnum2);
		
		$('#newmenu_main').css('left',randomleft).css('top',randomtop);

		
		}
		
		NEWMENU_button_select();
		NEWMENU_button_savecss();
		NEWMENU_set_board();

		
	

	});
	
	function NEWMENU_scroll()
	{
		var menu = $('#newmenu_main');
		var menup = $('#newmenu_main').offset();
		var window_current_scrolltop = $(window).scrollTop();
		
		menu_oldtop = menup.top - window_last_scrolltop;
		
		
		if ( window_current_scrolltop < window_last_scrolltop ) 	{ window_scroll_direction = 'up'; }
		if ( window_current_scrolltop == window_last_scrolltop ) 	{ window_scroll_direction = 'none'; }
		if ( window_current_scrolltop > window_last_scrolltop )		{ window_scroll_direction = 'down'; }
		
		menu_newtop = menu_oldtop + window_current_scrolltop;

		menu.css('top', menu_newtop );
		window_last_scrolltop = window_current_scrolltop;
		info();
		

	}


	function NEWMENU_button_savecss()
	{
		return; 
		NEWMENU_BUTTON_top_width = 		$('#newmenu_button_'+'top').css('width');
		NEWMENU_BUTTON_top_height = 	$('#newmenu_button_'+'top').css('height');
		NEWMENU_BUTTON_top_left = 		$('#newmenu_button_'+'top').css('margin-left');
		NEWMENU_BUTTON_top_right = 		$('#newmenu_button_'+'top').css('margin-right');
		NEWMENU_BUTTON_top_top = 		$('#newmenu_button_'+'top').css('margin-top');
		NEWMENU_BUTTON_top_bottom = 	$('#newmenu_button_'+'top').css('margin-bottom');
		
		NEWMENU_BUTTON_bot_width = 	$('#newmenu_button_'+'bot').css('width');
		NEWMENU_BUTTON_bot_height = $('#newmenu_button_'+'bot').css('height');
		NEWMENU_BUTTON_bot_left = 	$('#newmenu_button_'+'bot').css('margin-left');
		NEWMENU_BUTTON_bot_right = 	$('#newmenu_button_'+'bot').css('margin-right');
		NEWMENU_BUTTON_bot_top = 	$('#newmenu_button_'+'bot').css('margin-top');
		NEWMENU_BUTTON_bot_bottom = $('#newmenu_button_'+'bot').css('margin-bottom');
		
		NEWMENU_BUTTON_left_width = 	$('#newmenu_button_'+'left').css('width');
		NEWMENU_BUTTON_left_height = 	$('#newmenu_button_'+'left').css('height');
		NEWMENU_BUTTON_left_left = 		$('#newmenu_button_'+'left').css('margin-left');
		NEWMENU_BUTTON_left_right = 	$('#newmenu_button_'+'left').css('margin-right');
		NEWMENU_BUTTON_left_top = 		$('#newmenu_button_'+'left').css('margin-top');
		NEWMENU_BUTTON_left_bottom = 	$('#newmenu_button_'+'left').css('margin-bottom');
		
		NEWMENU_BUTTON_right_width = 	$('#newmenu_button_'+'right').css('width');
		NEWMENU_BUTTON_right_height = 	$('#newmenu_button_'+'right').css('height');
		NEWMENU_BUTTON_right_left = 	$('#newmenu_button_'+'right').css('margin-left');
		NEWMENU_BUTTON_right_right = 	$('#newmenu_button_'+'right').css('margin-right');
		NEWMENU_BUTTON_right_top = 		$('#newmenu_button_'+'right').css('margin-top');
		NEWMENU_BUTTON_right_bottom = 	$('#newmenu_button_'+'right').css('margin-bottom');
		

				
	}
	
	function NEWMENU_button_enlarge(quale)
	{

		button = $('#newmenu_button_'+quale);

		button.css('cursor', 'move');
		
		
		
		if ( quale == 'top' )
		{
			button.css('width', '90%');
			button.css('height', '190%');
			button.css('margin-top', '-95%');
			button.css('margin-left', '5%');
			button.css('margin-right', '5%');
			button.css('margin-bottom', '5%');

		}
		
		if ( quale == 'bot' )
		{
			button.css('width', '90%');
			button.css('height', '190%');
			button.css('margin-top', '5%');
			button.css('margin-bottom', '95%');
			button.css('margin-left', '5%');
			button.css('margin-right', '5%');
		}
		
		if ( quale == 'left' )
		{
			button.css('width', '190%');
			button.css('height', '90%');
			button.css('margin-top', '5%');
			button.css('margin-bottom', '5%');
			button.css('margin-left', '-95%');
			button.css('margin-right', '5%');
		}
		
		if ( quale == 'right' )
		{
			button.css('width', '190%');
			button.css('height', '90%');
			button.css('margin-top', '5%');
			button.css('margin-bottom', '5%');
			button.css('margin-left', '5%');
			button.css('margin-right', '95%');
		}
	}
	
	
	function NEWMENU_button_reduce(quale)
	{

		button = $('#newmenu_button_'+quale);

		button.css('cursor', 'default');

		if ( quale == 'top' )
		{
			button.css('width', 		NEWMENU_BUTTON_top_width);
			button.css('height', 		NEWMENU_BUTTON_top_height);
			button.css('margin-top', 	NEWMENU_BUTTON_top_top);
			button.css('margin-left', 	NEWMENU_BUTTON_top_left);
			button.css('margin-bottom',	NEWMENU_BUTTON_top_bottom);
			button.css('margin-right', 	NEWMENU_BUTTON_top_right);
		}
		
		if ( quale == 'bot' )
		{
			button.css('width', 		NEWMENU_BUTTON_bot_width);
			button.css('height', 		NEWMENU_BUTTON_bot_height);
			button.css('margin-top', 	NEWMENU_BUTTON_bot_top);
			button.css('margin-left', 	NEWMENU_BUTTON_bot_left);
			button.css('margin-bottom',	NEWMENU_BUTTON_bot_bottom);
			button.css('margin-right', 	NEWMENU_BUTTON_bot_right);
		}

		if ( quale == 'left' )
		{
			button.css('width', 		NEWMENU_BUTTON_left_width);
			button.css('height', 		NEWMENU_BUTTON_left_height);
			button.css('margin-top', 	NEWMENU_BUTTON_left_top);
			button.css('margin-left', 	NEWMENU_BUTTON_left_left);
			button.css('margin-bottom',	NEWMENU_BUTTON_left_bottom);
			button.css('margin-right', 	NEWMENU_BUTTON_left_right);
		}
		
		if ( quale == 'right' )
		{
			button.css('width', 		NEWMENU_BUTTON_right_width);
			button.css('height', 		NEWMENU_BUTTON_right_height);
			button.css('margin-top', 	NEWMENU_BUTTON_right_top);
			button.css('margin-left', 	NEWMENU_BUTTON_right_left);
			button.css('margin-bottom',	NEWMENU_BUTTON_right_bottom);
			button.css('margin-right', 	NEWMENU_BUTTON_right_right);
		}

	}
	
	
	function NEWMENU_button_select()
	{
		var colonna = $(window).width() / 3;
		var riga = $(window).height() / 2;
		var menu = $('#newmenu_main');
		var menup = $('#newmenu_main').offset();
		
		var menu_left = menup.left;
		var menu_width = menu.width();
		var menu_height = menu.height();
		var menu_top = menup.top ;
		
		
		if ( menu_left + menu_width  < colonna ) NEWMENU_direction_select('right');
		
		if ( menu_left > ( colonna + colonna ) ) NEWMENU_direction_select('left');
		
		if ( ( menu_left + menu_width > colonna ) && ( menu_left < ( colonna + colonna ) ) )
		{
			if ( (menu_top+(menu_height/2)) < (riga+ $(window).scrollTop()) )
			{NEWMENU_direction_select('bot');}
			else
			{NEWMENU_direction_select('top');}
		}
		
	}
	
	function NEWMENU_direction_select(quale)
	{
		NEWMENU_current_permission = 0;
		
		info();
		
		
		if ( NEWMENU_current_status == 'closed' )
		{
			$('.newmenu_main_opened_large').hide();
			$('.newmenu_links').hide();
			$('.newmenu_main').hide();
			$('.newmenu_button').hide();
			$('#newmenu_main_'+quale).show();
			$('#newmenu_button_'+quale).show();
			NEWMENU_button_reduce(quale);
		}else{
			$('.newmenu_main_opened_large').hide();
			$('#newmenu_main_'+quale+'_opened_large').show();
			newmenu_links_move(quale);
			$('.newmenu_button').hide();
			$('#newmenu_button_'+quale).show();
			NEWMENU_button_enlarge(quale);
		}
		
		NEWMENU_current_permission = 1;
		
		info();
		
	}
	
	
	
	function NEWMENU_bind_objects()
	{
		$(window).bind('resize', function() { });
		
		$(window).bind('scroll', function() { NEWMENU_scroll(); });
		
		$('#newmenu_main').height( $('#newmenu_main').width() );
		
		
		$('#newmenu_main').bind('mouseover', 		function()	{ NEWMENU_set_board(); } );
		

		$('#newmenu_button_bot').bind('mouseover', 		function()	{ NEWMENU_list_open('bot'); } );
		$('#newmenu_button_top').bind('mouseover', 		function()	{ NEWMENU_list_open('top'); } );
		$('#newmenu_button_left').bind('mouseover', 	function()	{ NEWMENU_list_open('left'); } );
		$('#newmenu_button_right').bind('mouseover', 	function()	{ NEWMENU_list_open('right'); } );
		
		$('#newmenu_main_bot_opened_large').bind('mouseout', 		function()	{ NEWMENU_list_close('bot'); } );
		$('#newmenu_main_top_opened_large').bind('mouseout', 		function()	{ NEWMENU_list_close('top'); } );
		$('#newmenu_main_left_opened_large').bind('mouseout', 		function()	{ NEWMENU_list_close('left'); } );
		$('#newmenu_main_right_opened_large').bind('mouseout', 		function()	{ NEWMENU_list_close('right'); } );
		
		$('#newmenu_logo').bind('mousedown', function () { $('#newmenu_main').draggable( "option", "disabled", true ); } );
		$('#newmenu_logo').bind('mouseover', function () { $('#newmenu_main').draggable( "option", "disabled", true ); } );
		$('#newmenu_logo').bind('mouseup', function () { $('#newmenu_main').draggable( "option", "disabled", false ); } );
		$('#newmenu_logo').bind('mouseout', function () { $('#newmenu_main').draggable( "option", "disabled", false ); } );

	}
	
	

	
	function NEWMENU_list_close(quale)
	{
		if ( NEWMENU_current_permission == 1 )
		{
			NEWMENU_current_status = 'closed';
			NEWMENU_current_mode = quale;
			
			info();
					
			$('#newmenu_links').hide();
			$('.newmenu_main_opened_large').hide();
			$('#newmenu_main_'+quale).show();
			NEWMENU_button_reduce(quale);
		}
	}
	

	
	function NEWMENU_list_open(quale)
	{
		if ( NEWMENU_current_permission == 1 )
		{
			NEWMENU_current_status = 'opened';
			NEWMENU_current_mode = quale;
			
			info();
			
			newmenu_links_move(quale);
			NEWMENU_button_enlarge(quale);
			$('.newmenu_main').hide();
			$('.newmenu_main_opened_large').hide();
			$('#newmenu_main_'+quale+'_opened_large').show();
		}
	}
	
	function NEWMENU_show()
	{
		$('#newmenu_main').draggable( { containment: 'document', scroll: true, drag: function (event, ui) { NEWMENU_drag();	}, stop: function (event, ui) {  NEWMENU_drop(); } } );
		
		
	}
	
	
	function NEWMENU_animate_hide()
	{
		var fuori = $(window).width()+($(window).width() * 21.3 / 100);
		var maxnum1 = $(window).width() - ($(window).width() * 21.3 / 100)-1;
		var maxnum2 = $(window).height() + $(window).scrollTop() - ($(window).width() * 21.3 / 100)-1;
		
		var randomleft=Math.floor(Math.random()*maxnum1);
		var randomtop=Math.floor(Math.random()*maxnum2);
		
		

		if ( NEWMENU_start == 0 )
		{
			NEWMENU_start = 1;
			//$('#newmenu_main').fadeTo(0,0);
			//$('#newmenu_main').delay(0).animate({ left: fuori }, 0, function () { $('#newmenu_main').fadeTo(0,1); }  );
		
			$('#newmenu_main').delay(0).animate({ left: fuori }, 0, function () { $('#newmenu_main').hide(); }  );
			
		}else{
			$('#newmenu_main').delay(0).animate({ left: fuori }, 2000 );
			
			
		}
		
	}

	function NEWMENU_animate_show(anim,callback)
	{
		//alert('show');
		var fetta = $(window).width()/4;
		var menuw = ($(window).width() * 21.3 / 100)-1;
		
		var fuori = $(window).width()+($(window).width() * 21.3 / 100);
		var maxnum1 = (fetta * 3)  - menuw;
		var maxnum2 = $(window).height() + $(window).scrollTop() - ($(window).width() * 21.3 / 100)-1;
		
		var randomleft=Math.floor(Math.random()*maxnum1)+fetta;
		var randomtop=Math.floor(Math.random()*maxnum2);
		
		if ( anim == 1 )
		{
			var anim1 = 1000;
			var anim2 = 1000;
		}else{
			var anim1 = 1000;
			var anim2 = 1000;
		}

		if ( NEWMENU_start == 0 )
		{
			NEWMENU_start = 1;
			//$('#newmenu_main').fadeTo(0,0);
			//$('#newmenu_main').css('top', randomtop).css('left', fuori).delay(0).animate({ top: +randomtop, left: +randomleft }, 0, function () { $('#newmenu_main').fadeTo(0,1); NEWMENU_button_select();  } );
			
			$('#newmenu_main').hide();

			
			$('#newmenu_main').css('top', randomtop).css('left', fuori).delay(0).animate({ top: +randomtop, left: +randomleft }, 0, function () { $('#newmenu_main').show(); NEWMENU_button_select(); if (callback != undefined) callback.call(); } );
			
		}else{
			
		
			$('#newmenu_main').show();
			$('#newmenu_main').css('top', randomtop).css('left', fuori).delay(anim1).animate({ top: +randomtop, left: +randomleft }, anim2, function () { NEWMENU_button_select(); if (callback != undefined) callback.call(); } );
		}
	}

	function NEWMENU_drag()
	{
		NEWMENU_button_select();
	}
	
	function NEWMENU_set_board()
	{
		var scrollTop = $(window).scrollTop();
		var main = $('#main').height();
		var win = $(window).height();
		

		if ( scrollTop == 0 && main < win )
		{
			$('#main').height($(window).height());
		}else{
			$('#main').height('');
		}
		
		//$('#fiscals_ul').css('top', ( $('#main').height() - $('#fiscals_ul').height() ));
		//$('#credits_ul').css('top', ( $('#main').height() - $('#credits_ul').height() ));

	}
	
	function NEWMENU_drop()
	{

	}

	function info()
	{
		$('#1').html('Window scroll top: '+$(window).scrollTop() );
		$('#2').html('Scroll direction: '+window_scroll_direction);
		$('#3').html('Menu oldtop: '+menu_oldtop);
		$('#4').html('Menu newtop: '+menu_newtop);
		$('#5').html('Window height: '+$(window).height());

	}


	
	function newmenu_links_move(quale)
	{
		var links = $('#newmenu_links');
		
		links.hide();
		
		if ( quale == 'right' )
		{
			links.css('margin-left', '110%');
			links.css('margin-top', '11%');
		}
		if ( quale == 'left' )
		{
			links.css('margin-left', '-90%');
			links.css('margin-top', '11%');
		}
		if ( quale == 'top' )
		{
			links.css('margin-left', '10%');
			links.css('margin-top', '-89%');
		}
		if ( quale == 'bot' )
		{
			links.css('margin-left', '10%');
			links.css('margin-top', '112%');
		}
		
		links.show();
	}
	
	

	

