//--Copyright (C) 2002 Neotek Limited (http://www.neotek.co.nz). All rights reserved. -->
/*
Add functionality to allow viewing of all products in a category and its sub categories (using setTheory).
Have also added the use of Visted links. This requires that 'MenuVisited' class be added to menu.css. This currently 
	will only be active on SetTheory menus. To disable, set the 'MenuVisited' class to the same as 'MenuUnselected'
*/
//i=integer, a=array, s=string, c=constant
var aMenuItem = new Array();
var cMenuItemDim = 14;
var cParent = 0;
var cLastChild = 1;
var cId = 2;
var cText = 3;
var cLinkId = 4;
var cReference = 5;
var cIndent = 6;
var cIsNode = 7;
var cIsLoaded = 8;
var cIsActive = 9;
var cFirstChild = 10;
var cNext = 11;
var cPrevious = 12;
var cImage = 13;
var aLink = new Array();
var cLinkDim = 2;
var cRef = 0;
var cTarget = 1;

var iMenuWidth = 190;
var iTitleChars = 32;
var sHiddenMenuItems = ''; //csv

var ShowMenuItemNo = 0;
var menuScrollTop = 0;
var basketScrollTop = 0;
var BasketTotalReady = false;
var iLink = 0;

var admin = false;
var iCurMenuType = 0;
var iCurMenuItem = 0;
var oTargetMenu;

var setTheory = false;
if (typeof(UseSetTheory)!='undefined'){	//Make Backward compat
	setTheory=UseSetTheory;
}
				
function MenuItemIndex(iItem) {
	return (iItem - 1) * cMenuItemDim;
}

function MenuItemIndentAmount(iItem) {
	var idx = MenuItemIndex(iItem);
	var iIndent = 0;
	while (aMenuItem[idx] != 0) {
		idx = MenuItemIndex(aMenuItem[idx]);
		iIndent = parseInt(iIndent) + parseInt(aMenuItem[idx + cIndent]);
	} 
	return iIndent;
}

function setParentIndent(iItem, iIndent) {
	return setIndent(aMenuItem[MenuItemIndex(iItem)], iIndent);
}

function setIndent(iItem, iIndent) {
	var idx = MenuItemIndex(iItem);
	if (aMenuItem[idx + cIndent] == 0) {
		iIndent = 0;
		while (aMenuItem[idx] != 0) {
			idx = MenuItemIndex(aMenuItem[idx]);
			iIndent = parseInt(iIndent) + parseInt(aMenuItem[idx + cIndent]);
		}
		idx = MenuItemIndex(iItem);
		aMenuItem[idx + cIndent] = iIndent - iIndent;
	}
	return aMenuItem[idx + cIndent];
}

function addMenuItem(iParent, id, text, linkId, linkRef, isNode, isActive, imageURL) {
	idx = aMenuItem.length;
	var iPos = idx / cMenuItemDim + 1;
	aMenuItem[idx++] = iParent;
	aMenuItem[idx++] = 0;
	aMenuItem[idx++] = id;
	aMenuItem[idx++] = text;
	aMenuItem[idx++] = linkId;
	aMenuItem[idx++] = linkRef;
	aMenuItem[idx++] = 0;
	aMenuItem[idx++] = isNode;
	aMenuItem[idx++] = !isNode;
	aMenuItem[idx++] = isActive;
	aMenuItem[idx++] = 0;
	aMenuItem[idx++] = 0;
	var iPrevious=0;
	if (iParent != 0) {
		iPrevious = aMenuItem[MenuItemIndex(iParent) + cLastChild];
		aMenuItem[MenuItemIndex(iParent) + cLastChild] = iPos;
		if (iPrevious == 0) {
			aMenuItem[MenuItemIndex(iParent) + cFirstChild] = iPos;
		}
		aMenuItem[MenuItemIndex(iParent) + cIsLoaded]=true;
	}else{
		iPrevious=iPos-1
	}
	aMenuItem[idx++] = iPrevious;
	if (iPrevious!=0){
		aMenuItem[MenuItemIndex(iPrevious) + cNext] = iPos;
	}
	aMenuItem[idx++] = imageURL;
	return iPos;
}

function addLink(Ref, Target) {
	idx = aLink.length;
	var iPos = idx / cLinkDim + 1;
	aLink[idx++] = Ref;
	aLink[idx++] = Target;
	return iPos;
}

function MenuItemCount() {
	return  aMenuItem.length / cMenuItemDim;
}

