if(!window.__ddmenu)
{
	__ddmenu = {
		initialized: false,
		menuOpen: false,
		classes: {
			click: "dd-click",
			menu: "dd-menu",
			more: "dd-more",
			open: "dd-hover",
			left: "dd-left",
			asRegExp: function (key) {
				if(this[key]) return new RegExp("( |^)"+this[key].replace(/\-/g, "\\-")+"\\b");
				else return new RegExp("( |^)"+key.replace(/\-/g, "\\-")+"\\b");;
			}
		},
		delays: {
			show: 400,
			hide: 600
		},
		isOpen: null, //regexp
		isLeft: null, //regexp
		isMenu: null, //regexp
		isClickable: null, //regexp
		showTimer: null,
		hideTimer: null,
		hideTarget: null,
		lastOpenedMenu: null,

		windowSize: function () {
			if(!window.innerWidth)
			{
				if(!(document.documentElement.clientWidth == 0))
				{
					w = document.documentElement.clientWidth;
					h = document.documentElement.clientHeight;
				}
				else
				{
					w = document.body.clientWidth;
					h = document.body.clientHeight;
				}
			}
			else
			{
				w = window.innerWidth;
				h = window.innerHeight;
			}
			return {width:w,height:h}
		},
		getElementAbsX: function (el) {
			var l=0;
			while (el.offsetParent)
			{
				l+=el.offsetLeft;
				el=el.offsetParent;
			}
			l+=el.offsetLeft;
			return l;
		},

		setShowDelay: function (delay) {
			this.delays.show = delay;
		},
		setHideDelay: function (delay) {
			this.delays.hide = delay;
		},
		_showMenu: function (el) {
			if(!this.isOpen.test(el.className))
			{
				var donthide = el;
				while(donthide)
				{
					if(this.isOpen.test(donthide.className)) break;
					if(donthide.parentNode.nodeName!="LI" && donthide.parentNode.nodeName!="UL") break;
					donthide = donthide.parentNode;
				}
				var tmp = this.lastOpenedMenu;
				while(tmp && tmp!=donthide)
				{
					if(tmp.nodeName!="LI" && tmp.nodeName!="UL") break;
					if(tmp.nodeName=="LI") this._hideMenu(tmp);
					tmp = tmp.parentNode;
				}

				try {
					var cul = el.firstChild;
					while(cul && cul.nodeName!="UL") cul = cul.nextSibling;
					var wSize = this.windowSize();
					var x = this.getElementAbsX(el.parentNode);
					var w1 = el.offsetWidth;
					var w2 = cul?cul.offsetWidth:0;
					if(wSize.width<=x+w1+w2 && x-w2>=0) el.className += " "+this.classes["left"];
					else removeClass(el, "left");
				} catch (e) { }

				this.menuOpen = true;
				el.className += " "+this.classes["open"];
				el.className = el.className.replace(this.classes.asRegExp("more"), "$1"+this.classes["more"]+"-"+this.classes["open"]);
				this.lastOpenedMenu = el;
			}
		},
		_hideMenu: function (el, timeout) {
			if(!this.isOpen.test(el.className)) return;
			if(timeout) this.menuOpen = false;
			el.className = el.className.replace(this.isOpen, "");
			el.className = el.className.replace(this.isLeft, "");
			el.className = el.className.replace(this.classes.asRegExp(this.classes["more"]+"-"+this.classes["open"]), "$1"+this.classes["more"]);
		},
		_menuElement: function (el) {
			while (el && !this.isMenu(el.className)) el = el.parentNode;
			return el;
		},
		onclick: function (el) {
			if(!this.isClickable.test(el.parentNode.className)) return;
			this.onmouseover(el, true);
		},
		onmouseover: function (el, clicked) {
			if(!this.menuOpen && this.isClickable.test(el.parentNode.className) && !clicked) return;
			if(this.showTimer) clearTimeout(this.showTimer);
			if(this.hideTimer) {
				clearTimeout(this.hideTimer);
				this.hideTimer=null;
			}
			if(this.menuOpen || clicked) {
				this._showMenu(el);
			} else {
				this.showTimer = setTimeout(function(){__ddmenu._showMenu(el);}, this.delays.show);
			}
		},
		onmouseout: function (el) {
			if(this.hideTimer) {
				clearTimeout(this.hideTimer);
				this.hideTimer=null;
			}
			this.hideTarget = el;
			this.hideTimer = setTimeout(function(){__ddmenu._hideMenu(__ddmenu.hideTarget,1);}, this.delays.hide);
		},
		_init: function (el, click2open) {
			this.isOpen = this.classes.asRegExp("open");
			this.isLeft = this.classes.asRegExp("left");
			var ul = el;
			while (ul && ul.nodeName!="UL") ul = ul.parentNode;
			if(ul)
			{
				var li = ul.firstChild;
				while(li)
				{
					if(li.nodeName!="LI") { li = li.nextSibling; continue; }

					li.onmouseover=function() { __ddmenu.onmouseover(this); }
					li.onmouseout=function() { __ddmenu.onmouseout(this); }
					if(click2open) li.onclick=function() { __ddmenu.onclick(this); }

					var list = li.getElementsByTagName("LI");
					for (var i=0; i<list.length; i++) {
						var subs = list[i].getElementsByTagName("UL");
						if(subs.length) list[i].className += this.classes["more"];
						list[i].onmouseover=function() { __ddmenu.onmouseover(this); }
						list[i].onmouseout=function() { __ddmenu.onmouseout(this); }
					}

					li = li.nextSibling;
				}
			}
			ul.setAttribute(ddinitialized, "1");
		},
		initAll: function () {
			if(this.initialized) return;
			this.initialized = true;
			try {
				this.isMenu = this.classes.asRegExp("menu");
				this.isClickable = this.classes.asRegExp("click");
				var ul = document.getElementsByTagName("UL");
				for(var i=0; i<ul.length; i++)
				{
					if(ul[i].getAttribute("ddinitialized")!=null) continue;
					if(this.isMenu.test(ul[i].className)) this._init(ul[i], this.isClickable.test(ul[i].className));
				}
			} catch (e) {
				;
			}
		},

		initOnLoad: function () {
			if(document.addEventListener) {
				document.addEventListener('DOMContentLoaded', function() { __ddmenu.initAll(); }, false);
				window.addEventListener('load', function() { __ddmenu.initAll(); }, false);
			}
			else if(window.attachEvent) { window.attachEvent('onload', function() { __ddmenu.initAll(); }); }
			else
			{
				var oldonload = window.onload;
				if (typeof window.onload != 'function') {
					window.onload = func;
				} else {
					window.onload = function() {
						if (oldonload) {
							oldonload();
						}
						func();
					}
				}
			}
		}
	}

	__ddmenu.initOnLoad();
}
