

function ubermenu () {
	
	
	 this.webkitversion = function () {
	    var version = 0;
	   
	    var regexp = /Safari\/([\d.]+)/;
	    var result = regexp.exec(navigator.userAgent);

	    if(result) {
	        version = parseFloat(result[1]);
	    }
	
	    return version;

	}
	
	
	
	this.openMenu = function (evt) {

		evt.stop ();
	
		//	get the li relative to the toggler element in evt.src ()
		var p = getFirstParentByTagAndClassName(evt.src (), 'li');
		
		//	get the hidden ul
		var todisplay = findChildElements (p, ['ul']);
				
		if (!todisplay || todisplay.length == 0) {
			return (false);	
		}
		

		//	check if the ul is already opened, if so we do not need to do 
		//	anything, cause it is already open
		if (hasElementClass (todisplay[0], 'opened')) {
			return (false);
		}
	
		var opts = {};
		opts.duration = 0.3;

		opts.afterFinish = function (e) {
			addElementClass (e.element, 'opened');
		};
		

		opts.beforeStart = function (e) {
			forEach ($$('.opened'), uberm.closeMenu);
		};
			
		blindDown(todisplay[0], opts);
		
	};
	
	
	this.closeMenu = function (el) {
		var opts = {};
		opts.duration = 0.3;
		opts.afterFinish = function (e) {
			removeElementClass (e.element, 'opened');
		};
		blindUp(el, opts);
	};
	

	this.flyToHash = function (h) {
		
		//	strip of the first char if it is a hash, so we can fly directly
		//	to the thingy ...
		var ha = h;
		var first = h.substr (0, 1);
		if (first == "#") {
			ha = h.substr (1, h.length);
		}
		
		var elems = $$('#holder_content a');
		var flyto = false;
		for (var i = 0; i < elems.length; i++) {
			if (elems[i].name == ha) {
				flyto = elems[i];
				break;
			}	
		}
		
		if (!flyto) {
			return (false);	
		}
		
		forEach ($$('#holder_content div.section'), function (el) {
			if (!hasElementClass (el, 'hidden')) {
				addElementClass (el, 'hidden');	
			}
		});
		
		var toshow = getFirstParentByTagAndClassName(flyto, 'div', 'section');

			
		if (!toshow) {
			return (false);	
		}
			
		removeElementClass (toshow, 'hidden');

	};
	

	this.init = function () {
		

		//	check if we got a hash - if so, we will diretly show the div
		var anc = location.href.split('#')[1];
		
		//	detect a load by anchor id
		if (anc && anc.length > 1) {
			uberm.flyToHash (anc); 
		}
		
		//	open the menu if needed
		var elems = $$('#holder_nav a');

		forEach (elems, function (el) {
			
			if (uberm.foundMenu) {
				return (false);	
			}
						
			//	we need to strip out all hashes 
			ref = location.href;
			ref = ref.split('#')[0];

			
			if (el.href == ref) {
				addElementClass (el, 'current');
				
							
				var p = getFirstParentByTagAndClassName (el, 'ul');
				
				if (!hasElementClass (p, 'mainnav')) {	
					var opts = {};
					opts.duration = 0.3;
				
					opts.afterFinish = function (e) {
						addElementClass (e.element, 'opened');
					};
				
					opts.beforeStart = function (e) {
						forEach ($$('.opened'), uberm.closeMenu);
					};
				
					blindDown(p, opts);				
					uberm.foundMenu = true;
				}
			}
		});
		
		
		//	connect all menu elements
		forEach ($$('#holder_nav ul li'), function (el) {
			//	check if the node has subelements
			var childs = findChildElements (el, ['ul li']);
			if (childs.length == 0) {
				return (false);
			}				
			//	it is a main element holder, find the main a and make if a
			//	toggler for this menu
			
			var toggler = getFirstElementByTagAndClassName('a', null, el);		
			connect (toggler, 'onclick', uberm.openMenu);
		});		
		
		//	connect all subselectors (if existant)
		forEach ($$('#holder_selection ul li a'), function (el) {
			connect (el, 'onclick', function (evt) {
				uberm.flyToHash (evt.src ().hash); 				
				//	evt.stop ();
			});
			
		});
		
	};
	
}

var uberm = new ubermenu ();

connect (window, 'onload', function () {
	uberm.init ();
});


