var menufunctions = {


	createMenu:function(menuname,rootname){
		

		for(var name in menuname){

			var isRoot = (name == rootname);

			var munuitem = menuname[name];
			
			// set ul first element is the root
			var ul = $element("ul", munuitem['ul'].attr, munuitem['ul'].txt,munuitem['ul'].style);

			// create all menu items
			var lis = munuitem['li'];

			lis.reduce(function(m){
		
					// create new list item	
					var li = $element("li",m.attr,m.txt,m.style);
					// create a link
					var a = $element("a",m['a'].attr,m['a'].txt,m['a'].style);
					// a to li
					$appendto(li,a);
					// add li2 to ul
					$appendto(ul,li);
				}
			);

			if(isRoot){
				$appendto(document.body,ul); // if root, append ul to the body of the document
			}
			else{

				// this is the li
				var elem = $ele(name);

				// add the ul to the li element
				$appendto(elem,ul);

				var mover = menufunctions.getMover.apply(null,[elem]);
				var mout = menufunctions.getMout.apply(null,[elem]);

				var evs = [ {event:'mouseover',fn:mover,useCapture:false},
			 		    {event:'mouseout',fn:mout,useCapture:false} ];
				
				// add events to li
				$events(elem,evs);
			}
		}

	},

	createSideMenu:function(menuname,submenu,idx){
		
		var munuitems = menuname[submenu]['li'];

		// create ul
		var ul = $ele("sidemenu");

		for(var i = 0;i<munuitems.length;i++){

			var li = null;
			var m = munuitems[i]['a'];

			if(i !== idx){
				li = $element("li",null,null,null);
				var a = $element("a",m.attr,m.txt,null);
				// a to li
				$appendto(li,a);
				// add li to ul
				$appendto(ul,li);
			}
			else{
				li = $element("li",{id:"selected"},m.txt,null);
				// add li to ul
				$appendto(ul,li);
			}
		}
		
		// add google translation to bottom of menu
			menufunctions.addGoogleTranslation(ul);
			menufunctions.addSocialLinks(ul);
	},

	addGoogleTranslation:function(ele){
		// add google translation to bottom of menu
		var div = $element("div",{id:"google_translate_element"},"Translate Website",null);
		var head = document.getElementsByTagName("head")[0];
		var att1 = {type:"text/javascript",src:"http://www.neuromedgroup.com/app-files/scripts/google_trans.js"};
		var script1 = $element("script",att1,null,null);
		var att2 = {type:"text/javascript",src:"//translate.google.com/translate_a/element.js?cb=googleTranslateElementInit"};
		var script2 = $element("script",att2,null,null);
		$appendto(head,script1);
		$appendto(head,script2);
		$appendto(ele,div);
	},
	
	addSocialLinks:function(ele){
		var div = $element("div",{id:"social-links-text"},"Tell Your Friends. . .",null);
		var fb = $element("a",{href:"http://www.facebook.com/sharer.php?u=http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3D3p1whD0Qun4&src=sp",id:"fb",target:"_blank"},null,null);
		var tw = $element("a",{href:"http://twitter.com/share?count=horizontal&original_referer=http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3D3p1whD0Qun4&text=Check%20this%20video%20out%20--%20Live%20Pain%20Free%2C%20The%20MATRIX%20by%20NeuroMed-Electroanalgesia&url=http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3D3p1whD0Qun4&via=youtube",id:"tw",target:"_blank"},null,null);
		var em = $element("a",{href:"https://www.google.com/accounts/ServiceLogin?uilel=3&service=youtube&passive=true&continue=http%3A%2F%2Fwww.youtube.com%2Fsignin%3Faction_handle_signin%3Dtrue%26nomobiletemp%3D1%26hl%3Den_US%26next%3D%252Fshare_inline%253Fv%253D3p1whD0Qun4&hl=en_US&ltmpl=sso",id:"em",target:"_blank"},null,null);
		var img1 = $element("img",{src:"http://www.neuromedinc.com/app-files/pics/fb_logo.png",alt:"Facebook",width:"44",height:"45"},null,null);
		$appendto(fb,img1);
		var img2 = $element("img",{src:"http://www.neuromedinc.com/app-files/pics/tw_logo.png",alt:"Twitter",width:"44",height:"44"},null,null);
		$appendto(tw,img2);		
		var img3 = $element("img",{src:"http://www.neuromedinc.com/app-files/pics/em_logo.png",alt:"Email to a friend",width:"44",height:"45"},null,null);
		$appendto(em,img3);		
		$appendto(div,fb);
		$appendto(div,tw);
		$appendto(div,em);
		$appendto(ele,div);
	},
	
	over:function(e,targetElement){

		// get the current element
		var el = window.event ? targetElement : e ? e.currentTarget : null;
		if(!el) return;
		// stop mouse out function if mouse is
		// over another element
		clearTimeout(el.outTimeout);
		// highlight the selected menuitem
		el.getElementsByTagName('a')[0].className = "selected";
		// display the submanu
		for(var i = 0;i < el.childNodes.length;i++)
		{
			var node = el.childNodes[i];
			if(node.nodeName.toLowerCase() == 'ul')
			{
				node.style.display = "block";
			}
		}

	},


	out:function(e,targetElement){

		// get the current element
		var el = window.event ? targetElement : e ? e.currentTarget : null;
		if(!el) return;
		el.outTimeout = setTimeout(function() {menufunctions.out2(el);},200);

	},

	out2:function(el){

		// un-highlight the selected menuitem
		el.getElementsByTagName('a')[0].className = "";
		for(var i = 0;i < el.childNodes.length;i++)
		{
			var node = el.childNodes[i];
			if(node.nodeName.toLowerCase() == 'ul')
			{
				node.style.display = "none";	
			}
		}
	},

	getMover:function(node){
		return function(e) { menufunctions.over(e,node); }; 
	},

	getMout:function(node){
		return function(e) { menufunctions.out(e,node); };  
	}


};