function MenuItemChild(iItem) {
	return aMenuItem[MenuItemIndex(iItem) + cLastChild];
}

function MenuItemLevel(iItem) {
	if (aMenuItem[MenuItemIndex(iItem)] == 0)
		return 1;
	else
		return MenuItemLevel(aMenuItem[MenuItemIndex(iItem)]) + 1;
}

function MenuItemIndexPath(iItem) {
	if (aMenuItem[MenuItemIndex(iItem)] == 0)
		return iItem;
	else
		return MenuItemIndexPath(aMenuItem[MenuItemIndex(iItem)]) + 
			',' + iItem;
}

function Menu(iItem, container) {
	ShowMenuItemNo = iItem; //container
	curMenuItemIndex = aMenuItem[MenuItemIndex(iItem)];
	try{
		container.writeln('<table width="' + iMenuWidth + '" border="0" cellspacing="5" cellpadding="0">');
		container.writeln('<div width="100%" id="div1">');
		MenuHTML(iItem, container);
		container.writeln('</div>');
		container.writeln('</TABLE>');
	}catch(e){
		container.innerHTML='<table width="' + iMenuWidth + '" border="0" cellspacing="5" cellpadding="0"><div width="100%" id="div1">' + MenuHTML(iItem,null) + '</div></TABLE>'
	}
	
}

function MenuHTML(iItem, container) {
	var iParent = iItem;
	var str = '';
	idx = MenuItemIndex(iItem);
	iLevel = MenuItemLevel(iItem);
	if (iLevel != 1) {
		if (aMenuItem[MenuItemIndex(iItem) + cIsActive] || admin){
			if (container==null){
				str = str + MenuItemHTML(iItem) + '<div width="100%" ID="div'+iItem+'" STYLE="display:none">';
			}else{
				container.writeln(MenuItemHTML(iItem) + '<div width="100%" ID="div'+iItem+'" STYLE="display:none">');
			}
		}
	}
	if (aMenuItem[idx+cFirstChild]!=0) {
		for (var find = aMenuItem[idx+cFirstChild]; find!=0; find=aMenuItem[MenuItemIndex(find)+cNext]){
			if (container==null){
				str = str + MenuHTML(find, null);
			}else{
				MenuHTML(find, container);
			}				
		}
	}
	if (iLevel != 1){
		if (container==null){
			str = str + '</div>';
		}else{
			container.writeln('</div>');
		}		
	}
	return str;
}

function InsertChildHTML(iItem, control, container, showItem) {
	idx = MenuItemIndex(iItem);
	if (showItem && (aMenuItem[MenuItemIndex(iItem) + cIsActive] || admin)) {
			control.innerHTML=control.innerHTML + MenuItemHTML(iItem) + '<div width="100%" ID="div'+iItem+'" STYLE="display:none">';
	}
	if (aMenuItem[idx+cFirstChild]!=0) {
		var childDiv=GetElement(container,'div' + iItem);
		for (var find = aMenuItem[idx+cFirstChild]; find!=0; find=aMenuItem[MenuItemIndex(find)+cNext]){
			InsertChildHTML(find,childDiv,container,true);					
		}
	}
	if (showItem) control.innerHTML=control.innerHTML + '</div>';
}

