$(document).ready(function() {
    $('span.btn input').mouseover(function() { $(this).parent().addClass('selected') })
    $('span.btn input').mouseout(function() { $(this).parent().removeClass('selected') })
    $('span.btn input').focus(function() { $(this).parent().addClass('active') })
    $('span.btn input').blur(function() { $(this).parent().removeClass('active') })
    $('.textfield').focus(function() { $(this).addClass('focus') })
    $('.textfield').blur(function() { $(this).removeClass('focus') })
    $('div.wrapper').each(function() { $(this).children('div.twocols:even').addClass('clear') })
    $('div.wrapper').each(function() { $(this).children('div.twocols:odd').addClass('last') })
    $('div.wrapper').each(function() { $(this).children('div.threecols:nth-child(3n-2)').addClass('clear') })
    $('div.wrapper').each(function() { $(this).children('div.threecols:nth-child(3n)').addClass('last') })
    $('div.wrapper').each(function() { $(this).children('div.fourcols:nth-child(4n-3)').addClass('clear') })
    $('div.wrapper').each(function() { $(this).children('div.fourcols:nth-child(4n)').addClass('last') })
    $('ul li a').each(function() {
        $(this).parent(':first-child').addClass('first');
        $(this).parent(':last-child').addClass('last');
        $(this).html('<span>' + $(this).html() + '</span>');
    })
    $('div.wrapper').each(function() { $(this).children('div.twocols').width(($(this).width() - 10) / 2) })
    $('div.wrapper').each(function() { $(this).children('div.threecols').width(($(this).width() - 20) / 3) })
    $('div.wrapper').each(function() { $(this).children('div.fourcols').width(($(this).width() - 30) / 4) })
    $('div.subnav a br').parent().addClass('tworules');
    $('.bghover').mouseover(function() { $(this).addClass('bggray') })
    $('.bghover').mouseout(function() { $(this).removeClass('bggray') })
    $('div.imgtxt div.img img').each(function() {
        var imgtxtImgWidth = $(this).width()
        var imgtxtParentDivWidth = $(this).closest('div.imgtxt').width()
        var imgtxtImgPercentage = (65 * imgtxtParentDivWidth) / 100
        if (imgtxtImgWidth > imgtxtImgPercentage) {
            $(this).closest('div.img').addClass('xl')
        }
    });

    $(".ms-WPBody").removeClass("ms-WPBody");

    $('#faq').find('dd').hide().end().find('dt').click(function() {
        var answer = $(this).next();
        if (answer.is(':visible')) {
            answer.slideUp();
        } else {
            answer.slideDown('fast');
        }
    });

    //To override default option values, include them in JSON format in the call to the plugin, like so:
    $('#gallery').galleryView({
        panel_width: 340,
        panel_height: 255,
        frame_width: 75,
        frame_height: 56,
        pause_on_hover: true
    });
})


