var menuOpen = false;

function showMenu(strId,objLink)
{
	closeMenus();
	var strMenuId = 'subMenu' + strId;
	var objMenu = document.getElementById(strMenuId);
	var intX = findPos(objLink)[0] - 0;
	var intY = findPos(objLink)[1] + 20;
	
	objMenu.style.left = intX + 'px';
	objMenu.style.top = intY + 'px';
	objMenu.style.display = 'block';
	menuOpen = true;
}

function findPos(obj)
{
	var curleft = curtop = 0;
	if (obj.offsetParent)
	{
		curleft = obj.offsetLeft;
		curtop = obj.offsetTop;
		while (obj = obj.offsetParent)
		{
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		}
	}
	return [curleft,curtop];
}

function closeMenus()
{
	if(menuOpen == true)
	{
		var arAllDivs = document.getElementsByTagName('div');
		for (i=0; i<arAllDivs.length; i++)
		{
			if (arAllDivs[i].className == 'subMenu')
			{
				arAllDivs[i].style.display = 'none';
				menuOpen = false;
			}
		}
	}
}

function switchToPoll()
{
	var PollForm = document.getElementById('PollForm');
	var SwingoMeter = document.getElementById('SwingoMeter');
	
	PollForm.style.display = 'block';
	SwingoMeter.style.display = 'none';
}

var eGuide = "";
var eGuideClass = new Class({
	initialize: function(stepName, setName){
		this.stepName = stepName;
		this.setName = setName;
		this.step = 1;
		this.set = 1;
		this.nextStep = 1;
		this.nextSet = 1;
		this.location = 0;
		this.path = [[this.step, this.set]];
		this.arSteps = $$(this.stepName);
		
		// bah..
		this.convStart = "";
	},
	goToPosition: function(step, set) {
		
		objCur = this.arSteps[this.step-1].getElements(this.setName)[this.set-1];
		objNext = this.arSteps[step-1].getElements(this.setName)[set-1];

		// update internal variables
		this.step = step;
		this.set = set;
		
		/*
		// resulttext
		if(step == 8) {
			objNext.getElement(".setContent").grab($("appPrep"));
			objNext.getElement(".convStart").set("html", this.convStart);
		}
		*/
		
		// hide old set
		var myHideEffect = new Fx.Morph(objCur, {duration: 600, transition: Fx.Transitions.Sine.easeOut});
		myHideEffect.start({ "height": 0 }).chain(
			function(){ this.set({"border-width": 0}); }
		);

		// show new set
		myShowEffect = new Fx.Morph(objNext, {duration: 600, transition: Fx.Transitions.Sine.easeOut});
		myShowEffect.set({ "border-width": "0px 0px;"});
		if(objNext.scrollHeight < 320)
			myShowEffect.start({"height": "320px"});
		else
			myShowEffect.start({"height": objNext.scrollHeight});
		
		this.path[this.location] = [step, set];
		
		// dynamic navigation buttons
		if(this.arSteps[this.step-1].hasClass("next")) {
			myShowEffect = new Fx.Morph($("nextlink"), {duration: 600, transition: Fx.Transitions.Sine.easeOut});
			myShowEffect.start({"opacity": "1"});
			if(this.arSteps[this.step-1].hasClass("nochoice")) {
				this.nextStep = step + 1;
				this.nextSet = 1;
			}
		} else {
			myHideEffect = new Fx.Morph($("nextlink"), {duration: 600, transition: Fx.Transitions.Sine.easeOut});
			myHideEffect.start({"opacity": "0"}); 
		}
		if(step == 2) {
			myShowEffect = new Fx.Morph($("backlink"), {duration: 600, transition: Fx.Transitions.Sine.easeOut});
			myShowEffect.start({"opacity": "1"}); 
		} else if(step == 1) {
			myHideEffect = new Fx.Morph($("backlink"), {duration: 600, transition: Fx.Transitions.Sine.easeOut});
			myHideEffect.start({"opacity": "0"}); 
		}
			
		// remove the bottombox
		if($("bottomboxes").getStyle("height").toInt() > 0) {
			myHideEffect = new Fx.Morph($("bottomboxes"), {duration: 600, transition: Fx.Transitions.Sine.easeOut});
			myHideEffect.start({ "height": 0 }).chain(function() {
				
				eGuide.showBB();
			});
		} else {
			this.showBB();
		}
		
		// remove the speechpullout
		if($("speechPullouts").getStyle("height").toInt() > 0) {
			myHideEffect = new Fx.Morph($("speechPullouts"), {duration: 600, transition: Fx.Transitions.Sine.easeOut});
			myHideEffect.start({ "height": 0 }).chain(function() {
				$$(".speechPullout").setStyle("display", "none");
				
				eGuide.showSP();		
			});
		} else {
			this.showSP();
		}
		
		// cleanup
		delete myHideEffect;
		delete myShowEffect;
	},
	showBB: function() {
		// triggers (show bottombox)
      		if(this.step == 5) {
      			$("bb1").setStyle("display", "block");
      			myShowEffect = new Fx.Morph($("bottomboxes"), {duration: 600, transition: Fx.Transitions.Sine.easeOut});
      			myShowEffect.start({"height": $("bb1").scrollHeight + 10});
      		}
      		
	},
	showSP: function() {
        	// triggers (show speechpullout)
        	if(this.step == 5 && this.set == 1) {
        		$("sp1").setStyle("display", "block");
        		myShowEffect = new Fx.Morph($("speechPullouts"), {duration: 600, transition: Fx.Transitions.Sine.easeOut});
        		myShowEffect.start({"height": "352px"});
        	}
        	if(this.step == 2) {
        		$("sp2").setStyle("display", "block");
        		myShowEffect = new Fx.Morph($("speechPullouts"), {duration: 600, transition: Fx.Transitions.Sine.easeOut});
        		myShowEffect.start({"height": "540px"});
        	}
        	
      		
	},
	goBack: function() {
		if(this.location == 0)
			return false;
		this.nextStep = this.step;
		this.nextSet = this.set;
		this.location--;
		this.goToPosition(this.path[this.location][0], this.path[this.location][1]);
	},
	goTo: function(step, set) {
		this.location++;
		
		// for forward navigation after backwards navigation
		if($chk(this.path[this.location+1])) {
			this.nextStep = this.path[this.location+1][0];
			this.nextSet = this.path[this.location+1][1];
		}
		this.goToPosition(step, set);
	}
	
});


