$(document).ready( function() {
    if($.browser.msie && ($.browser.version == 7.0 || $.browser.version == 6.0)) {
	$.each($('#nav ul'), function () {
	    $(this).css('margin-left', (-$($(this).parents()[0]).width() + 5));
	});
	var total = 0;
	$.each($('#nav > li'), function () {
	    total += $(this).width() + 10;
	});
	$('#nav').width(total);

    }
    // onclick: remove default value, set color to black, remove this callback
    $('.call-field').focus( function() {
	$(this).val("").css("color", "#000").unbind();
    });


    if ($('#phonescan').length > 0) {
	setProgress();
	if ($('#selectVast').val() == "Nee")
	    $('#vastnet').hide();
	if ($('#selectMobiel').val() == "Nee")
	    $("#mobiel").hide();
	$("#phonescan input").blur(function () {
	    if ($(this).val() == "") $(this).val("0");
	    setProgress();
	});
	$('#selectVast').change(function () {
	    $("#vastnet").slideToggle(1000);
	    // Hiding takes slightly longer than 1000ms
	    setTimeout("setProgress()", 1100);
	});
	$('#selectMobiel').change(function () {
	    $("#mobiel").slideToggle(1000);
	    // Hiding takes slightly longer than 1000ms
	    setTimeout("setProgress()", 1100);
	});
	$('#selectGateway').change(function () {
	    setProgress();
	});

	$('#submit input').click(function () {
	    if ($('#selectVast').val() == "Nee" &&
		$('#selectMobiel').val() == "Nee" &&
		$('#selectGateway').val() == "Selecteer") {
		alert('Indien Vastnet telefonie en mobiele telefonie niet meegnomen worden moet u aangeven of u een SMS Gateway gebruikt.');
		return false;
	    } else 
		return true;
	});
    }

    if ($('#main.collapsable').length > 0) {
	$('#main.collapsable h2').each(function () {
	    var next = $(this).next();
	    var elements = [];
	    while (next.length > 0 && next[0].tagName != "H2") {
		last = next;
		elements.push(last);
		next = next.next();
		last.remove();
	    }
	    // Border toevoegen voorkomt verspringen
	    var newDiv = $('<div class="border">').insertAfter(this);
	    $(newDiv).hide();
	    elements = $(elements);
	    elements.each(function () {
		$(this).appendTo(newDiv);
	    });
	    $(this).click(function () {
		var h = $(this);
		if ($(this).next().is(':hidden')) {
		    $('#main div').hide();
		    $('#main h2').each(function () {
			var h = $(this);
			h.html("+" + h.html().substr(1, h.html().length));
		    });
		    h.next().slideToggle(500);
		    h.html("-" + h.html().substr(1, h.html().length));
		} else {
		    $(this).next().slideToggle();
		    h.html("+" + h.html().substr(1,h.html().length));
		}
	    });
	    $(this).prepend("+ ");
	});
    }

    if ($('#contactform').length) {
	$('#contactform').validate();
    }

    $('#call-button').click(function () {
	var correct = true;
	$.each($('.call-field'), function () {
	    var val = $(this).val();
	    if (correct && (val == "" || val == "Naam" ||
			    val == "Telefoonnummer")) {
		correct = false;
		$(this).focus();
	    }
	});
	if (!correct)
	    alert("U dient alle velden in te vullen!");
	return correct;
    });
});

function setProgress() {
    // Adjust for the submit input
    var ready = -1;
    var total = -1;

    $("#phonescan input:visible").each(function() {
	total++;
	if ($(this).val() != "")
	    ready++;
    });
    var gatewayVal = $("#selectGateway").val();
    if (gatewayVal == "Ja" || gatewayVal == "Nee") {
	ready++;
	total++;
    }
    
    var factor = 0.95
    var value = ready / total;
    if (total == 0) value = 0;
    $("#balk").width(value * $("#voortgang").width() * factor);
    $("#voortgang p").html((value * factor * 100).toFixed(0) + "%");
}

