$(function()
{
  $("#contentLeft").css("padding-bottom",$("#footer").height()+20);
  $(".topMenuLink").each(function(i) { $(this).hover(function() { hoverTopMenu(this,i,"in"); },function() { hoverTopMenu(this,i,"out"); }); })
  $(".menuLink").not(".active").each(function() { $(this).hover(function() { hoverMenu(this,"in"); },function() { hoverMenu(this,"out"); }); });
  $(".dumbCrossFade .dumbItem").dumbCrossFade({"slideType":"fade","index":Math.floor(Math.random()*5),"doHoverPause":false});
  $(function()
  {
    $(".lightbox").lightBox(
    {
      imageLoading: "images/lightbox-ico-loading.gif",
      imageBtnClose: "images/lightbox-btn-close.gif",
      imageBtnPrev: "images/lightbox-btn-prev.gif",
      imageBtnNext: "images/lightbox-btn-next.gif",
      imageBlank: "images/lightbox-blank.gif",
      txtImage: "",
      txtOf: "/"
    });
  });
	setContentHeight();
});

$(window).load(function()
{
  fixFacePage();
});

$(window).resize(function()
{
  setContentHeight();
});

function hoverMenu(obj,direction)
{
  if (direction=="in") $(obj).find(".arrow").attr("src","images/arrow.png");
  else if (direction=="out") $(obj).find(".arrow").attr("src","images/spacer.gif");
}

function setContentHeight()
{
  var lh = $("#contentLeft2").outerHeight() + $("#footer").height() + 20 + 20;
  var rh = 90;//$("#contentRight2").height();
  $(".rightBlock").each(function() { rh += $(this).outerHeight(); });

	if (rh>lh)
	{
		$("#contentRight").height(rh);
		$("#contentLeft").height(rh - $("#footer").height() - 20 - 20);
	}
	else
	{
		$("#contentRight").height(lh + 20 + 20);
		$("#contentLeft").height(lh - $("#footer").height());
	}
}

function toggleEvent(id)
{
  if ($("#"+id).css("display")!="none") $(".toggleEvent").slideUp("slow",function() { setContentHeight(); });
  else
  {
    $(".toggleEvent").each(function()
    {
      if ($(this).attr("id")==id) $(this).slideDown("slow",function() { setContentHeight(); });
      else $(this).slideUp("slow",function() { setContentHeight(); });
    });
  }
}

function toggleMedia(id)
{
  if ($("#"+id).css("display")!="none") $(".toggleMedia").slideUp("slow",function() { setContentHeight(); });
  else
  {
    $(".toggleMedia").each(function()
    {
      if ($(this).attr("id")==id) $(this).slideDown("slow",function() { setContentHeight(); });
      else $(this).slideUp("slow",function() { setContentHeight(); });
    });
  }
}

function toggleNews(id)
{
  if ($("#"+id).css("display")!="none") $(".toggleNews").slideUp("slow",function() { setContentHeight(); });
  else
  {
    $(".toggleNews").each(function()
    {
      if ($(this).attr("id")==id) $(this).slideDown("slow",function() { setContentHeight(); });
      else $(this).slideUp("slow",function() { setContentHeight(); });
    });
  }
}

function toggleDiv(prefix,nr)
{
  if ($("#"+prefix+nr).css("display")!="none") $(".toggleDiv3").slideUp("slow",function() { setContentHeight(); });
  else
  {
    $(".toggleDiv3").each(function()
    {
      if ($(this).attr("id")==prefix+nr) $(this).slideDown("slow",function() { setContentHeight(); });
      else $(this).slideUp("slow",function() { setContentHeight(); });
    });
  }
}

function toggleSlide(id)
{
  if ($("#"+id).css("display")!="none") $("#"+id).slideUp("slow",function() { setContentHeight(); });
  else $("#"+id).slideDown("slow",function() { setContentHeight(); });
}

function checkEmail(address)
{
  var filter = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
  if (filter.test(address)) return true;
  return false;
}

function checkForm(formName)
{
  var form = document.getElementById(formName);
  var error = false;
  var errorMsg = "De volgende velden zijn niet (correct) ingevuld:\n";
  if (form.firstName.value == null || form.firstName.value == "") { error = true; errorMsg += "\n- voornaam"; }
  if (form.lastName.value  == null || form.lastName.value  == "") { error = true; errorMsg += "\n- achternaam"; }
  if (form.address.value   == null || form.address.value   == "") { error = true; errorMsg += "\n- straat + huisnummer"; }
  if (form.zipcode.value   == null || form.zipcode.value   == "") { error = true; errorMsg += "\n- postcode"; }
  if (form.city.value      == null || form.city.value      == "") { error = true; errorMsg += "\n- plaats"; }
  if (form.email.value     == null || form.email.value     == "" || !checkEmail(form.email.value)) { error = true; errorMsg += "\n- e-mailadres"; }
  if (formName.indexOf("contact")>-1)
  { 
    if (form.info.value == null || form.info.value == "") { error = true; errorMsg += "\n- bericht"; }
  }
  if (error) alert(errorMsg);
  else document.getElementById(formName).submit();
}

