/*
 * menuScript.js
 * 
 * ProMIS Geographic & Thematic Layers CDROM project
 * June 2001
 *
 * Author: Andrew Ross (ross@undpafg.org.pk)
 * Programme Management Information System (ProMIS)
 * Food & Agriculture Organisation of the United Nations, Afghanistan.
 *
 */


var _IMAGE_UNSELECTED = 0;
var _IMAGE_SELECTED = 1;
var _IMAGE_ACTION = 2;
var _IMAGE_TARGET = 3;
var _IMAGE_VISIBLE = 4;

var _MENU_NAME = 0;
var _MENU_ARRAY = 1;
var _MENU_TARGET = 2;
var _MENU_OPEN = 3;

var currentMenuID = 0;
var currentImageID = 0;

var image_index;

// generateButtonCode()
function generateButtonCode( menuID )  {

	var _buttonCode = '<body bgcolor="#663333">';
	var _menu = MENULIST[menuID][_MENU_ARRAY];
	
	if( menuID == 0 )
		image_index = 0;
	
	for( var i=0; i < _menu.length; i++ )  {

		_buttonCode +=	'<A HREF="" ' +
					' onmouseover="parent.selectImage(' + image_index + ', '+ i + ', ' + menuID+  ')"' +
					' onmouseout="parent.unselectImage(' + image_index + ', '+ i + ', ' + menuID+  ')"' +
					' onClick="parent.clickAction(' + image_index + ', '+ i + ', ' + menuID+  '); return false;"' +
					'>\n' +
					'<IMG SRC="' +IMGDIR + _menu[i][_IMAGE_UNSELECTED] + '" ' +
					' WIDTH=' + WIDTH +
					' HEIGHT=' + HEIGHT +
					' BORDER=0 HSPACE=0 VSPACE=0 ALIGN="absmiddle"></A><br>\n'
		
		image_index++;
		
		if( _menu[i][_IMAGE_TARGET] == "" ) {
			if( MENULIST[MENUIDLIST[_menu[i][_IMAGE_ACTION]]][_MENU_OPEN] == true )  {
				_buttonCode += generateButtonCode( MENUIDLIST[_menu[i][_IMAGE_ACTION]] );
			}
		}

	}
	return _buttonCode;
}


// updateMenu()

function updateMenu()  {
		this.document.write( generateButtonCode( 0 ) );
		this.document.close();
}

// selectImage()
function selectImage( imageIndex, imageID, menuID )  {
	this.buttonFrame.document.images[imageIndex].src = IMGDIR+ MENULIST[menuID][_MENU_ARRAY][imageID][_IMAGE_SELECTED];
}

// unselectImage()
function unselectImage( imageIndex, imageID, menuID )  {
	this.buttonFrame.document.images[imageIndex].src = IMGDIR+ MENULIST[menuID][_MENU_ARRAY][imageID][_IMAGE_UNSELECTED];
}

// clickAction()
function clickAction( imageIndex, imageID, menuID )  {
	var _menu = MENULIST[menuID][_MENU_ARRAY];

	if( _menu[imageID][_IMAGE_TARGET] == "target" )  {
		this.MainFrameWindow.location.href = TARGETDIR + _menu[imageID][_IMAGE_ACTION];
	} else {
		MENULIST[MENUIDLIST[_menu[imageID][_IMAGE_ACTION]]][_MENU_OPEN] = MENULIST[MENUIDLIST[_menu[imageID][_IMAGE_ACTION]]][_MENU_OPEN] ? false : true;
		this.buttonFrame.document.write( generateButtonCode( 0 ) );
		this.buttonFrame.document.close();
	}
}

var MENUIDLIST = new Array();
for( var i=0; i < MENULIST.length; i++ )  {
	MENUIDLIST[ MENULIST[i][_MENU_NAME] ] = i;
}
