/****************************************************************************************/
/*  Copyright (C) 2004 Rob Johnson <robjohnson@black-hole.com>                          */
/*                                                                                      */
/*  This program IS NOT free software; you may not use, redistribute or modify          */
/*  without the permission of the author.  For licensing and purchasing information     */
/*  please visit http://www.script-source.net/jshelp/ .                                 */
/*                                                                                      */
/*  For more information and a demo please visit http://www.script-source.net/jshelp/ . */
/****************************************************************************************/

function JSMenuLibClass() {
	this.init = function() {
		this.curPos = new mouseCoords(false);
		if(this.isIE()) document.onmousemove = window.JSMenuLib.eTrackCoords;
		else {
			window.captureEvents(Event.MOUSEMOVE);
			window.onmousemove = window.JSMenuLib.eTrackCoords;
		}
	}
	this.isIE = function() {
		return navigator.appName.indexOf('Microsoft') >= 0;
	}
	this.getLeft = function(obj) {
		return (obj.offsetParent==null ? obj.offsetLeft : obj.offsetLeft + this.getLeft(obj.offsetParent));
	}
	this.getTop = function(obj) {
		return (obj.offsetParent==null ? obj.offsetTop : obj.offsetTop + this.getTop(obj.offsetParent));
	}
	this.in_array = function(needle, haystack) {
		for(var i in haystack) {
			if(haystack[i] == needle) return true;
		}
		return false;
	}

	this.getElementByClassName = function(that, name) {
		for(var i=0; i<that.childNodes.length; ++i) {
			if(that.childNodes[i].className == name) return that.childNodes[i];
			else {
				var f = this.getElementByClassName(that.childNodes[i], name);
				if(f) return f;
			}
		}
		return false;
	}

	this.copyObjectByVal = function(obj, recursive) {
		var ret = new Object;
		for(var i in obj) {
	        if (recursive && typeof obj[i] == 'object') {
	            ret[i] = window.JSMenuLib.copyObjectByVal(obj[i], recursive);
	        }
	        else ret[i] = obj[i];
		}
	}

	this.eTrackCoords = function(e) {
		window.JSMenuLib.curPos.x = (window.JSMenuLib.isIE()) ? event.clientX : e.pageX;
		window.JSMenuLib.curPos.y = (window.JSMenuLib.isIE()) ? event.clientY : e.pageY;
		if(window.JSMenuLib.curPos.display) self.status = window.JSMenuLib.curPos.x + ' x ' + window.JSMenuLib.curPos.y;
	}

	function mouseCoords(display) {
		this.x = this.y = 0;
		this.display = display;
	
		this.isOver = function(that) {
			if(typeof(that) == 'undefined') return;
			if(that.style.visibilty == 'hidden') return;
			var left = window.JSMenuLib.getLeft(that);
			var top = window.JSMenuLib.getTop(that);
			if(window.JSMenuLib.isIE()) {
				left -= document.body.scrollLeft;
				top -= document.body.scrollTop;
			}
			var right = left + that.offsetWidth;
			var bot = top + that.offsetHeight;
			return (this.x > left) && (this.x < right) && (this.y > top) && (this.y < bot);
		}
	}
}
function JSMenuOffset(x, y) {
	this.x = x;
	this.y = y;
}