var SlideStatus = new Array();
var Element = new Array();
$(document).ready(function() {
    $('.homenav li.trigger').each(function() {
        // default top setting
        $(this).children('ul').css({ 'top': '235px' });
        // remember height, top and key.
        // key is an indication of the element
        var height = $(this).children('ul').height() + 59
        var key = this.className
        var top = $(this).children('ul').position().top
        $(this).mouseover(function() {
            if (SlideStatus[key] == 'awaiting-hide') {
                // cancel waiting for a move down
                SlideStatus[key] = 'up'
            }
            // Not down? nevermind
            if (SlideStatus[key] != null) return
            // Focus button
            $(this).children('a.trigger').addClass('selected')
            // Set status
            SlideStatus[key] = 'sliding-up'
            // Start animation
            $(this).children('ul').animate({ "top": "-=" + height + "px" }, "fast", function() {
                // On End function: Set status
                SlideStatus[key] = 'up'
            })
        })
        $(this).mouseout(function() {
            // Remember element
            Element[key] = this
            // Set status
            SlideStatus[key] = 'awaiting-hide'
            // Start waiting
            setTimeout('Hide("' + key + '", ' + top + ')', 10)
        })
    })
})
function Hide(key, top) {
    // Waiting canceled?
    if (SlideStatus[key] != 'awaiting-hide') return
    // remembered element
    var This = Element[key]
    // set height
    var height = $(This).children('ul').height() + 20
    // already down? nevermind
    if (SlideStatus[key] == null) return
    // set status
    SlideStatus[key] = 'sliding-down'
    // start animation
    $(This).children('ul').stop().animate({ "top": "+=" + height + "px" }, "fast", function() {
        // On End function: Set status
        SlideStatus[key] = null
        // Recover to initial top
        $(This).children('ul').css({ 'top': top + 'px' });
        // Blur button
        $(This).children('a.trigger').removeClass('selected')
    })
   }

   // Form functions

   function CheckNumberFormatTextBox(sender, args) {
   	CheckField(sender, args, "^[0-9][0-9]*$", "Geen geldig nummer");
   }

   function CheckDecimalFormatTextBox(sender, args) {
   	CheckField(sender, args, "^[0-9]+([\.\,][0-9]{1,3})?$", "Geen geldig nummer");
   }

   function CheckDateFormatTextBox(sender, args) {
   	CheckField(sender, args, "[0-3][0-9]-(0|1)[0-9]-(19|20)[0-9]{2}", "Geen geldige datum");
   }

   function CheckEmailFormatTextBox(sender, args) {
   	CheckField(sender, args,
		"^[A-Za-z0-9_-]+([.][A-Za-z0-9_-]+)*[@][A-Za-z0-9-][A-Za-z0-9-]+([.-][A-Za-z0-9-]+)*[.]([A-Za-z]){2,6}$",
		"Geen geldig e-mailadres");
   }

   function CheckMaxCharacters(sender, args) {
   	var max = sender.MaxCharacters;
   	var length = args.Value.length;
   	var value = args.Value;

   	if (max == 0) {
   		args.isValid = true;
   		return;
   	}

   	if (args.Value.length > max) {
   		args.IsValid = false;
   		SetErrorMessage(sender, args.IsValid, "Tekst mag maximaal " + max + " tekens bevatten");
   		return;
   	}
   }

   function CheckField(sender, args, regex, errormessage) {
   	if (IsHidden(document.getElementById(sender.controltovalidate))) {
   		args.IsValid = true;
   		return
   	}

   	var error;

   	var arrMatch = new RegExp(regex).exec(args.Value);

   	if (args.valueOf = '') {
   		args.IsValid = false;
   	}
   	else if (!arrMatch) {
   		args.IsValid = false;
   		error = errormessage;
   	}
   	else {
   		args.IsValid = true;
   	}

   	if (args.isValid) {
   		CheckMaxCharacters(sender, args);
   	}

   	SetErrorMessage(sender, args.IsValid, error);
   }

   function SetErrorMessage(sender, isValid, error) {
   	control = document.getElementById(sender.ControlToValidate);

   	if (sender.errormessage == null || sender.errormessage.indexOf(": " + sender.FieldName) == -1) {
   		sender.errormessage = error + ": " + sender.FieldName;
   	}

   	if (isValid) {
   		control.className = control.className.replace(" error", "");
   	}
   	else {
   		if (control.className.indexOf(" error") == -1) {
   			control.className = control.className + " error";
   		}
   	}
   }

   function IsHidden(element) {
   	if (!element.style) return false
   	if (element.style.display == 'none') return true
   	if (!element.parentNode) return false
   	return IsHidden(element.parentNode)
   }

   function CheckRequired(sender, args) {
   	if (IsHidden(document.getElementById(sender.controltovalidate))) {
   		args.IsValid = true;
   		return
   	}

   	if (args.Value != '') {
   		args.IsValid = true;
   	}
   	else {
   		args.IsValid = false;
   	}

   	SetErrorMessage(sender, args.IsValid, "Veld is verplicht");
   	CheckMaxCharacters(sender, args);
   }

   function ClearTexFromControl(object) {

       if (object.value == "Uw e-mailadres") {
           object.value = "";
       }
       

   }
