// Original Code for DynamicDrive
// Visit http://www.dynamicdrive.com for original
//		now re-written for AGN

var myMenu={
	speed:3,                     // Menu sliding speed (1 - 5 recomended)
	remember: true,              // Store menu states (expanded or collapsed) in cookie and restore later
	oneSmOnly: true,             // One expanded submenu at a time
	markCurrent: true,
	menu: '',
	submenus: '',

	init:function() {
		this.menu = document.getElementById('main_menu');
		this.submenus = this.menu.getElementsByTagName('div');
		var mainInstance = this;

		for (var i = 0; i < this.submenus.length; i++){
			this.submenus[i].getElementsByTagName('span')[0].onclick = function() {
				mainInstance.toggleMenu(this.parentNode);
			}
			for (var j = 0; j < this.submenus[i].getElementsByTagName('a').length; j++){
				this.submenus[i].getElementsByTagName('a')[j].onclick = function() {
					mainInstance.followlink(this)
					return false
				}
			}
		}		
		if (this.markCurrent) {
			var links = this.menu.getElementsByTagName("a");
			for (var i = 0; i < links.length; i++){
				if (links[i].href == document.location.href) {
					links[i].className = "current";
					break;
				}
			}
		}
		if (this.remember) {
			var regex = new RegExp("sdmenu_" + encodeURIComponent(this.menu.id) + "=([01]+)");
			var match = regex.exec(document.cookie);
			if (match) {
				var states = match[1].split("");
				for (var i = 0; i < states.length; i++){
					this.submenus[i].className = (states[i] == 0 ? "collapsed" : "");
				}
			}
		}
	},

	followlink:function(submenulink){
		var LinkTarget = submenulink.getAttribute('href')
		//alert('will go to: include/include_' +  LinkTarget.replace('http://www.absolutegamerz.com/NewFolder/', '') )
		this.AJAXConnect('Site_Content', 'include/include_' +  LinkTarget.replace('http://www.absolutegamerz.com/', '') )
		return false
	},

	toggleMenu:function(submenu) {
		if (submenu.className == "collapsed"){
			this.expandMenu(submenu);
		}else{
			this.collapseMenu(submenu);
		}
	},

	collapseMenu:function(submenu) {
		var minHeight = submenu.getElementsByTagName("span")[0].offsetHeight;
		var moveBy = Math.round(this.speed * submenu.getElementsByTagName("a").length);
		var mainInstance = this;
		var intId = setInterval(function() {
			var curHeight = submenu.offsetHeight;
			var newHeight = curHeight - moveBy;
			if (newHeight > minHeight){
				submenu.style.height = newHeight + "px";
			} else {
				clearInterval(intId);
				submenu.style.height = "";
				submenu.className = "collapsed";
				mainInstance.memorize();
			}
		}, 30);
	},

	expandMenu:function(submenu) {
		var fullHeight = submenu.getElementsByTagName("span")[0].offsetHeight;
		var links = submenu.getElementsByTagName("a");
		for (var i = 0; i < links.length; i++){
			fullHeight += links[i].offsetHeight;
		}
		var moveBy = Math.round(this.speed * links.length);
	
		var mainInstance = this;
		var intId = setInterval(function() {
			var curHeight = submenu.offsetHeight;
			var newHeight = curHeight + moveBy;
			if (newHeight < fullHeight){
				submenu.style.height = newHeight + "px";
			}else{
				clearInterval(intId);
				submenu.style.height = "";
				submenu.className = "";
				mainInstance.memorize();
			}
		}, 30);
		this.collapseOthers(submenu);
	},

	collapseOthers:function(submenu) {
		if (this.oneSmOnly) {
			for (var i = 0; i < this.submenus.length; i++){
				if (this.submenus[i] != submenu && this.submenus[i].className != "collapsed"){
					this.collapseMenu(this.submenus[i]);
				}
			}
		}
	},

	expandAll:function() {
		var oldOneSmOnly = this.oneSmOnly;
		this.oneSmOnly = false;
		for (var i = 0; i < this.submenus.length; i++){
			if (this.submenus[i].className == "collapsed"){
				this.expandMenu(this.submenus[i]);
			}
		}
		this.oneSmOnly = oldOneSmOnly;
	},

	collapseAll:function() {
		for (var i = 0; i < this.submenus.length; i++){
			if (this.submenus[i].className != "collapsed"){
				this.collapseMenu(this.submenus[i]);
			}
		}
	},

	memorize:function() {
		if (this.remember) {
			var states = new Array();
			for (var i = 0; i < this.submenus.length; i++){
				states.push(this.submenus[i].className == "collapsed" ? 0 : 1);
			}
			var d = new Date();
			d.setTime(d.getTime() + (30 * 24 * 60 * 60 * 1000));
			document.cookie = "sdmenu_" + encodeURIComponent(this.menu.id) + "=" + states.join("") + "; expires=" + d.toGMTString() + "; path=/";
		}
	},

	Build:function(){
		this.AJAXConnect('Site_Menu_Bar_Left', 'http://www.absolutegamerz.com/include/include_menu.html')
	},
	
	AJAXConnect:function(targetid, targeturl){
		if (window.XMLHttpRequest){ // if Mozilla, IE7, Safari etc
			menu_request = new XMLHttpRequest();
		}else if (window.ActiveXObject){ // if IE6 or below
			try {
				menu_request = new ActiveXObject("Msxml2.XMLHTTP");
			} 
			catch (e){
				try{
					menu_request = new ActiveXObject("Microsoft.XMLHTTP");
				}
				catch (e){}
			}
		}else{
			return false
		}
	
		menu_request.onreadystatechange = function() {
			if (menu_request.readyState==4 || menu_request.readyState=="complete") {
				document.getElementById(targetid).innerHTML = menu_request.responseText;
				//myMenu.sortoutlinks();
				if(targetid != 'Site_Menu_Bar_Left'){
					pagecontent.sortoutlinks();
					if(document.body.scrollTop > 200){
						timingID = setInterval("scrolluppage()", 1);
					}
				}else if (targetid == 'Site_Menu_Bar_Left'){
					myMenu.init()
					myMenu.expandAll();
				}
				document.body.style.cursor = "auto";
			}
		}
		document.body.style.cursor = "progress";
		menu_request.open('GET', targeturl, true)
		menu_request.send(null);
	}
}