window.JSMenuStyleOptions = new Array(
	'itemWidth',
	'itemHeight',
	'backgroundColor',
	'backgroundImage',
	'fontColor',
	'fontWeight',
	'fontStyle',
	'fontFamily',
	'fontSize',
	'textDecoration',
	'linkColor',
	'linkStyle',
	'linkDecoration',
	'borderStyle',
	'borderColor',
	'borderWidth',
	'separatorColor',
	'separatorWidth',
	'separatorStyle',
	'horizontalPadding',
	'verticalPadding',
	'cursorStyle',
	'backgroundColorActive',
	'backgroundImageActive',
	'fontColorActive',
	'fontWeightActive',
	'fontStyleActive',
	'fontFamilyActive',
	'textDecorationActive',
	'fontSizeActive',
	'arrowImage',
	'arrowAlign'
);
function JSMenuStyle() {
	this.styleString = false;
	this.fontStyleString = false;
	this.initflag = false;
	this.arrowImages = new Array();
	this.lib = window.JSMenuLib;

	this.init = function() {
		if(this.initflag) return;

		if(!this.horizontalPadding) this.horizontalPadding = 0;
		if(!this.verticalPadding) this.verticalPadding = 0;

		if(!this.backgroundColor) this.backgroundColor = 'transparent';
		if(!this.fontColor) this.fontColor = '#000000';
		if(!this.fontWeight) this.fontWeight = 'normal';
		if(!this.fontStyle) this.fontStyle = 'normal';
		if(!this.textDecoration) this.textDecoration = 'none';
		if(!this.cursorStyle) this.cursorStyle = 'default';

		if(!this.borderWidth) this.borderWidth = '1';
		if(!this.borderStyle) this.borderStyle = 'solid';
		if(!this.borderColor) this.borderColor = '#000000';

		if(!this.separatorStyle) this.separatorStyle = this.borderStyle;
		if(!this.separatorWidth) this.separatorWidth = this.borderWidth;
		if(!this.separatorColor) this.separatorColor = this.borderColor;

		if(!this.arrowImage) this.arrowImage = '';
		if(!this.arrowAlign) this.arrowAlign = '';
		if(this.arrowImage) {
			var arrowkey = this.arrowImages.length;
			this.arrowImages[arrowkey] = new Image();
			this.arrowImages[arrowkey].src = this.arrowImage;
		}

		this.initflag = true;
	}

	this.copy = function() {
		this.init();
		var newstyle = new JSMenuStyle();
		for(var i=0; i<window.JSMenuStyleOptions.length; ++i) {
			eval("newstyle."+window.JSMenuStyleOptions[i]+"=this."+window.JSMenuStyleOptions[i]+";");
		}
		return newstyle;
	}

	this.cat = function(key, val) {
		if(!val) return '';
		var buf = '';
		buf += key + ':' +val+ ';';
		return buf;
	}

	this.getFontStyleString = function() {
		this.init();
		if(this.fontStyleString) return this.fontStyleString;

		var buf = '';

		buf += this.cat('color', this.fontColor);
		buf += this.cat('font-size', this.fontSize);
		buf += this.cat('font-weight', this.fontWeight);
		buf += this.cat('font-family', this.fontFamily);
		buf += this.cat('font-style', this.fontStyle);
		buf += this.cat('text-decoration', this.textDecoration);

		this.fontStyleString = buf;
		return this.fontStyleString;
	}
	this.getTDStyleString = function() {
		this.init();
		if(this.styleString) return this.styleString;

		var buf = '';

		buf += this.cat('height', this.itemHeight);

//		buf += this.cat('color', this.fontColor);
//		buf += this.cat('font-size', this.fontSize);
//		buf += this.cat('font-weight', this.fontWeight);
//		buf += this.cat('font-family', this.fontFamily);
//		buf += this.cat('font-style', this.fontStyle);
//		buf += this.cat('text-decoration', this.textDecoration);

		buf += this.cat('cursor', this.cursorStyle);
		buf += this.cat('padding', this.verticalPadding+' '+this.horizontalPadding);

		buf += this.cat('background-color', this.backgroundColor);
		if(this.backgroundImage) buf += this.cat('background', "url('"+this.backgroundImage+"')");

		this.styleString = buf;
		return this.styleString;
	}
}

window.JSMenuLib = new JSMenuLibClass(); window.JSMenuLib.init();
window.JSMenuCounter = window.JSMenuItemCounter = 0;


