var idCounter = 0;
var debugOutput = undefined;
var OPEN = "open";
var CLOSE = "close";
var animations = new Array();


function handleTimer(type, id) {
	if(animations[id]) {
		if(type == OPEN) {
			animations[id].open();
		} else if(type == CLOSE) {
			animations[id].close();
		}
	}
}

var animationIds = 0;
function NavigationAnimation(control, folder) {
	this.id = animationIds++;
	
	this.open = function() {
		var container = control.findInnerFolder(folder);
		control.addId(container);
		Effect.SlideDown(container.id, { queue: { position: "end", scope: container.id }, duration: 0.1 });
	}
	
	this.close = function() {
		var container = control.findInnerFolder(folder);
		Effect.SlideUp(container, { queue: { position: "end", scope: container.id }, duration: 0.1 });
	}
}

function NavigationControl(classNames, folderNames, innerFolderNames) {
	this.activeTimers = new Array();
	this.hoverClasses = new Array();
	this.disabledFolders = new Array();
	this.debug = false;
	this.ids = 0;
	this.noOnClick = false;
	
	this.addId = function(object) {
		if(object.id == "" || object.id == null || object.id == undefined)
			object.id = this.getNewId();
	}
	
	this.handleOnMouseOver = function(link) {
		this.addId(link);
		if(this.hoverClasses[link.className])
			return;
		this.hoverClasses[classNames[link.className]] = link.className;
		link.className = classNames[link.className];
	}
	
	this.handleOnMouseOut = function(link) {
		this.addId(link);
		if(classNames[link.className])
			return;
		link.className = this.hoverClasses[link.className];
	}
	
	this.handleOnClick = function(link) {
		if(this.noOnClick) {
			this.noOnClick = false;
			return;
		}
		var a = this.getAnchor(link);
		if(a)
			document.location.href = a.href;
	}
	
	this.disableOnClickEvent = function() {
		this.noOnClick = true;
	}
	
	this.getAnchor = function(link) {
		var childs = link.childNodes;
		for(var i = 0; i < childs.length; i++) {
			if(childs[i].nodeType == 1)
				if(childs[i].nodeName == "a" || childs[i].nodeName == "A")
					return childs[i];
		}
	}
	
	this.handleOnMouseOverFolder = function(folder) {
		this.addId(folder);
		if(this.disabledFolders[folder.id])
			return;
		if(this.activeTimers[folder.id])
			window.clearTimeout(this.activeTimers[folder.id]);
		
		if(!this.isOpen(this.findInnerFolder(folder))) {
			var animation = new NavigationAnimation(this, folder);
			animations[animation.id] = animation;
			var timer = window.setTimeout("handleTimer(OPEN, '" + animation.id +"')", 500);
			this.activeTimers[folder.id] = timer;
		}
	}
	
	this.handleOnMouseOutFolder = function(folder) {
		this.addId(folder);
		if(this.disabledFolders[folder.id])
			return;
		if(this.activeTimers[folder.id])
			window.clearTimeout(this.activeTimers[folder.id]);
			
		if(this.isOpen(this.findInnerFolder(folder))) {
			var animation = new NavigationAnimation(this, folder);
			animations[animation.id] = animation;
			var timer = window.setTimeout("handleTimer(CLOSE, '" + animation.id +"')", 500);
			this.activeTimers[folder.id] = timer;
		}
	}
	
	this.findInnerFolder = function(folder) {
		for(var i = 0; i < folder.childNodes.length; i++) {
			if(innerFolderNames[folder.childNodes[i].className]) {
				return folder.childNodes[i];
			}
		}
	}
	
	this.startUp = function() {
		var divs = document.getElementsByTagName("div");
		for(var i = 0; i < divs.length; i++) {
			if(innerFolderNames[divs[i].className]) {
				if(divs[i].style.display != 'none') {
					this.addId(divs[i].parentNode);
					if(divs[i].parentNode)
						this.disabledFolders[divs[i].parentNode.id] = true;
				}
			}
		}
	}
	
	this.setDebug = function(flag) {
		this.debug = flag;
	}
	
	this.handleError = function(msg) {
		this.print("Error", msg);
	}
	
	this.echo = function(msg) {
		this.print("Hinweis", msg);
	}
	
	this.print = function(type, msg) {
		if(this.debug) {
			if(debugOutput == undefined) {
				var div = document.createElement("div");
				div.style.position = "absolute";
				div.style.right = "5px";
				div.style.bottom = "5px";
				div.style.border = "1px solid black";
				div.style.width = "250px";
				div.style.backgroundColor = "white";
				document.getElementsByTagName("body")[0].appendChild(div);
				debugOutput = div;
			}
			debugOutput.innerHTML = debugOutput.innerHTML + type + ": " + msg + "<br>"; 
		}
	}
	
	this.getNewId = function() {
		return "nav" + (this.ids++);
	}
	
	this.isOpen = function(folder) {
		return folder.style.display != 'none';
	}
}


