/* 
	MyMenu 2.0.1 (2009.1)
	Carsten Ruppert
	2009-02-19
	
	Copyright 2008 by HEAD. MARKETING-PARTNER
	http://www.headmarketing.de/

*/

function myMenuNode(node){
	var mynode = this;
	this.dropnode; // Dropdown Container
	this.align; // horizontal oder vertical
	this.dir; // ltor oder rtol 
	this.anchors = new Array(); // Anker innerhalb des Dropdowns
	
	/*
		Aktiviert (zeigt) Dropdown
	*/
	this.engage = function(){
		mynode.dropnode.style.display = 'block';
		//$(mynode.dropnode).fadeIn(400);
		return;
		}
	
	/*
		Deaktiviert (verbirgt) Dropdown
	*/
	this.disengage = function(){
		mynode.dropnode.style.display = 'none';	
		//$(mynode.dropnode).fadeOut(400);
		return;
		}
	
	/*
		Findet div element zum aufklappen
	*/
	this.getDropdown = function(){
		var child;
		for(i = 0; i < this.node.childNodes.length; i++){
			child = this.node.childNodes[i];
			if(child && child.nodeName.toLowerCase() == 'div'){
				child.id = "mydrop"+i;
				this.dropnode = child;
				return true;
				}
			}
		return false;
		}
	
	/*
		Setzt Ereignisüberwachung
	*/
	this.setEventListeners = function(){
		if(this.node.attachEvent){
			this.node.attachEvent('onmouseover',this.engage);
			this.node.attachEvent('onmouseout',this.disengage);
			return true;
			}
		else if(this.node.addEventListener){
			this.node.addEventListener('mouseover',this.engage,false);
			this.node.addEventListener('mouseout',this.disengage,false);
			return true;
			}
		else{
			return false;
			}
		}
	
	/*
		Richtet Dropdown/Untermenü aus
	*/
	this.adjustDropdown = function(){
		if(this.dir == "ltor"){
			if(this.align == 'horizontal'){
				this.dropnode.style.left = '100%';
				this.dropnode.style.top = '0';
				}else{
				this.dropnode.style.left = 0;
				this.dropnode.style.top = '100%';
				}
		}else{
			if(this.align == 'horizontal'){
				this.dropnode.style.right = '100%';
				this.dropnode.style.top = 0;
				}else{
				this.dropnode.style.left = 0;
				this.dropnode.style.top = '100%';
				}
			}
		}

	/*
		Constructor
	*/
	if(node){
		this.node = node;
		this.node.className = 'mymenuContainer';
		
		this.align = this.node.getAttributeNode('myalign');
		this.align = this.align ? this.align.nodeValue : undefined;
		this.dir = this.node.getAttribute('mydir');
		this.dir = this.dir ? this.dir.nodeValue : "ltor";
		if(this.getDropdown()){
			// Finde alle Anker innerhalb des Dropdowns und setzte CSS Klasse 
			for(i = 0; i < this.dropnode.childNodes.length; i++){
				if(this.dropnode.childNodes[i].nodeName.toLowerCase() == 'a'){
					this.anchors[this.anchors.length] = this.dropnode.childNodes[i];
					this.anchors[this.anchors.length -1].className = 'mymenuAnchor';
					}
				else if(this.dropnode.childNodes[i].nodeName.toLowerCase() == 'div' && this.dropnode.childNodes[i].getAttributeNode('mymenu')){
					for(var x = 0; x < this.dropnode.childNodes[i].childNodes.length; x++){
						if(this.dropnode.childNodes[i].childNodes[x].nodeName.toLowerCase() == 'a'){
							this.anchors[this.anchors.length] = this.dropnode.childNodes[i].childNodes[x];
							this.anchors[this.anchors.length -1].className = 'mymenuAnchor';
							}
						}
					}
				}
			// Setze Breite des Dropdowns anhand des breitesten Ankers im Dropdown
			var dropwidth = 0;
			for(z = 0; z < this.anchors.length; z++){
				dropwidth = this.anchors[z].offsetWidth > dropwidth ? this.anchors[z].offsetWidth : dropwidth;
				}
			
			this.dropnode.style.width = dropwidth +'px';
			this.dropnode.className = 'mymenuDropdown';
			this.adjustDropdown(); // Dropdown ausrichten
			this.setEventListeners(); // Event Handler setzen
			}
		}
	}


function myMenu(){
	var mymen = this;
	this.nodes = new Array();
	this.getNodes();
	}
myMenu.prototype.getNodes = function(){
		var c = 0; var div = null;
		// Instanzen von myMenuNode erzeugen:
		while(div = document.getElementsByTagName('div')[c]){
			var ismenu = div.getAttributeNode('mymenu');
			if(ismenu && ismenu.nodeValue == 'yes'){
				this.nodes[this.nodes.length] = new myMenuNode(div);
				}
			++c;
			}
		// 2009.1 schleife rückwärts anstatt vorwärts!
		for(var i = (this.nodes.length -1); i >= 0; i--){
			// Dropdowns verbergen
			// Die Schleige läuft rückwärts da die Untermenüs sonst nicht abgeschaltet werden
			this.nodes[i].disengage();
			}
		}

function runMyMenu(){
	var menu = new myMenu();
	}

window.addEventListener ? window.addEventListener('load',runMyMenu,false) : window.attachEvent('onload',runMyMenu);

