document.observe("dom:loaded", function() {
	$(document.body).addClassName("js")
	init_text_size()
	init_category_links()
	init_submit_button()
	init_expanded()
	init_external_links()
	init_form()
	addExpandShadow()
})

function init_text_size() {
	if (Cookie.get("OverlijdenTextSize") == "large") {
		$(document.body).addClassName("large")
	}
	var answers = Cookie.get("OverlijdenAnswers")
	if (answers) {
		answers = answers.evalJSON()
		var sb = $$(".questions select")
		var rb = $$(".questions input[type='radio']")
		if (sb.length >= 3) {
			sb[0].selectedIndex = answers[0]
			sb[1].selectedIndex = answers[1]
			sb[2].selectedIndex = answers[2]
			rb[answers[3] ? 0 : 1].checked = true
			rb[answers[4] ? 2 : 3].checked = true
		}
  }
	Event.observe(
		$("enlarge"), "click",
		function() {
			if ($(document.body).hasClassName("large")) {
				$(document.body).removeClassName("large")
				Cookie.erase("OverlijdenTextSize")
			} else {
				$(document.body).addClassName("large")
				Cookie.set("OverlijdenTextSize", "large")
			}
		}
	)
}

function init_category_links() {
	var functions = $("functions")
	if (functions) {
		functions.insert('<a class="expand-all" href="#expand">Alles uitklappen</a>')
		functions.insert('<a class="collapse-all" href="#collapse" style="display:none">Alles inklappen</a>')
		functions.insert('<span>&nbsp;-&nbsp;</span>')
		functions.insert('<a class="print" href="#print" onclick="return print_all()">Print</a>')
		functions.select(".collapse-all")[0].observe(
			"click",
			function(event) {
				collapse_all()
				Event.stop(event)
			}
		)
		functions.select(".expand-all")[0].observe(
			"click",
			function(event) {
				expand_all()
				Event.stop(event)
			}
		)
	}
}

function init_submit_button() {
	$$("#submit-button").invoke(
		"observe", "click",
		function(event) {
			var i = 0
			var sb = $$(".questions select")
			var rb = $$(".questions input[type='radio']")
			sb.each(function(item) {
				var node = $(item.parentNode.parentNode)
				if (item.selectedIndex == 0) {
					node.addClassName("not-answered")
					item.style.backgroundColor = "#ffde00"
					i += 1
				} else {
					node.removeClassName("not-answered")
					item.style.backgroundColor = "white"
				}
			})
			
			if (i > 0) {
				$$(".not-all-answers")[0].show()
				Event.stop(event)
			} else {
				Cookie.set("OverlijdenAnswers", [sb[0].selectedIndex, sb[1].selectedIndex, sb[2].selectedIndex, rb[0].checked, rb[2].checked].toJSON())
				if (sb[0].selectedIndex == 1) {
					sb[0].selectedIndex = 1
				}
				$$(".not-all-answers")[0].hide()
			}
		}
	)
}

function init_expanded() {
	$$("div.expanded").invoke("removeClassName", "expanded")
  $$("#content-box-right h3 a").invoke(
  	"observe", "click",
		function(event) {
			$(this.parentNode.parentNode).toggleClassName("expanded")
			show_expanded_collapsed()
			Event.stop(event)
		}
  )
}

function init_external_links() {
  $$('[class="external-link"]').invoke(
  	"observe", "click",
		function(event) {
			var newWindow = window.open(this.getAttribute("href"))
			newWindow.focus()
			Event.stop(event)
		}
  )
}

function init_form() {
	$$("ul.questions").invoke("insert", {top:'<li class="not-all-answers" style="display:none">U heeft nog niet alle vragen beantwoord.</li>'})

	var w = $$('[name="jningezetene"]');
	var x = $$('[name="waar_bent_u_momenteel"]');
	var y = $$('#componentform ul.questions li');
	var z = $$('#componentform ul.questions li.first select');

	function checkQuestion() {
		if (z[0].value == 'buiten_nederland_overleden')
		{
			y[2].style.display='block';
			y[3].style.display='block';
		}
		else {
			y[2].style.display='none';
			y[3].style.display='none';
			w[0].checked='checked';
			x[0].value='aangever_in_nederland';
		}
	}

	if(z!=''){
		checkQuestion();
		Event.observe(z[0], 'change', checkQuestion);
	}
	var divs = $$('div.answer');
	for (var i=0;i<divs.length;i++){
		if (divs[i].className=="vraag"){
			divs[i].onclick = toggleAnswer;
		}
	}
}

function show_expanded_collapsed() {
	if ($$(".expanded").length > 0) {
		$$("#functions .expand-all")[0].hide()
		$$("#functions .collapse-all")[0].show()
	} else {
		$$("#functions .expand-all")[0].show()
		$$("#functions .collapse-all")[0].hide()
	}
}

function expand_all() {
	$$("#content-box-right-content > div.section").each(function(item) {item.addClassName("expanded")})
	show_expanded_collapsed()
}

function collapse_all() {
	$$(".expanded").each(function(item) {item.removeClassName("expanded")})
	show_expanded_collapsed()
}

function print_all() {
	window.print()
	return false
}

// make sure the page shadow expands to the viewport or the entire document
function addExpandShadow() {
  if(1 || Prototype.Browser.Gecko) {
    /* sniff for FF, IE uses css expression, other browsers unsupported */
    function _setHeight(_height) {
      $$('html')[0].setStyle({height: _height});
      $(document.body).setStyle({height: _height});
      $('bi-container').setStyle({height: _height});
      $('bi-main').setStyle({height: _height});
    }
    function _doCheck() {
      //_setHeight(scrollMaxY ? 'auto' : '100%');
      _setHeight(document.height > window.innerHeight ? 'auto' : '100%');
    }
    _doCheck();
    Event.observe(window, 'overflow', _doCheck);
    Event.observe(window, 'underflow', _doCheck);
  }
}

