// Title: T-MENU 
// Description: See the demo at url
// URL: 
// Version: "dudziak 1.0" - Based on TIGRA MENU 2.0 (http://www.softcomplex.com/products/tigra_menu/)
// Date: 13-Feb-2007
// Tech. Support: 
// Notes: This script is free. Visit official site for further details.
//




// --------------------------------------------------------------------------------
// global collection containing all menus on current page
var A_MENUS = [];

// --------------------------------------------------------------------------------
// menu class
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;

	// set menu node id
	this.n_id = -1;
	
	// get menu id
	this.m_id = A_MENUS.length;

	// declare collections
	this.a_index = [];
	this.a_children = [];

	// assign 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.onmousedown = menu_onmousedown;

	// default level scope description structure 
	this.a_tpl_def = {
		'block_top'  : 16,
		'block_left' : 16,
		'top'        : 20,
		'left'       : 4,
		'width'      : 120,
		'height'     : 22,
		'hide_delay' : 0,
		'expd_delay' : 0,
		'css'        : {
			'inner' : '',
			'outer' : ''
		}
	};
	
	// assign methods and properties required to imulate parent item
	this.getprop = function (s_key) {
		return this.a_tpl_def[s_key];
	};

	this.o_root = this;
	this.n_depth = -1;
	this.n_x = 0;
	this.n_y = 0;
	
	this.myHTML = "";
	
	// the last item to be moused-over
	this.n_current = null;
	// current expanded menu
	this.o_current = null;

	// 	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.m_id] = this;

	// make root level visible
	for (var n_order = 0; n_order < this.a_children.length; n_order++)
		this.a_children[n_order].setVisible(true);
}

// --------------------------------------------------------------------------------
function menu_collapse (n_id) {
	// cancel item open delay
	clearTimeout(this.o_showtimer);

	// by default collapse to root level
	var n_tolevel = (n_id > 0 ? this.a_index[n_id].n_depth : 0);
	
	// reset current item if mouse has gone out of items
	if (n_id == this.n_id)
	{
		if (this.n_current != null)
			menu_onmouseover_maintain_highlight_removal(this, this.n_current.n_id);
		this.o_current = null;
		this.n_current = null;
	}
	
	// hide all items over the level specified
	for (i = 0; i < this.a_index.length; i++) {
		var o_curritem = this.a_index[i];
		if (o_curritem.n_depth > n_tolevel && o_curritem.isVisible) {
			o_curritem.setVisible(false);
		}
	}
	
	// reset all the menu positions if we leave the menu completely
	if (n_id == this.n_id)
		for (var n_order = 0; n_order < this.a_children.length; n_order++)
			this.a_children[n_order].reset_position();

}

// --------------------------------------------------------------------------------
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];
	
	if (o_item == this.o_current) return;

	// 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 || o_item.a_children.length < 1)
		return;

	if (o_item.getprop('vertical_safeguard'))
	{
		// set correct child positions (do not want children to display below current viewing window)
		if (parseInt(navigator.appVersion)>3) {
			if (navigator.appName=="Netscape") {
				var winW = window.innerWidth;
				var winH = window.innerHeight;
				var scrollammt = window.pageYOffset;
			}
			if (navigator.appName.indexOf("Microsoft")!=-1) {
				var winW = document.body.offsetWidth;
				var winH = document.body.offsetHeight;
				var scrollammt = document.body.scrollTop;
			}
		}
	
		var desired_top = o_item.a_children[0].yy;
		var desired_bottom = o_item.a_children[o_item.a_children.length-1].yy + o_item.a_children[0].getprop('height');
		var available_adjustment = o_item.a_children[0].yy-30-scrollammt;
		var needed_adjustment = desired_bottom-winH-scrollammt+30;
		var adjustment_ammount = (available_adjustment > needed_adjustment) ? needed_adjustment : available_adjustment;
		
		// adjust the positioning of the submenu elements (if adjustment is deemed necessary)
		if (adjustment_ammount > 0)
			for (var n_order = 0; n_order < o_item.a_children.length; n_order++)
				o_item.a_children[n_order].adjust_y_position(-adjustment_ammount);
	}
	
	// show child elements
	for (var n_order = 0; n_order < o_item.a_children.length; n_order++)
		o_item.a_children[n_order].setVisible(true);
}

// --------------------------------------------------------------------------------
//
// --------------------------------------------------------------------------------
function menu_onclick (n_id) {
	
	//var o_item = this.a_index[n_id];
	
	// destination
	//var d = o_item.a_config[1];
	
	// don't go anywhere if item has no link defined
	//if (!d) return;
	
	// if there is a target window, apply the target
	//if (o_item.a_config[2] && o_item.a_config[2]['tw'])
	//	open(o_item.a_config[1], o_item.a_config[2]['tw']);
	//else
	//	window.location=d;
}

