// *********************
// conf section 	   *
// *********************

window.onload=createSubmenus;
//document.releaseEvents();
window.captureEvents(Event.FOCUS);
window.onfocus=hideMenu;

ns4 = (document.layers)? true:false;
ie4 = (document.all)? true:false;

// curently active menu
ACTIVE_MENU=0;
DOC=window.document;
C_BG_COLOR='#ffffff';
I_BG_COLOR='#764eae';
I_HILITE='#923461';
BORDER_HEIGHT=1;
ITEM_HEIGHT=30;
delayed=0;

// *********************
// data def  section   *
// *********************

// layers array
var lay=new Array();
var sb=new Array();

// **********************
// functions section    *
// **********************

function image_on(name, mode) {
	var common=name.replace('_up', '');	
	common=common.replace('_down', '');
	if (mode=='ordinary') document.images[name].src=active[common].src;
//	printSubmenu(common, name);
}

function image_off(name, mode) {
	var common=name.replace('_up', '');	
	common=common.replace('_down', '');
	if (mode=='ordinary') document.images[name].src=ordinary[common].src;
	if (mode=='disabled') document.images[name].src=disabled[common].src;
}

function createSubmenus() {
	var p;
	// creates submenus func, activated when document loaded completely
	for(var i=0;i<names.length; i++) {
		p=submenus[names[i]];
		if (p) {
			createSubmenu(names[i], 'up');
			createSubmenu(names[i], 'down');
		}	
	} 
}

function createSubmenu(name, mode) {
	// creates submenu for menu item 'name'; 
	// mode ~ {'up', 'down'}
	var anc=DOC.anchors[name+'_anchor_'+mode];
	if (!anc) return;
	var nm=name+'_'+mode;
	img_width=DOC.images[nm].width;
	img_height=DOC.images[nm].height;
	ITEM_HEIGHT=img_height;
	// container 
	c_width=img_width;
	c_height=(ITEM_HEIGHT+BORDER_HEIGHT)*submenus[name].length;
	c_x=DOC.anchors[name+'_anchor_'+mode].x;
	if (mode=='up')   c_y=DOC.anchors[name+'_anchor_'+mode].y+img_height;
	if (mode=='down') c_y=DOC.anchors[name+'_anchor_'+mode].y-c_height;
	v=createContainer(name, mode, c_x, c_y, c_width, c_height);
	createItems(v, submenus[name], mode);
}

function createContainer(name, mode, x, y, width, height) {
	nm=name+'_'+mode;
	id=name+'_layer_'+mode;
	lay[nm]=new Layer(1, window); 
	p=lay[nm];
	// set container props
	p.pageX=x;
	p.pageY=y;
	p.bgColor=C_BG_COLOR;
	p.clip.width=width;
	p.clip.height=height;
	p.visibility='hide';
//	p.id=id;
	p.x1=x;
	p.y1=y;
	p.x2=x+width;
	p.y2=y+height;
	p.onmouseover=hideMenuCanceled;
	p.onmouseout=hideMenuDelayed;
	return p;	
}

function createItems(parent, data, mode) {
	// parent - container layer
	// data - submenus array element
	var p, i;
	for (i=0; i<data.length; i++) { 
	p=new Layer(1, parent);
		// set container props
		p.left=0;
		if (mode=='up') {
			p.top=BORDER_HEIGHT+(BORDER_HEIGHT+ITEM_HEIGHT)*i;
		}
		if (mode=='down') {
			p.top=(BORDER_HEIGHT+ITEM_HEIGHT)*i;
		}
//		p.ref=data[i][1];
		p.bgColor=I_BG_COLOR;
		p.clip.width=parent.clip.width;
		p.clip.height=ITEM_HEIGHT;
		p.document.open();
		p.document.write(itemContents(data[i]));
		p.document.close();
		p.visibility='inherit'; 
		p.onmouseover=hiliteItem;
		p.onmouseout=unhiliteItem;
		p.document.ref=data[i][1];
		p.document.onclick=jumpTo;
	} 
}

function itemContents(data) {
	//returns formatted string
	// data=submenu[name]
	var s='<div class="cntl"><a class="cntl" href="'+data[1]+'">'+data[0]+'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</a></div>\n'; 
	return s;
}

function hiliteItem() {
	this.bgColor=I_HILITE;
}

function unhiliteItem() {
	this.bgColor=I_BG_COLOR;
}

function jumpTo(ev) {
	window.location.href=this.ref;
}

function menu_handler(name, mode) {
	var p;
	if (ACTIVE_MENU!=0) ACTIVE_MENU.visibility='hide';
	image_on(name, mode);
	p=lay[name];
	if (p) { ACTIVE_MENU=p; p.visibility='show'; }
	else ACTIVE_MENU=0;
}

function hideMenu(ev) {
	//test(ev.target);
//	if (!(ACTIVE_MENU==0) || (ev.target.href) || (ev.target.bgColor==I_HILITE)) { 
	if (((ACTIVE_MENU!=0) && ((ev.target.bgColor==I_HILITE) || (ev.target.bgColor==I_BG_COLOR))) || (ev.target.href) || (ACTIVE_MENU==0)) { 
		routeEvent(ev);
	}
	else {
		ACTIVE_MENU.visibility='hide';
		routeEvent(ev);
	}
} 

function hideMenuDelayed() {
	delayed=setTimeout('if (ACTIVE_MENU!=0) { ACTIVE_MENU.visibility="hide"; ACTIVE_MENU=0; delayed=0; }', 2000);
}

function hideMenuCanceled() {
	if (delayed!=0) clearTimeout(delayed);
}

function test(obj) {
	var s='';
	for (j in obj) s+=j+'='+obj[j]+'<br>\n';
	document.open();
	document.write(s);
	document.close();	
}