var A_MENUS = [];
var m_selectsAlreadyHidden = false;

document.write('<script type="text/javascript" language="JavaScript" src="Utility.js"></script>')

function menu (a_items, a_tpl) {

	// browser check
	if (!document.body || !document.body.style)
		return;

	// store items structure
	this.a_config = a_items;

	// store template structure
	this.a_tpl = a_tpl;

	// get menu id
	this.n_id = A_MENUS.length;

	// declare collections
	this.a_index = [];
	this.a_children = [];

	// assigh methods and event handlers
	this.expand = menu_expand;
	this.collapse = menu_collapse;

	this.onclick = menu_onclick;
	this.onmouseout = menu_onmouseout;
	this.onmouseover = menu_onmouseover;
	this.Redraw = menu_redraw;

	this.o_root = this;
	this.n_depth = -1;
	this.n_x = 0;
	this.n_y = 0;
	
	// 	init items recursively
	for (n_order = 0; n_order < a_items.length; n_order++)
		new menu_item(this, n_order);

	// register self in global collection
	A_MENUS[this.n_id] = this;

	// make root level visible
	for (var n_order = 0; n_order < this.a_children.length; n_order++)
		this.a_children[n_order].e_ielement.style.visibility = 'visible';
}

// --------------------------------------------------------------------------------
function menu_collapse (n_id)
{
	// cancel item open delay
	clearTimeout(this.o_showtimer);

	// by default collapse to root level
	var n_tolevel = (n_id ? this.a_index[n_id].n_depth : 0);
	
	// hide all items over the level specified
	for (n_id = 0; n_id < this.a_index.length; n_id++)
	{
		var o_curritem = this.a_index[n_id];
		if (o_curritem.n_depth > n_tolevel && o_curritem.b_visible) 
		{
			o_curritem.e_ielement.style.visibility = 'hidden';
			o_curritem.b_visible = false;
		}
	}

	// reset current item if mouse has gone out of items
	if(!n_id)
	{
		this.o_current = null;
	}
	
	if(n_tolevel == 0)
	{	
		if(IsInternetExplorer6())
		{
		    var hideSelects = true;
		
		    if(typeof(m_hideDropdowns) != 'undefined')
	        {
	            hideSelects = m_hideDropdowns;
	        }

	        if (hideSelects && m_selectsAlreadyHidden)
	        {
	            var doc = document.frames(0).document;
	            if (doc != null)
	            {
	                var oControls = doc.getElementsByTagName('select');
	                var iControl;
	                var oControl;

	                iControls = oControls.length;
	                for (iControl = 0; iControl < iControls; iControl++)
	                {
	                    oControl = oControls[iControl];
	                    oControl.style.visibility = 'visible';
	                }
	            }

	            m_selectsAlreadyHidden = false;
	        }
		}
	}
}

// --------------------------------------------------------------------------------
function menu_expand (n_id)
{
	// expand only when mouse is over some menu item
	if (this.o_hidetimer)
	{
		return;
	}

	// lookup current item
	var o_item = this.a_index[n_id];

	// close previously opened items
	if (this.o_current && this.o_current.n_depth >= o_item.n_depth)
	{
		this.collapse(o_item.n_id);
	}
	this.o_current = o_item;

	// exit if there are no children to open
	if (!o_item.a_children)
	{
		return;
	}

	// show direct child items
	for (var n_order = 0; n_order < o_item.a_children.length; n_order++)
	{
		var o_curritem = o_item.a_children[n_order];
		if(o_curritem.a_config[0] != 'Null')
		{
			o_curritem.e_ielement.style.visibility = 'visible';
			o_curritem.b_visible = true;
		}
	}

	if(document.frames.length > 0)
	{	
		if(IsInternetExplorer6())
		{
		    var hideSelects = true;
		
		    if(typeof(m_hideDropdowns) != 'undefined')
	        {
	            hideSelects = m_hideDropdowns;
	        }

	        if (hideSelects && !m_selectsAlreadyHidden)
	        {
	            var doc = document.frames(0).document;
	            if (doc != null)
	            {
	                var oControls = doc.getElementsByTagName('select');

	                var iControl;
	                var oControl;

	                iControls = oControls.length;
	                for (iControl = 0; iControl < iControls; iControl++)
	                {
	                    oControl = oControls[iControl];
	                    oControl.style.visibility = 'hidden';
	                }
	            }

	            m_selectsAlreadyHidden = true;
	        }
		}
	}
}