// --------------------------------------------------------------------------------
function menu_onmouseout (n_id) {

	// lookup new item's object	
	var o_item = this.a_index[n_id];
			
	// update status line	
	o_item.upstatus(7);

	// run mouseout timer (cpu intesive)
	this.o_hidetimer = setTimeout('A_MENUS['+ this.m_id +'].collapse('+ this.n_id +');', o_item.getprop('hide_delay'));
}

function menu_onmouseover_maintain_highlight_removal (table, n_id)
{
	if (n_id == table.n_id) return;
	table.a_index[n_id].set_style('normal');
	menu_onmouseover_maintain_highlight_removal(table, table.a_index[n_id].o_parent.n_id);
}

// --------------------------------------------------------------------------------
function menu_onmouseover (n_id) {

	thisActionTime = new Date();
	this.MostRecentInput = thisActionTime;

	// 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();
	
	// apply rollover if the element has a value and is not already selected
	if (o_item.a_config[0] != "")
		o_item.set_style('hover');
	if (this.n_current != null && this.n_current != o_item)
		this.n_current.set_style('normal');

	// apply parent highlighting
	if (o_item.getprop('maintain_highlight'))
	{
		if (this.n_current != null && this.n_current.o_parent.n_id != o_item.o_parent.n_id)
			menu_onmouseover_maintain_highlight_removal(this, this.n_current.n_id);
		menu_onmouseover_maintain_highlight(this, o_item.n_id);
	}
	
	this.n_current = o_item;
	
	// if onclick open is set then no more actions required
	if (o_item.getprop('expd_delay') < 0)
		return;

	// run expand timer if we have to (cpu intensive)
	if (o_item.getprop('expd_delay'))
		this.o_showtimer = setTimeout('A_MENUS['+ this.m_id +'].expand(' + n_id + ');',	o_item.getprop('expd_delay'));
	else
		this.expand(n_id);
}

function menu_onmouseover_maintain_highlight (table, n_id)
{
	if (n_id == table.n_id) return;
	table.a_index[n_id].set_style('hover');
	menu_onmouseover_maintain_highlight(table, table.a_index[n_id].o_parent.n_id);
}

// --------------------------------------------------------------------------------
// called when mouse button is pressed on menu item
// --------------------------------------------------------------------------------
function menu_onmousedown (n_id) {
	
	// lookup new item's object	
	var o_item = this.a_index[n_id];

	// apply mouse down style
	o_item.set_style('pressed');

	this.expand(n_id);
}


// --------------------------------------------------------------------------------
// menu item Class
function menu_item (o_parent, n_order) {

	// 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.getstyle = mitem_getstyle;
	this.upstatus = mitem_upstatus;
	this.adjust_y_position = mitem_adjust_y_position;
	this.draw = mitem_draw;
	this.rendercontrols = mitem_rendercontrols;
	this.popout = mitem_popout;
	this.popback = mitem_popback;
	this.setx = mitem_setx;
	this.sety = mitem_sety;
	this.setVisible = mitem_setVisibility;
	this.reset_position = mitem_reset_position;
	this.set_style = mitem_set_style;

	// original desired x and y 
	this.n_x = n_order
		? o_parent.a_children[n_order - 1].n_x + this.getprop('left')
		: o_parent.n_x + this.getprop('block_left');

	this.n_y = n_order
		? o_parent.a_children[n_order - 1].n_y + this.getprop('top')
		: o_parent.n_y + this.getprop('block_top');
		
	// current x and y
	this.xx = this.n_x;
	this.yy = this.n_y;

	// visual objects
	this.innerButton = null;
	this.button = null;
	this.shadow = null;
	
	// current state information
	this.isDrawn = false;
	this.isRendered = false;
	this.isVisible = false;
	this.style = 'normal';

	// 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++)
		new menu_item(this, n_order);
}

