/* --------------------------------------------------------

Script: d3vMenu [DOM+IE4 OPT.] [ver. 0.2 / 2004-10-21] 
Copyright © 2004 Hristo Drumev [d3v] [d3v@hdrumev.com].
All rights reserved.

-------------------------------------------------------- */

function setform(formtext,searchtext)
{
	var $search = searchtext
	if (formtext.value==$search) {
		formtext.value='';
	} else if (formtext.value=="") {
		formtext.value=$search;
	}
}

function d3vMenu( id, data, options, settings )
{
	eval( ( this.id = id ) + '=this' );
	this.options = options;
	this.settings = settings;
	this.count = 0;
	this.popups = []; 
	this.items = [];
	this.showTimer = this.hideTimer = null;
	for( var i = 0; i < data.length; i++ )
		this.items[this.items.length] = new this.menuItem( this, null, data[i] );
	this.root = new this.popupMenu( this, null, this.items );
	for( var i = 0; i < this.popups.length; i++ )
		this.popups[i].write();
	this.root.show();
	return this
}

d3vMenu.prototype.menuItem = function( menu, parentItem, data )
{
	this.menu = menu;
	eval( ( this.id = this.menu.id + '_item' + this.menu.count++ ) + '=this' );
	this.parentItem = parentItem;
	this.data = data;
	this.level = this.parentItem == null ? 0 : this.parentItem.level + 1;
	this.items = [];
	if( this.data[3] )
		for( var i = 0; i < this.data[3].length; i++ )
			this.items[this.items.length] = new this.menu.menuItem( this.menu, this, this.data[3][i] );
	this.hasPopup = this.items.length > 0;
	this.popup = this.hasPopup ? new this.menu.popupMenu( this.menu, this, this.items ) : null;
	this.disabled = this.data[2] == '#' && !this.data[3];
	this.css = this.disabled ? this.menu.options.css.disabled : this.menu.options.css.normal;
	this.html = function()
	{
		return this.menu.options.html.cycle.replace( /\$item\$/i, '<table id="' + this.id + '"' + ( this.menu.options.size.auto ? ' width=100%' : ' width=' + this.menu.options.size.width + ' height=' + this.menu.options.size.height ) + ' border=0 cellpadding=0 cellspacing=0 style="background-color: ' + this.menu.options.color.normal + '"><tr><td' + ( this.menu.options.size.auto ? ' nowrap' : '' ) + ( this.data[1] && this.data[1] != '' && this.data[0] != this.data[1] ? ' title="' + this.data[1] + '"' : '' ) + ' onmouseover="' + this.id + '.over();" onmouseout="' + this.id + '.out();" onclick="' + this.id + '.click();" class="' + this.css + '" style="background-color: transparent;">' + this.data[0] + ( this.hasPopup ? '</td><td width=' + this.menu.options.images.imgOffset + ' align=left valign="middle" class="' + this.css + '" style="background-color: transparent;"><img src="' + this.menu.options.images.sub + '">' : '' ) + '</td></tr></table>' )
	}
	this.over = function()
	{
		this.setStyle( 0 );
		if( this.selfPopup.overItem != null && this.selfPopup.overItem !== this )
			this.selfPopup.overItem.hidePopups();
		if( this.hasPopup && !this.popup.showed && !this.menu.showTimer )
			this.menu.showTimer = window.setTimeout( this.popup.id + '.show()', this.menu.settings.delayShow )
		if( this.menu.hideTimer != null )
		{
			window.clearTimeout( this.menu.hideTimer );
			this.menu.hideTimer = null
		}
		this.selfPopup.overItem = this;
		return 1
	}
	this.out = function()
	{		
		if( this.menu.hideTimer == null )
			this.menu.hideTimer = window.setTimeout( this.menu.root.overItem.id + '.hidePopups()', this.menu.settings.delayHide )
		return 1
	}
	this.click = function()
	{
		document.location = this.data[2];
	}
	this.setupPosition = function()
	{
		this.left = this.element.offsetLeft;
		this.top = this.element.offsetTop;
		this.width = this.element.offsetWidth;
		this.height = this.element.offsetHeight;
		var parentElement = this.element.offsetParent;
		while( parentElement != null )
		{
			this.left += parentElement.offsetLeft;
			this.top += parentElement.offsetTop;
			parentElement = parentElement.offsetParent
		}
	}
	this.hidePopups = function()
	{
		var popupMenu = this.selfPopup;
		while( popupMenu != null )
		{
			if( popupMenu.overItem != null )
			{
				popupMenu.overItem.setStyle( 1 );
				if( popupMenu.overItem.hasPopup  )
				{
					if( this.menu.showTimer != null )
					{
						window.clearTimeout( this.menu.showTimer );
						this.menu.showTimer = null
					}
					if( !popupMenu.overItem.popup.showed )
						break;
					else
					{
						popupMenu = popupMenu.overItem.popup;
						popupMenu.hide()
					}
				}
				else
					break
			}
			else
				break
		} 
	}
	this.setStyle = function( target )
	{
		if( !this.disabled )
			this.element.style.backgroundColor = target ? this.menu.options.color.normal : this.menu.options.color.over;
	}
	return this
}

