/////////////////////////////////////////////////////////////////
//Project:	Webshop
//Author:	Nikos Tsiligaridis
//Version:	1.0
/////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////
// Globals
/////////////////////////////////////////////////////////////////
var arr_menu_chain = new Array("root"); // Currently open menus
var arr_items_on = new Array(); // Turned "ON" items
var last_opened_menu = null;
var last_item_hovered = null;
var g_menu_timer = null;

/////////////////////////////////////////////////////////////////
// Init cat menu
/////////////////////////////////////////////////////////////////
function menu_init()
{

}

/////////////////////////////////////////////////////////////////
// Menu item MOUSE OVER
/////////////////////////////////////////////////////////////////
function on_item_mouse_over(_this, target_menu_id)
{
	var obj_target_menu;
	var container_id = $(_this).attr("item_cont");

	// Clear timer
	clearTimeout(g_menu_timer);

	// Get target menu obj
	if(target_menu_id != "")
		obj_target_menu = $("#" + target_menu_id);

	$(_this).addClass("menu_item_on");

	// If mouse didnt go to the newly opened menu
	// if mouse didnt hover to other elements of the same menu (bubble)
	// Hide all subsequent menus
	if(last_opened_menu != container_id && last_opened_menu != 0)
		submenu_hide_after(container_id);

	last_item_hovered = _this.id;

	// If points to menu
	if(obj_target_menu)
	{
		submenu_show(obj_target_menu, _this);

		// Log last opened menu
		last_opened_menu = target_menu_id;
	}
}

/////////////////////////////////////////////////////////////////
// Menu item MOUSE OUT
/////////////////////////////////////////////////////////////////
function on_item_mouse_out(_this, target_menu_id)
{
	var container_id = $(_this).attr("item_cont");

	$(_this).removeClass("menu_item_on");

	g_menu_timer = setTimeout(submenu_hide_all, 1000);
}

/////////////////////////////////////////////////////////////////
// Show submenu in correct position, add to chain
/////////////////////////////////////////////////////////////////
function submenu_show(obj_submenu, obj_item)
{
	arr_menu_chain[arr_menu_chain.length] = obj_submenu.attr("id");

	//$(obj_submenu).css("left", $(obj_item).offset().left + $(obj_item).width() - 10);
	//$(obj_submenu).css("top", $(obj_item).offset().top + $(obj_item).height()/2);
	$(obj_submenu).css("left", $(obj_item).parent().offset().left + $(obj_item).width() + 3);
	$(obj_submenu).css("top", $(obj_item).offset().top);

	$(obj_submenu).show();
}

/////////////////////////////////////////////////////////////////
// Hide and remove from chain all subsequent menus
/////////////////////////////////////////////////////////////////
function submenu_hide_after(menu_id)
{
	// Find id index in queue
	var index = -1;
	for(var i=0; i < arr_menu_chain.length; i++)
	{
		if(arr_menu_chain[i] == menu_id)
		{
			index = i;
			break;
		}
	}

	if(index != -1)
	{
		// +1 to prevent hiding self
		for(var i=index+1; i < arr_menu_chain.length; i++)
		{
			$("#" + arr_menu_chain[i]).hide();
		}

		arr_menu_chain.length = index + 1;
	}
}

/////////////////////////////////////////////////////////////////
// Hide all submenus
/////////////////////////////////////////////////////////////////
function submenu_hide_all()
{
	submenu_hide_after(arr_menu_chain[0]);
}

// Init menu
$(document).ready(function()
{
	$("body").click(submenu_hide_all);
});
