/*

	MyMenu 0.4(a) - Banana Version!

*/

var openmenu;
function myMenu (mid,direction,customAnchorClass,subdirection,classprefix) {
	var mymen = this;
	//this.menu = mid;
	this.menu = document.getElementById(mid);
	this.submenus;
	this.anchors;
	this.dir = direction;
	this.subdir = subdirection;
	this.customAClass = customAnchorClass;
	
	this.classprefix = '.mymenu';
	this.skin = new Array();

	this.removePx = function (val) { 
		if ( val.indexOf('px') ) {
			val = Number(val.substr(0,val.indexOf('px')));
			}
		return val;
		}

	this.getSkin = function () {
		// alt... 
		// Aus myWindow 1.0.2 kopiert
		var cssnode;
		for ( i = 0; i < document.styleSheets.length; i++ ) {
			if ( document.styleSheets[i].cssRules ) {
				// DOM
				for ( x = 0; x < document.styleSheets[i].cssRules.length; x++ ) {
					cssnode = document.styleSheets[i].cssRules[x];
					if ( cssnode.selectorText ) {
						if ( cssnode.selectorText.indexOf(this.classprefix) > -1 ) {
							this.skin[cssnode.selectorText] = cssnode; // this.skin['.' + this.classprefix + 'SELECTORNAME'].style.PROPERTY;
							}
						}
					}
				}
			else {
				// Microsoft
				for ( x = 0; x < document.styleSheets[i].rules.length; x++ ) {
					cssnode = document.styleSheets[i].rules[x];
					if ( cssnode.selectorText.indexOf(this.classprefix) > -1 ) {
						this.skin[cssnode.selectorText] = cssnode; // this.skin['.' + this.classprefix + 'SELECTORNAME'].style.PROPERTY;
						}
					}
				}
			}
		return;
		}


	this.getNodes = function (name) {
		if (mymen.menu.childNodes.length > 1) {
			var ch;
			var nodes = new Array();
			for ( i = 0; i < mymen.menu.childNodes.length; i++ ) {
				ch = mymen.menu.childNodes[i];
				if ( ch.nodeName.toLowerCase() == name ) {
					nodes[nodes.length] = ch;
					}
				}
			}
		return nodes;
		}
		
	this.getSubMenuNodes = function (submenu,name) {
		if (submenu.childNodes.length > 1) {
			var ch;
			var nodes = new Array();
			for ( i = 0; i < submenu.childNodes.length; i++ ) {
				ch = submenu.childNodes[i];
				if ( ch.nodeName.toLowerCase() == name ) {
					nodes[nodes.length] = ch;
					}
				}
			}
		return nodes;
		}		
			
	this.activate = function () {
		if ( openmenu ) {
			openmenu.disable();
			}
		
		var parent; var x; var y; var w; var h; var subwidth; var nx;
		for ( i = 0; i < mymen.submenus.length; i++ ) {
			
			parent = mymen.submenus[i].parentNode;
			x = parent.offsetLeft;		
			y = parent.offsetTop;			
			w = parent.offsetWidth;
			h = parent.offsetHeight;
			// Display Menu:
			mymen.submenus[i].style.display = 'block';		
			
			switch(mymen.subdir) {
				case 'ltor' : // left to right
					switch(mymen.dir) {
						case 'horizontal' :	
							mymen.submenus[i].style.left = w + 'px';
							mymen.submenus[i].style.top = y + 'px';
						break;
						case 'vertical' :
							mymen.submenus[i].style.left = x + 'px';
							mymen.submenus[i].style.top = (y + (parent.offsetHeight -1)) + 'px';
						break;
						}
				break;
				case 'rtol' : // right to left 
					switch(mymen.dir) {
						case 'horizontal' :	
							mymen.submenus[i].style.right = x + 'px';
							mymen.submenus[i].style.top = y + 'px';
						break;
						case 'vertical' :
							subwidth = mymen.submenus[i].offsetWidth;
							nx = (x + w) - subwidth;
							mymen.submenus[i].style.left = nx + 'px';
							mymen.submenus[i].style.top = (y + (parent.offsetHeight -1)) + 'px';
						break;
						}
				break;
				}
			}
		}
	
	this.disable = function () {
		if ( openmenu && openmenu != mymen ) {
			openmenu.activate();
			}
		for ( i = 0; i < mymen.submenus.length; i++ ) {
			mymen.submenus[i].style.display = 'none';
			}
		}
	
	this.setSubMenus = function (z) {
		var ch;
		for ( i = 0; i < mymen.submenus.length; i++ ) {
			ch = this.submenus[i];
			ch.style.display = 'none';
			ch.style.position = 'absolute';
			ch.style.zIndex = z;
			ch.className = 'mymenuSubMenu';
			}
		}
	
	this.setAnchors = function () {
		for ( i = 0; i < mymen.anchors.lenght; i++ ) {
			/*if ( mymen.customAClass != '' ) {
				mymen.anchors[i].className = mymen.customAClass;
				}*/
		//	else {
				mymen.anchors[i].className = 'mymenuAnchor';
		//		}
			}
		}
		
	this.setSubAnchors = function () {
		for ( i = 0; i < mymen.anchors.length; i++ ) {
			/*if ( mymen.customAClass != '' ) {
				mymen.subAnchors[i].className = mymen.customAClass;
				}
			else { */
			//	mymen.aubAnchors[i].className = 'mymenuSubMenuAnchor';
			//	}
			}
		}	
			
	if ( this.menu ) {
		this.submenus = this.getNodes('div');
		this.anchors = this.getNodes('a');
		if ( this.anchors.length > 0 ) {
			this.setAnchors();
			}
			
		this.getSkin(); // Getting all in the document embedded CSS rules
		//alert(this.skin.length);
		
		
		this.subAnchors = this.getSubMenuNodes(this.submenus[0],'a');
		this.setSubAnchors();
		var subw = 0;
		for ( i = 0; i < this.subAnchors.length; i++) {
			subw += this.subAnchors[i].offsetWidth;
			// if ( i == 0) { alert(this.skin['.mymenuSubMenuAnchor'].style.paddingLeft); }
			subw += this.removePx(this.skin['.mymenuSubMenuAnchor'].style.paddingLeft);
			subw += this.removePx(this.skin['.mymenuSubMenuAnchor'].style.paddingRight);
			subw += this.removePx(this.skin['.mymenuSubMenuAnchor'].style.marginLeft);
			subw += this.removePx(this.skin['.mymenuSubMenuAnchor'].style.marginRight);
			}

		
		if ( this.submenus.length > 0 ) {
			this.setSubMenus(100);
			this.submenus[0].style.width = subw + 'px';
			}

		this.isopen = this.menu.getAttributeNode('isopen');
		if ( this.isopen && this.isopen.nodeValue == 'yes' ) {
			this.setSubMenus(10);
			this.activate();
			openmenu = this;
			}
		else {
			if ( this.menu.attachEvent ) {
				this.menu.attachEvent('onmouseover',mymen.activate);
				this.menu.attachEvent('onmouseout',mymen.disable);			
				}
			else {
				this.menu.addEventListener('mouseover',this.activate,false);
				this.menu.addEventListener('mouseout',this.disable,false);
				}
			this.activate();
			this.disable();
			}

		}
	}


function initMyMenu () {
	// Das funktioniert so nicht.
	var c = 0;
	var div = document.getElementsByTagName('div')[c];
	var men;
	
	if ( div ) {
		var atr;
		while(div) {
			atr = div.getAttributeNode('mymenu');
			if ( atr && atr != '' ) {
				atr.nodeValue.split(',');
				men = new myMenu (div,atr[0],'');
				}
			++c;
			div = document.getElementsByTagName('div')[c];
			}
		}
	}