// --------------------------------------------------------------------------------
//
// --------------------------------------------------------------------------------
function menu_onclick (n_id)
{
	if(this.a_index[n_id].a_config[1] != null)
	{
	    if((String(this.a_index[n_id].a_config[1]).substring(0,4) == 'http') || (String(this.a_index[n_id].a_config[1]).indexOf('FullWindow') > -1))
	    {
	        window.location.href = this.a_index[n_id].a_config[1];
	    }
	    else
	    {
	        A_MENUS[this.n_id].collapse();
	        var oMainPage = document.getElementById("MainPage");
	        ShowInProgress();
	        oMainPage.src = this.a_index[n_id].a_config[1];
	    }
	}
}

// --------------------------------------------------------------------------------
function menu_onmouseout (n_id) {

	// lookup new item's object	
	var o_item = this.a_index[n_id];
	var bTop = o_item.bTop;
	var bBottom = o_item.bBottom;
	
	sTarget = this.a_index[n_id].a_config[1];
	if(sTarget != null)
	{
	    if(sTarget.substring(0, 8) == 'Disabled')
	    {
	        bMenuDisabled = true;
	    }
	    else
	    {
	        bMenuDisabled = false;
	    }
    }
	
	if(bMenuDisabled)
	{
	    var sClass = 'DisabledMenu';
	}
	else
	{
	    var sClass = o_item.getprop('Class');
    }
    
    if(bTop)
    {
        sClass = sClass + ' ' + o_item.getprop('Class') + 'Top';
    }

    if(bBottom)
    {
        sClass = sClass + ' ' + o_item.getprop('Class') + 'Bottom';
    }

	// apply rollout
	o_item.e_ielement.className = sClass;
	
	var oParent
	oParent = o_item.o_parent;
	if(oParent.e_ielement != null)
	{
		bTop = oParent.bTop;
	    bBottom = oParent.bBottom;
	    sClass = oParent.getprop('Class');
	
	    if(bTop)
	        sClass = sClass + ' ' + oParent.getprop('Class') + 'Top';

	    if(bBottom)
	        sClass = sClass + ' ' + oParent.getprop('Class') + 'Bottom';

		oParent.e_ielement.className = sClass;
		oParent = oParent.o_parent;
		if(oParent != null)
		{
			if(oParent.e_ielement != null)
			{
				bTop = oParent.bTop;
	            bBottom = oParent.bBottom;
	            sClass = oParent.getprop('Class');
        	
	            if(bTop)
	                sClass = sClass + ' ' + oParent.getprop('Class') + 'Top';

	            if(bBottom)
	                sClass = sClass + ' ' + oParent.getprop('Class') + 'Bottom';

				oParent.e_ielement.className = sClass;
				oParent = oParent.o_parent;
				if(oParent != null)
				{
					if(oParent.e_ielement != null)
					{
		                bTop = oParent.bTop;
	                    bBottom = oParent.bBottom;
	                    sClass = oParent.getprop('Class');
                	
	                    if(bTop)
	                        sClass = sClass + ' ' + oParent.getprop('Class') + 'Top';

	                    if(bBottom)
	                        sClass = sClass + ' ' + oParent.getprop('Class') + 'Bottom';
					
						oParent.e_ielement.className = sClass;
					}
				}
			}
		}
	}
	
	// update status line	
	o_item.upstatus(7);

	// run mouseover timer
	this.o_hidetimer = setTimeout('A_MENUS['+ this.n_id +'].collapse();',
		o_item.getprop('hide_delay'));
}

