var SideMenu = {
	
	open : function(element) {
		//trace(element);
		var element, v;
		element.moving = true;
		this.openLoop = function() {
			//trace(velFriction(element.origHeight,element.minHeight,8));
			v = velFriction(element.currHeight,element.origHeight,8);
			//v = velElastic(element.currHeight, element.origHeight, 0.50, 0.25, 0.75);
			element.currHeight += v;
			element.scrollTop = 0;
			if (element.currHeight+0.6 > element.origHeight) {
				element.currHeight = element.origHeight;
				element.style.height = Math.floor(element.currHeight)+"px";
				element.moving = false;
				Poller.drop(element.oLoopID);
			}
			element.style.height = Math.floor(element.currHeight)+"px";
		}
		Poller.drop(element.cLoopID);
		Poller.add(this.openLoop,element.oLoopID);
	},
	close : function(element) {
		var element, v;
		this.closeLoop = function() {
			element.moving = false;
			//v = velElastic(element.currHeight, element.minHeight, 0.25, 0.25, 0.75);
			v = velFriction(element.currHeight,element.minHeight,8);
			element.currHeight += v;
			element.scrollTop = 0;

			if (element.currHeight-0.6 < element.minHeight) {
				element.currHeight = element.minHeight;
				element.style.height = Math.floor(element.currHeight)+"px";
				Poller.drop(element.cLoopID);
			}
			element.style.height = Math.floor(element.currHeight)+"px";
		}
		Poller.drop(element.oLoopID);
		Poller.add(this.closeLoop,element.cLoopID);
	},
	
	init : function() {
		var sideMenu = get('sidemenu');
		if (!sideMenu) return;
		
		this.activeMenuID = getURLVariable("m");
		
		this.selected = (this.activeMenuID!==false) ? this.activeMenuID : 0;
		this.subMenu = sideMenu.getElementsByTagName('dl');
		
		if (!this.subMenu) return;
		
		
		
		for (var i = 0, l = this.subMenu.length; i < l; i++) {
			
			this.subMenu[i].a = this.subMenu[i].getElementsByTagName('a').item(0);
			if (!this.subMenu[i].a) return;
			this.subMenu[i].a._parent = this.subMenu[i];
			
			this.subMenu[i].origHeight = getElementHeight(this.subMenu[i]);
			this.subMenu[i].minHeight  = 0;
			this.subMenu[i].currHeight = 0;
			//this.subMenu[i].id = this.subMenu[i].uniqueID;
			this.subMenu[i].id = i;
			this.subMenu[i].oLoopID = "openLoop_"+this.subMenu[i].id;
			this.subMenu[i].cLoopID = "closeLoop_"+this.subMenu[i].id;
			
			if (this.activeMenuID !== false && i == this.activeMenuID) {
				//trace(i + " " + this.activeMenuID);
				addClassName(this.subMenu[i].a, 'active');
				this.subMenu[i].currHeight = this.subMenu[i].origHeight;
			}
			this.subMenu[i].style.height = this.subMenu[i].currHeight+'px';
			this.subMenu[i].style.overflow = 'hidden';
			this.subMenu[i].moving = false;
			
			
			this.subMenu[i].onmouseover = 
			this.subMenu[i].onmousemove = 
			this.subMenu[i].onmouseup = function(x) {
				//trace("x = "+x+" this.id = "+this.id+" \nSideMenu.activeMenuID = "+SideMenu.activeMenuID+" \nSideMenu.selected = "+SideMenu.selected+" \nSideMenu.subMenu["+SideMenu.selected+"].moving = "+SideMenu.subMenu[SideMenu.selected].moving);
				if (x!=1 && this.id >= SideMenu.selected && SideMenu.subMenu[SideMenu.selected].moving) return;

				var _this = this;
				var run = function() {
					SideMenu.selected = _this.id;
					for (var i = 0, l = SideMenu.subMenu.length; i < l; i++) {
						SideMenu.close(SideMenu.subMenu[i]);
						removeClassName(SideMenu.subMenu[i].a, 'active');
						//SideMenu.subMenu[i].a.blur();
					}
					SideMenu.open(_this);
					addClassName(_this.a, 'active');
				}
				clearTimeout(SideMenu.tmo);
				SideMenu.tmo = setTimeout(run,100);

			}

			this.subMenu[i].onmouseout = function() {
				if (SideMenu.selected != this.id) {
					clearTimeout(SideMenu.tmo);
				}
			}
			this.subMenu[i].a.onclick = function() {
				this._parent.onmouseover(1);
				return false;
			}
			this.subMenu[i].getID = function(){return this.id};
			
			/* onFocus events */
			this.subMenu[i].menuItem = sideMenu.getElementsByTagName('a');
			for (var j = 0, jl = this.subMenu[i].menuItem.length; j < jl; j++) {
				this.subMenu[i].menuItem[j].onfocus = function() {
					this.parentNode.parentNode.onmouseover(1);
				}
				this.subMenu[i].menuItem[j].onblur = function() {
					this.parentNode.parentNode.onmouseout();
					this.parentNode.parentNode.scrollTop = 0;
				}/**/
			}
			
			// IE before fix
			/*if (ieversion && !this.subMenu[i].spanbefore) {
				trace("IE before fix");
				this.subMenu[i].span = this.subMenu[i].a.getElementsByTagName('span').item(0);
				this.subMenu[i].spanbefore = document.createElement('b');
				this.subMenu[i].spanbefore.className = 'before';
				this.subMenu[i].spanbefore.innerHTML = this.subMenu[i].span.innerHTML;
				this.subMenu[i].a.insertBefore(this.subMenu[i].spanbefore,this.subMenu[i].span);
			}*/
		}
		// Open first
		
		
		if (!this.activeMenuID) {
			//trace(this.activeMenuID);
			var AciveMenu = this.subMenu[0];
			setTimeout(function(){AciveMenu.onmouseover(1)},0);
		}
		// IE screen redraw fix
		if (document.all) {
			var oh = get('root').offsetHeight;
			get('root').style.height = oh;
			get('root').style.height = 'auto';
			delete oh;
		}
	}
}


var startLoad = function() {
	SideMenu.init();
}
addLoadEvent(startLoad);
