var isDOM = (document.getElementById ? true : false);
var isIE4 = ((document.all && !isDOM) ? true : false);
var isNS4 = (document.layers ? true : false);

var myWidth = 0;
var myHeight = 0;
var menuWidth = 0;
var imgWidth = 0;

function getRef(id) {
	if (isDOM) return document.getElementById(id);
	if (isIE4) return document.all[id];
	if (isNS4) return document.layers[id];
}
function getSty(id) {
	return (isNS4 ? getRef(id) : getRef(id).style);
}

var popTimer = 0; // Hide timeout.
function popOver(menuNum, itemNum) {
	clearTimeout(popTimer);
	hideAllBut(menuNum);
	targetNum = menu[menuNum][itemNum].target;
	if (targetNum > 0) {
		thisX = parseInt(menu[menuNum][0].ref.left) + parseInt(menu[menuNum][itemNum].ref.left);
		thisY = parseInt(menu[menuNum][0].ref.top) + parseInt(menu[menuNum][itemNum].ref.top);
		with (menu[targetNum][0].ref) {
			left = parseInt(thisX + menu[targetNum][0].x);
			top = parseInt(thisY + menu[targetNum][0].y);
			visibility = 'visible';
      }
   }
}
function popOut(menuNum, itemNum) {
	if ((menuNum == 0) && !menu[menuNum][itemNum].target){
		hideAllBut(0);
	}
	else
		popTimer = setTimeout('hideAllBut(0)', 500);
}

function getTree(menuNum, itemNum) {
	// Array index is the menu number. The contents are null (if that menu is not a parent)
	// or the item number in that menu that is an ancestor (to light it up).
	itemArray = new Array(menu.length);

	while(1) {
		itemArray[menuNum] = itemNum;
		// If we've reached the top of the hierarchy, return.
		if (menuNum == 0) return itemArray;
		itemNum = menu[menuNum][0].parentItem;
		menuNum = menu[menuNum][0].parentMenu;
   }
}

function hideAllBut(menuNum) {
	var keepMenus = getTree(menuNum, 1);
	for (count = 0; count < menu.length; count++)
	if (!keepMenus[count])
		menu[count][0].ref.visibility = 'hidden';
}
// *** MENU CONSTRUCTION FUNCTIONS ***
function Menu(isVert, popInd, x, y, width, overCol, backCol, borderClass, textClass) {
	// True or false - a vertical menu?
	this.isVert = isVert;
	// The popout indicator used (if any) for this menu.
	this.popInd = popInd
	// Position and size settings.
	this.x = x;
	this.y = y;
	this.width = width;
	// Colours of menu and items.
	this.overCol = overCol;
	this.backCol = backCol;
	// The stylesheet class used for item borders and the text within items.
	this.borderClass = borderClass;
	this.textClass = textClass;
	// Parent menu and item numbers, indexed later.
	this.parentMenu = null;
	this.parentItem = null;
	// Reference to the object's style properties (set later).
	this.ref = null;
}
function Item(text, href, frame, length, spacing, target, overCol, backCol, align, valign) {
	this.text = text;
	this.href = href;
	this.frame = frame;
	this.length = length;
	this.spacing = spacing;
	this.target = target;
	this.overCol = overCol;
	this.backCol = backCol;
	//this.align = align;
	//this.valign = valign;
	// Reference to the object's style properties (set later).
	this.ref = null;
}

function resizeImages() {
	if(!isNS4) {
		document.goldbed.width = imgWidth;
		document.mantra.width = imgWidth;
	}
}