// --------------------------------------------------------------------------------
function menu_onmouseover (n_id)
{	
	// cancel mouseoute menu close and item open delay
	clearTimeout(this.o_hidetimer);
	this.o_hidetimer = null;
	clearTimeout(this.o_showtimer);

	// lookup new item's object	
	var o_item = this.a_index[n_id];

	// update status line	
	o_item.upstatus();

	var bTop = o_item.bTop;
	var bBottom = o_item.bBottom;
	
	if(this.a_index[n_id].a_config[0] == 'Separator')
	{
	    bSeparator = true;
	}
	else
	{
	    bSeparator = false;
	}
	
    sTarget = this.a_index[n_id].a_config[1];
	if(sTarget != null)
	{
	    if(sTarget.substring(0, 8) == 'Disabled')
	    {
	        bMenuDisabled = true;
	    }
	    else
	    {
	        bMenuDisabled = false;
	    }
    }
	
	if(bMenuDisabled)
	{
	    var sClass = 'DisabledMenu';
	}
	else
	{
	    if(!bSeparator)
	    {
	        var sClass = o_item.getprop('Class') + ' ' + o_item.getprop('HoverClass');
	    }
	    else
	    {
	        var sClass = o_item.getprop('Class');
	    }
	}
    	
    if(bTop)
    {
        sClass = sClass + ' ' + o_item.getprop('Class') + 'Top';
    }

    if(bBottom)
    {
        sClass = sClass + ' ' + o_item.getprop('Class') + 'Bottom';
    }

	// apply rollover
	o_item.e_ielement.className = sClass;
	
	var oParent
	oParent = o_item.o_parent;
	if(oParent.e_ielement != null)
	{
		bTop = oParent.bTop;
	    bBottom = oParent.bBottom;
	    sClass = oParent.getprop('Class') + ' ' + oParent.getprop('HoverClass');
	
	    if(bTop)
	        sClass = sClass + ' ' + oParent.getprop('Class') + 'Top';

	    if(bBottom)
	        sClass = sClass + ' ' + oParent.getprop('Class') + 'Bottom';

		oParent.e_ielement.className = sClass;
		oParent = oParent.o_parent;
		if(oParent != null)
		{
			if(oParent.e_ielement != null)
			{
		        bTop = oParent.bTop;
	            bBottom = oParent.bBottom;
	            sClass = oParent.getprop('Class') + ' ' + oParent.getprop('HoverClass');
        	
	            if(bTop)
	                sClass = sClass + ' ' + oParent.getprop('Class') + 'Top';

	            if(bBottom)
	                sClass = sClass + ' ' + oParent.getprop('Class') + 'Bottom';

				oParent.e_ielement.className = sClass;
				oParent = oParent.o_parent;
				if(oParent != null)
				{
					if(oParent.e_ielement != null)
					{
		                bTop = oParent.bTop;
	                    bBottom = oParent.bBottom;
	                    sClass = oParent.getprop('Class') + ' ' + oParent.getprop('HoverClass');
                	
	                    if(bTop)
	                        sClass = sClass + ' ' + oParent.getprop('Class') + 'Top';

	                    if(bBottom)
	                        sClass = sClass + ' ' + oParent.getprop('Class') + 'Bottom';

						oParent.e_ielement.className = sClass;
					}
				}
			}
		}
	}

	if (o_item.getprop('expd_delay') < 0)
	{
		return;
	}

	// run expand timer
	this.o_showtimer = setTimeout('A_MENUS['+ this.n_id +'].expand(' + n_id + ');', o_item.getprop('expd_delay'));
}


