//nothing menu basiert auf script.aculo.us

RayMenu = Class.create();

RayMenu.prototype = {
	initialize: function(element,structure,showTimeout,hideTimeout) {
		this.element = $(element);
		this.structure = structure;
		this.showTimeout = showTimeout;
		this.hideTimeout = hideTimeout;
    this.currentlyExecuting = false;
    this.showHandle = null;
    this.hideHandle = null;
    this.showname = "";
    this.newname = "";
    this.self = this;
	},

	setSubMenu: function(name) {		
		var innerDiv = Builder.node('div',{id:'menu_inner'}); 
		innerDiv.appendChild(this.createSpacer());
		for(var i=0;i<this.structure[name].length;i++) {
			innerDiv.appendChild(this.createItem(this.structure[name][i]));
			innerDiv.appendChild(this.createSpacer());
		}
		//set aligment
		if( document.all )
			innerDiv.style.styleFloat = menuSettings[name].align; //ie
		else
			innerDiv.style.cssFloat = menuSettings[name].align; //the rest
		
		//if menu_inner exists, delete it
		var toDelete;
		if(toDelete=$('menu_inner')) Element.remove(toDelete);
		this.element.appendChild(innerDiv);
	},
	
	createSpacer: function() {
		return Builder.node('p',{className:'menu_spacer'});
	},
	
	createItem: function(structure) {
		return Builder.node('p',{className:'menu_item',onmouseover:'hoverSubMenuOn(this)',onmouseout:'hoverSubMenuOff(this)',onclick:"location.href='"+structure["link"]+"'"},[
			Builder.node('nobr',[
				Builder.node('a',{href:structure["link"]},structure["name"])
			])
		]);
	},
	
	showMenu: function() {
    if (!this.currentlyExecuting) {
      try { 
        this.currentlyExecuting = true;
				for(var which in menuStructure)
					document.images[which].src = menuSettings[which].imgOut.src;
				if(this.showname != "" && this.showname != "undefined" )
					document.images[this.showname].src = menuSettings[this.showname].imgOut.src;
				this.showname = this.newname;
				document.images[this.showname].src = menuSettings[this.showname].imgHover.src;
				this.setSubMenu(this.showname);
				if(!Element.visible(this.element)) {
				  new Effect.Appear(this.element,
			    { duration: 0.2, 
			      transition: Effect.Transitions.linear, 
			      to: 0.6 });
				}
      } finally { 
        this.currentlyExecuting = false;
      }
    } else {
    	printfire("currently executing");
    }
	},
	
	hideMenu: function() {
    if (!this.currentlyExecuting) {
      try { 
        this.currentlyExecuting = true;
				//all images back to initial state
				for(var which in menuStructure)
					document.images[which].src = menuSettings[which].imgNorm.src;
				this.showname = "";
				if(Element.visible(this.element)) {
				  new Effect.Fade(this.element,
			    { duration: 0.2, 
			      transition: Effect.Transitions.linear });
				}
      } finally { 
        this.currentlyExecuting = false;
      }
    }
	},
  
	onNavOver: function(name) {
		if(this.hideHandle != null) {
			clearTimeout(this.hideHandle);
			this.hideHandle = null;
		}
		this.newname = name;
		if(!Element.visible(this.element)) {
			this.showMenu();
			return;
		}
		if(this.showHandle != null) {
			clearTimeout(this.showHandle);
			this.showHandle = null;
		}		
		this.showHandle = setTimeout(this.showMenu.bind(this),this.showTimeout);
	},
	
	onNavOut: function() {
		if(this.showHandle != null) {
			clearTimeout(this.showHandle);
			this.showHandle = null;
		}
		if(this.hideHandle != null) {
			clearTimeout(this.hideHandle);
			this.hideHandle = null;
		}
		this.hideHandle = setTimeout(this.hideMenu.bind(this),this.hideTimeout);
	},
	
	onSubnavOver: function() {
		if(this.hideHandle != null) {
			clearTimeout(this.hideHandle);
			this.hideHandle = null;
		}
		if(this.showHandle != null) {
			clearTimeout(this.showHandle);
			this.showHandle = null;
		}
	},		
		
	onSubnavOut: function() {
		if(this.hideHandle != null) {
			clearTimeout(this.hideHandle);
			this.hideHandle = null;
		}
		this.hideHandle = setTimeout(this.hideMenu.bind(this),this.hideTimeout);		
	}
};

function initMenu() {
	//generate menu div
	menuDiv = Builder.node('div',{id:'menu',style:'display:none;'});
	$('shell').appendChild(menuDiv);
	//init menu
	mymenu = new RayMenu('menu',menuStructure,300,500);
	//preload pictures
	for( var subName in menuSettings ) {
		menuSettings[subName]["imgNorm"] = new Image();
		menuSettings[subName]["imgNorm"].src = document.images[subName].src;
		menuSettings[subName]["imgHover"] = new Image();
		menuSettings[subName]["imgHover"].src = menuSettings[subName].hover;
		menuSettings[subName]["imgOut"] = new Image();
		menuSettings[subName]["imgOut"].src = menuSettings[subName].normal;
	}
	menuLoaded = true;
	if( showMeWhenLoaded != "" ) mymenu.onNavOver( showMeWhenLoaded );
}

menuLoaded = false;
showMeWhenLoaded = "";

function menuOver(name) {
	showMeWhenLoaded = name;
	if(menuLoaded) mymenu.onNavOver(name);
}

function menuOut(name) {
	showMeWhenLoaded = "";
	if(menuLoaded) mymenu.onNavOut();
}
	