function MenuItemHTML(iItem) {
	idx = MenuItemIndex(iItem);
	s_Validation = new String(',' + sHiddenMenuItems + ',');
	if (s_Validation.indexOf(',' + aMenuItem[idx + cId] + ',') > -1) {
		if (aMenuItem[idx] == 0) return '';
	}
	var title = aMenuItem[idx + cText];
	var image = aMenuItem[idx + cImage];
	var menuitemid = aMenuItem[idx + cId];
	text = aMenuItem[idx + cText];
	iLevel = MenuItemLevel(iItem);
	if (text.length > iTitleChars-(iLevel*3)) text = text.substring(0,iTitleChars-(iLevel*3)) + '...';
	iLinkId = aMenuItem[idx + cLinkId];
	iLinkIndex = (iLinkId - 1) * 2;
	if (iLevel == 1) return '';
	iIndent = (iLevel - 2) * 13;		
	iRemainingWidth = iMenuWidth - iIndent;
	
	if (inMenuItem(iItem, curMenuItemIndex)) {
		sImage = '<img src="../../Owner/Images/menu/arrow.gif" width="7" height="8" border="0" align="right">';
		sCSSClass = 'Class=MenuSelected';
	} else {
		sImage = '';
		sCSSClass = 'Class=MenuUnselected';
	}
	
	if (aMenuItem[MenuItemIndex(iItem) + cIsActive]==false) sCSSClass=sCSSClass + ' style="color:gray"';
	
	//Here I am going to try to figure out which action to call (toggle or click)
	if(!setTheory || aMenuItem[MenuItemIndex(iItem) + cIsNode] == 0){
		sHRef = '<A id="n' + iItem + '" name="n' + iItem + '" href="" ' + sCSSClass + ' onclick="javascript: return top.nodeClick(' + iItem + ', ' + iLinkId + ', ';
		if (aLink[iLinkIndex + cRef] == '') {
			sHRef = 'parent.menu';
		} else {
			sHRef +=  aLink[iLinkIndex + cTarget];
		}
		sHRef += ', parent.menu)" ';
		sHRef +=' onMouseOver="return top.sSts(this);"';
		sHRef +=' onMouseDown="return top.sSts(this);"';
		sHRef +=' onMouseUp="return top.cSts();"';
		sHRef +=' onMouseOut="return top.cSts();"';
		sHRef += ' Title="' + title + '">';
	}else{
		sHRef = '';
		if(setTheory){
			sHRef = '<A href="" ' + sCSSClass + ' onclick="javascript: return top.toggleNode(' + iItem + ', parent.menu)" ';
			sHRef +=' onMouseOver="return top.sSts(this);"';
			sHRef +=' onMouseDown="return top.sSts(this);"';
			sHRef +=' onMouseUp="return top.cSts();"';
			sHRef +=' onMouseOut="return top.cSts();"';
			sHRef += ' Title="' + title + '" Style="padding:0px;margin:0px;vertical-align: middle"><img id="img'+iItem+'" src="../../Owner/Images/menu/+.gif" border="0" Style="padding:0px;margin:0px;vertical-align: middle"></A>';
		}
		sHRef += '<A id="n' + iItem + '" name="n' + iItem + '" href="" ' + sCSSClass + ' onclick="javascript: return top.nodeAction(' + iItem + ', ' + iLinkId + ', ';
		if (aLink[iLinkIndex + cRef] == '') {
			sHRef = 'parent.menu';
		} else {
			sHRef +=  aLink[iLinkIndex + cTarget];
		}
		sHRef += ', parent.menu)" ';
		sHRef +=' onMouseOver="return top.sSts(this);"';
		sHRef +=' onMouseDown="return top.sSts(this);"';
		sHRef +=' onMouseUp="return top.cSts();"';
		sHRef +=' onMouseOut="return top.cSts();"';
		sHRef += ' Title="' + title + '">';
	}
	if (image==null){
		sHRef += text;
	}else{
		sHRef += '<img onmouseover="top.Fade(this,60,10,5);" onmouseout="top.Fade(this,100,10,5);" style="FILTER: alpha(opacity=100)" src="' + image + '">';
	}
	sHRef += '</A>';
	
	if (admin) {
		sHRef += '<input ID="edit'+iItem+'" class="textbox" size="15" STYLE="display:none" onBlur="top.MenuItemNameSet(' + iItem + ',this.value, parent.menu);">';
	}
	sMenu = '<div width="100%" ';
	if (admin) sMenu += 'onmousedown="DragDrop(this)" onmouseup="DragStop()" ondragstart="DragStart(\'top.DropMenuItemParentSet(' + iItem + ', <this>);\')" ondragenter="DragEnter()" ondragover="DragOver(this)" ondragleave="DragLeave()" ondrop="drop(this)" mid="' + menuitemid + '" node="' + iItem + '" ';
	sMenu += 'id="ndiv' + iItem + '" STYLE="display:">';
	if (iIndent>0) sMenu += '<img src="../../Images/spacer.gif" width="' + iIndent + '" height="1" border="0">';
	sMenu += sHRef + sImage + '</div>';
	return sMenu;
}

function inMenuItem(iItem, cur_item) {
	if (iItem == cur_item)
		return true;
	else {
		if (cur_item == 0) 
			return false;
		else
			return inMenuItem(iItem, aMenuItem[MenuItemIndex(cur_item)]);
	}
}