// wrapper...
function setnext(step, set) {
	eGuide.nextStep = step;
	eGuide.nextSet = set;
}
function go() {
	if(eGuide.nextStep != eGuide.step)
		eGuide.goTo(eGuide.nextStep, eGuide.nextSet);
}
function goto(step, set) {
	eGuide.goTo(step, set);
}
function goback() {
	return eGuide.goBack();
}

window.addEvent('load', function() {
	eGuide = new eGuideClass(".step", ".set");
	
	// make all choose-lists have onclick events and radio button logics
	var chooseLists = $$("ul.choose");
	if($chk(chooseLists)) {
		chooseLists.each(function(list,i) {
			choices = list.getElements("li");
			choices.each(function(item,i) {
				item.myChecked = false;
				item.addEvent("click", function() {
					var siblings = this.getParent().getChildren();
					siblings.each(function(sibling,i) {
						if(sibling.myChecked) {
							sibling.myChecked = false;
							sibling.setStyle("background-image", "url(imgs/checkbox_un.gif)");
						}
					});
					this.myChecked = true;
					this.setStyle("background-image", "url(imgs/checkbox_ch.gif)");
				});
			});
		});
	}
	
	// make links with rel="external" open in new window
	$$("a").each(function(link, i) {
		if(link.get("rel") == "external")
			link.setProperty("target", "_blank");
	});
});