// --------------------------------------------------------------------------------
// menu item Class
function menu_item (o_parent, n_order)
{
	var hasChildren
	var menuHTML
	var bTop
	var bBottom
	
	// store parameters passed to the constructor
	this.n_depth  = o_parent.n_depth + 1;
	this.a_config = o_parent.a_config[n_order + (this.n_depth ? 3 : 0)];

	// return if required parameters are missing
	if (!this.a_config) return;

	// store info from parent item
	this.o_root    = o_parent.o_root;
	this.o_parent  = o_parent;
	this.n_order   = n_order;
	
	// register in global and parent's collections
	this.n_id = this.o_root.a_index.length;
	this.o_root.a_index[this.n_id] = this;
	o_parent.a_children[n_order] = this;

	// calculate item's coordinates
	var o_root = this.o_root,
	a_tpl  = this.o_root.a_tpl;

	// assign methods
	this.getprop  = mitem_getprop;
	this.upstatus = mitem_upstatus;

	if(n_order == 0)
	{
		var leftOffsetFromParent;
		leftOffsetFromParent = this.getprop('block_left')
		if(leftOffsetFromParent == -1)
		{
			this.n_x = o_parent.n_x + o_parent.e_ielement.clientWidth;
		}
		else
		{
			this.n_x = o_parent.n_x + leftOffsetFromParent;
		}
		
		this.n_y = o_parent.n_y + this.getprop('block_top');
	}
	else
	{
		var offsetFromSibling;
		offsetFromSibling = this.getprop('left')
		
		var sibling;
		sibling = o_parent.a_children[n_order - 1]

		if(offsetFromSibling > -1)
		{
			this.n_x = sibling.n_x + this.getprop('left');
		}
		else
		{
			var parentWidth;
			parentWidth = sibling.e_ielement.clientWidth;
			
			if(this.n_depth == 0)
		    {
		        this.n_x = sibling.n_x + parentWidth + 10;
		    }
		    else
		    {
			    this.n_x = sibling.n_x + parentWidth - 1;
			}
		}
		
		this.n_y = o_parent.a_children[n_order - 1].n_y + this.getprop('top')
	}
		
	if (this.a_config.length >= 4)
	{
		hasChildren = true;
	}
	else
	{
		hasChildren = false;
	}
	
    var sTarget = this.a_config[1];
    if(sTarget != null)
    {
        if(sTarget.substring(0, 8) == "Disabled")
        {
            bMenuDisabled = true;
        }
        else
        {
            bMenuDisabled = false;
        }
    }
    else
    {
        bMenuDisabled = false;
    }
		
	// generate item's HMTL
	this.bTop = false;
	this.bBottom = false;
	menuHTML = '';
	menuHTML = menuHTML.concat('<table');
	menuHTML = menuHTML.concat(' id="e' + o_root.n_id + '_' + this.n_id + 'i"');
	menuHTML = menuHTML.concat(' style="position: absolute; z-index: ' + this.n_depth + ';"');
	if(bMenuDisabled)
    {
        menuHTML = menuHTML.concat(' class="DisabledMenu"');
    }
    else
    {
	    menuHTML = menuHTML.concat(' class="' + this.getprop('Class'));
	    if(n_order == 0)
	    {
	        this.bTop = true;
	        menuHTML = menuHTML.concat(' ' + this.getprop('Class') + 'Top');
	    }
	    if(n_order == (this.o_parent.a_config.length-4))
	    {
	        this.bBottom = true;
	        menuHTML = menuHTML.concat(' ' + this.getprop('Class') + 'Bottom');
	    }
	    menuHTML = menuHTML.concat('"');
	}
	if(this.a_config[0] != 'Separator')
	{	    
	    if(bMenuDisabled)
	    {
	        menuHTML = menuHTML.concat(' onmouseout="A_MENUS[' + o_root.n_id + '].onmouseout(' + this.n_id + ');"'); 
        	menuHTML = menuHTML.concat(' onmouseover="A_MENUS[' + o_root.n_id + '].onmouseover(' + this.n_id + ');"');
	        menuHTML = menuHTML.concat('>');
	        menuHTML = menuHTML.concat('<tr height="100%">');
	        menuHTML = menuHTML.concat('<td nowrap id="Label' + o_root.n_id + '_' + this.n_id + '">');
	        menuHTML = menuHTML.concat(this.a_config[0]);
	        menuHTML = menuHTML.concat('</td>');
	    }
	    else
	    {
	        menuHTML = menuHTML.concat(' onmouseout="A_MENUS[' + o_root.n_id + '].onmouseout(' + this.n_id + ');"'); 
	        menuHTML = menuHTML.concat(' onmouseover="A_MENUS[' + o_root.n_id + '].onmouseover(' + this.n_id + ');"');
	        menuHTML = menuHTML.concat(' onclick="return A_MENUS[' + o_root.n_id + '].onclick('+ this.n_id + ');"');
	        menuHTML = menuHTML.concat('">');
	        menuHTML = menuHTML.concat('<tr height="100%">');
	        menuHTML = menuHTML.concat('<td nowrap id="Label' + o_root.n_id + '_' + this.n_id + '">');
	        menuHTML = menuHTML.concat(this.a_config[0]);
	        menuHTML = menuHTML.concat('</td>');
        
	        menuHTML = menuHTML.concat('<td valign="middle" align="right">');

	        if(hasChildren && o_parent != o_parent.root)
	        {
		        var menuImage;
		        menuImage = this.getprop('SubMenuImage');
		        menuHTML = menuHTML.concat('<img src="Images/');
		        menuHTML = menuHTML.concat(menuImage);
		        menuHTML = menuHTML.concat('">');
        	    menuHTML = menuHTML.concat('</td>');
	        }
	    }
	}
	else
	{	    
	    menuHTML = menuHTML.concat(' onmouseout="A_MENUS[' + o_root.n_id + '].onmouseout(' + this.n_id + ');"'); 
	    menuHTML = menuHTML.concat(' onmouseover="A_MENUS[' + o_root.n_id + '].onmouseover(' + this.n_id + ');"');
	    menuHTML = menuHTML.concat('>');
	    menuHTML = menuHTML.concat('<tr>');
	    menuHTML = menuHTML.concat('<td nowrap><hr style="height:1px;">');
	    menuHTML = menuHTML.concat('</td>');
	}
	menuHTML = menuHTML.concat('</tr>');
	menuHTML = menuHTML.concat('</table>');
	menuHTML = menuHTML.concat('\n');
		
	document.write(menuHTML);

	this.e_ielement = document.getElementById('e' + o_root.n_id + '_' + this.n_id + 'i');
	this.m_label = document.getElementById('Label' + o_root.n_id + '_' + this.n_id);
	if(!IsInternetExplorer55())
	{
	    this.e_ielement.style.cursor = 'pointer';
	}
	this.b_visible = !this.n_depth;
	
	var width;
	width = this.getprop('width');
		
	if(width == -1)
	{
		if(n_order == 0)
		{
			width = this.e_ielement.clientWidth;
			for(iSibling = 4; iSibling < o_parent.a_config.length; iSibling++)
			{
				arrSibling = o_parent.a_config[iSibling];
				
				oLabel = document.getElementById('Label' + o_root.n_id + '_' + this.n_id);
				//this below
				oLabel.innerHTML = arrSibling[0];
				if(oLabel.clientWidth > width)
				{
					width = this.e_ielement.clientWidth;
				}
				oLabel.innerHTML = this.a_config[0];
			}
			width = width + 20;
		}
		else
		{
			firstSibling = o_parent.a_children[0];
			width = firstSibling.e_ielement.style.width;
		}
	}
		
	var minimumTotalWidth;
	minimumTotalWidth = this.getprop('MinimumTotalWidth');
	if(minimumTotalWidth >  -1)
	{
		if(n_order == 0)
		{
			var totalMenuItems;
			totalMenuItems = o_parent.a_config.length - 2;
		
			var totalMenuWidth;
			totalMenuWidth = width * totalMenuItems;
			
			var totalBrowserWidth;
			totalBrowserWidth = document.body.clientWidth + document.body.scrollLeft;
			
			if(totalMenuWidth > totalBrowserWidth)
			{
				width = totalBrowserWidth / totalMenuItems;
				if((width * totalMenuItems) < minimumTotalWidth)
				{
					width = minimumTotalWidth / totalMenuItems;
				}
			}
		}
		else
		{
			firstSibling = o_parent.a_children[0];
			width = firstSibling.e_ielement.style.width;
		}
	}
	
	this.e_ielement.style.width = width;
	this.e_ielement.style.height = this.getprop('height');
	
	if((this.n_y + this.getprop('height')) > (document.body.clientHeight + document.body.scrollTop))
	{
		this.n_y = (o_parent.n_y - this.getprop('height')) + 3;
	}
	this.e_ielement.style.top = this.n_y + 'px';
	
	if((this.n_x + width) > (document.body.clientWidth + document.body.scrollLeft))
	{
		this.n_x = (o_parent.n_x - width) + 3;
	}	
	this.e_ielement.style.left = this.n_x;
	
	this.e_ielement.style.visibility = 'hidden';
	
	// no more initialization if leaf
	if (this.a_config.length < 4)
		return;

	// node specific methods and properties
	this.a_children = [];

	// init downline recursively
	for (var n_order = 0; n_order < this.a_config.length - 3; n_order++)
	{
		oChild = new menu_item(this, n_order);
	}	
}


