function getElementsByClass(searchClass,node,tag) {
        var classElements = new Array();
        if ( node == null )
                node = document;
        if ( tag == null )
                tag = '*';
        var els = node.getElementsByTagName(tag);
        var elsLen = els.length;
        var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
        for (i = 0, j = 0; i < elsLen; i++) {
                if ( pattern.test(els[i].className) ) {
                        classElements[j] = els[i];
                        j++;
                }
        }
        return classElements;
}


window.onresize  = function() {
	OverflowManager.init();
};

var OverflowManager = {

	screenWidth : null,
	pageWidth : 988,

	init: function() {
		this.screenWidth = document.width ? document.width : document.documentElement.offsetWidth;
		this.setOverflow();
	},

	setOverflow: function() {
		if (OverflowManager.screenWidth < OverflowManager.pageWidth) {
			document.getElementsByTagName("html")[0].style.overflowX = "visible";
		} else {
			document.getElementsByTagName("html")[0].style.overflowX = "hidden";
		}
	}
};

var Navigation = {

	navUl: null,

	timeout: null,

	isHomepage: false,

	init: function() {
		this.navUl = jQuery("#header ul.menu");
		this.inactivateParent();
		this.addMouseOvers();
		
		this.isHomepage = jQuery("body.frontpage").length > 0;
	},

	centerSubnav: function() {
		var level1Els = getElementsByClass("level1", document.getElementById("header"), "UL");
		var length = level1Els.length;
		var childLength, textLength;
		for (var i=0; i<length; i++) {
			childLength = level1Els[i].childNodes.length;
			if (childLength > 1) {
			  textLength = 0;
			  for (var j=0; j<childLength; j++) textLength+= level1Els[i].childNodes[j].childNodes[0].childNodes[0].innerHTML.length;
        textLength+= (childLength-1)*5;
				level1Els[i].style.left = (Math.round (textLength * -2.2) - 5) + 'px';
			}
		}
	},

	writeLevel2Header: function() {
		var tmp = jQuery("ul.level1 li.active span").html();
		if (tmp) {
			document.write('<h3 id="level2Header">' + tmp + '</h3>');
		}
	},

	inactivateParent: function() {
		var activeParent;

		if (jQuery("#current").parent().hasClass("level2")) {
			activeParent = jQuery("#current").parent().parent().parent().parent();
		} else if (jQuery("#current").parent().hasClass("level1")) {
			activeParent = jQuery("#current").parent().parent();
		}
		if (activeParent && activeParent.hasClass("active")) {
			activeParent.addClass("inactive");
		}
	},

	addMouseOvers: function() {
		//mouseovers on the main navigation items
		this.navUl.children().each(function() {
			var thiz = this;
			thiz.onmouseover = function(e){
				clearTimeout(Navigation.timeout);
				Navigation.timeout = setTimeout(function(){Navigation.showSubNav(thiz, true);}, 200);
			};
			thiz.onmouseout = function(e){
				clearTimeout(Navigation.timeout);
				Navigation.timeout = setTimeout(function(){Navigation.hideSubNav(thiz, true);}, 200);
			};
		});

		//mouseovers on the sub navigation lists
		this.navUl.children().children().each(function(){
			var thiz = this;
			if (thiz.className == "level1") {
				thiz.onmouseover = function(e){
					clearTimeout(Navigation.timeout);
					Navigation.timeout = setTimeout(function(){Navigation.showSubNav(thiz.parentNode, true);}, 200);
				};
				thiz.onmouseout = function(e){
					clearTimeout(Navigation.timeout);
					Navigation.timeout = setTimeout(function(){Navigation.hideSubNav(thiz.parentNode, true);}, 200);
				};
			}
		});

		//mouseovers on the sub navigation items
		this.navUl.children().children().children().each(function() {
			var thiz = this;
			var isChildOfCurrent = (thiz.parentNode.parentNode.className.indexOf("inactive") > -1);
			if (thiz.parentNode.className == "level1" && thiz.className.indexOf("parent") > -1) {
				thiz.onmouseover = function(e){
					clearTimeout(Navigation.timeout);
					Navigation.showSubNav(thiz, false);
					Navigation.cancelPropagation(e);
				};
				if (isChildOfCurrent) {
					thiz.onmouseout = function(e){
						clearTimeout(Navigation.timeout);
						Navigation.hideSubNav(thiz, true);
						Navigation.cancelPropagation(e);
					};
				}
			}
		});
	},

	cancelPropagation : function(e) {
		if (!e && window.event) {
			e = window.event;
			e.cancelBubble = true;
		} else {
			e.stopPropagation();
		}
	},

	showSubNavBg: function(el) {
		var subNavBg = jQuery("#subnavbg");
		subNavBg.addClass("show");
		subNavBg.mouseover(function(){
			clearTimeout(Navigation.timeout);
			Navigation.timeout = setTimeout(function(){Navigation.showSubNav(el, true);}, 200);
		});
		subNavBg.mouseout(function(){
			clearTimeout(Navigation.timeout);
			Navigation.timeout = setTimeout(function(){Navigation.hideSubNav(el, true);}, 200);
		});
	},

	showSubNav: function(el, doReset) {
		if (doReset) {
			Navigation.resetSubnav();
		} else {
			Navigation.resetSubSubnav();
		}
		if (Navigation.isHomepage) {
			Navigation.showSubNavBg(el);
		}
		jQuery(el).addClass("active");
		var children = jQuery(el).children();
		var l = children.length;
		for (var i=0; i<l; i++) {
			if(children[i].tagName == "UL") {
				children[i].style.display = "block";
			}
		}
	},

	hideSubNav: function(el, doReset) {
		if (doReset) {
			Navigation.resetSubnav();
		}
		if (this.isHomepage) {
			jQuery("#subnavbg").removeClass("show");
		}
		jQuery(el).removeClass("active");
		var current = jQuery("#current");
		if(current.length > 0) {
			current.addClass("active");
			if (current.parent().hasClass("menu")) {
				var children = current.children();
				var l = children.length;
				for (var i=0; i<l; i++) {
					if(children[i].tagName == "UL") {
						children[i].style.display = "block";
						var listitems = children[i].childNodes;
						var ll = listitems.length;
						for (var j=0; j<ll; j++) {
							jQuery(listitems[j]).removeClass("active");
						}
					}
				}
			} else if (current.parent().hasClass("level1")) {
				current.parent().parent().addClass("active");
				current.parent()[0].style.display = "block";
			} else if (current.parent().hasClass("level2")) {
				current.parent().parent().addClass("active");
				current.parent().parent().parent().parent().addClass("active");
				current.parent().parent().parent()[0].style.display = "block";
			}
		}
	},

	resetSubnav: function() {
		var children;
		this.navUl.children().each(function() {
			jQuery(this).removeClass("active");
			children = jQuery(this).children();
			children.each(function(){
				if (this.tagName == "UL") {
					this.style.display = "none";
					children = jQuery(this).children().children();
					for (var i=0; i<children.size(); i++) {
						if (children[i].tagName == "UL") {
							children[i].style.display = "none";
						}
					}
				}
			});
		});
	},

	resetSubSubnav: function() {
		var children;
		jQuery("#header ul.level1").children().each(function() {
			jQuery(this).removeClass("active");
			children = jQuery(this).children();
			children.each(function(){
				if (this.tagName == "UL") {
					this.style.display = "none";
					children = jQuery(this).children().children();
					for (var i=0; i<children.size(); i++) {
						if (children[i].tagName == "UL") {
							children[i].style.display = "none";
						}
					}
				}
			});
		});

	}
};
