/************************************************************
	Drop-down Menu module
	copyright (c) 2003-2005 by Andy V. Kasatkin (aka taker)
	http://www.taker.ru, taker@mail.ru
************************************************************/

var color_on  = '#FFFFFF';
var color_off = '#FFFFFF';
var color_div = '#0470b0';
var bg_color_on  = '#0470b0';
var bg_color_off = '#04498e';
var item_height = 28;
var deactivate_timeout = 2000; // msec

var roll_supported = (document.images) ? true : false;
var subnav_supported = false;
var timeout = 0;
var target;

var menu_counter = 0;
var item_counter = 0;
var active = null;
var menu_list = new Array;
var item_list = new Array;
var menu_over = false;
var menu_active = null;

var path_prefix = (english_version) ? '/eng' : '';

function Menu(xpos, ypos, width, picture, items) {
	this.id = menu_list.length;
	this.xpos = xpos;
	this.ypos = ypos;
	this.width = width;
	this.picture = picture;
	this.height = (items) ? items.length * item_height : 0;
	this.items = items;
	this.skin = menu_skin;
	menu_list[this.id] = this;
}

function Item(name, url, onclick) {
	this.id = item_list.length;
	this.name = name;
	this.url = url;
	this.onclick = onclick;
	this.skin = item_skin;
	this.div = item_div;
	item_list[this.id] = this;
}

function roll(name, picture) {
	if (roll_supported && document.images[name]) document.images[name].src = picture;
}

function preloadImg(picture) {
	var image = new Image();
	image.src = picture;
}

preloadImg(path_prefix + '/img/menu/active/about.gif');
preloadImg(path_prefix + '/img/menu/active/conference.gif');
preloadImg(path_prefix + '/img/menu/active/organizer.gif');
preloadImg(path_prefix + '/img/menu/active/participate.gif');
preloadImg(path_prefix + '/img/menu/active/sponsorship.gif');
preloadImg(path_prefix + '/img/menu/active/support.gif');
preloadImg(path_prefix + '/img/menu/active/visiting.gif');

// ======================================

function item_skin() {
	var div_start = (browser.nn4) ? '' : '<div id="item_div_' + this.id + '" style="background-color:' + bg_color_off + '">';
	var div_end = (browser.nn4) ? '' : '</div>';
	var onclick = (this.onclick) ? this.onclick : 'location.href=\'' + this.url + '\'';
	var str =
		'<tr>' +
		'<td class="TDMenu" onClick="' + onclick + '" onMouseOver="item_over(' + this.id + ');" onMouseOut="item_out(' + this.id + ');">' + div_start +
		'<table cellpadding="0" cellspacing="3" border="0"><tr valign="top">' +
		'<td><a href="' + this.url + '" class="dropmenu"' + ((this.onclick) ? ' onClick="' + this.onclick + '"' : '') + '>' + 
		'<img alt="" border="0" width="11" height="10" src="/img/dropmenu-arrow.gif"></a></td>' +
		'<td><a href="' + this.url + '" class="dropmenu"' + ((this.onclick) ? ' onClick="' + this.onclick + '"' : '') + '>' + this.name + '</a></td>' +
		'</tr></table>' + div_end +
		'</td>' +
		'</tr>\n';
	return str;
}

function item_div() {
	return '<tr><td bgcolor="' + color_div + '"><img src="/img/void.gif" alt="" border="0" width="1" height="1"></td></tr>\n';
}

function menu_skin() {
	var str = 
		'<div class="subnav" id="menu_' + this.id + '">';
	if (this.items) {
		str += 
			'<table cellpadding="0" cellspacing="0" border="0" width="' + this.width + '">\n' +
			'<tr><td align="right" bgcolor="' + bg_color_off + '"><img src="/img/white.gif" alt="" width="1" height="1" border="0"></td></tr>\n';
		for (var i = 0; i < this.items.length; i++) {
			if (i > 0) str += this.items[i].div();
			str += this.items[i].skin();
		}
		str +=
			'<tr><td align="center"><img src="/img/blue.gif" alt="" width="' + (this.width - 2) + '" height="1" border="0"></td></tr>\n' +
			'</table>';
	}
	str +=
		'</div>';
	return str;
}

// ======================================

function deactivate() {
	timeout = 0;
	if (active != null) {
		active.layer.hide();
		roll('menu' + active.id, path_prefix + '/img/menu/' + active.picture + '.gif');
		active = null;
	}
}

function over(item) {
	menu_over = true;
	menu_active = item;
	if (subnav_supported) {
		if (timeout != 0) { clearTimeout(timeout); timeout = 0; }
		if (active == null || active.id != item) {
			if (active) {
				active.layer.hide();
				roll('menu' + active.id, path_prefix + '/img/menu/' + active.picture + '.gif');
			}
			if (menu_list[item]) {
				menu_list[item].layer.show();
				active = menu_list[item];
				roll('menu' + active.id, path_prefix + '/img/menu/active/' + active.picture + '.gif');
			} else {
				active = null;
			}
		}
	}
}

function out(item) {
	menu_over = false;
	if (subnav_supported && (timeout == 0)) timeout = setTimeout("deactivate()", deactivate_timeout);
}

function item_over(item) {
	var id = item_list[item].id;
	if (browser.dom || browser.ie4) {
		layerRef('item_div_' + id).style.backgroundColor = bg_color_on;
//		layerRef('item_link_' + id).style.color = color_on;
	}
}

function item_out(item) {
	var id = item_list[item].id;
	if (browser.dom || browser.ie4) {
		layerRef('item_div_' + id).style.backgroundColor = bg_color_off;
//		layerRef('item_link_' + id).style.color = color_off;
	}
}

function inArea(checkX, checkY, areaX, areaY, areaW, areaH) {
	return ((checkX >= areaX) && (checkY >= areaY) && (checkX < areaX + areaW) && (checkY < areaY + areaH));
}

function mouseMove(e) {
	var x = (browser.netscape)? e.pageX : event.clientX;
	var y = (browser.netscape)? e.pageY : event.clientY;
	if ((browser.explorer) && (document.body.scrollTop)) y += document.body.scrollTop;
	if ((browser.explorer) && (document.body.scrollLeft)) x += document.body.scrollLeft;
	if (active) {
		if (inArea(x, y, active.xpos, active.ypos, active.width, active.height)) {
			if (timeout != 0) { clearTimeout(timeout); timeout = 0; }
		} else {
			if ((!menu_over) && (timeout == 0)) { timeout = setTimeout("deactivate()", deactivate_timeout); }
		}
	}
	return true;
}

function init() {
	for (var i = 0; i < menu_list.length; i++) document.writeln(menu_list[i].skin());
}

function activate() {
	for (var i = 0; i < menu_list.length; i++) {
		menu_list[i].layer = new Layer('menu_' + menu_list[i].id);
		menu_list[i].layer.move(menu_list[i].xpos, menu_list[i].ypos);
	}
	if (browser.netscape) document.captureEvents(Event.MOUSEMOVE);
	document.onmousemove = mouseMove;
	subnav_supported = (browser.dom || browser.nn4 || browser.ie4);
}

