/*
	Menus
	Mark Simon, Comparity Training
	http://widgets.comparity.net
	Feel Free to use

	This script creates displays and manages dropdown or ribbon menus.
	To use:

	1.	Copy the menu.js file somewhere

	2.	Head Section:

		<script type="text/javascript" src="includes/menu.js"></script>
		<script type="text/javascript" src="includes/misc.js"></script>

		<script type="text/javascript">
			window.onload=function() {
				//	new menu(menuID,openStyle,type);
				new menu('menu','open','style','width');
			}
		</script>

	3. 	Create the following hierarchical structure:

		<ul id="menu"><li>Menu 1
				<ul><li>Item 1</li>
					<li>Item 2</li>
					<li>Item 3</li>
					<li>Item 4</li>
				</ul>
			</li>
			<li>Menu 2
				<ul><li>Item 1</li>
					<li>Item 2</li>
					<li>Item 3</li>
					<li>Item 4</li>
				</ul>
			</li>
			<li>Menu 3
				<ul><li>Item 1</li>
					<li>Item 2</li>
					<li>Item 3</li>
					<li>Item 4</li>
				</ul>
			</li>
		</ul>

		That is:

		(a)	You have a ul called "menu"
		(b)	The ul contains a set of nested lists.

	The id of your list above defaults to "menu".
	The class of your open menus defaults to "open".
	The type may be "menu" or "ribbon"; it defaults to "menu".

	The variables at the beginning of the function can be set
	by you. They are mostly colours, but the last is the width
	of a ribbon (defaults to 640-2 px);

	Unobtrusive JavaScript

	Unobtrusive JavaScript refers to JavaScript which is in no way
	embedded in your HTML code. The script resides entirely in external
	files.

	Naturally, your HTML will need to link to the code. In the sample Pages,
	there is a reference to an external file:

		<script type="text/javascript" src="....js"></script>

	You would possibly have several links to external files. Most of these
	would be links to libraries of code, such as these widgets.

	Generally, you would also have a link to a JavaScript file which
	contains your miscellaneous code, and, in particular, your startup
	code.

		<script type="text/javascript" src="includes/misc.js"></script>

	This linked script might include some startup code such as:

		window.onload=function() {
			//	new menu(menuID,openStyle,type);
			new menu('menu','open','menu');
			//	Other startup code

		}

	In these examples the startup has been included in the main HTML
	file for simplicity.

	Disclaimer:

	This script probably works and it does what it does.
	What it doesn’t do it doesn’t do. E & OE

	Widgets

	The aim of these widgets is to provide functionality with as little
	imposition on the HTML as possible.

	The widgets use unobtrusive JavaScript and CSS. JavaScript is also used
	to iron out the differences between browsers.

	Generally speaking, the widgets work on basic structures;
	if JavaScript is disabled, the structures will appear in their
	default formats.

	Doctype

	If you haven't already done so, you should start your page with
	a Doctype. This will encourage most browsers to behave themselves.
	You will get the best restults if you use XHTML Strict:

	<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
		"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">


*/