function writeMenus() {
	if (!isDOM && !isIE4 && !isNS4) return;

	// detecting window width and height to set main menu size
	if(typeof(window.innerWidth) == 'number') {
		//Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	}
	else if(document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	}
	else if(document.body && (document.body.clientWidth || document.body.clientHeight)) {
		//IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	}
	menuWidth = (myWidth/8);
	imgWidth = (myWidth/2);

	for (currMenu = 0; currMenu < menu.length; currMenu++)
	with (menu[currMenu][0]) {
		// Variable for holding HTML for items and positions of next item.
		var str = '', itemX = 0, itemY = 0;

		// Remember, items start from 1 in the array (0 is menu object itself, above).
		// Also use properties of each item nested in the other with() for construction.
		for (currItem = 1; currItem < menu[currMenu].length; currItem++)
		with (menu[currMenu][currItem]) {
			var itemID = 'menu' + currMenu + 'item' + currItem;

			// The width and height of the menu item - dependent on orientation!
			if(currMenu == 0 && (!isDOM))
				length = menuWidth;

			var w = (isVert ? width : length);
			var h = (isVert ? length : width);

			// Create a span or layer text string with appropriate styles/properties.
			if (isDOM || isIE4) {
				str += '<span id="' + itemID + '" style="position: absolute; left: ' + itemX + '; top: ' + itemY + '; width: ' + w + '; height: ' + h + '; visibility: inherit; ';
				if (backCol) str += 'background: ' + backCol + '; ';
				str += '" ';
			}
			if (isNS4) {
				str += '<layer id="' + itemID + '" left="' + itemX + '" top="' + itemY + '" width="' + w + '" height="' + h + '" visibility="inherit" ';
				if (backCol) str += 'bgcolor="' + backCol + '" ';
			}
			if (borderClass) str += 'class="' + borderClass + '" ';

			// Add mouseover handlers and finish span/layer.
			if(target > 0)	{
				// Add mouseover handlers and finish span/layer.
				str += 'onMouseOver="popOver(' + currMenu + ',' + currItem + ')" onMouseOut="popOut(' + currMenu + ',' + currItem + ')"';
			}
			else	{
				if(currMenu > 1)	{
					// Add mouseover handlers and finish span/layer.
					str += 'onMouseOver="popOver(' + currMenu + ',' + currItem + ')" onMouseOut="popOut(' + currMenu + ',' + currItem + ')"';
				}
			}
			str += '>';

			// Add contents of item (default: table with link inside).
			// In IE/NS6+, add padding if there's a border to emulate NS4's layer padding.
			// If a target frame is specified, also add that to the <a> tag.


			if(isNS4)
			{
				str += '<table width="' + (w - 8) + '" border="0" cellspacing="0" cellpadding="' + (!isNS4 && borderClass ? 3 : 0) + '"><tr><td align="center" height="' + (h - 7) + '">' + '<a class="' + textClass + '" href="' + href + '"' + (frame ? ' target="' + frame + '">' : '>') + text + '</a></td>';
				if (target > 0) {

					// Set target's parents to this menu item.
					menu[target][0].parentMenu = currMenu;
					menu[target][0].parentItem = currItem;

					// Add a popout indicator.
					if (popInd) str += '<td class="' + textClass + '" align="right"><font size=-4 color="#FFECCC">' + popInd + '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font></td>';
				}
				str += '</tr></table>' + (isNS4 ? '</layer>' : '</div>');
				if (isVert) itemY += length + spacing;
				else itemX += length + spacing;
			}
			else
			{
				str += '<center><a class="' + textClass + '" href="' + href + '"' + (frame ? ' target="' + frame + '">' : '>') + text + '</a>';

				if (target > 0) {
					// Set target's parents to this menu item.
					menu[target][0].parentMenu = currMenu;
					menu[target][0].parentItem = currItem;

					// Add a popout indicator.
					if (popInd) str += '<font size=-4 color="#FFECCC">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' + popInd + '</font>';
				}
				str += '</center>' + (isNS4 ? '</layer>' : '</span>');

				if (isVert) itemY += length + spacing;
				else itemX += length + spacing;
			}
		}
		if (isDOM) {
			var newSpan = document.createElement('span');
			document.getElementsByTagName('body').item(0).appendChild(newSpan);
			newSpan.innerHTML = str;
			ref = newSpan.style;
			ref.position = 'absolute';
			ref.visibility = 'hidden';
//			document.write(str);
//			document.write("<br>");
		}
		// Insert a span tag to the end of the BODY with menu HTML in place for IE4.
		if (isIE4) {
			document.body.insertAdjacentHTML('beforeEnd', '<span id="menu' + currMenu + 'span" ' + 'style="position: absolute; visibility: hidden">' + str + '</span>');
			ref = getSty('menu' + currMenu + 'span');
		}

		// In NS4, create a reference to a new layer and write the items to it.
		if (isNS4) {
			ref = new Layer(0);
			ref.document.write(str);
			ref.document.close();
		}
		for (currItem = 1; currItem < menu[currMenu].length; currItem++) {
			itemName = 'menu' + currMenu + 'item' + currItem;
			if (isDOM || isIE4) menu[currMenu][currItem].ref = getSty(itemName);
			if (isNS4) menu[currMenu][currItem].ref = ref.document[itemName];
	   }
	}
	with(menu[0][0]) {
		ref.left = x;
		ref.top = y;
		ref.visibility = 'visible';
   }
}


/************************************************************ Start of Menu Definitions ************************************************************/

var menu = new Array();
var defOver = '#C5A78E'; // Default colours passed to most menu constructors
var defBack = '#A0613A'; // (just passed to functions, not a global variable - makes things easier to change later in bulk).
var defLength = 22; // Default 'length' of menu items - item height if menu is vertical, width if horizontal.

// If no target menu (popout) is desired, set it to 0. Likewise, if your site does not use
// frames, pass an empty string as a frame target.

//function Menu(isVert, popInd, x, y, width, overCol, backCol, borderClass, textClass) {
//function Item(text, href, frame, length, spacing, target, overCol, backCol, align, valign) {

// Menu 0 is the special, 'root' menu from which everything else arises.
menu[0] = new Array();
menu[0][0] = new Menu(false, '', 0, 82, 17, '#A0613A', '#A0613A', '', 'itemText');
menu[0][1] = new Item('Home', 'index.htm', '', 150, 0, 0,"#FFECCC","#CCB4A2");
menu[0][2] = new Item('Profile', 'profile.htm', '', 150, 0, 0,"#FFECCC","#C5A78E");
menu[0][3] = new Item('Products', 'products.htm', '', 150, 0, 3,"#FFECCC","#BD9675");
menu[0][4] = new Item('Specifications', 'specifications.htm', '', 150, 0, 0,"#FFECCC","#B88B68");
menu[0][5] = new Item('Standards', 'standards.htm', '', 150, 0, 5,"#FFECCC","#B07E59");
menu[0][6] = new Item('Process', 'process.htm', '', 150, 0, 6,"#FFECCC","#AB7451");
menu[0][7] = new Item('Enquiry', 'enquiry.htm', '', 150, 0, 0,"#FFECCC","#A56A44");
menu[0][8] = new Item('Contact Us', 'contactus.htm', '', 150, 0, 0,"#FFECCC","#A0613A");

/*********** First Level of Menu ***********/

// Home menu.
menu[1] = new Array();
menu[1][0] = new Menu(false, '>>', 0, 18, 100, "#FFECCC", "#CCB4A2", 'itemBorder', 'itemText');

// Profile menu.
menu[2] = new Array();
menu[2][0] = new Menu(false, '>>', 0, 18, 100, "#FFECCC", "#C5A78E", 'itemBorder', 'itemText');

// Products menu
menu[3] = new Array();
menu[3][0] = new Menu(true, '>>', -6, 18, 170, "#FFECCC", "#C5A78E", 'itemBorder', 'itemText');
menu[3][1] = new Item('Mattresses', 'mattress.htm', '', defLength, 0, 9, "#FFECCC", "#BD9675");
menu[3][2] = new Item('Cushions&nbsp;&nbsp;&nbsp;', 'cushions.htm', '', defLength, 0, 10, "#FFECCC", "#BD9675");
menu[3][3] = new Item('Others&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;', 'otherprods.htm', '', defLength, 0, 11, "#FFECCC", "#BD9675");

// Specifications menu
menu[4] = new Array();
menu[4][0] = new Menu(false, '>>', 0, 18, 100, "#FFECCC","#B88B68", 'itemBorder', 'itemText');

// Standards menu
menu[5] = new Array();
menu[5][0] = new Menu(true, '>>', 10, 18, 140,"#FFECCC","#B07E59", 'itemBorder', 'itemText');
menu[5][1] = new Item('ISI', 'standards.htm#isi', '', defLength, 0, 0,"#FFECCC","#B07E59");
menu[5][2] = new Item('ISO 9001:2000', 'standards.htm#iso', '', defLength, 0, 0,"#FFECCC","#B07E59");

// Process menu
menu[6] = new Array();
menu[6][0] = new Menu(true, '>>', -3, 18, 165,"#FFECCC","#AB7451", 'itemBorder', 'itemText');
menu[6][1] = new Item('Curling of Coir', 'process.htm#curling', '', defLength, 0, 0,"#FFECCC","#AB7451");
menu[6][2] = new Item('Production of Bare Mattress', 'process.htm#bare', '', defLength, 0, 0,"#FFECCC","#AB7451");
menu[6][3] = new Item('Finishing', 'process.htm#finishing', '', defLength, 0, 0,"#FFECCC","#AB7451");

// Enquiry menu
menu[7] = new Array();
menu[7][0] = new Menu(false, '>>', 0, 18, 100,"#FFECCC","#A56A44", 'itemBorder', 'itemText');

// Contact Us menu
menu[8] = new Array();
menu[8][0] = new Menu(false, '>>', 0, 18, 100, "#FFECCC","#A0613A", 'itemBorder', 'itemText');

/*********** Second Level of Menu (Only Products) ***********/

// Mattress menu
menu[9] = new Array();
menu[9][0] = new Menu(true, '>>', 172, 10, 140, "#FFECCC", "#BD9675", 'itemBorder', 'itemText');
menu[9][1] = new Item('Fibroflex', 'mattress.htm#fibroflex', '', defLength, 0, 12, "#FFECCC", "#BD9675");
menu[9][2] = new Item('Koolflex&nbsp;', 'mattress.htm#koolflex', '', defLength, 0, 13, "#FFECCC", "#BD9675");

// Cushions menu
menu[10] = new Array();
menu[10][0] = new Menu(true, '>>', 172, 10, 150, "#FFECCC", "#BD9675", 'itemBorder', 'itemText');
menu[10][1] = new Item('Sofa Cushions & Bolsters', 'cushions.htm#sc', '', defLength, 0, 0, "#FFECCC", "#BD9675");
menu[10][2] = new Item('Folding Cool Cushions', 'cushions.htm#fcc', '', defLength, 0, 0, "#FFECCC", "#BD9675");
//menu[10][3] = new Item('Cane Chair Cushions', 'cushions.htm#ccc', '', defLength, 0, 0, "#FFECCC", "#BD9675");
//menu[10][4] = new Item('Bolsters', 'cushions.htm#bol', '', defLength, 0, 0, "#FFECCC", "#BD9675");

//Others Menu
menu[11] = new Array();
menu[11][0] = new Menu(true, '>>', 172, 10, 140, "#FFECCC", "#BD9675", 'itemBorder', 'itemText');
menu[11][1] = new Item('Carpet Underlays', 'otherprods.htm#carpet', '', defLength, 0, 0, "#FFECCC", "#BD9675");
menu[11][2] = new Item('Medical Beds', 'otherprods.htm#medical', '', defLength, 0, 0, "#FFECCC", "#BD9675");
menu[11][3] = new Item('Travel Beds', 'otherprods.htm#travel', '', defLength, 0, 0, "#FFECCC", "#BD9675");
menu[11][4] = new Item('Pillows', 'otherprods.htm#pillow', '', defLength, 0, 0, "#FFECCC", "#BD9675");
//menu[11][5] = new Item('Bath Mats', 'otherprods.htm#bath', '', defLength, 0, 0, "#FFECCC", "#BD9675");

/*********** Third Level of Menu (Only Mattresses) ***********/

// Fibroflex menu
menu[12] = new Array();
menu[12][0] = new Menu(true, '>>', 142, 10, 140, "#FFECCC", "#BD9675", 'itemBorder', 'itemText');
menu[12][1] = new Item('Fibroflex Gold', 'mattress.htm#ffg', '', defLength, 0, 0, "#FFECCC", "#BD9675");
menu[12][2] = new Item('Fibroflex Quilt', 'mattress.htm#ffq', '', defLength, 0, 0, "#FFECCC", "#BD9675");
menu[12][3] = new Item('Fibroflex Deluxe', 'mattress.htm#ffd', '', defLength, 0, 0, "#FFECCC", "#BD9675");

// Koolflex menu
menu[13] = new Array();
menu[13][0] = new Menu(true, '>>', 142, 10, 140, "#FFECCC", "#BD9675", 'itemBorder', 'itemText');
menu[13][1] = new Item('Koolflex Quilt', 'mattress.htm#kfq', '', defLength, 0, 0, "#FFECCC", "#BD9675");
menu[13][2] = new Item('Koolflex Deluxe', 'mattress.htm#kfd', '', defLength, 0, 0, "#FFECCC", "#BD9675");

/**************************************** End of Menu Definitions ****************************************/

var popOldWidth = window.innerWidth;
nsResizeHandler = new Function('if (popOldWidth != window.innerWidth) location.reload()');

if (isNS4) document.captureEvents(Event.CLICK);
document.onclick = clickHandle;

function clickHandle(evt)
{
	if (isNS4) document.routeEvent(evt);
	hideAllBut(0);
}

function moveRoot()
{
	with(menu[0][0].ref) left = ((parseInt(left) < 100) ? 100 : 5);
}