function checkRegForm(formName)
{
  var form  = document.getElementById(formName);
  var error = false;
  var type  = getCheckedValue(form.type);
  var errorMsg = "De volgende velden zijn niet (correct) ingevuld:\n";
  if (form.firstName.value == null || form.firstName.value == "") { error = true; errorMsg += "\n- voornaam"; }
  if (form.lastName.value  == null || form.lastName.value  == "") { error = true; errorMsg += "\n- achternaam"; }
  if (type == "Student")
  {
    if (form.schoolSelect.value == null || form.schoolSelect.value == "") { error = true; errorMsg += "\n- school"; }
    if (form.zipcode.value      == null || form.zipcode.value      == "") { error = true; errorMsg += "\n- postcode"; }
    if (form.city.value         == null || form.city.value         == "") { error = true; errorMsg += "\n- plaats"; }
  }
  if (type == "Bedrijf")
  {
		if (form.companyName.value == null || form.companyName.value == "") { error = true; errorMsg += "\n- bedrijfsnaam"; }
    if (form.address.value     == null || form.address.value     == "") { error = true; errorMsg += "\n- straat + huisnummer"; }
    if (form.zipcode.value     == null || form.zipcode.value     == "") { error = true; errorMsg += "\n- postcode"; }
    if (form.city.value        == null || form.city.value        == "") { error = true; errorMsg += "\n- plaats"; }
  }
  if (type == "Medewerker")
  {
    if (form.address.value == null || form.address.value == "") { error = true; errorMsg += "\n- straat + huisnummer"; }
    if (form.zipcode.value == null || form.zipcode.value == "") { error = true; errorMsg += "\n- postcode"; }
    if (form.city.value    == null || form.city.value    == "") { error = true; errorMsg += "\n- plaats"; }
  }
  if (form.email.value     == null || form.email.value     == "" || !checkEmail(form.email.value)) { error = true; errorMsg += "\n- e-mailadres (login)"; }
  if (form.password1.value == null || form.password1.value == "") { error = true; errorMsg += "\n- wachtwoord"; }
  if (form.password2.value == null || form.password2.value == "") { error = true; errorMsg += "\n- wachtwoord (nogmaals)"; }
  if (form.password1.value != form.password2.value) { error = true; errorMsg += "\n- de wachtwoorden zijn niet gelijk"; }
  if (error) alert(errorMsg);
  else form.submit();
}

function checkPassForm(formName)
{
  var form = document.getElementById(formName);
  var error = false;
  var errorMsg = "";
  if (form.passUserName.value == null || form.passUserName.value == "") { error = true; errorMsg = "U heeft geen gebruikersnaam ingevuld."; }
  if (error) alert(errorMsg);
  else form.submit();
}

function check_keycode()
{
  var k = window.event.keyCode;
  if ( k >= 48 && k <= 57 || k >= 96 && k <= 105 || k == 8 || k == 9 || k == 46 || k == 27 || k >= 33 && k <= 40 || k == 45 ) window.event.returnValue = true;
  else window.event.returnValue = false;
}

function getCheckedValue(radioObj)
{
  if (!radioObj) return "";
  var radioLength = radioObj.length;
  if (radioLength == undefined)
  {
    if (radioObj.checked) return radioObj.value;
    else return "";
  }
  for (var i=0; i<radioLength; i++)
  {
    if (radioObj[i].checked) return radioObj[i].value;
  }
  return "";
}

function setRegistrationForm(type)
{
  if (type=="company") 
	{
    $(".showstudent").hide();
    $(".showemployee").hide();
		$(".showcompany").show();
	}
  else if (type=="student") 
  {
    $(".showcompany").hide();
    $(".showemployee").hide();
    $(".showstudent").show();
  }
  else if (type=="employee") 
  {
    $(".showcompany").hide();
    $(".showstudent").hide();
    $(".showemployee").show();
  }
}

function multiselect_validate(select) 
{     
  var valid = false;   
  for (var i=0; i<select.options.length; i++) 
  {   
    if (select.options[i].selected) { valid = true; break; }
  }
  if (valid) $("#sendmessage").show();
  else $("#sendmessage").hide();  
}

function toggleTree(obj,id)
{
	if ($("#"+id).css("display")=="none")
	{
		$(obj).attr("src","images/arrow_open.png");
		$("#"+id).show(function() { setContentHeight(); });
	}
	else
	{
    $(obj).attr("src","images/arrow_closed.png");
    $("#"+id).hide(function() { setContentHeight(); });
	}
}

function fixFacePage()
{
  var highest = 0;
	$(".faceItem").each(function() 
  {
    if ($(this).height()>highest) highest = $(this).height();
  });
  $(".faceItem").height(highest);
	/*
	$(".leftFace").each(function() 
	{
		if ($(this).next(".rightFace"))
		{
	    var leftHeight  = $(this).height();
	    var rightHeight = $(this).next(".rightFace").height();
			if (leftHeight>rightHeight) $(this).next(".rightFace").height(leftHeight);
			else if (leftHeight<rightHeight) $(this).height(rightHeight);
		}
	});
	*/
	setContentHeight();
}
