var Menu = {
	Programs: [],
	ParentMenuWrapper: false,
	ParentMenu: false,
	ChildMenuWrapper: false,
	ChildMenu: false,
	ChildMenus: [],
	PMoving: false,
	CurChild: "",
	CMoving: false,
	Load: function() {
		this.Programs = Menu_Programs.evalJSON();
		this.ParentMenuWrapper = $('dropdown-parent-wrapper');
		this.ParentMenu = $('dropdown-parent');
		this.ChildMenuWrapper = $('dropdown-child-wrapper');
		this.ChildMenu = $('dropdown-child');
		Element.extend(this.ParentMenuWrapper);
		Element.extend(this.ParentMenu);
		Element.extend(this.ChildMenuWrapper);
		Element.extend(this.ChildMenu);
		this.ParentMenu.hide();
		this.ChildMenu.hide();
		var Par = "<div><ul>";
		var Child = "";
		for (var P in this.Programs) {
			Par = Par + "<li>"
			if (this.Programs[P].length > 0) {
				Par = Par + "<div class='rightarrow'><img src=\"images/arrow_right.png\" alt=\"Arrow\"/></div><a href=\"#\" onclick=\"return Menu.Child('" + P + "', this);\">" + P + "</a></li>";
				Child = "<div><ul>";
				this.Programs[P].each(function(V, K){
					Child = Child + "<li><a href=\"index.php?p=" + V + "\">" + V + "</li>";
				});
				Child = Child + "</ul></div>";
				this.ChildMenus[P] = Child;
			} else {
				Par = Par + "<a href=\"index.php?p=" + P + "\">" + P + "</a></li>";
			}
		}
		Par = Par + "</ul></div>";
		this.ParentMenu.innerHTML = Par;
		Event.observe(document.body, 'click', this.HideParent.bind(this));
	},
	Child: function(Parent, Link) {
		if (!this.CMoving) {
			var Button = false;
			if (Link != null) {
				Element.extend(Link);
				Button = Link.up();
			}
			if (Parent == this.CurChild) { //Hide and proceed to do nothing
				this.ChildShrink();
				return false;
			} else if (Parent != this.CurChild) { //Hide and proceed to open new one
				if (this.CurChild != "") {
					this.ChildShrink(function(){
						Menu.Child(Parent, Link);
					});
					return false;
				} else {
					this.ChildMenu.innerHTML = this.ChildMenus[Parent];
				}
				this.ChildGrow(Button);
			} else { //Hide whatever is open right now
				if (this.CurChild != "") {
					this.ChildShrink();
				}
			}
			this.CurChild = Parent;
		}
		return false;
	},
	ChildGrow: function(ListItem) {
		if (!this.CMoving) {
			this.ChildMove();
			var X = 0;
			var Y = 0;
			var LO = 0;
			var LX = 0;
			var LY = 0;
			var LD = 0;
			var LW = 0;
			var LH = 0;
			LO = ListItem.cumulativeOffset();
			LD = ListItem.getDimensions();
			LX = LO.left;
			LY = LO.top;
			LW = LD.width;
			LH = LD.height;
			X = LX + LW;
			Y = LY + LH / 3;
			this.ChildMenuWrapper.style.left = X + "px";
			this.ChildMenuWrapper.style.top = Y + "px";
			this.ChildMenu.blindDown({
				duration: 0.4,
				afterFinish: this.ChildMove.bind(this)
			});
		}
	},
	ChildShrinkAfter: null,
	ChildShrink: function(F, A) {
		if (A != null && A == true) {
			if (this.ChildShrinkAfter != null) {
				this.ChildShrinkAfter();
			}
		}
		if (!this.CMoving && A == null) {
			this.ChildMove();
			this.ChildShrinkAfter = F;
			this.ChildMenu.blindUp({
				duration: 0.2,
				afterFinish: function(){
					Menu.ChildMove();
					Menu.ChildShrink(null, true);
				}
			});
			this.CurChild = "";
		}
	},
	ChildMove: function() {
		this.CMoving = (this.CMoving ? false : true);
	},
	ParentMove: function() {
		this.PMoving = (this.PMoving ? false : true);
		if (!this.PMoving) {
			var CurSource = $('dropdownarrow').src;
			if (CurSource.match("arrow_down.png")) {
				CurSource = "images/arrow_up.png";
			}
			else {
				CurSource = "images/arrow_down.png";
			}
			$('dropdownarrow').src = CurSource;
		}
	},
	ToggleParent: function(Caller) {
		if (!this.PMoving) {
			if (this.CurChild != "") {
				this.ChildShrink(function(){
					Menu.ToggleParent();
				});
				return false;
			}
			this.ParentMove();
			if (this.ParentMenu.visible()) {
				this.ParentMenu.slideUp({
					duration: 0.2,
					afterFinish: this.ParentMove.bind(this)
				});
			} else {
				Element.extend(Caller);
				var Offsets = Caller.cumulativeOffset();
				var X = 0;
				var Y = 0;
				X = Offsets.left - 4;
				Y = Offsets.top + Caller.getHeight() + 5;
				this.ParentMenuWrapper.style.left = X + "px";
				this.ParentMenuWrapper.style.top = Y + "px";
				this.ParentMenu.slideDown({
					duration: 0.4,
					afterFinish: this.ParentMove.bind(this)
				});
			}
		}
	},
	HideParent: function(E) {
		if (!this.PMoving && !this.CMoving) {
			if (this.ParentMenu.visible()) {
				if (E == null) { //Hide parent
					this.ParentMove();
					this.ParentMenu.slideUp({
						duration: 0.2,
						afterFinish: this.ParentMove.bind(this)
					});
					return false;
				}
				var MX = E.pointerX();
				var MY = E.pointerY();
				var Offsets = this.ParentMenuWrapper.cumulativeOffset();
				var Dimensions = this.ParentMenuWrapper.getDimensions();
				var X1 = Offsets.left;
				var Y1 = Offsets.top;
				var D1 = MX - X1;
				var D2 = MY - Y1;
				if (D1 > Dimensions.width || D2 > Dimensions.height || D1 < 0 || D2 < 0) {
					if (this.CurChild != "") {
						Offsets = this.ChildMenuWrapper.cumulativeOffset();
						Dimensions = this.ChildMenuWrapper.getDimensions();
						X1 = Offsets.left;
						Y1 = Offsets.top;
						D1 = MX - X1;
						D2 = MY - Y1;
						if (D1 > Dimensions.width || D2 > Dimensions.height || D1 < 0 || D2 < 0) {
							this.ChildShrink(function(){
								Menu.HideParent();
							});
						}
						return false;
					}
					this.ParentMove();
					this.ParentMenu.slideUp({
						duration: 0.2,
						afterFinish: this.ParentMove.bind(this)
					});
				}
			}
		}
	}
}

var PageFix = {
	PosFootOrigOff: -1,
	PosFootFix: false,
	Load: function() {
		var Footer = $('footer');
		this.PosFootOrigOff = Footer.cumulativeOffset();
		this.Fix();
	},
	Fix: function() {
		Element.extend(document.body);
		var Footer = $('footer');
		var Height = Footer.cumulativeOffset().top + Footer.getHeight();
		if (Height < document.body.getHeight()) {
			this.PosFootFix = true;
		}
		if (this.PosFootFix) {
			Height = document.body.getHeight();
			var Footer = $('footer');
			var Prev = Footer.previous();
			var PrevY = Prev.cumulativeOffset().top + Prev.getHeight();
			var NewY = Height - this.PosFootOrigOff.top - Footer.getHeight();
			if (NewY + this.PosFootOrigOff.top < PrevY) {
				Footer.style.position = "static";
			}
			else {
				Footer.style.position = "relative";
				Footer.style.top = NewY + "px";
			}
		}
		Height = Footer.cumulativeOffset().top + Footer.getHeight();
		$('container').style.height = Height + "px"
		setTimeout(this.Fix.bind(this), 500);
	}
}
document.observe('dom:loaded', function() {
	Menu.Load();
	PageFix.Load();
})