function hoverSubMenuOn(obj) {
	if(!menuLoaded) return;
	mymenu.onSubnavOver();
	obj.style.borderBottom = "5px solid #FFCE51";
	obj.firstChild.style.color = "#E50000";
	obj.style.cursor = "pointer";
}

function hoverSubMenuOff(obj) {
	if(!menuLoaded) return;
	mymenu.onSubnavOut();
	obj.style.borderBottom = "none";
	obj.firstChild.style.color = "#620000";
	obj.style.cursor = "auto";
}

//Structure of menu
menuStructure = new Object();
menuStructure["profiles"] = new Array();
menuStructure["profiles"][0] = new Object();
menuStructure["profiles"][0]["name"] = "Sun Lu Tang";
menuStructure["profiles"][0]["link"] = "sunlutang.html";
menuStructure["profiles"][1] = new Object();
menuStructure["profiles"][1]["name"] = "Sun Cunzhou";
menuStructure["profiles"][1]["link"] = "suncunzhou.html";
menuStructure["profiles"][2] = new Object();
menuStructure["profiles"][2]["name"] = "Sun Jian Yun";
menuStructure["profiles"][2]["link"] = "sunjianyun.html";
menuStructure["profiles"][3] = new Object();
menuStructure["profiles"][3]["name"] = "Lei Shi Mo";
menuStructure["profiles"][3]["link"] = "leishimo.html";
menuStructure["profiles"][4] = new Object();
menuStructure["profiles"][4]["name"] = "Lei Shitai";
menuStructure["profiles"][4]["link"] = "leishitai.html";
menuStructure["instructors"] = new Array();
menuStructure["instructors"][0] = new Object();
menuStructure["instructors"][0]["name"] = "Bob Melia";
menuStructure["instructors"][0]["link"] = "bobmelia.html";
menuStructure["instructors"][1] = new Object();
menuStructure["instructors"][1]["name"] = "Joan Hernandez Serret";
menuStructure["instructors"][1]["link"] = "joan_hernandez_serret.html";
menuStructure["instructors"][2] = new Object();
menuStructure["instructors"][2]["name"] = "Lineage";
menuStructure["instructors"][2]["link"] = "lineage.html";
menuStructure["sunstyle"] = new Array();
menuStructure["sunstyle"][0] = new Object();
menuStructure["sunstyle"][0]["name"] = "Taiji";
menuStructure["sunstyle"][0]["link"] = "taiji.html";
menuStructure["sunstyle"][1] = new Object();
menuStructure["sunstyle"][1]["name"] = "Xing Yi";
menuStructure["sunstyle"][1]["link"] = "xingyi.html";
menuStructure["sunstyle"][2] = new Object();
menuStructure["sunstyle"][2]["name"] = "Bagua";
menuStructure["sunstyle"][2]["link"] = "bagua.html";
menuStructure["sunstyle"][3] = new Object();
menuStructure["sunstyle"][3]["name"] = "Tui Shou";
menuStructure["sunstyle"][3]["link"] = "tuishou.html";
menuStructure["clubs"] = new Array();
menuStructure["clubs"][0] = new Object();
menuStructure["clubs"][0]["name"] = "Newsletters";
menuStructure["clubs"][0]["link"] = "newsletters.html";
menuStructure["clubs"][1] = new Object();
menuStructure["clubs"][1]["name"] = "Contact Us";
menuStructure["clubs"][1]["link"] = "contact.html";
menuStructure["events"] = new Array();
menuStructure["events"][0] = new Object();
menuStructure["events"][0]["name"] = "Events/News";
menuStructure["events"][0]["link"] = "events.html";
menuStructure["events"][1] = new Object();
menuStructure["events"][1]["name"] = "Links";
menuStructure["events"][1]["link"] = "links.html";
menuStructure["gallery"] = new Array();
menuStructure["gallery"][0] = new Object();
menuStructure["gallery"][0]["name"] = "Gallery";
menuStructure["gallery"][0]["link"] = "gallery/index.html";
menuStructure["gallery"][1] = new Object();
menuStructure["gallery"][1]["name"] = "Articles";
menuStructure["gallery"][1]["link"] = "articles.html";

menuSettings = new Object();
menuSettings["profiles"] = new Object();
menuSettings["profiles"]["align"] = "left";
menuSettings["profiles"]["normal"] = "images/but_profile.gif";
menuSettings["profiles"]["hover"] = "images/but_profiles_over.gif";
menuSettings["instructors"] = new Object();
menuSettings["instructors"]["align"] = "left";
menuSettings["instructors"]["normal"] = "images/but_instruct.gif";
menuSettings["instructors"]["hover"] = "images/but_instruct_over.gif";
menuSettings["sunstyle"] = new Object();
menuSettings["sunstyle"]["align"] = "left";
menuSettings["sunstyle"]["normal"] = "images/but_sunstyle.gif";
menuSettings["sunstyle"]["hover"] = "images/but_sunstyle_over.gif";
menuSettings["clubs"] = new Object();
menuSettings["clubs"]["align"] = "right";
menuSettings["clubs"]["normal"] = "images/but_clubs.gif";
menuSettings["clubs"]["hover"] = "images/but_clubs_over.gif";
menuSettings["events"] = new Object();
menuSettings["events"]["align"] = "right";
menuSettings["events"]["normal"] = "images/but_events.gif";
menuSettings["events"]["hover"] = "images/but_events_over.gif";
menuSettings["gallery"] = new Object();
menuSettings["gallery"]["align"] = "right";
menuSettings["gallery"]["normal"] = "images/but_gallery.gif";
menuSettings["gallery"]["hover"] = "images/but_gallery_over.gif";

Event.observe(window,'load',initMenu,0);