// --------------------------------------------------------------------------------
// sets the style of the object ['normal', 'hover', 'pressed'] (cpu intensive)
// ------------------------------------------------------------------------------------------
function mitem_set_style ( style )
{
	if (this.style == style) return;
	this.rendercontrols();
	
	if (style == 'hover')
	{
		this.button.className = this.getstyle(0, 1);
		this.innerButton.className = this.getstyle(1, 1);
		if (this.getprop('use_shadow'))
		{
			this.shadow.className = this.getstyle(2, 1);
			if (this.getprop('pop_out_effect'))
				this.popout();
		}
		this.style = 'hover';
	}
	else if (style == 'pressed')
	{
		this.button.className = this.getstyle(0, 2);
		this.innerButton.className = this.getstyle(1, 2);
		if (this.getprop('use_shadow'))
			this.shadow.className = this.getstyle(2, 2);
		this.style = 'pressed';
	}
	else
	{
		this.button.className = this.getstyle(0, 0);
		this.innerButton.className = this.getstyle(1, 0);
		if (this.getprop('use_shadow'))
		{
			this.shadow.className = this.getstyle(2, 0);
			if (this.getprop('pop_out_effect'))
				this.popback();
		}
		this.style = 'normal';
	}
}

// --------------------------------------------------------------------------------
// pop out the element from the rest of its peers
// ------------------------------------------------------------------------------------------
function mitem_popout ()
{
	this.shadow.style.zIndex = (this.n_depth*8+3);
	this.button.style.zIndex = (this.n_depth*8+4);
}

// --------------------------------------------------------------------------------
// undo a popout operation
// ------------------------------------------------------------------------------------------
function mitem_popback ()
{
	this.shadow.style.zIndex = (this.n_depth*8-1);
	this.button.style.zIndex = (this.n_depth*8);
}

// --------------------------------------------------------------------------------
//  adds the shadow and button to the document (cpu intensive)
// ------------------------------------------------------------------------------------------
function mitem_draw ()
{
	if (this.isDrawn) return;
	
	this.rendercontrols();
	document.getElementsByTagName('body')[0].appendChild(this.button);
	if (this.getprop('use_shadow'))
		document.getElementsByTagName('body')[0].appendChild(this.shadow);
	
	this.isDrawn = true;	
}

// --------------------------------------------------------------------------------
//  renders the 'shadow', 'button', and 'internalButton' placing them in memory (cpu intensive)
// ------------------------------------------------------------------------------------------
function mitem_rendercontrols ()
{
	if (this.isRendered) return;
	
	// generate the shadow element
	this.shadow = document.createElement('div');
	this.shadow.id = 'e' + this.o_root.n_id + '_' + this.n_id + 's';
	if (this.getprop('use_shadow'))
	{
		this.shadow.className = this.getstyle(2,0);
		this.shadow.style.position = 'absolute';
		this.shadow.style.top = '' + (this.yy+this.getprop('shadow_y')) + 'px';
		this.shadow.style.left = '' + (this.xx+this.getprop('shadow_x')) + 'px';
		this.shadow.style.width = '' + this.getprop('width') + 'px';
		this.shadow.style.height = '' + this.getprop('height') + 'px';
		this.shadow.style.zIndex = '' + (this.n_depth*8-1);
		this.shadow.style.visibility = 'hidden';
		this.shadow.onclick = new Function( 'A_MENUS[' + this.o_root.m_id + '].onclick(' + this.n_id + ');' );
		this.shadow.onmouseout = new Function( 'A_MENUS[' + this.o_root.m_id + '].onmouseout(' + this.n_id + ');' );
		this.shadow.onmouseover = new Function( 'A_MENUS[' + this.o_root.m_id + '].onmouseover(' + this.n_id + ');' );
		this.shadow.onmousedown = new Function( 'A_MENUS[' + this.o_root.m_id + '].onmousedown(' + this.n_id + ');' );	}
	else
	{
		this.shadow.style.visibility = 'hidden';
	}
	
	// generate little expand image (ie, ">>" or "+").
	var is_expandable_image = null;
	if (this.getprop('indicate_expandable') && this.a_config.length >= 4)
	{
		is_expandable_image = document.createElement('img');
		is_expandable_image.src = this.getprop('indicate_expandable_image');
		is_expandable_image.border = '0';
		is_expandable_image.hspace = '0';
		is_expandable_image.vspace = '0';
		is_expandable_image.align = 'right';
	}

	// generate the button element
	this.button = document.createElement('a');
	this.button.id = 'e' + this.o_root.n_id + '_' + this.n_id +'o';
	this.button.className = this.getstyle(0, 0);
	this.button.href = this.a_config[1];
	if (this.a_config[2] && this.a_config[2]['tw'])
		this.button.target = this.a_config[2]['tw'];
	this.button.style.position = 'absolute';
	this.button.style.top = '' + this.yy + 'px';
	this.button.style.left = '' + this.xx + 'px';
	this.button.style.width = '' + this.getprop('width') + 'px';
	this.button.style.height = '' + this.getprop('height') + 'px';
	this.button.style.zIndex = '' + (this.n_depth*8);
	this.button.style.visibility = 'hidden';
	this.button.onclick = new Function( 'A_MENUS[' + this.o_root.m_id + '].onclick(' + this.n_id + ');' );
	this.button.onmouseout = new Function( 'A_MENUS[' + this.o_root.m_id + '].onmouseout(' + this.n_id + ');' );
	this.button.onmouseover = new Function( 'A_MENUS[' + this.o_root.m_id + '].onmouseover(' + this.n_id + ');' );
	this.button.onmousedown = new Function( 'A_MENUS[' + this.o_root.m_id + '].onmousedown(' + this.n_id + ');' );
	
	this.innerButton = document.createElement('div');
	this.innerButton.id = 'e' + this.o_root.n_id + '_' + this.n_id +'i';
	this.innerButton.className = this.getstyle(1, 0);
	var txt = document.createTextNode(this.a_config[0]);
	if (is_expandable_image) this.innerButton.appendChild(is_expandable_image);
	this.innerButton.appendChild(txt); 
	this.button.appendChild(this.innerButton);

	this.isRendered = true;
}

