/*
 * Unisico Administrative Interface
 * Copyright (c) 2003 Unisico Content Management System, http://www.unisico.com
 * Copyright (c) 2003 Advanware, http://www.advanware.com
 * Copyright (c) 2003 Dimitri Kucher, dimitri@advanware.com
 * Copyright (c) 2003 Dmitry Breslavets, dima@advanware.com
 *
 * All rights reserved.
 * No part of this code can be reproduced, copied, or used elsewhere without explicit written
 * approval from Advanware.
 */

function getElementsByFunc(parent, func, tagName)
{
	if (!parent) return;
	if (!tagName) tagName = "DIV"; // default func container tagName

	var elements = parent.getElementsByTagName(tagName);
	var resultElements = new Array();

	for (var i = 0; i < elements.length; i++)
		if (elements[i].getAttribute("func") == func)
			resultElements[resultElements.length] = elements[i];

	return resultElements;
}

function getBaseClassName(str)
{
	var delimeter = "_";
	if (!str || str == "") return delimeter;

	var lastIndex = str.lastIndexOf(delimeter);
	if (lastIndex == -1) return delimeter;
	return str.substr(0, lastIndex) + delimeter;
}

/**
 * Do not jchange className attribute if it coincidences with already set value
 */
function safeSetClassName(element, className)
{
	if (!element) return;
	if (element.className != className) element.className = className;
}

//==========================================================================

function init()
{
	if (!document.body) return;

	initializeMenu(getElementsByFunc(document.body, "top_menu_item", "TD"));
	initializeMenu(getElementsByFunc(document.body, "right_menu_item", "TR"));
}

function initializeMenu(items)
{
	for (var i = 0; i < items.length; i++)
	{
		items[i].onclick = _menu_click;
		if (!items[i].getAttribute("active"))
		{
			items[i].onmouseover = _menu_mouse_over;
			items[i].onmouseout = _menu_mouse_out;
		}
	}
}
function _menu_click()
{
	var As = this.getElementsByTagName("A");
	if (As.length == 0) return;
	document.location.href = As[0].href;
}

function _menu_mouse_over()
{
	set_menu_hover(this, true)
}

function _menu_mouse_out()
{
	set_menu_hover(this, false)
}

function set_menu_hover(menuItem, value)
{
	if (!menuItem.As)
		menuItem.As = menuItem.getElementsByTagName("A");
	if (!menuItem.TDs)
		menuItem.TDs = menuItem.getElementsByTagName("TD");

	var As = menuItem.As;
	var TDs = menuItem.TDs;

	var AAddon = ((value) ? "hover" : "normal");

	for (var i = 0; i < As.length; i++)
		As[i].className = getBaseClassName(As[i].className) + AAddon;

	if (!menuItem.colorTD)
		for (var i = 0; i < TDs.length; i++)
			if (getBaseClassName(TDs[i].className) != "_")
				menuItem.colorTD = TDs[i];

	menuItem.colorTD.className = getBaseClassName(menuItem.colorTD.className) + ((value) ? "active" : "normal");
}