d3vMenu.prototype.popupMenu = function( menu, parentItem, items )
{
	this.menu = menu;
	eval( ( this.id = this.menu.id + '_popupMenu' + this.menu.popups.length ) + '=this' );
	this.menu.popups[this.menu.popups.length] = this;
	this.parentItem = parentItem;
	this.items = items;
	this.isRoot = this.parentItem == null;
	this.showed = 0;
	this.level = this.isRoot ? 0 : this.parentItem.level + 1;
	for( var i = 0; i < this.items.length; i++ )
		this.items[i].selfPopup = this;
	this.overItem = null;
	this.html = function()
	{
		var res = '';
		for( var i = 0; i < this.items.length; i++ )
			res += this.items[i].html();
		res = this.menu.options.html.container.replace( /\$ITEMS\$/i, res )
		res = '<div><div id="' + this.id + '" style="position: absolute; left: 1px; top: 1px; ' + ( window.opera ? '' : 'width: 1px; height: 1px; ' ) + 'display: none; z-index: ' + ( 9 + ( this.isRoot ? 1 : this.parentItem.level + 2 ) ) + ';"><table border=0 cellpadding=0 cellspacing=0><tr><td id="' + ( this.id + '_styleContainer' ) + '" class="' + this.menu.options.css.popup + '">' + res + '</td></tr></table></div></div>'; 
		return res
	}
	this.write = function()
	{
		document.write( this.html() );
		this.element = document.getElementById ? document.getElementById( this.id ) : document.all[this.id];
		this.styleContainer = document.getElementById ? document.getElementById( this.id + '_styleContainer' ) : document.all[this.id + '_styleContainer'];
		for( var i = 0; i < this.items.length; i++ )
			this.items[i].element = document.getElementById ? document.getElementById( this.items[i].id ) : document.all[this.items[i].id]
	}
	this.show = function()
	{
		this.element.style.top = -9999;
		this.element.style.left = -9999;
		this.element.style.display = 'inline';
		this.showed = 1;
		window.clearTimeout( this.menu.showTimer );
		this.menu.showTimer = null;
		this.setPosition()
	}
	this.hide = function()
	{
		this.element.style.display = 'none';
		this.showed = 0
	}
	this.setPosition = function()
	{
		if( this.isRoot )
		{
			this.element.style.left = this.menu.settings.x;
			this.element.style.top = this.menu.settings.y
		}
		else
		{
			this.parentItem.setupPosition();
			var pop = this.parentItem.menu.options.popupPos;
			var parentOffsetX = this.menu.settings.isHorizontal && this.level == 1 ? 0 : this.parentItem.width;
			var parentOffsetY = this.menu.settings.isHorizontal && this.level == 1 ? this.parentItem.height : 0;
			var x = pop.x + ( pop.auto ? this.parentItem.left + parentOffsetX : 0 );
			var y = pop.y + ( pop.auto ? this.parentItem.top + parentOffsetY : 0 );
			this.width = this.styleContainer.offsetWidth;
			this.height = this.styleContainer.offsetHeight;
			var x0 = document.body.scrollLeft;
			var y0 = document.body.scrollTop;
			var x1 = document.body.clientWidth;
			var y1 = document.body.clientHeight;
			var x2 = x0 + x1;
			var y2 = y0 + y1;
			if( ( x + this.width ) > ( x1 + x0 ) )
				if( x - this.width - parentOffsetX > x0 )
					x -= ( this.width + parentOffsetX + 2*pop.x - 1 );
				else
					x = this.width >= x1 ? x0 : x2 - this.width;
			if( ( y + this.height ) > ( y1 + y0 ) )
				y = this.height >= y1 ? y0 : y2 - this.height;
			this.element.style.left = x;
			this.element.style.top = y
		}
	}
	return this
}