function menu(menuID,openStyle,type,width,colours) {
	colours=colours||Array();
	//	The following may be set (carefully)
	var MainBackground=			colours['MainBackground']		|| '#BFDBFF';
	var MainBorder=				colours['MainBorder']			|| '#A2B3CF';
	var MainHover=				colours['MainHover']			|| '#DFD5B1';
	var SecondaryBackground=	colours['SecondaryBackground']	|| '#D7E3F2';
	var SecondaryHover=			colours['SecondaryHover']		|| '#FFD44D';
	var Text=					colours['Text'] 				|| '#15428B';
	//	The following does the work

	menuID=menuID?menuID:'menu';
	openStyle=openStyle?openStyle:'open';
	if(!type||(type!='ribbon'&&type!='vertical')) type='horizontal';
	if(type=='vertical') {
		width=parseInt(width);
		var marginLeft=width+40;
		var liOffset=width+20;
	}

	var ululleft='8px';
	var ulultop='6px';
	//	Adjust for old IE
	/*@cc_on
		@if(@_jscript_version<5.8)
			ululleft='10px';
			ulultop='21px';
		@end
	@*/

	var tabs=new Array();
	var menu=document.getElementById(menuID);
	var li=menu.getElementsByTagName('li');
	li=menu.childNodes;
	var openTab=getCookies('tab');
	if(!openTab) openTab=0;
	for(var i=0,j=0;i<li.length;i++)
		if(li[i].nodeName.toLowerCase()=='li') {
			if(type=='ribbon' && openTab==j) {
				setCookie('tab',j);
				li[i].className=openStyle;
			}
			j++;
			tabs.push(li[i]);
			li[i].onclick=selectTab;
			if(type=='ribbon') li[i].getElementsByTagName('ul')[0].onclick=nothing;
		}

	var nextSibling=menu;
	do {
		nextSibling=nextSibling.nextSibling;
	} while(nextSibling && nextSibling.nodeType !== 1); // 1 == Node.ELEMENT_NODE

	if(type=='vertical') {
		nextSibling.style.marginLeft=marginLeft+'px';
		//nextSibling.style.styleFloat=nextSibling.style.cssFloat='left';
	}
	else nextSibling.style.clear="both";

	//document.getElementsByTagName('body')[0].onclick=selectTab;
	if(type!='ribbon') document.getElementsByTagName('html')[0].onclick=selectTab;

	function setCookie(name,value,date) {
		if(name==undefined||value==undefined) return false;
		//if(!name||!value) return false;
		value=escape(value);
		date=new Date(date);
		if(isNaN(date)) date="";
		else date="; expires="+date.toGMTString();
		var cookie=name+"="+value+date;
		document.cookie=cookie;
		return true;
	}

	function getCookies(name) {
		var item, items, i, c;
		c=new Array();
		items=document.cookie.split("; ");
		for (i in items) {
			item=items[i].split("=");
			c[item[0]]=item[1];
		}
		if(name) return c[name];
		else return c;
	}

	function selectTab(e) {
		e=e||window.event;
		var target=e.target||e.srcElement||null;
		//alert(target);
		for(var i=0;i<tabs.length;i++) {
			//alert(target.className);
			if(target==tabs[i]&&(type=='ribbon'||target.className!=openStyle)) {
				target.className=openStyle;
				if(type=='ribbon') setCookie('tab',i);
				else setCookie('tab',null);
			}
			else tabs[i].className='';
		}

		if(e.stopPropogation) e.stopPropogation();
		else e.cancelBubble=true;
	}
	function nothing(e) {
		e=e||window.event;
		if(e.stopPropogation) e.stopPropogation();
		else e.cancelBubble=true;
	}

	String.prototype.interpolate=function() {
			return this.replace(/(\[)(.*?)(\])/g,function() {
				return eval(arguments[2]);
			});
		};

	function addCSS(cssCode) {
		//  http://yuiblog.com/blog/2007/06/07/style/
		var styleElement=document.createElement('style');
		styleElement.type='text/css';
		//	The else doesn’t work with IE 5.5
		if(styleElement.styleSheet) styleElement.styleSheet.cssText=cssCode;
		else styleElement.appendChild(document.createTextNode(cssCode));
		var head=document.getElementsByTagName('head')[0];
		head.insertBefore(styleElement,head.childNodes[0]);
	}
var ColourCSS="\
ul#menu {\
	background-color: [MainBackground];\
	border-color: [MainBorder];\
	color: [Text];\
}\
\
ul#menu a {\
	color: [Text];\
}\
\
ul#menu li {\
	background-color: [MainBackground];\
	border-color: [MainBorder];\
}\
ul#menu li:hover {\
	background-color: [MainHover];\
}\
ul#menu li.[openStyle] {\
	background-color: [SecondaryBackground];\
	border-color: [MainBorder] [MainBorder] [SecondaryBackground] [MainBorder];\
}\
\
ul#menu ul {\
	background-color: [SecondaryBackground];\
	border-color: [MainBorder];\
}\
\
ul#menu ul li {\
	background-color: [SecondaryBackground];\
}\
ul#menu ul li:hover {\
	background-color: [SecondaryHover];\
}\
\
";

var DifferencesCSS;

if(type=='vertical'||type=='horizontal') {
	DifferencesCSS="\
ul#menu li {\
	position: relative;\
}\
ul#menu ul {\
	width: auto;\
	left: -1px;\
	white-space: nowrap;\
}\
ul#menu ul li {\
	float: none;\
	width: auto;\
	width: inherit;\
}\
";
if(type=='vertical') DifferencesCSS+="\
ul#menu {\
	margin-top: 6px;\
	float: left;\
	margin-right: 1em;\
	width: [liOffset]px;\
}\
ul#menu li {\
	clear: both;\
	width: [width]px;\
}\
ul#menu ul {\
	border-width: 1px 1px 1px 1px;\
}\
ul#menu li.open ul {\
	left: [liOffset]px;\
	top: -7px;\
}\
";
}
else if(type=='ribbon') DifferencesCSS="\
ul#menu {\
	width: [width];\
}\
ul#menu ul {\
	width: [width];\
}\
";

var EssentialCSS="\
ul#menu {\
	float: left;\
	margin-left: 0px;				/* IE */\
	padding-left: 0px;				/* Firefox */\
	margin-bottom: 28px;				/* Arbitrary: Used for next element */\
	border-style: solid;\
	border-width: 1px;\
}\
\
ul#menu li {\
	list-style-type: none;			/* Firefox */\
	padding: .5em 1em .5em 1em;		/* Arbitrary */\
	padding: 5px 10px 5px 10px;\
	float: left;\
	border-style: solid;\
	border-width: 0px 1px 1px 0px;	/* Tab-like appearance */\
	margin-bottom: -1px;			/* Show the bottom border */\
}\
\
ul#menu ul {\
	margin-left: 0px;				/* IE */\
	padding-left: 0px;				/* Firefox */\
	border-style: solid;\
	border-width: 0px 1px 1px 1px;\
	display: none;					/* Hide */\
	left: [ululleft];				/* IE<8: 10px; */\
	margin-top: [ulultop];			/* IE<8: 21px; */\
	position: absolute;\
}\
\
ul#menu li.[openStyle] ul {\
	display: block;					/* Show */\
}\
\
ul#menu ul li {\
	border: none;\
	margin-bottom: 0px;				/* Fit inside ul */\
";
var OtherCSS="\
ul#menu {\
	font-family: sans-serif;\
	font-size: 1em;\
}\
ul#menu a {\
	text-decoration: none;\
}\
ul#menu li {\
	font-size: .7em;\
	font-weight: bold;\
}\
\
ul#menu ul li {\
	font-size: 1em;		/* Same as parent */\
}\
\
";
	addCSS(DifferencesCSS.interpolate());
	addCSS(EssentialCSS.interpolate());
	addCSS(ColourCSS.interpolate());
	addCSS(OtherCSS);
}

