$(document).ready(function() {

	mymenu.init();

});

var mymenu = {
	
	init: function() {

		var config = {    
			 sensitivity: 7, // number = sensitivity threshold (must be 1 or higher)    
			 interval: 200, // number = milliseconds for onMouseOver polling interval    
			 over: mymenu.showmenu, // function = onMouseOver callback (REQUIRED)    
			 timeout: 750, // number = milliseconds delay before onMouseOut    
			 out: mymenu.hidemenu // function = onMouseOut callback (REQUIRED)    
		};
		
		$('#menu2 ul ul li').hoverIntent( config );
		
		/*
		$('#menu2 ul ul li').hover(function() {
			
			mymenu.showmenu(this);
			
		}, function() {
			
			mymenu.hidemenu(this);
			
		});
		*/
		
	}
	
	,showmenu: function() {
		var obj = this;
		
		var height = $(obj).find('ul:first').height();
		var offset = $(obj).offset();
		var window_height = $(window).height();
		var scroll_offset = $(window).scrollTop();

		if( (offset.top + height) > (window_height + scroll_offset) ) {
			var positionfrombottom = true;
		} else {
			var positionfrombottom = false;
		}
		//alert( height + " - " + offset.top + " - " + document_height );
		
		if( positionfrombottom ) {
			$(obj).find('ul:first').css('top', '-' + (height - 22) + 'px').fadeIn('fast');
		} else {
			$(obj).find('ul:first').css('top', '0px').fadeIn('fast');
		}
		
	}
	
	,hidemenu: function() {
		
		var obj = this;
		
		$(obj).find('ul:first').hide();
		
	}
	
};
