var popupStatus = 0;

function loadPopup(popupname) {
	if (popupStatus==0) {
		$("#achtergrond").css({
			"opacity": "0.7"
		});
		$("#achtergrond").fadeIn("slow");
		$("#div"+popupname).fadeIn("slow");
		popupStatus = 1;
	}
}

function disablePopup() {
	if (popupStatus==1) {
		$("#achtergrond").fadeOut("slow");
		$(".provinciepopup").fadeOut("slow");
		popupStatus = 0;
	}
}

function centerPopup(popupname) {
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $("#div"+popupname).height();
	var popupWidth = $("#div"+popupname).width();
	$("#div"+popupname).css({
		"position": "absolute",
		"top": windowHeight/2-popupHeight/2 + $(window).scrollTop(),
		"left": windowWidth/2-popupWidth/2 + $(window).scrollLeft()
	});
	$("#achtergrond").css({
		"height": windowHeight
	});
}


$(document).ready(function() {
    $("input#q").autocomplete("/lookup.php", {
      formatItem: function(item) {
        return item.toString().split(";")[0];
      },
      extraParams: { auto:true },
      highlight: false,
      max: 30,
      width: 247
    }).result(function(event, item) {
        location.href = item.toString().split(";")[1];
    });
    $('input#q').simpleWaterMark('empty');
    $('input#search, input#verstuur').hover(
        function() {
            $(this).attr('src', $(this).attr('src').replace('button_', 'buttona_'));
        },
        function() {
            $(this).attr('src', $(this).attr('src').replace('buttona_', 'button_'));
        }
    );
            
    $('ul#faq li div').hide();
    $('ul#faq li q').click(function() {
        $(this).next('div').toggle('def');
        return false;
    }); 

	$("div#tagcloud a").click(function(){
		centerPopup($(this).attr("id"));
		loadPopup($(this).attr("id"));
		return false;
	});
				
	$(".provinciepopupx").click(function(){
		disablePopup();
	});
	$("#achtergrond").click(function(){
		disablePopup();
	});
	$(document).keypress(function(e){
		if(e.keyCode==27 && popupStatus==1){
			disablePopup();
		}
	});   
	
    $(window).resize(function(){
        if (popupStatus==1) {
            centerPopup();
        }
    });	    
    $(window).scroll(function(){
        if (popupStatus==1) {
            centerPopup();
        }
    });	    
	$('form.requireform').submit(function(){
		$('.required[value]',this).css('borderColor','inherit');
		$('.required[value=""]',this).css('borderColor','red');
		return !$('.required',this).is('[value=""]');
	});
});