// --------------------------------------------------------------------------------
//  sets the visibility of the control
// ------------------------------------------------------------------------------------------
function mitem_setVisibility (val)
{
	if (this.isVisible == val) return;
	if (val == true)
	{
		this.draw();
		this.button.style.visibility = 'visible';
		if (this.getprop('use_shadow'))
			this.shadow.style.visibility = 'visible';
	}
	else
	{
		this.rendercontrols();
		this.button.style.visibility = 'hidden';
		this.shadow.style.visibility = 'hidden';
	}
	this.isVisible = val;
}

// --------------------------------------------------------------------------------
//  resets the position of the control and all of its children to the original values
// ------------------------------------------------------------------------------------------
function mitem_reset_position ()
{
	this.setx(this.n_x);
	this.sety(this.n_y);
	for (var i=0;i<this.a_children.length;i++)
		this.a_children[i].reset_position();
}

// --------------------------------------------------------------------------------
//  sets the x-position of the control
// ------------------------------------------------------------------------------------------
function mitem_setx (x)
{
	if (this.xx == x) return;
	this.xx = x;
	if (this.isRendered)
	{
		this.button.style.left = '' + this.xx + 'px';
		if (this.getprop('use_shadow'))
			this.shadow.style.left = '' + (this.xx+this.getprop('shadow_x')) + 'px';
	}
}

// --------------------------------------------------------------------------------
//  sets the y-position of the control
// ------------------------------------------------------------------------------------------
function mitem_sety (y)
{
	if (this.yy == y) return;
	this.yy = y;
	if (this.isRendered)
	{
		this.button.style.top = '' + this.yy + 'px';
		if (this.getprop('use_shadow'))
			this.shadow.style.top = '' + (this.yy+this.getprop('shadow_y')) + 'px';
	}
}

// --------------------------------------------------------------------------------
//  adjusts the y-positions of this element and all of its children (used to prevent elements from displaying below the screen)
// ------------------------------------------------------------------------------------------
function mitem_adjust_y_position (adjustment)
{
	this.sety(this.yy+adjustment);
	for (var i=0;i<this.a_children.length;i++)
		this.a_children[i].adjust_y_position(adjustment);
}

// --------------------------------------------------------------------------------
// 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);
}
// --------------------------------------------------------------------------------
// reads property from template file, inherits from parent level if not found
// ------------------------------------------------------------------------------------------
function mitem_getstyle (n_pos, n_state) {

	var a_css = this.getprop('css');
	var a_oclass;// = a_css[n_pos ? 'inner' : 'outer'];
	if (n_pos == 0) a_oclass = a_css['outer'];
	if (n_pos == 1) a_oclass = a_css['inner'];
	if (n_pos == 2) a_oclass = a_css['shadow'];

	// same class for all states	
	if (typeof(a_oclass) == 'string')
		return a_oclass;

	// inherit class from previous state if not explicitly defined
	for (var n_currst = n_state; n_currst >= 0; n_currst--)
		if (a_oclass[n_currst])
			return a_oclass[n_currst];
}

// ------------------------------------------------------------------------------------------
// 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 set_opacity  (control, opacity)
{
	if (navigator.appName=="Netscape")
		control.style.MozOpacity = opacity;
	if (navigator.appName.indexOf("Microsoft")!=-1)
		control.style.filter = 'alpha(opacity=' + opacity*100 + ')';
	control.style.opacity = opacity;
}