(function($)
{
	SUBMENU_CLASS_NAME = 'submenu';
	SUBMENU_TIMEOUT = 100;
	SUBMENU_ANIMATION_SPEED = 'fast';
	
	SUBMENU_INT_CLASS = 'navmenu_class';
	SUBMENU_INT_PROP_IDX = 'navmenu_index';
	SUBMENU_INT_TIMER = 'navmenu_instTimer';
	SUBMENU_INT_VISIBLE = 'navmenu_showing';
	SUBMENU_INT_FULL_HEIGHT = 'navmenu_full_height';
	
	function NavMenu(nav)
	{
		this.nav = $(nav);
		this.submenus = $(nav).find('a + ul.'+SUBMENU_CLASS_NAME).css('position', 'absolute').hide().addClass(SUBMENU_INT_CLASS);
		this.menuTrig = $(this.submenus).prev().addClass(SUBMENU_INT_CLASS).attr(SUBMENU_INT_PROP_IDX, function(i) { $(this).next().attr(SUBMENU_INT_PROP_IDX, i); return i; });
		
		this.currentMenu = undefined;
		
/*		if (navigator && /^iP((o|a)d|hone)$/i.test(navigator.platform))
		{
		}
		else
		{*/
			$(nav).delegate('.'+SUBMENU_INT_CLASS, 'mouseenter mouseleave', this.handleEvent);
//		}	
	}
	
	NavMenu.prototype.handleEvent = function(event)
	{
		switch(event.type)
		{
			case 'mouseenter':
				break;
			case 'mouseleave':
				break;
		}
	};
	
	NavMenu.prototype.TriggerMenu = function(event)
	{
	};
	
	NavMenu.prototype.ScheduleClose = function(event)
	{
	};
	
	function closefn(obj) { return $.proxy( function() { this.removeData(SUBMENU_INT_VISIBLE).stop(true, false).slideUp(SUBMENU_ANIMATION_SPEED, clearTimer); }, obj); }
	function scheduleClose() { var obj = $(this); obj.data(SUBMENU_INT_TIMER, window.setTimeout(closefn(obj), SUBMENU_TIMEOUT)); }
	function clearTimer() { var obj = $(this); window.clearTimeout(obj.data(SUBMENU_INT_TIMER)); obj.removeData(SUBMENU_INT_TIMER); }
	function openSubMenu() { var smenu = $(this).next(); $.proxy(clearTimer, smenu)(); $.proxy(positionSubMenu, smenu)();
							if (!(smenu.data(SUBMENU_INT_VISIBLE))) smenu.data(SUBMENU_INT_VISIBLE, true).stop(true,true).height(smenu.data(SUBMENU_INT_FULL_HEIGHT)).slideDown(SUBMENU_ANIMATION_SPEED); }
	function positionSubMenu() { if (!(this.data(SUBMENU_INT_FULL_HEIGHT))) this.data(SUBMENU_INT_FULL_HEIGHT, this.height()).css('min-width', this.prev().outerWidth() + 'px');
							var mypos = this.prev().position(), topOffset = this.prev().outerHeight(); this.css({'left': Math.round(mypos.left), 'top': Math.round(mypos.top)+topOffset}) }
	
	$(function()
	{
		
/*		if (/^iP((o|a)d|hone)$/i.test(navigator.platform))
		{
			$(document.body).bind('touchstart', function(e)
			{
				if (e.originalEvent.touches.length==1)
				{
					console.log(e.originalEvent.touches.length);
					e.preventDefault();
					//var t = e.originalEvent.touches[0];
					//console.log('touchstart  x: ' + t.clientX + '   y: ' + t.clientY + '  Node: ' + $(document.elementFromPoint(t.clientX, t.clientY)).toArray().toString());
					
					$(document.body).bind('touchmove', function(e)
					{
						//var t = e.originalEvent.touches[0];
						e.preventDefault();
						//console.log('touchmove  x: ' + t.clientX + '   y: ' + t.clientY + '  Node: ' + $(document.elementFromPoint(t.clientX, t.clientY)).toArray().toString());
						return false;
					});
					
					$(document.body).bind('touchend', function(e)
					{
						var t = e.originalEvent.changedTouches[0];
						e.preventDefault();
						console.log('touchend  x: ' + t.clientX + '   y: ' + t.clientY + '  Node: ' + $(document.elementFromPoint(t.clientX, t.clientY)).toArray().toString());
						window.setTimeout( function() { $(document.body).unbind('touchmove'); $(document.body).unbind('touchend'); }, 10);
//						$(document.body).unbind('touchend');
						return false;
					});
					return false;
				}
				else
				{
						window.setTimeout( function() { $(document.body).unbind('touchmove'); $(document.body).unbind('touchend'); }, 10);
					return true;
				}
			});
		}
		else
		{*/
			$('a + ul.' + SUBMENU_CLASS_NAME).css('position', 'absolute').hide().prev().mouseenter(openSubMenu).mouseout(function(event)
			{
				var smenu = $(this).next(), relTgt = $(event.relatedTarget);
				if (!relTgt.is(smenu) && !relTgt.parent('a').is(this) && !relTgt.parent('ul.'+SUBMENU_CLASS_NAME).is(smenu) && smenu.data(SUBMENU_INT_VISIBLE))
				{
					$.proxy(scheduleClose,smenu)();
				}
			}).end().mouseenter(clearTimer).mouseleave(scheduleClose);
//		}
	});
})(jQuery);

