jQuery.fn.ForceNumericOnly =
function()
{
    return this.each(function()
    {
        $(this).keydown(function(e)
        {
            var key = e.charCode || e.keyCode || 0;
            // allow backspace, tab, delete, arrows, numbers and keypad numbers ONLY
            return (
                key == 8 || 
                key == 9 ||
                key == 46 ||
                (key >= 37 && key <= 40) ||
                (key >= 48 && key <= 57) ||
                (key >= 96 && key <= 105));
        })
    })
};

$(document).ready(function(){
	//URL
	function getHost() {
		var a = document.URL.split("//");
		a = (a[1] ? a[1] : a[0]).split("/"); 
		//alert(a.join("\n"));
		currentPage = a[1];
		siteurl = a[0];
		if(siteurl == 'officialdesk.nl') siteurl = 'www.officialdesk.nl';
		//alert('siteurl='+siteurl);
	}
	getHost();
	
	
	$("#amount").ForceNumericOnly();
	
	//NAVIGATION
	if($("#menu li div a").hasClass('active')) {
		$("#menu li div a.active").css("color","white");
		$("#menu li div a.active").parent().addClass('activeItem');
		$("#menu li div a.active").parent().parent().addClass('active');
	};

	$("#menu li").mouseover(function() {
		if(!$(this).children("div").children("a").hasClass('active')) {
			$(this).children("div").children("a").css("color","white");
			$(this).children("div").addClass('activeItemRoll');
		}
	});
	
	$("#menu li").mouseout(function () {
		if(!$(this).children("div").children("a").hasClass('active')) {
			$(this).children("div").children("a").css("color","#549fd4");
			$(this).children("div").removeClass('activeItemRoll');
		}
	});
	
	if($("#submenu li div a").hasClass('active')) {
		$("#submenu li div a.active").parent().addClass('activeItem');
		$("#submenu li div a.active").parent().parent().addClass('active');
	};
	
	$("#submenu li").mouseover(function() {
		if(!$(this).children("div").children("a").hasClass('active')) {
			$(this).children("div").addClass('activeItemRoll');
		}
	});
	
	$("#submenu li").mouseout(function () {
		if(!$(this).children("div").children("a").hasClass('active')) {
			$(this).children("div").removeClass('activeItemRoll');
		}
	});
	
	$("#formdata").hide();
	$("#opsom p").hide();
	
	if(currentPage == 'nieuws') {
		$(".sortableblocks div div.dropdownlink").mouseover(function() {
			if(!$(this).hasClass('active')) $(this).children("h1").css("color","#303030");
		});
	
		$(".sortableblocks div div.dropdownlink").mouseout(function() {
			if(!$(this).hasClass('active')) $(this).children("h1").css("color","#505050");
		});
		
		$(".sortableblocks div").click(function() {
			$(this).children("p").slideToggle("slow")
			.parent().siblings().children("p").slideUp("slow");
			$(this).toggleClass("active")
			$(this).siblings("div").removeClass("active");
			$(this).siblings("div").children("h1").css("color","#505050");
		});
		
		$(".sortableblocks").each(function(){ 
			first = $(this).find('div.dropdownlink:first');
			first.children("p").show();
			first.toggleClass("active");
		});
	}
																	 
	telusps=0;
	$('.uspcontainer').each(function(){
		if(telusps==1) {
			$(this).css('margin-left', '16px');
		}
		telusps++;
	});
	
	$(".uspcontainer div p").hide();
	$(".uspHolder").mouseover(function() {
		if(!$(this).hasClass('active')) $(this).children("div").css("color","#026db9");
	});
	
	$(".uspHolder").mouseout(function() {
		if(!$(this).hasClass('active')) $(this).children("div").css("color","#549fd4");
	});
	
	$('.uspHolder p').each(function(){
		$(this).show();
		$(this).attr('box-h',$(this).height());
		$(this).css('height','12px');					   
	});
	
	$('.uspHolder').click(function(){
		$(this).children('p').animate({"height": $(this).children('p').attr('box-h')+'px'});
		$(this).children('.usp').addClass('current');		
		
		if($(this).children('p').height()==$(this).children('p').attr('box-h')){
			$(this).children('.usp').removeClass('current');
		}

		$('.uspHolder').each(function(){
			if(!$(this).children('.usp').hasClass('current')){
				$(this).children('p').animate({"height": "12px"});
			}
			$(this).children('.usp').removeClass('current');
		});
	});
	
	//KOSTEN MODULE
	CheckCheckboxes();
	
	$(".CheckBoxClass").change(function(){
		if($(this).is(":checked")) {
			$(this).next("label").addClass("LabelSelected");
			$(".CheckBoxLabelClass").css("color","#549FD4");
		}
		else {
			$(this).next("label").removeClass("LabelSelected");
		}
	});
	
	$(".CheckBoxLabelClass").mouseover(function(){
		if(!$(this).hasClass('LabelSelected')) $(this).css("color","#000000");
	});
	
	$(".CheckBoxLabelClass").mouseout(function(){
		if(!$(this).hasClass('LabelSelected')) $(this).css("color","#549FD4");
	});
	
	$("form#orderdata").validate({
		onblur: function(el) {
		if(validator.check(el))
			$(el).removeClass(validator.settings.errorClass);
		else
			$(el).addClass(validator.settings.errorClass);
		},
		
		onkeyup: function(el) {
		if(validator.check(el))
			$(el).removeClass(validator.settings.errorClass);
		else
			$(el).addClass(validator.settings.errorClass);
		}
	});
	
	$("input#submit").mouseover(function(){
		$(this).attr('src','layouts/officialdesk/images/verzenden_on.png');
	});
	
	$("input#submit").mouseout(function(){
		$(this).attr('src','layouts/officialdesk/images/verzenden_off.png');
	});
	
	//CONTACT FORM
	
	$("form#contact").validate({
		onblur: function(el) {
		if(validator.check(el))
			$(el).removeClass(validator.settings.errorClass);
		else
			$(el).addClass(validator.settings.errorClass);
		},
		
		onkeyup: function(el) {
		if(validator.check(el))
			$(el).removeClass(validator.settings.errorClass);
		else
			$(el).addClass(validator.settings.errorClass);
		}
	});
	
	jQuery.extend(jQuery.validator.messages, {
		required: 'verplicht veld',
		remote: 'verplicht veld'
	});
	
	$("a.showimage").fancybox({
		'hideOnContentClick' : true,
		'titlePosition'	: 'inside',
		'speedIn'		:	600,
		'speedOut'		:	150
	});
	
	
	$(".faqblock").each(function() {
		$(this).hide();
	});
	
	$('.faqcontainer h2').click(function() {
		$(this).parent().children('#faqblock').children('div').children('.faqblock').slideToggle("slow");
		$(this).parent().siblings().children('#faqblock').children('div').children('.faqblock').slideUp("slow");
		$(this).parent().siblings().removeClass("active");
		
		$(this).parent().toggleClass("active");
	});
	
	$('.answer').hide();
	
	/*$('.faqblock').click(function() {
		$(this).children('.answer').slideToggle("slow");
	});*/
	
	
	var volgarray = [877,904,928];
	
	$("#footer-right a").each(function() {
		var id = $(this).attr("id");
		var margin_x = volgarray[id];
		$(this).hover(
			function () {
				$(".volgicon").css("margin-left", margin_x+"px");
				$(".volgicon").css("display","block");
			},
			function () {
				$(".volgicon").css("display","none");
			}
		);
	});
});

