/*jslint browser: true, onevar: true, undef: true, nomen: true, eqeqeq: true, plusplus: true, bitwise: true, regexp: true, newcap: true, immed: true, strict: true */
"use strict";
(function ( ) {
	
	var menuItem,		// Menu item behaviour
	    getPosition,	// Get the position of a submenu
	    menuItems,		// List of menuItem instances
	    activeItem,		// The menuItem instance of the active item
	    $menuItems,		// List of menu item jquery objects
	    $subMenus;		// List of submenu jquery objects
	
	getPosition	=	function ($el, $submenu) {
		var $menu,		// jQuery object of the menu
		    pos,		// Position of the submenu
		    width,		// Width of the submenu
		    menuWidth;		// Width of the menu
		
		width		=	$submenu.width();
		$menu		=	$("#menu");
		menuWidth	=	$menu.width() - 5;
		pos		=	Math.ceil($el.offset().left) - Math.ceil($menu.offset().left) + (parseInt($el.find("a").css("paddingLeft"), 10) - parseInt($submenu.find(".item:first a").css("paddingLeft"), 10));

		while (pos + width > menuWidth) {
			pos	-=	1;
		}
		
		return pos;
	};
	
	menuItem	=	function ($el) {
		var status,		// Status of the menu item (open or closed)
		    methods,		// The methods of the menu item
		    hasSub,		// Does the item have a submenu
		    $subMenu;		// jQuery object of the submenu
		
		methods	=	{
			// Initialize the menu item
			"init":			function ( ) {
				$subMenu	=	$(".sub_" + $el.data("submenu"));
				if ($subMenu[0]) {
					hasSub	=	true;
				}
				
				methods.status("closed");
				
				$el.bind("mouseover", methods.over);
				$el.bind("mouseout", methods.out);
				$subMenu.bind("mouseover", methods.over);
				$subMenu.bind("mouseout", methods.out);
				
				return methods;
			},
			// Activate the menu item
			"over":			function ( ) {
				methods.status("open");
				$el.addClass("hover");
				
				if (!$el.hasClass("active")) {
					activeItem.out();
				}
				
				if (hasSub) {
					$subMenu
						.css({
							"left":		getPosition($el, $subMenu) + "px"
						})
						.show();
				}
			},
			// Deactivate the menu item
			"out":			function (e, timed) {
				if (timed) {
					if (methods.status() === "closed") {
						$subMenu.hide();
					}
				} else {
					methods.status("closed");
					$el.removeClass("hover");
					setTimeout(function ( ) {
						var showActive, i;
						showActive	=	true;
						methods.out(e, true);
						for (i = 0; i < menuItems.length; i += 1) {
							if (menuItems[i].status() === "open") {
								showActive	=	false;
							}
						}
						if (showActive && activeItem.status() === "closed") {
							activeItem.over();
						}
					}, 100);
				}
			},
			// Get or set the status of the menu item
			"status":		function (arg) {
				if (arg) {
					status	=	arg;
					return methods;
				} else {
					return status;
				}
			}
		};
		
		methods.init();
		
		return methods;
	};
	
	menuItems	=	[];
	$menuItems	=	$("#menu .item");
	$subMenus	=	$("#submenu .submenu");
	
	$menuItems.each(function ( ) {
		var item	=	menuItem( $(this) );
		if ($(this).hasClass("active")) {
			activeItem		=	item;
			activeItem.status("open");
			item.over();
		}
		menuItems.push(item);
	});
	
}());

