/*
 * ACCIDENT GALLERY.COM
 *
 * Class: menu.js
 * Version: 1.0.0
 * Desc:	The class responsible for all menu
 *		functionality. Defines behavior and
 *		text strings for navigation buttons.
 */

var Menu = function() {

	var xmlhttp = null;
	var xmlDoc = null;
	var button = null;
	var container = null;
	var activeTab = null;
	var buttons = null;
	var content = null;
	var activeSection = null;

	function _construct() {
		button = [];

		button[0] = {	name : "home",
				obj  : document.getElementById("button1"),
				link : "agMenu.goto(0)",
				item : []	};
		button[1] = {	name : "gallery",
				obj  : document.getElementById("button2"),
				link : "agMenu.goto(1)",
				item : []	};
		button[2] = {	name : "exhibits",
				obj  : document.getElementById("button3"),
				link : "agMenu.goto(2)",
				item : []	};
		button[3] = {	name : "events",
				obj  : document.getElementById("button4"),
				link : "agMenu.goto(3)",
				item : []	};
		button[4] = {	name : "artists",
				obj  : document.getElementById("button5"),
				link : "agMenu.goto(4)",
				item : []	};
		button[5] = {	name : "store",
				obj  : document.getElementById("button6"),
				link : "agMenu.goto(5)",
				item : []	};

		for(var b=0;b<button.length;b++) {
			if(button[b].name=="home") {
					button[b].item[0] = {	name : "headlines",
								link : "agMenu.enter(0)",
								path  : "http://www.accidentgallery.com/"
								};
					button[b].item[1] = {	name : "promos",
								link : "agMenu.enter(1)",
								path  : "/"
								};
					button[b].item[2] = {	name : "press",
								link : "agMenu.enter(2)",
								path  : "/"
								};
					button[b].item[3] = {	name : "sitemap",
								link : "agMenu.enter(3)",
								path  : "/"
								};
					button[b].item[4] = {	name : "&nbsp;",
								link : null,
								path  : null
								};
					button[b].item[5] = {	name : "&nbsp;",
								link : null,
								path  : null
								};
			}
			if(button[b].name=="gallery") {
					button[b].item[0] = {	name : "mission",
								link : "agMenu.enter(0)",
								path  : "/"
								};
					button[b].item[1] = {	name : "tour",
								link : "agMenu.enter(1)",
								path  : "/"
								};
					button[b].item[2] = {	name : "beer bar",
								link : "agMenu.enter(2)",
								path  : "/"
								};
					button[b].item[3] = {	name : "submit",
								link : "agMenu.enter(3)",
								path  : "/"
								};
					button[b].item[4] = {	name : "join",
								link : "agMenu.enter(4)",
								path  : "/"
								};
					button[b].item[5] = {	name : "&nbsp;",
								link : null,
								path  : null
								};
			}
			if(button[b].name=="exhibits") {
					button[b].item[0] = {	name : "present",
								link : "agMenu.enter(0)",
								path  : "/"
								};
					button[b].item[1] = {	name : "future",
								link : "agMenu.enter(1)",
								path  : "/"
								};
					button[b].item[2] = {	name : "past",
								link : "agMenu.enter(2)",
								path  : "/"
								};
					button[b].item[3] = {	name : "&nbsp;",
								link : null,
								path  : null
								};
					button[b].item[4] = {	name : "&nbsp;",
								link : null,
								path  : null
								};
					button[b].item[5] = {	name : "&nbsp;",
								link : null,
								path  : null
								};
			}
			if(button[b].name=="events") {
					button[b].item[0] = {	name : "calendar",
								link : "agMenu.enter(0)",
								path  : "/"
								};
					button[b].item[1] = {	name : "booking",
								link : "agMenu.enter(1)",
								path  : "/"
								};
					button[b].item[2] = {	name : "archive",
								link : "agMenu.enter(2)",
								path  : "/"
								};
					button[b].item[3] = {	name : "updates",
								link : "agMenu.enter(3)",
								path  : "/"
								};
					button[b].item[4] = {	name : "&nbsp;",
								link : null,
								path  : null
								};
					button[b].item[5] = {	name : "&nbsp;",
								link : null,
								path  : null
								};
			}
			if(button[b].name=="artists") {
					button[b].item[0] = {	name : "members",
								link : "agMenu.enter(0)",
								path  : "/"
								};
					button[b].item[1] = {	name : "featured",
								link : "agMenu.enter(1)",
								path  : "/"
								};
					button[b].item[2] = {	name : "guests",
								link : "agMenu.enter(2)",
								path  : "/"
								};
					button[b].item[3] = {	name : "&nbsp;",
								link : null,
								path  : null
								};
					button[b].item[4] = {	name : "&nbsp;",
								link : null,
								path  : null
								};
					button[b].item[5] = {	name : "&nbsp;",
								link : null,
								path  : null
								};
			}
			if(button[b].name=="store") {
					button[b].item[0] = {	name : "etsy shop",
								link : "agMenu.enter(0)",
								path  : "/"
								};
					button[b].item[1] = {	name : "&nbsp;",
								link : null,
								path  : null
								};
					button[b].item[2] = {	name : "&nbsp;",
								link : null,
								path  : null
								};
					button[b].item[3] = {	name : "&nbsp;",
								link : null,
								path  : null
								};
					button[b].item[4] = {	name : "&nbsp;",
								link : null,
								path  : null
								};
					button[b].item[5] = {	name : "&nbsp;",
								link : null,
								path  : null
								};
			}
		}
		container	= document.getElementById("container");
		activeTab	= document.getElementById("activeTab");
		buttons		= document.getElementById("buttons");
		content		= document.getElementById("content");

		loadButtons();
	}
	function setActiveSection(n) {
		activeSection = n;
	}
	function revealTab(n) {
		activeTab.style.left = parseInt(container.style.paddingLeft) + ((n+1) * 110) + "px";
		activeTab.innerHTML = button[n].name.toUpperCase();
		activeTab.style.visibility = "visible";
		activeTab.style.display = "block";
	}
	function forceTab(n) {
		activeTab.style.left = parseInt(container.style.paddingLeft) + "px";
		activeTab.innerHTML = button[n].name.toUpperCase();
		activeTab.style.visibility = "visible";
		activeTab.style.display = "block";
	}
	function allowTabClick() {
		activeTab.innerHTML = 	'<a href="javascript:void(0);" onClick="agMenu.back(); return false;">' +
					button[activeSection].name.toUpperCase() +
					'<div id="x_icon"></div>' +
					'</a>';
		var xIcon = document.getElementById("x_icon");

		xIcon.style.position = "absolute";
		xIcon.style.left = "101px";
		xIcon.style.top = "2px";
		xIcon.style.width = "9px";
		xIcon.style.height = "9px";
		xIcon.style.backgroundImage = "url('images/global/x_icon.gif')";
	}
	function preventTabClick() {
		activeTab.innerHTML = button[activeSection].name.toUpperCase();
	}
	function hideTab() {
		activeTab.style.visibility = "hidden";
		activeTab.style.left = parseInt(container.style.paddingLeft) + "px";
		activeTab.innerHTML = "";
	}
	function start(n) {
		forceTab(n);
		allowTabClick();
		setActiveSection(n);
		loadItems(n);
	}
	function xmlGetRequest() {
		if(window.ActiveXObject) {
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		} else {
			if(window.XMLHttpRequest) {
				xmlhttp = new XMLHttpRequest();
			} else {
				alert("failed to create XMLHttpRequest");
			}
		}
	}
	function xmlGetResponse() {
		return xmlhttp.responseText;
	}
	function xmlAttachEvents() {
		xmlhttp.onreadystatechange = xmlChange;
	}
	function xmlChange() {
		if(xmlhttp.readyState == 4 && xmlDoc == null) {
			xmlDoc = xmlGetResponse();
			xmlResult();
			xmlRevealResult();
		}
	}	
	function xmlLoadFile(xDoc, sFlag) {
		xmlhttp.open("GET", xDoc, sFlag);
		xmlhttp.send(null);
	}
	function xmlResult() {
		content.innerHTML = xmlDoc;

		if(content.innerHTML.indexOf("artist_page") != (-1)) {
			if(agArtistMenu) {
				agArtistMenu.init();
			}
		} else {
			if(agArtistMenu) {
				agArtistMenu.suspend();
			}
		}
	}
	function load(i) {
		xmlGetRequest();
		xmlAttachEvents();
		xmlDoc = null;

		if(xmlhttp) {
			if(activeSection == 0) {
				if(i == 0) {
					xmlLoadFile("xml/headlines.xml",true);
				} else if(i == 1) {
					xmlLoadFile("xml/comingsoon.xml",true);
				} else if(i == 2) {
					xmlLoadFile("xml/comingsoon.xml",true);
				} else if(i == 3) {
					xmlLoadFile("xml/comingsoon.xml",true);
				}
			} else if(activeSection == 1) {
				if(i == 0) {
					xmlLoadFile("xml/mission.xml",true);
				} else if(i == 1) {
					xmlLoadFile("xml/comingsoon.xml",true);
				} else if(i == 2) {
					xmlLoadFile("xml/beer_bar.xml",true);
				} else if(i == 3) {
					xmlLoadFile("xml/comingsoon.xml",true);
				} else if(i == 4) {
					xmlLoadFile("xml/comingsoon.xml",true);
				}
			} else if(activeSection == 2) {
				if(i == 0) {
					xmlLoadFile("xml/present_exhibit.xml",true);
				} else if(i == 1) {
					xmlLoadFile("xml/comingsoon.xml",true);
				} else if(i == 2) {
					xmlLoadFile("xml/comingsoon.xml",true);
				}
			} else if(activeSection == 3) {
				if(i == 0) {
					xmlLoadFile("xml/calendar.xml",true);
				} else if(i == 1) {
					xmlLoadFile("xml/comingsoon.xml",true);
				} else if(i == 2) {
					xmlLoadFile("xml/comingsoon.xml",true);
				} else if(i == 3) {
					xmlLoadFile("xml/comingsoon.xml",true);
				}
			} else if(activeSection == 4) {
				if(i == 0) {
					xmlLoadFile("xml/members.xml",true);
				} else if(i == 1) {
					xmlLoadFile("xml/comingsoon.xml",true);
				} else if(i == 2) {
					xmlLoadFile("xml/comingsoon.xml",true);
				}
			} else if(activeSection == 5) {
				if(i == 0) {
					xmlLoadFile("xml/store.xml",true);
				}
			}
		}
	}
	function xmlRevealResult() {

		var fadeIn = 20;

		var anim = new Animator();

		var obj1 = new MovingObject(content);
			obj1.loadMotionA(new Tween(	0,
							100,
							0,
							fadeIn,
							new Spline(fadeIn,
								[	{x: 0.00,  y: 0.00,  c: 1.00,  m:null},
									{x: 0.00,  y: 0.00,  c: 1.00,  m:null},
									{x: 0.40,  y: 0.20,  c: 0.50,  m:null},
									{x: 0.60,  y: 0.80,  c: 0.50,  m:null},
									{x: 1.00,  y: 1.00,  c: 1.00,  m:null},
									{x: 1.00,  y: 1.00,  c: 1.00,  m:null}	]),
								function(){ removeAlpha() }));
		var objArray = [obj1];
		anim.init(0, 60);
		anim.importList(objArray);
		anim.initScene();
		anim.animate();
	}
	function removeAlpha() {
		content.style.opacity = "";
		content.style.filter = "";
	}
	function enter(i) {

		var fadeOut = 20;
		
		var anim = new Animator();

		var obj1 = new MovingObject(content);
			obj1.loadMotionA(new Tween(	100,
							0,
							0,
							fadeOut,
							new Spline(fadeOut,
								[	{x: 0.00,  y: 0.00,  c: 1.00,  m:null},
									{x: 0.00,  y: 0.00,  c: 1.00,  m:null},
									{x: 0.40,  y: 0.20,  c: 0.50,  m:null},
									{x: 0.60,  y: 0.80,  c: 0.50,  m:null},
									{x: 1.00,  y: 1.00,  c: 1.00,  m:null},
									{x: 1.00,  y: 1.00,  c: 1.00,  m:null}	]),
								function(){ load(i) }));
		var objArray = [obj1];
		anim.init(0, 60);
		anim.importList(objArray);
		anim.initScene();
		anim.animate();
	}
	function returnToWelcome() {
		xmlGetRequest();
		xmlAttachEvents();
		xmlDoc = null;

		xmlLoadFile("xml/welcome.xml", true);

		loadButtons();
	}	
	function back() {
		preventTabClick();

		var fadeOut = 10;
		var slideOver = 20;
		var fadeIn = 10;

		var anim = new Animator();

		var obj1 = new MovingObject(buttons);
			obj1.loadMotionA(new Tween(	100,
							0,
							0,
							fadeOut,
							new Spline(fadeOut,
								[	{x: 0.00,  y: 0.00,  c: 1.00,  m:null},
									{x: 0.00,  y: 0.00,  c: 1.00,  m:null},
									{x: 0.40,  y: 0.20,  c: 0.50,  m:null},
									{x: 0.60,  y: 0.80,  c: 0.50,  m:null},
									{x: 1.00,  y: 1.00,  c: 1.00,  m:null},
									{x: 1.00,  y: 1.00,  c: 1.00,  m:null}	]),
								function(){ returnToWelcome() }));
		var obj2 = new MovingObject(activeTab);
			obj2.loadMotionX(new Tween(	parseInt(activeTab.style.left),
							parseInt(container.style.paddingLeft) + ((activeSection+1) * 110),
							fadeOut,
							slideOver,
							new Spline(slideOver,
								[	{x: 0.00,  y: 0.00,  c: 1.00,  m:null},
									{x: 0.00,  y: 0.00,  c: 1.00,  m:null},
									{x: 0.40,  y: 0.20,  c: 0.50,  m:null},
									{x: 0.60,  y: 0.80,  c: 0.50,  m:null},
									{x: 1.00,  y: 1.00,  c: 1.00,  m:null},
									{x: 1.00,  y: 1.00,  c: 1.00,  m:null}	]),
								null));
		var obj3 = new MovingObject(buttons);
			obj3.loadMotionA(new Tween(	0,
							100,
							fadeOut + slideOver,
							fadeIn,
							new Spline(fadeIn,
								[	{x: 0.00,  y: 0.00,  c: 1.00,  m:null},
									{x: 0.00,  y: 0.00,  c: 1.00,  m:null},
									{x: 0.40,  y: 0.20,  c: 0.50,  m:null},
									{x: 0.60,  y: 0.80,  c: 0.50,  m:null},
									{x: 1.00,  y: 1.00,  c: 1.00,  m:null},
									{x: 1.00,  y: 1.00,  c: 1.00,  m:null}	]),
								function(){ hideTab() }));
		var objArray = [obj1, obj2, obj3];
		anim.init(0, 60);
		anim.importList(objArray);
		anim.initScene();
		anim.animate();
	}
	function goToHeadlines() {
		setActiveSection(0);
		forceTab(0);
		allowTabClick();
		loadItems(0);

		xmlGetRequest();
		xmlAttachEvents();
		xmlDoc = null;

		xmlLoadFile("xml/headlines.xml", true);
	}
	function goToCalendar() {
		setActiveSection(3);
		forceTab(3);
		allowTabClick();
		loadItems(3);
	
		xmlGetRequest();
		xmlAttachEvents();
		xmlDoc = null;

		xmlLoadFile("xml/calendar.xml", true);
	}
	function goToMembers() {
		setActiveSection(4);
		forceTab(4);
		allowTabClick();
		loadItems(4);
	
		xmlGetRequest();
		xmlAttachEvents();
		xmlDoc = null;

		xmlLoadFile("xml/members.xml", true);
	}
	function goto(n) {
		
		setActiveSection(n);
		revealTab(n);

		var fadeOut = 10;
		var slideOver = 20;
		var fadeIn = 10;

		var anim = new Animator();
		
		var obj1 = new MovingObject(buttons);
			obj1.loadMotionA(new Tween(	100,
							0,
							0,
							fadeOut,
							new Spline(fadeOut,
								[	{x: 0.00,  y: 0.00,  c: 1.00,  m:null},
									{x: 0.00,  y: 0.00,  c: 1.00,  m:null},
									{x: 0.40,  y: 0.20,  c: 0.50,  m:null},
									{x: 0.60,  y: 0.80,  c: 0.50,  m:null},
									{x: 1.00,  y: 1.00,  c: 1.00,  m:null},
									{x: 1.00,  y: 1.00,  c: 1.00,  m:null}	]),
								function(){ loadItems(n) }));
		var obj2 = new MovingObject(activeTab);
			obj2.loadMotionX(new Tween(	parseInt(activeTab.style.left),
							parseInt(container.style.paddingLeft),
							fadeOut,
							slideOver,
							new Spline(slideOver,
								[	{x: 0.00,  y: 0.00,  c: 1.00,  m:null},
									{x: 0.00,  y: 0.00,  c: 1.00,  m:null},
									{x: 0.40,  y: 0.20,  c: 0.50,  m:null},
									{x: 0.60,  y: 0.80,  c: 0.50,  m:null},
									{x: 1.00,  y: 1.00,  c: 1.00,  m:null},
									{x: 1.00,  y: 1.00,  c: 1.00,  m:null}	]),
								null));
		var obj3 = new MovingObject(buttons);
			obj3.loadMotionA(new Tween(	0,
							100,
							fadeOut + slideOver,
							fadeIn,
							new Spline(fadeIn,
								[	{x: 0.00,  y: 0.00,  c: 1.00,  m:null},
									{x: 0.00,  y: 0.00,  c: 1.00,  m:null},
									{x: 0.40,  y: 0.20,  c: 0.50,  m:null},
									{x: 0.60,  y: 0.80,  c: 0.50,  m:null},
									{x: 1.00,  y: 1.00,  c: 1.00,  m:null},
									{x: 1.00,  y: 1.00,  c: 1.00,  m:null}	]),
								function(){ allowTabClick() }));
		var objArray = [obj1, obj2, obj3];
		anim.init(0, 60);
		anim.importList(objArray);
		anim.initScene();
		anim.animate();
	}
	function loadItems(n) {
		for(var i=0; i<button[n].item.length; i++) {
			if(button[n].item[i].name == "&nbsp;") {
				button[i].obj.innerHTML = 	button[n].item[i].name;
			} else {
				var htmlString =	'<a href="javascript:void(0);" onclick="' +
							button[n].item[i].link +
							'; return false;">' +
							button[n].item[i].name.toLowerCase() +
							'</a>';
				
				button[i].obj.innerHTML = htmlString;
			}
		}
	}
	function loadButtons() {
		for(var b=0; b<button.length; b++) {
			var htmlString =	'<a href="javascript:void(0);" onclick="' +
						button[b].link +
						'; return false;">' +
						button[b].name.toUpperCase() +
						'</a>';

			button[b].obj.innerHTML = htmlString;
		}
	}
	this._construct = _construct;
	this.goto = goto;
	this.goToHeadlines = goToHeadlines;
	this.goToCalendar = goToCalendar;
	this.goToMembers = goToMembers;
	this.back = back;
	this.enter = enter;
	this.start = start;
	_construct();

}