// --------------------------------------------------------------------------------
// reads property from template file, inherits from parent level if not found
// ------------------------------------------------------------------------------------------
function mitem_getprop (s_key) {

	// check if value is defined for current level
	var s_value = null,
		a_level = this.o_root.a_tpl[this.n_depth];

	// return value if explicitly defined
	if (a_level)
		s_value = a_level[s_key];

	// request recursively from parent levels if not defined
	return (s_value == null ? this.o_parent.getprop(s_key) : s_value);
}


// ------------------------------------------------------------------------------------------
// updates status bar message of the browser
// ------------------------------------------------------------------------------------------
function mitem_upstatus (b_clear) {
	window.setTimeout("window.status=unescape('" + (b_clear
		? ''
		: (this.a_config[2] && this.a_config[2]['sb']
			? escape(this.a_config[2]['sb'])
			: escape(this.a_config[0]) + (this.a_config[1]
				? ' ('+ escape(this.a_config[1]) + ')'
				: ''))) + "')", 10);
}

function menu_redraw()
{
	for (var order = 0; order < this.a_children.length; order++)
	{
		menuItem = this.a_children[order];		
		ResizeMenuItem(order, menuItem)
	}
}

function ResizeMenuItem(order, menuItem)
{
	oParent = menuItem.o_parent;
	oElement = menuItem.e_ielement;

	if(order == 0)
	{
		var leftOffsetFromParent;
		leftOffsetFromParent = menuItem.getprop('block_left')
		if(leftOffsetFromParent == -1)
		{
			menuItem.n_x = oParent.n_x + oParent.e_ielement.clientWidth;
		}
		else
		{
			menuItem.n_x = oParent.n_x + leftOffsetFromParent;
		}
		
		menuItem.n_y = oParent.n_y + menuItem.getprop('block_top');
	}
	else
	{
		var offsetFromSibling;
		offsetFromSibling = menuItem.getprop('left')
		
		var sibling;
		sibling = oParent.a_children[order - 1]

		if(offsetFromSibling > -1)
		{
			menuItem.n_x = sibling.n_x + menuItem.getprop('left');
		}
		else
		{
			var parentWidth;
			parentWidth = sibling.e_ielement.clientWidth;
			
			menuItem.n_x = sibling.n_x + parentWidth - 1;
		}
		
		menuItem.n_y = oParent.a_children[order - 1].n_y + menuItem.getprop('top')
	}
		
	if (menuItem.a_config.length >= 4)
	{
		hasChildren = true;
	}
	else
	{
		hasChildren = false;
	}
			
	var width;
	width = menuItem.getprop('width');
		
	if(width == -1)
	{
		width = oElement.style.width;
		width = width.substring(0, width.length - 2);
		width = parseInt(width);
	}
				
	var minimumTotalWidth;
	minimumTotalWidth = menuItem.getprop('MinimumTotalWidth');
	if(minimumTotalWidth >  -1)
	{
		if(order == 0)
		{
			var totalMenuItems;
			totalMenuItems = oParent.a_config.length - 2;
		
			var totalMenuWidth;
			totalMenuWidth = width * totalMenuItems;
			
			var totalBrowserWidth;
			totalBrowserWidth = document.body.clientWidth + document.body.scrollLeft;
			
			if(totalMenuWidth > totalBrowserWidth)
			{
				width = totalBrowserWidth / totalMenuItems;
				if((width * totalMenuItems) < minimumTotalWidth)
				{
					width = minimumTotalWidth / totalMenuItems;
				}
			}
		}
		else
		{
			firstSibling = oParent.a_children[0];
			width = firstSibling.e_ielement.style.width;
		}
	}
	
	oElement.style.width = width;
	oElement.style.height = menuItem.getprop('height');
	
	if((menuItem.n_y + menuItem.getprop('height')) > (document.body.clientHeight + document.body.scrollTop))
	{
		menuItem.n_y = (oParent.n_y - menuItem.getprop('height')) + 3;
	}
	oElement.style.top = menuItem.n_y;
	
	if((menuItem.n_x + width) > (document.body.clientWidth + document.body.scrollLeft))
	{
		menuItem.n_x = (oParent.n_x - width) + 3;
	}	
	oElement.style.left = menuItem.n_x;
	
	if(hasChildren)
	{
		for (var order = 0; order < menuItem.a_config.length - 3; order++)
		{
			oChild = menuItem.a_children[order];
			ResizeMenuItem(order, oChild);
		}
	}
}