function JSMenu(menutype, menustyle, parent, style_off, style_on) {
	if(!menutype) menutype = 'v';
	this.menutype = (menutype.substr(0,1).toLowerCase() == 'h') ? 'horizontal' : 'vertical';
	this.menustyle = menustyle;
	this.style_on = style_on;
	this.style_off = style_off;
	this.timeout = 1000;
	this.offset = new JSMenuOffset(0, 0);
	this.scrictpositioning = false;

	this.align = 'right';
	this.valign = 'bottom';

	this.parent = parent;
	this.isRoot = (parent) ? false : true;
	this.menudiv = false;
	this.lib = window.JSMenuLib;
	this.initflag = false;
	this.menuid = ++window.JSMenuCounter;
	this.menuitems = new Array();
	this.triggers = new Array();
	this.triggerswap = new Array();
	this.triggercount = 0;
	this.timer;
	this.alignopts = new Array('top', 'bottom', 'left', 'right');
	this.visible = false;

	if(this.parent) {
		this.align = parent.align;
		this.valign = parent.valign;
		this.timeout = parent.timeout;
		this.offset = parent.offset;
		this.scrictpositioning = parent.scrictpositioning;
	}

	this.init = function() {
		if(!window.JSMenus) window.JSMenus = new Array();
		window.JSMenus[this.menuid] = this;

		for(var i=0; i<this.menuitems.length; ++i) {
			if(this.menuitems[i].childmenu) this.menuitems[i].childmenu.init();
		}

		this.initflag = true;
	}

	this.write = function() {
		document.write(this.writemenutable());
		document.write(this.writemenudivs());
	}
	this.writemenutable = function() {
		this.menustyle.init();

		var tablestylestr = tdstylestr = '';
		var dohorz = (this.isRoot && this.menutype == 'horizontal');
		var widthstylestr = (this.menustyle.itemWidth) ? 'width:'+this.menustyle.itemWidth+'px;' : '';
		if(!dohorz) tablestylestr += widthstylestr;
		else {
			if(this.menustyle.itemWidth) tablestylestr += 'width:'+(this.menustyle.itemWidth*this.menuitems.length)+'px;';
			tdstylestr += widthstylestr;
		}
		var rootmenu = this.getRootMenu();

		var buf = '';
		
		if (this.parent == null) buf += '<table id="mm' + i + '" class="top_menu_box" cellspacing="0" cellpadding="0" border="0">'
		  else buf += '<table id="mm' + i + '" class="dyn_menu_box" cellspacing="0" cellpadding="0" border="0">';
		  
		if(dohorz) buf += '<tr>';

		var menuref = "window.JSMenus["+this.menuid+"]";
		var arrowalign = (this.menustyle.arrowAlign) ? this.menustyle.arrowAlign : this.align;

		if(arrowalign != 'left' && arrowalign != 'right') arrowalign = this.align;


		for(var i=0; i<this.menuitems.length; ++i) {
			var menuitemref = menuref + ".menuitems["+i+"]";
			var showmenujs = hidemenujs = '';
			var arrowcode = '';

			if(this.menuitems[i].childmenu) {
				showmenujs += 'window.JSMenus['+this.menuitems[i].childmenu.menuid+'].showMenu(this);';
				//hidemenujs = 'window.JSMenus['+this.menuitems[i].childmenu.menuid+'].hideMenu();';
			}
			else {
				showmenujs += 'window.JSMenuItems['+this.menuitems[i].menuitemid+'].parent.doHideAllMenus();';
				showmenujs += 'window.JSMenuItems['+this.menuitems[i].menuitemid+'].parent.mouseOver(this, \''+this.menuid+'\');';
				hidemenujs += 'window.JSMenuItems['+this.menuitems[i].menuitemid+'].parent.mouseOut(this, \''+this.menuid+'\');';
			}
			hidemenujs += 'window.JSMenus['+rootmenu.menuid+'].hideMenu();'

			if(this.menuitems[i].childmenu && this.menustyle.arrowImage && this.parent != null) {
				arrowcode = ' class="mn_arrow"';
			}

			var tdstyle = '';
			var fontstyle = '';

			if(!dohorz) buf += '<tr>';
			
			
			
			
			if (this.act_link == this.menuitems[i].href) {

				buf += '<td class="item_link_act" ';
			}
			else {
			
				buf += '<td class="item_link" ';
			}
			

			
		
			
			
			
			
			buf += 'onmouseover="window.status=\''+this.menuitems[i].alttext+'\';'+showmenujs+'" ';
			buf += 'onmouseout="window.status=\'\';'+hidemenujs+'" ';
			buf += 'onclick="'+menuitemref+'.click();">';

			buf += '<div '+arrowcode+'>'+this.menuitems[i].title + '</div>';
			
			buf += '</td>';
			if(!dohorz) buf += '</tr>';
		}

		if(dohorz) buf += '</tr>';
		buf += '</table>';
		return buf;
	}
	this.writemenudivs = function() {
		var buf = '';
		for(var i=0; i<this.menuitems.length; ++i) {
			if(this.menuitems[i].childmenu) {
				var stylestr = 'visibility:hidden;position:absolute;';
				if(this.menustyle.itemWidth) stylestr += 'width:'+this.menustyle.itemWidth+';';

				buf += '<div id="jsmenudiv'+this.menuitems[i].childmenu.menuid+'" style="'+stylestr+'">';
				buf += this.menuitems[i].childmenu.writemenutable();
				buf += '</div>';

				buf += this.menuitems[i].childmenu.writemenudivs();
			}
		}

		return buf;
	}

	this.addItem = function(title, href, target, alttext) {
		var key = this.menuitems.length;
		this.menuitems[key] = new JSMenuMenuItem(title, href, target, alttext, this);
		this.menuitems[key].init();
		this.menuitems[key].init();
		return this.menuitems[key];
	}
	this.addMenu = function(title, href, target, alttext) {
		var key = this.menuitems.length;
		this.menuitems[key] = new JSMenuMenuItem(title, href, target, alttext, this);
		this.menuitems[key].childmenu = new JSMenu(this.menutype, this.menustyle, this);
		this.menuitems[key].init();
		return this.menuitems[key].childmenu;
	}

	this.getMenuDiv = function() {
		return document.getElementById('jsmenudiv'+this.menuid);
	}
	this.getRootMenu = function() {
		var that = this;
		while(that.parent) that = that.parent;
		return that;
	}

	this.showMenu = function(that) {
		if(!this.initflag) return;
		clearTimeout(this.timer);

		this.parent.doHideAllMenus();

		var thismenudiv = this.getMenuDiv();

		var rootmenu = this.getRootMenu();
		if(!that.id) that.id = 'menutrigger_' + this.menuid + '_' + ++rootmenu.triggercount;
		this.triggers[this.menuid] = that;

		this.mouseOver(that);
		
		
			var screenright = document.body.clientWidth + document.body.scrollLeft;
			var screenleft = screenright - document.body.clientWidth;
			
		var thisoffset = this.offset;
		var posleft = postop = 0;
		var thisdivheight = thismenudiv.offsetHeight;
		var thisdivwidth = thismenudiv.offsetWidth;

		if(this.parent.isRoot && this.menutype == 'horizontal') {
			if(this.valign == 'top') {
				postop = this.lib.getTop(that) - thisdivheight;
				posleft = this.lib.getLeft(that);
				if(this.menustyle.borderWidth) postop += (this.menustyle.borderWidth*1);
			} else { // bottom
				postop = this.lib.getTop(that) + that.offsetHeight;
				
				posleft = this.lib.getLeft(that) - ((document.body.clientWidth - 852) / 2);

				if(this.menustyle.borderWidth) postop -= (this.menustyle.borderWidth*1);
			}
		} else {
			if(this.align == 'left') {
				postop = this.lib.getTop(that) + thisoffset.y;
				posleft = this.lib.getLeft(that) - ((document.body.clientWidth - 852) / 2) - thisdivwidth - thisoffset.x;
			} else { // right
				postop = this.lib.getTop(that) + thisoffset.y;
				posleft = this.lib.getLeft(that) - ((document.body.clientWidth - 852) / 2) + that.offsetWidth + thisoffset.x;
			}
		}

				postop = 20;

				
		// try to keep the menu on the screen
		if(!this.scrictpositioning) {
			var divbot = postop + thisdivheight;
			var screenbot = document.body.clientHeight + document.body.scrollTop;
			var screentop = screenbot - document.body.clientHeight;

			if(divbot > screenbot) {
				if(this.menutype == 'horizontal' && this.parent.isRoot) postop = this.lib.getTop(that) - thisdivheight;
				else postop = screenbot - thisdivheight - 1;
			}
			else if(postop < screentop) {
				if(this.menutype == 'horizontal' && this.parent.isRoot) postop = this.lib.getTop(that) + that.offsetHeight;
				else postop = screentop;
			}
	
			var divright = posleft + thisdivwidth;


			if(divright > screenright) {
				if(this.menutype == 'horizontal' && this.parent.isRoot) posleft = screenright - thisdivwidth - 1;
				else posleft = this.lib.getLeft(that) - thisdivwidth;
 			}
			else if(posleft < screenleft) {
				if(this.menutype == 'horizontal' && this.parent.isRoot) posleft = screenleft + 1;
				else posleft = this.lib.getLeft(that) + that.offsetWidth;
			}
		}

		// position & display
		thismenudiv.style.top = postop;
		thismenudiv.style.left = posleft;
		thismenudiv.style.visibility = 'visible';
		this.visible = true;
	}

	this.mouseOver = function(that, menuid) {
		
		if (that.className == 'item_link_act') {
			that.className = 'item_link_act';
		}
		else {
			that.className = 'item_link_on';
		}
		
	}
	this.mouseOut = function(that, menuid) {
		
		if (that.className == 'item_link_act') {
			that.className = 'item_link_act';
		}
		else {
			that.className = 'item_link';
		}
		
	}

	this.hideMenu = function() {
		if(!this.initflag) return;
		var rootmenu = this;
		clearTimeout(rootmenu.timer);
		rootmenu.timer = setTimeout("window.JSMenus["+rootmenu.menuid+"].startDoHideMenu()", rootmenu.timeout);
	}
	this.startDoHideMenu = function() {
		this.doHideAllMenus();
	}
	this.doHideMenu = function() {
		this.getMenuDiv().style.visibility = 'hidden';
		//this.mouseOut(this.triggers[this.menuid]);
		if(this.triggers[this.menuid]) this.mouseOut(this.triggers[this.menuid]);
		//alert(this.getRootMenu().triggers[this.menuid]);
		this.visible = false;
	}

	this.doHideAllMenus = function() {
		var rootmenu = this.getRootMenu();
		clearTimeout(rootmenu.timer);
		for(var i=0; i<this.menuitems.length; ++i) {
			if(this.menuitems[i].childmenu) {
				this.menuitems[i].childmenu.doHideAllMenus();
				this.menuitems[i].childmenu.doHideMenu();
			}
		}
	}

	function JSMenuMenuItem(title, href, target, alttext, parent) {
		this.title = title;
		this.href = href;
		this.target = target;
		this.alttext = alttext;
		this.childmenu = false;
		this.parent = parent;
		this.menuitemid = ++window.JSMenuItemCounter;

		this.init = function() {
			if(!window.JSMenuItems) window.JSMenuItems = new Array();
			window.JSMenuItems[this.menuitemid] = this;

			if(!this.target) this.target = '_self';

			if(!this.href) this.href = '';
			var re = new RegExp("'", 'gi') ;
			this.alttext = (this.alttext) ? this.alttext : this.href;
			this.alttext = this.alttext.replace(re, '');

			if(!window.JSMenuWindowCount) window.JSMenuWindowCount = 0;
		}

		this.click = function() {
			if(!this.href) return;
			switch(this.target) {
				case '_self':
					document.location = this.href;
					break;
				case '_top':
					top.location = this.href;
					break;
				case '_blank':
					window.open(this.href, 'jswindow' + ++window.JSMenuWindowCount, 'resizable=yes,scrollbars=yes,toolbar=yes,location=yes,directories=yes,status=yes,menubar=yes');
					break;
				default:
					if(typeof(window.frames[this.target]) != 'undefined') {
						window.frames[this.target].location = this.href;
					}
			}

		}
	}
}


menu_style = new JSMenuStyle();
menu_style.arrowImage = 'arrow-right.png';





