$(document).ready(function() {
  // default textbox values
  var defZoeken = INPUT_Search;
  var defUsername = INPUT_Username;
  var defPassword = INPUT_Password;
  var defMeerNieuws = INPUT_MoreNews;

  // menu mouseovers
  $("#menu li").mouseover(function() { $(this).addClass("menuitem-o"); });
  $("#menu li").mouseout(function() { $(this).removeClass("menuitem-o"); });

  // submenu mouseovers
  $("a").not(".ullink").mouseover(function() { $(this).addClass("li-o"); });
  $("a").mouseout(function() { $(this).removeClass("li-o"); });

  // search
  $("#header .search INPUT").focus(function() { if ($(this).val() == defZoeken) $(this).val(""); });
  $("#header .search INPUT").blur(function() { if ($(this).val() == "") $(this).val(defZoeken); });

  // login - username
  $("#login #username").focus(function() { if ($(this).val() == defUsername) $(this).val(""); });
  $("#login #username").blur(function() { if ($(this).val() == "") $(this).val(defUsername); });

  // login - password
  $("#login #password").focus(function() { if ($(this).val() == defPassword) $(this).val(""); });
  $("#login #password").blur(function() { if ($(this).val() == "") $(this).val(defPassword); });

  // login - expand
  $("#login-container-collapsed").click(function() {
    $(this).css("display", "none");
    $("#login").removeClass("green");
    $("#login-container-expanded").css("display", "block");
    $("#menu").removeClass("fullwidth");
  });

  // login - submit
  $("#frmLogin-submit").click(function() { submitLogin(); });

  // homepage - spotlight
  $(".spotlight LI").mouseover(function() { $(this).addClass("hover"); });
  $(".spotlight LI").mouseout(function() { $(this).removeClass("hover"); });

  // homepage - nieuws
  $("#nieuws LI").mouseover(function() { $(this).addClass("hover"); });
  $("#nieuws LI").mouseout(function() { $(this).removeClass("hover"); });

  // meer nieuws
  //	$("#nieuws .search INPUT").focus(function() { if ($(this).val() == defMeerNieuws) $(this).val(""); });
  //	$("#nieuws .search INPUT").blur(function() { if ($(this).val() == "") $(this).val(defMeerNieuws); });

  // lightweight UL
  $("UL.lightweight LI").mouseover(function() { $(this).addClass("news-li-o"); });
  $("UL.lightweight LI").mouseout(function() { $(this).removeClass("news-li-o"); });

  // contact
  $("#frmContact-submit").click(function() { submitContact(); });

  // usertext - images
  $(".usertext INPUT[type='image']").each(function() {
    $(this).css("width", this.attributes["width"].value + "px");
    $(this).css("height", this.attributes["height"].value + "px");
  });

  // links naar flash video's in een player zetten
  $('a[href$=".flv"]').click(function() {
    //
    var pathInUploads = this.href.replace(UPLOADS_URL, ""),
        urlToOpen = PROJECT_URL + "videoplayer/?path=" + encodeURIComponent(pathInUploads) + "&d=mmy",
        windowSettings = "location=0,scrollbars=0,status=0,resizable=0,directories=0,menubar=0,width=540,height=380";

    window.open(urlToOpen, "Video", windowSettings);
    return false;
  });
});

function submitLogin() {
	if (submit_onclick('frmLogin', false)) {
		toggleLoginStatus("busy");
		$.ajax({
			type: "POST",
			url: PATHLANG + "xml/FormLogin",
			data: $("#frmLogin").serialize(),
			success: function(data, status) {
				if (data == "TRUE") {
					toggleLoginStatus("success");
					setTimeout(function() { $(location).attr("href", PATHLANG); }, 1000);
				}
				else
					toggleLoginStatus("error");
			},
			error: function() {
				toggleLoginStatus("error");
			}
		});
		return true;
	} else {
		return false;
	}
}

function toggleLoginStatus(status) {
	$("#frmLogin").css("display", status == "submit" || status == "sending" ? "block" : "none");
	$("#frmLogin-submit").css("display", status == "submit" ? "block" : "none");
	$("#frmLogin-sending").css("display", status == "sending" ? "block" : "none");
	
	$("#frmLogin-success").css("display", status == "success" ? "block" : "none");
	$("#frmLogin-error").css("display", status == "error" ? "block" : "none");
}

function submitContact() {
	if (submit_onclick('frmContact', false)) {
		toggleContactStatus("sending");
		var dataString = $("#frmContact").serialize();
		$.ajax({
			type: "POST",
			url: PATHLANG + "xml/FormContact",
			data: dataString,
			success: function(data, status) {
				if (data == "TRUE") {
					toggleContactStatus("success");
				}
				else
					toggleContactStatus("error");
			},
			error: function() {
				toggleContactStatus("error");
			}
		});
		return true;
	} else {
		return false;
	}
}

function toggleContactStatus(status) {
	$("#frmContact-submit").css("display", status == "submit" ? "block" : "none");
	$("#frmContact-sending").css("display", status == "sending" ? "block" : "none");
	$("#frmContact-success").css("display", status == "success" ? "block" : "none");
	$("#frmContact-error").css("display", status == "error" ? "block" : "none");
}