function CheckCheckboxes(){
	if(document.modulecalc!=undefined){
		var elLength = document.modulecalc.elements.length;
	
		for(i=0; i<elLength; i++){
			var type = document.modulecalc.elements[i].type;
			if (type=="checkbox" && document.modulecalc.elements[i].checked){
				$(document.modulecalc.elements[i]).next("label").addClass("LabelSelected");
			}
		}
	}
}

function updateprice() {
	rel = $('#modulecalc').serialize();
	$.ajax({
		type: "get",
		url: siteurl+"/investering",
		data: rel,
		success: function(data){
			data = data.split('|');
			if(data.length<2){
				$('#amount').val('2');
				updateprice();
			}
			else {
				$('#total').html(data[0]);
				$('#totalpg').html(data[2]);
				$('#korting').html(data[1]);
			}
		}
	});
}

function dataOrderForm() {
	$("#formcalc").fadeOut(300, function() {
		var VALUE = '';
		$(".CheckBoxClass").each(function(){
			if($(this).is(":checked")){
				VALUE += $(this).attr('id')+',';
			}
		});
		
		$('#modules').val(VALUE);
		
		$discount = $('#korting i').html().split(" ");
		$price = $('#total').html().split(" ");
		
		$('#odprice').val($("#amount").attr('value')+'|'+$discount[2]+'|'+$price[1]);
		$("#formdata").fadeIn(100);
	});
};

function costOrderForm() {
	$("#formdata").fadeOut(300, function() {
		$("#formcalc").fadeIn(100);
	});
};
