//2015-03-07
/* Script for navigation menus. Requires files "axs.css" and "axs.js" included first. 

Include the script in the <head>: 
<link href="axs.css" type="text/css" rel="stylesheet" media="screen,projection,tv" />
<script src="axs.js"></script>
<script src="axs.menu.js"></script>

Menu HTML structure supported by this script: 
<nav id="menu1">
 <h2 class="title"><a class="toggle"><span class="icon"></span> Menu</a></h2>
 <ul>
{$menu1}
 </ul>
 <script>axs.menu.attach("menu1",{'dropdown':true,'dropout':true,'toggle':true,'fixed':true});</script>
</nav>

Function arguments for attach(): 
	0: ID of the menu element. 
	1: Config options: 
		dropdown: enable dropdown menu functionality; 
		toggle: enable menu toggle switch (mostly used for mobil layout); 
*/
axs.menu={
	cfg:{},
	defaults:{'dropdown':false,'dropout':false,'toggle':false,'fixed':false},
	init:function(){
		var menus=document.querySelectorAll('nav.menu');
		for (var i=0, len=menus.length; i<len; ++i) this.attach(menus[i]);
		},//</init()>
	attach:function(node){//<Attach functions to menus />
		var a=axs.fn_args(arguments,{'node':null,'cfg':{}});
		if (typeof(node)==='string') node=document.getElementById(node);
		if (typeof(this.cfg[node.id])==="undefined") this.cfg[node.id]={};
		for (k in this.defaults) {
			if (typeof(a.cfg[k])!=="undefined") this.cfg[node.id][k]=a.cfg[k];
			if (typeof(this.cfg[node.id][k])==="undefined") this.cfg[node.id][k]=this.defaults[k];
			if (this.cfg[node.id][k]) axs.class_add(node,k);
			}
		if (axs.class_has(node,'toggle')) this.menuToggle(node);
		if (axs.class_has(node,'dropdown')) this.dropdown(node);
		if (axs.class_has(node,'img-swap')) this.img(node);
		},//</attach()>
	menuToggle:function(menu){//<Close menu />
		axs.class_add(menu,'js');
		if (axs.class_has(menu,'fixed')) axs.class_add(document.getElementsByTagName('html')[0],'menu_fixed');
		var sw=menu.querySelectorAll('.toggle');
		for (var i=0, len=sw.length; i<len; ++i){
			//sw[i].setAttribute('href','#'+menu.id);
			sw[i].onclick=function(){
				var nav=false;
				var p=this.parentNode;
				for (var i=0, p=p.parentNode; i<1000; ++i){
					if (p.tagName.toLowerCase()!=='nav') continue;
					nav=p;
					break;
					}
				(axs.class_has(nav,'js_open')) ? axs.class_rem(nav,'js_open'):axs.class_add(nav,'js_open');
				return false;
				}
			}
		},//</menuToggle()>
	dropdown:function(menu){//<Function to build dropdown menus />
		axs.class_add(menu,'js');
		var el=menu.querySelectorAll("li>a");
		for (var i=0; i<el.length; i++) el[i].onclick=function(e){
			var submenu=this.parentNode.querySelector('ul');
			if (!submenu&&!axs.class_has(this.parentNode,'submenu')) return true;
			var li=this.parentNode;
			var action=(axs.class_has(li,'js_open')) ? 'close':'open';
			axs.menu.dropdownClose(li.parentNode.childNodes);
			if (action==='open') axs.class_add(li,'js_open');
			if (submenu) return false;
			}
		var el=menu.querySelectorAll("a");
		for (var i=0; i<el.length; i++) el[i].onfocus=function(e){
			if (!axs.class_has(this.parentNode,'js_open')) axs.menu.dropdownClose(this.parentNode.parentNode.childNodes);
			return false;
			}
		el[i-1].onblur=function(){
			var li=this.parentNode;
			while (li.parentNode.tagName.toLowerCase()==='ul'||li.parentNode.tagName.toLowerCase()==='li') li=li.parentNode;
			axs.menu.dropdownClose(li.childNodes);
			}
		},//</dropdown()>
	dropdownClose:function(li){//<Close dropdown menu branches />
		for(var i=0; i<li.length; i++) if ((li[i].tagName)&&(li[i].tagName.toLowerCase()==='li')) axs.class_rem(li[i],'js_open');
		},//</dropdownClose()>
	//<Functions to swap images in menus >
	img:function(node){
		var img=new Image();//<Preload images />
		var li=node.getElementsByTagName('li');
		for (var i=0, len=li.length; i<len2; ++i) {
			img.src=this.imgGet(li[i]).src;//<Preload images />
			li[i].onmouseover=li[i].onmouseout=axs.menu.imgSwap;
			}
		},//<img()>
	imgGet:function(node){
		var o={
			src:node.childNodes[0].getElementsByTagName('img')[0].src,
			hover:(/\bhover\b/.exec(node.className)||/\bact\b/.exec(node.className))
			};
		if (o.hover) o.src=o.src.replace(/_act\.(([a-z]|[0-9])+$)/i,'\.$1');
		else o.src=o.src.replace(/\.(([a-z]|[0-9])+$)/i,'_act\.$1');
		return o;
		},//<imgGet()>
	imgSwap:function(){
		if (/\bact\b/.exec(this.className)) return false;
		var o=axs.menu.imgGet(this);
		if (o.hover) this.className=this.className.replace(/hover| hover/, '');
		else this.className=(this.className) ? this.className+' hover':'hover';
		this.childNodes[0].getElementsByTagName('img')[0].src=o.src;
		}//</imgSwap()>
	//</Functions to swap images in menus>
	}//</class::axs.menu>
axs.menu.init();
//2010-01-07