/*
 * 1. include this menu js file in your html pages that need menu bars
 * 2. include menu.css in your html pages that need menu bars
 * 3. make your body init <body onload="init()">
 * 4. in your page, make a div with the id="menu-bar"
 * 5. edit the list of menu items at the top of this file
 * 
 */
menuItems = [
     {"label": "Home",                    "link": "/index.html"}
    ,{"label": "EOHA Board and Minutes",  "link": "/552.html"}
    ,{"label": "Neighborly News",         "link": "/573.html"}
    ,{"label": "Real Estate",             "link": "/594.html"}
    ,{"label": "Newsletters",             "link": "/636.html"}
    ,{"label": "History and Stories",     "link": "/657.html"}
    ,{"label": "EHOA Events and Photos",  "link": "/678.html"} 
    ,{"label": "Yahoo Groups Site",       "link": "/599.html"} 
    ,{"label": "Links",                   "link": "/699.html"} 
/*    ,{"label": "",     "link": ""} */
];



var __d = document;
__d.get = __d.getElementById;
__d.ce = __d.createElement;
__d.ctn = __d.createTextNode;

util = {};
util.Dom = new function() {
	this.removeAllChildNodes = function(parent) {
		if (parent) {
			parent.innerHTML = '';
		}
	},
	appendStyle = function (media, styleId, selector, definitions) {
		var styleEl;
		//create style element
		if (__d.ce && (styleEl=__d.ce('style'))) {
			styleEl.type = 'text/css';
			styleEl.id = styleId;
			if( typeof(styleEl.media) == 'string' ) {
				try { styleEl.media = media; } catch(e) {}
			} else {
				styleEl.media.mediaText = media;
			}
			__d.getElementsByTagName('head').item(0).appendChild(styleEl);
			return styleEl;
		}
	},
	addRule = function(styleEl, selector, definitions) {
		if (styleEl) {
			var rules,sheet;
			if(styleEl["styleSheet"]) {
				sheet = styleEl.styleSheet;
			} else if(styleEl["sheet"]) {
				sheet = styleEl.sheet;
			}
			var ruleText = selector + ' { ' + definitions + ' } ';
			if( sheet.insertRule ) {
				sheet.insertRule(ruleText,0);
			} else if( sheet.addRule ) {
				sheet.addRule(selector,definitions);
			}
			//printfire('ask.util.Dom.addRule: '+ ruleText);
		}
	},
	setRule = function(styleEl, selector, styleName, styleValue) {
		if (styleEl) {
			var rules;
			if(styleEl["styleSheet"]) {
				rules=styleEl.styleSheet.rules;
			} else if(styleEl["sheet"]) {
				rules=styleEl.sheet.cssRules;
			}
			var foundRule = false;
			if (rules) {
				for(var i=0; i<rules.length; i++) {
					if(rules[i].selectorText.toUpperCase()==selector.toUpperCase()) {
						rules[i].style[styleName]=styleValue;
						//printfire('ask.util.Dom.setRule: '+ selector+ '{' + styleName + ':' + styleValue + '}');
						foundRule = true;
						break;
					}
				}
			}
			if (!foundRule) {
				this.addRule(styleEl,selector, styleName + ':'+styleValue+';');
			}
		}
	},
	getXY = function(el) {
		if (el.parentNode === null || el.style.display=='none') {
			return false;
		}
		var parent = null;
		var pos = [];
		var box;
		if (el.getBoundingClientRect) {
			box = el.getBoundingClientRect();
			var scrollTop = __d.documentElement.scrollTop || __d.body.scrollTop;
			var scrollLeft = __d.documentElement.scrollLeft || __d.body.scrollLeft;
			return[box.left + scrollLeft,box.top + scrollTop];
		}
		else if (__d.getBoxObjectFor) {
			box = __d.getBoxObjectFor(el);
			pos = [box.x,box.y];
		}
		else {
			pos = [el.offsetLeft,el.offsetTop];
			parent = el.offsetParent;
			if (parent != el) {
				while (parent) {
					pos[0] += parent.offsetLeft;
					pos[1] += parent.offsetTop;
					parent = parent.offsetParent;
				}
			}
			var ua = navigator.userAgent.toLowerCase();
			if (ua.indexOf('opera') != -1 || (ua.indexOf('safari') != -1 && el.style.position=='absolute')) {
				pos[1] -= __d.body.offsetTop;
			}
		}
		if (el.parentNode) {
			parent = el.parentNode;
		}
		else {
			parent = null;
		}
		while (parent && parent.tagName != 'BODY' && parent.tagName != 'HTML') {
			pos[0] -= parent.scrollLeft;
			pos[1] -= parent.scrollTop;
			if (parent.parentNode) {
				parent = parent.parentNode;
			}
			else {
				parent = null;
			}
		}
		return pos;
	},
	getX = function(el) {
		return this.getXY(el)[0];
	},
	getY = function(el) {
		return this.getXY(el)[1];
	},
	convertRGBToHex = function(rgbVal) {
		//rgba(180,0,0,0.60)
		var hexColor ='00000000';
		var vals = /rgba?\((.+?)\)/.exec(rgbVal);
		if (vals && vals.length==2) {
			hexColor = '';
			var parts = vals[1].split(',');
			for (var i=0;i<3;i++) {
				var hxc = ask.util.Math.dec2hex(parseInt(parts[i]));
				hexColor += hxc.length==1?hxc+''+hxc:hxc;
			}
			if (parts[3]) {
				var alpha = parseInt(parts[3]*10);
				alpha += 2;
				hexColor += alpha + '' + alpha;
			}
		}
		return hexColor;
	},
	getOpacity = function(el) {
		var value = 1;
		if (el) {
			if (el.filters) {// IE opacity
				try {
					value = el.filters.item('DXImageTransform.Microsoft.Alpha').opacity/100;
				} catch(e) {
					try {
						value = el.filters.item('alpha').opacity/100;
					} catch(e) {}
				}
			} else {
				value = el.style.opacity.toString().length>0 ? el.style.opacity : 1;
			}
		}
		return value;
	},
	setOpacity = function(el,opacity) {
		if (ask.util.Browser.isIE) {
			el.style.filter='alpha(opacity='+opacity+')';
		} else {
			el.style.opacity= opacity/100;
		}
	},

	/**
	 * Creates a DOM tree based on a descriptor.
	 * @method createTree
	 * @param desc:	descriptor; can be a string or an object; 
	 *				a strings specifies a text node; 
	 *				an object specifies an element and can have the following properties:
	 *		tag:	tag name
	 *		attr:	attributes object with properties corresponding to attributes;
	 *				values can be strings or objects;
	 *				example: { src: 'x.gif', style: { width: '10px', height: '10px' } }
	 *		key:	if present, the key is used to add the node to the map
	 *		skip:	skip this node
	 *		kids:	child node(s); can be a single descriptor or an array of descriptors
	 * @param map: optional node map; if specified, upon return map[desc.key] == node
	 * @return: DOM tree
	 */
	this.createTree = function(desc, map)
	{
		if (typeof desc == 'string') {
			return desc ? __d.ctn(desc) : null;
		}
		if (desc.skip) {
			return null;
		}
		var node = __d.ce(desc.tag);
		if (map && desc.key) {
			map[desc.key] = node;
		}
		if (desc.attr) {
			for (var i in desc.attr) {
				if (desc.attr.hasOwnProperty(i)) {
					if (typeof desc.attr[i] == 'string') {
						node[i] = desc.attr[i];
					}
					else {
						for (var j in desc.attr[i]) {
							if (desc.attr[i].hasOwnProperty(j)) {
								node[i][j] = desc.attr[i][j];
							}
						}
					}
				}
			}
		}
		if (desc.kids) {
			if (typeof desc.kids == 'object' && desc.kids instanceof Array) {
				for (var i = 0; i < desc.kids.length; i++) {
					var child = this.createTree(desc.kids[i], map);
					if (child) {
						node.appendChild(child);
					}
				}
			}
			else {
				var child = this.createTree(desc.kids, map);
				if (child) {
					node.appendChild(child);
				}
			}
		}
		if (desc.klass) {
			node.className = desc.klass;
		}
		return node;
	}
};

function init() {
	var mnu = {
		tag: 'ul'
		,attr: {id: 'simple-menu'}
		,kids: function() {
			var k=[], itm;
			for (var i=0; i<menuItems.length; i++) {
				itm = {
	                tag: 'li'
	                ,kids: {
		                	tag: 'a'
		                	,attr: {href: menuItems[i].link}
		                	,kids: menuItems[i].label
		                    ,klass: (window.location.pathname == menuItems[i].link) ? 'current' : ''
	                } 
	            };
	            k.push(itm);
			}
			return k;
		}()
	}
	var node = util.Dom.createTree(mnu);
	__d.get('menu-bar').appendChild(node);
}