function nodeClick(node_num, linkId, target, container) {
	oTargetMenu=container;
	var node = GetElement(container,'n' + node_num);
	if (iCurMenuItem!=0){
		if (setTheory){
			GetElement(container,'n' + iCurMenuItem).className = 'MenuVisited';
		}else{
			GetElement(container,'n' + iCurMenuItem).className = 'MenuUnselected';
		}
	}
	node.className = 'MenuSelected';
	iCurMenuItem=node_num;
	if (node.style.color == 'gray') return false;
	idx = MenuItemIndex(node_num);
	text = aMenuItem[idx + cText];
	iNodeId = aMenuItem[idx + cId];
	linkRef = aMenuItem[idx + cReference];
	iHRefId = (linkId - 1) * 2;
	if (aMenuItem[idx + cIsNode] == 0) {
		sURL = aLink[iHRefId];
		if (sURL.indexOf('mailto:')!=0){
			if (sURL.indexOf('?')==-1) 
				sURL+='?';
			else
				sURL+='&';
			sURL+=	'Id=' + iNodeId + '&Ref=' + linkRef; 
			top.showSplash();
		}
		target.location = sURL;
	} else {
		toggleNode(node_num, container);
	}		
	return false;
}
function nodeAction(node_num, linkId, target, container) {
	oTargetMenu=container;
	var node = GetElement(container,'n' + node_num);
	if (iCurMenuItem!=0){
		if (setTheory){
			GetElement(container,'n' + iCurMenuItem).className = 'MenuVisited';
		}else{
			GetElement(container,'n' + iCurMenuItem).className = 'MenuUnselected';
		}
	}
	node.className = 'MenuSelected';
	iCurMenuItem=node_num;
	if (node.style.color == 'gray') return false;
	idx = MenuItemIndex(node_num);
	text = aMenuItem[idx + cText];
	iNodeId = aMenuItem[idx + cId];
	linkRef = aMenuItem[idx + cReference];
	iHRefId = (linkId - 1) * 2;
	sURL = aLink[iHRefId];
	if (sURL.indexOf('mailto:')!=0){
		if (sURL.indexOf('?')==-1) 
			sURL+='?';
		else
			sURL+='&';
		sURL+= 'Id=' + iNodeId + '&Ref=' + linkRef; 
		top.showSplash();
		target.location = sURL;
		return false;
	}
	document.location.href=sURL;
	return false;
}

function nodeSelect(node_num, container) {
	if (iCurMenuItem!=0){
	
		if (setTheory){
			GetElement(container,'n' + iCurMenuItem).className = 'MenuVisited';
		}else{
			GetElement(container,'n' + iCurMenuItem).className = 'MenuUnselected';		
		}
	}
	
	GetElement( container, 'n' + node_num ).className = 'MenuSelected';
	iCurMenuItem=node_num;
	return false;
}

function toggleNode(node_num, container){
	oTargetMenu=container;
	var Node=GetElement(container,'div'+node_num);
	var img=GetElement(container,'img'+node_num);
	if (Node.style.display=="none"){
		idx = MenuItemIndex(node_num);
		if (aMenuItem[idx + cIsLoaded]) {
			Node.style.display="";
		}else{
			if (top.admin) {
				top.control.location.replace('menuItem.aspx?admin=1&parentNode=' + node_num + '&Id=' + aMenuItem[idx + cId] + '&TypeId=' + top.iCurMenuType);
			}else{
				top.control.location.replace('menuItem.aspx?parentNode=' + node_num + '&Id=' + aMenuItem[idx + cId] + '&TypeId=' + top.iCurMenuType);
			}
		}
		if(setTheory){img.src='../../Owner/Images/menu/-.gif';}
	}else{
		Node.style.display="none";
		if(setTheory){img.src='../../Owner/Images/menu/+.gif';}
	}
	return false;
}

function NodeHTML(iParent, sHTML){
	for (var find = aMenuItem[MenuItemIndex(iParent)+cFirstChild]; find!=0; find=aMenuItem[MenuItemIndex(find)+cNext]){
		if (aMenuItem[MenuItemIndex(find) + cIsActive] || admin){
			sHTML = sHTML + MenuItemHTML(find) + '<div width="100%" ID="div'+find+'" STYLE="display:none"></div>';
		}
	}
	aMenuItem[MenuItemIndex(iParent) + cIsLoaded]=true;
	return sHTML;
}

function GetElement(container,id){
	return container.document.getElementById(id);
}

function insertChildNodes(iParent,container){
	if (container==null) container=oTargetMenu;
	var sContent = NodeHTML(iParent, '');
	Node = GetElement(container,'div'+iParent);
	top.setText(Node,sContent);
	Node.style.display="";
}