function handleBillingChange() {
	var radio = document.register.billing_type;
	var value = 0;
	
	for(var i = 0; radio[i]; i++) {
		if(radio[i].checked)
			value = radio[i].value;
	}
	if(value == 1) {
		$("bank_transfer_informations_tr").show();
		if($("bank_transfer_informations").style.display == "none")
			Effect.SlideDown($("bank_transfer_informations"), { queue: { position: "end", scope: "bank_transfer_informations" }, duration: 0.2 });
	} else {
		if($("bank_transfer_informations").style.display != "none")
			Effect.SlideUp($("bank_transfer_informations"), { queue: { position: "end", scope: "bank_transfer_informations" }, duration: 0.2, afterFinish: handleBankTransferViewFinish });
	}
}

function handleBankTransferViewFinish() {
	$("bank_transfer_informations_tr").hide();
}

function handleBillingChangeFinish() {
	$("credit_card_informations_tr").hide();
}

function handleSlideDown(target) {
	var object = $(target);
	if(object.parentNode.parentNode.style.display == "none") {
		$(object.parentNode.parentNode).show();
		Effect.SlideDown(object, { queue: { position: "end", scope: object.id }, duration: 0.2 });
	}
}

function handleSlideUp(target) {
	var object = $(target);
	if(object.parentNode.parentNode.style.display != "none")
		Effect.SlideUp(object, { queue: { position: "end", scope: object.id }, duration: 0.2, afterFinish: handleSlideUpFinish });
}

function handleSlideUpFinish(effect) {
	$(effect.element.parentNode.parentNode).hide();
}

function handleBillingChangeStart() {
}

var nr = 0;
function addNewLine(offset) {
	var id = nr++;
	var newLine = Builder.node("tr", [
		Builder.node("td", {className: "odd"}, ["Person " + (id + 1 + offset) +":", Builder.node("br"), Builder.node("span", {className: "note"}, ["(First Name, Name)"])]),
		Builder.node("td", {className: "even"}, [Builder.node("input", {name: "new_person_first_name[" + id + "]", type: "text" ,size: 30}), " ", Builder.node("input", {name: "new_person_name[" + id + "]", type: "text" ,size: 30})])
	]);
	Element.hide(newLine);
	$($('newPersonLine').parentNode).insert(newLine);
	Effect.Appear(newLine);
}

function handlePrice(id, dinnerPrice, togetherPrice) {
	var dinner = document.getElementsByName("dinner[" + id + "]")[0];
	var together = document.getElementsByName("together[" + id + "]")[0];
	
	var result = "<span style=\"color: gray\">0 &euro;</span>";
	var price = 0;
	if(dinner.checked)
		price += dinnerPrice;
	if(together.checked)
		price += togetherPrice;
	if(price > 0)
		result = price + " &euro;";
	
	document.getElementById("price_" + id).innerHTML = result;
}

function getOverallPrice(name, price) {
	var i = 0;
	var overall = 0;
	
	while(document.getElementsByName(name + "[" + i + "]")[0]) {
		var dinner = document.getElementsByName(name + "[" + i + "]")[0];
		
		if(dinner.checked)
			overall += price;
		i++;
	}
	
	return overall;
}

function updateSummary(fee, dinner, together, proceedings) {
	var dinner_price = getOverallPrice("dinner", dinner);
	var together_price = getOverallPrice("together", together);
	
	extra_proceedings = document.getElementsByName("extra_proceedings")[0].value * 1;
	document.getElementById("extra_proceedings_price").innerHTML = (extra_proceedings * proceedings) + " &euro;";
	
	if(document.getElementById("dinner_overall")) {
		document.getElementById("dinner_overall").innerHTML = dinner_price + " &euro;";
		document.getElementById("together_overall").innerHTML = together_price + " &euro;";
	}
	
	document.getElementById("proceedings_overall").innerHTML = extra_proceedings * proceedings + " &euro;";
	
	document.getElementById("overall").innerHTML = (together_price + dinner_price) + " &euro;";
	document.getElementById("overall_registrant").innerHTML = (fee + proceedings * extra_proceedings) + " &euro;";
	document.getElementById("amount").innerHTML = (fee + together_price + dinner_price + proceedings * extra_proceedings) + " &euro;";
}

function handleUpdateProceedings(target, fee, dinner, together, proceedings) {
	target.value = target.value * 1;
	updateSummary(fee, dinner, together, proceedings);
}

function handleDinnerPrice(id, fee, dinner, together, proceedings) {
	handlePrice(id, dinner, together);
	updateSummary(fee, dinner, together, proceedings);
}

function handleTogetherPrice(id, fee, dinner, together, proceedings) {
	handlePrice(id, dinner, together);
	updateSummary(fee, dinner, together, proceedings);
}

function handleReduces(target) {
	if(target.options[target.selectedIndex].value == 3)
		handleSlideDown($('gi_number_tr'));
	else
		handleSlideUp($('gi_number_tr'));
}

function handleInvoice(target) {
	if(target.options[target.selectedIndex].value == 2)
		handleSlideDown($('separate_invoice_adress'));
	else
		handleSlideUp($('separate_invoice_adress'));
}

function handleCachingProblems() {
	if(document.register.dummy) {
		document.register.dummy.value = parseInt(Math.random() * 10000000);
	}
}

function nextStep(step) {
	handleCachingProblems();
	if(step)
		document.register.action.value = step;
	document.register.submit();
}
