var currentId;
var open = "";

$(document).ready(function(){ 
	// fix transparent png's for IE6
	$(document).pngFix();
	$("ul.submenu").hide();
	
	if (open != "") {
		$("#submenu_" + open).toggle();
    	var active = $("#parent-" + open).find("ul.submenu");
    	var activeId = active.attr('id');
    	currentId = activeId;
	}
	$("a.parent").click(function(event){
		event.preventDefault();
	});
	$("a.external").click(function(event) {
		/*
		event.preventDefault();
		var url = $(this).attr("href");
		window.open(http://www.zap.be, '_blank');
		*/
	});
	//$("#inputSearch").autocomplete(suggestData,{matchContains:true});
	$("#btnSearch").click(function(event) {
		var searchString = ($("#inputSearch").attr("value"));
		window.location = baseURL + "/search/index/q/" + searchString + "/page/1";
	});
    $("tr.list").hover(
    	      function () {
    	    	  $(this).find("td").addClass("highlight");
    	      }, 
    	      function () {
    	    	  $(this).find("td").removeClass("highlight");
    	      }
    );
    $("a.main").hover(function(event){
    	var id = this.id;
    	var active = $("#parent-" + this.id).find("ul.submenu");
    	var activeId = active.attr('id')
    	if (currentId != activeId) {
        	$("ul.submenu").hide(200);
        	active.toggle(200);	
          	currentId = activeId;
    	}
    });    
    $("#showbg").hover(
    	function() {
    		$("#container").hide();
    		$("#footer").hide();
    	},
    	function() {
    		$("#container").show();
    		$("#footer").show();
    	}
    );
    $("#showbg").click(function(){
		$.ajax({
			   type: "POST",
			   url: "/ajax/change-bg/",
			   success: function(msg){
				//location.reload(true);
				var url = eval(msg);
				$('body').css('background-image', 'url("' + url + '")');
			   }
		 });
    });
    $("#nextbg").click(function(){
    	alert("show next bg");
    });
    $("#prevbg").click(function(){
    	alert("show prev bg");
    });
    $("a[rel='set']").colorbox({transition:"fade"});
	$('#form_guestbook_submit').click( function() {
		return validateForm('form_guestbook');
	});
	$('#form_guestbook_submit').submit( function() {
		return validateForm('form_guestbook');
	});
	$('#form_contact_submit').click( function() {
		return validateForm('form_contact');
	});
	$('#form_contact_submit').submit( function() {
		return validateForm('form_contact');
	});
	$('#form_ticket_user_submit').click( function() {
		return validateForm('form_ticket_user');
	});
	$('#form_ticket_user_submit').submit( function() {
		return validateForm('form_ticket_user');
	});
	$('#form_reservation_next').click(function(){
		$(this).attr("disabled", "true");
		var arrChecked = new Array();
		$("input[type='checkbox']:checked").each(function(){
			var id = $(this).attr('id');
			id = id.split('id_').join('');
			arrChecked.push(id);
		});
		if (arrChecked.length > 0) {
			$.ajax({
				   type: "POST",
				   url: "/ajax/save-order/",
				   data: "arrChecked=" + arrChecked,
				   success: function(msg){
				   window.location = "/reserveren/formulier";
				   }
			 });
		} else {
			alert("U heeft nog geen voorstelling gekozen.")
			$(this).attr("disabled", "");
		}
	});
});
function validateForm(form)
{
	
	var params = $("#" + form).serialize();
	params += "&form_id=" + form;
	params += "&id=" + 0;
	$("#" + form + "_submit").attr("disabled", true);

	var validate = true;
	if (validate) {
		$.ajax( {
			type : "POST",
			url : "/ajax/validate-form/",
			data : params,
			error : function(msg){
				alert("error");
			},
			success : function(msg) {
				jQuery("#" + form + "_submit").removeAttr("disabled");
				msg = eval(msg);
				
				var errors = msg[2];
				$("input[type=text],textarea").each( function() {
					if (this.id.indexOf(form) != -1) {
						if (errors[this.id]) {
							var error = errors[this.id];
							if (error == "isEmpty") {
								error = "dit veld mag niet leeg zijn";
							}
							if (error == "invalid") {
							 	error = "dit is geen geldige datum";
							}
							if (error == "emailAddressInvalid") {
								error = "dit is geen geldig email-adres";
							}
							if (error == "notInt") {
								error = "dit is geen geldig aantal";
							}
							$("#" + this.id + "-error").remove();
							$(this).parent().append("<span id='" + this.id + "-error' class='error'>" + error + "</span>");
						}
						//$(this).removeAttr("class");
						//$(this).attr("class", $(this).attr("type"));
					}	
				});
				if (msg[0]) {
					jQuery("#" + form + "_submit").show();
					for ( var i = 0; i < msg[1].length; i++) {
						var obj = $("#" + msg[1][i]);
						if (!obj.attr("id")) {
							$('input').each( function() {
								if (this.id.indexOf(msg[1][i])!=-1) {	
									obj = $("#" + this.id);
								}
							});	
						}
						if (i == 0) {
							obj.focus();
							obj.select();
						}
						var error = "error";
						var type = obj.attr("type");
						if (type == "text" || type == "password") {
							error += "_" + type;
						}
						if (obj.attr("type") == "checkbox") {
							obj.parent().attr("class", error);			
						}
						//obj.attr("class", error);
					}
				} else {
					window.location = msg[1];
				}
			}
		});
	}
	return false;
}
