
// Menu Beta
// - Every submenu has a table with 'menutable_' + parent + '-' + level
// - Every menuitem has a item with id 'menutitem_' + index;
// - @TODO hide all menu on mouse out
// - @TODO add nice CSS

// Curent menu items on the different levels

var menucurrentshow = new Array();

// Margin
var menuleftmargin = 100;
var menutopmargin = 23;
var maxlevels = 3; // Only for hidding
var lastitem = null;
var imgdefaultimg = './addons/pro/img/menu/menu_item_lo_middle_bg.png';
var imghighigh = './addons/pro/img/menu/menu_item_hi_middle_bg.png';

/*
* Locate
*/

function menu_navto(link, index){

	link += '&menuindex=' + index;

	window.location = link;
}

/*
* Input current item
*/

function menu_mouseover(index){

	// Hide menu other menu lower then this level
	menu_showdhideother(index);

	// Check for child menu
	var childindex = menu_getchildindex(index);

	// Show Child menu
	if(childindex){
		menu_show_childmenu(childindex, index);
	}

	// Do some style changes
	var level = menu_getlevel(index);
	if(level == 1){
		menu_switch_philox_colors(index,false);
	}

	// Add mouse over effect
	mouse_over_effect(index);

	// Store lastest
	lastitem = index;
}

/*
* Mouse over effect
*/

function mouse_over_effect(index){

	// Switch mouse over
	var menuitem = $('menuitem_' + index);

	// Check for background image
	if(menu_checkcolors(Math.ceil(index))){
		//menuitem.style.backgroundImage = 'url(./addons/pro/img/menu/menu_item_lo_middle_' + menu_colors[Math.floor(index)] + '_bg.png)';
	}
	else{
		menuitem.style.backgroundImage = 'url(' + imghighigh + ')';
	}

	// Restore last one
	menu_restorelastitem_mouseover();

}

/**
* Check if head style head is diffent
*/

function menu_checkcolors(index){

	if (menu_colors[index]){
		return(menu_colors[index]);
	}

}

/*
* Switch philox head colors
*/

function menu_switch_philox_colors(index,init){

	var philoxhead = $('PhiloxHead');
	var image = 'topbg_';

	if(menu_checkcolors(index)){
		image += menu_colors[index] + '.png';
		philoxhead.style.backgroundImage = 'url(./addons/pro/img/head/' + image + ')';

		var menuitem = $('menuitem_' + index);

		// Check for background image
		// Highlight last click on open page
		if(init === true){
			var menuitem = $('menuitem_' + index);
			if(menu_checkcolors(index)){
				//menuitem.style.backgroundImage = 'url(./addons/pro/img/menu/menu_item_lo_middle_' + menu_colors[index] + '_bg.png)';
			}
			else{
				menuitem.style.backgroundImage = 'url(' + imghighigh + ')';
			}
		}

		//philoxhead.className = 'cPhiloxHead cPhiloxHead_' + menu_colors[index];
	}
	else{
		image += 'philox.png';
		//philoxhead.className = 'cPhiloxHead';
		philoxhead.style.backgroundImage = 'url(./addons/pro/img/head/' + image + ')';
	}

}

/**
* Restore last item
*/

function menu_restorelastitem_mouseover(){

	if(lastitem){
		var oLastitem = $('menuitem_' + lastitem);
		oLastitem.style.backgroundImage = 'url(' + imgdefaultimg + ')';
	}

}

/*
* Hide menu 's lower this then the current level
* @param index of the menu item
*/

function menu_showdhideother(index){

	var level = menu_getlevel(index);
	var start = level;

	for (var x = start; x <= maxlevels; x++){

		var hide = menucurrentshow[x];

		var objhide = $(hide);
		if(objhide){
			objhide.style.display = 'none';
		}
	}

	// Disableb last mouse over menu item
	if(index == "0"){
		if(lastitem){
			var oLastitem = $('menuitem_' + lastitem);
			oLastitem.style.backgroundImage = 'url(' +  imgdefaultimg + ')';

			menu_switch_philox_colors(menuindexstart,true);
		}
	}
}

/*
* Show's the menu
*
* @param childindex - index of the child
* @param index - index of current item
*/

function menu_show_childmenu(childindex,index){

	// Get Child Object
	var child = $(childindex);

	// Get Parent Object
	var parent = $('menuitem_' + index);

	// Get Level of child
	var level = menu_getlevel(childindex);

	// Get current postion of parent
	var pos = Position.cumulativeOffset(parent);

	// Calculated new postion (horizontal of vertical)

	if(level > 1){

		var left = parseInt(pos[0]) + parseInt(menuleftmargin);
		var top = pos[1];
	}
	else{
		var left = pos[0];
		var top = pos[1] + menutopmargin;
	}

	// Set new postion
	if(child){
		Position.absolutize(child);
		child.style.display = 'inline';
		child.style.left = left+'px';
		child.style.top = top+'px';
		//Effect.BlindDown(childindex);
	}

	// Store last menu item per level
	menucurrentshow[level] = childindex;

}

// Get level of item based on the index

function menu_getlevel(index){

	var split = index.split('.');
	var level = split.length;

	return(level);

}

/*
* Check if item as child menu
* @param index - index of an item
*/

function menu_getchildindex(index){

	// Parent id + Level
	var split = index.split('.');

	var childindex = 'menutable_' + index + '-' + (split.length + 1);

	var childobj = $(childindex);
	if(childobj){
		return(childindex);
	}
	else{
		return(false);
	}

}

