/*
 * jQuery doTimeout: Like setTimeout, but better! - v1.0 - 3/3/2010
 * http://benalman.com/projects/jquery-dotimeout-plugin/
 * 
 * Copyright (c) 2010 "Cowboy" Ben Alman
 * Dual licensed under the MIT and GPL licenses.
 * http://benalman.com/about/license/
 */
(function($){var a={},c="doTimeout",d=Array.prototype.slice;$[c]=function(){return b.apply(window,[0].concat(d.call(arguments)))};$.fn[c]=function(){var f=d.call(arguments),e=b.apply(this,[c+f[0]].concat(f));return typeof f[0]==="number"||typeof f[1]==="number"?this:e};function b(l){var m=this,h,k={},g=l?$.fn:$,n=arguments,i=4,f=n[1],j=n[2],p=n[3];if(typeof f!=="string"){i--;f=l=0;j=n[1];p=n[2]}if(l){h=m.eq(0);h.data(l,k=h.data(l)||{})}else{if(f){k=a[f]||(a[f]={})}}k.id&&clearTimeout(k.id);delete k.id;function e(){if(l){h.removeData(l)}else{if(f){delete a[f]}}}function o(){k.id=setTimeout(function(){k.fn()},j)}if(p){k.fn=function(q){if(typeof p==="string"){p=g[p]}p.apply(m,d.call(n,i))===true&&!q?o():e()};o()}else{if(k.fn){j===undefined?e():k.fn(j===false);return true}else{e()}}}})(jQuery);

$.fn.searchFieldBehaviour = function (options) {
    var defaults = {
        speed: 250,
        findclass: '.fixvalue',
        closebtn: '.searchreset_standard'
    };
    var options = $.extend(defaults, options);
    $('.searchreset_standard').click(function () {
        var previnputs = $(this).siblings('input');
        previnputs.val($(this).siblings('.fixvalue').data("state").defaulttext);
        $(this).siblings('.fixvalue').data("state").touched = 'no';
        return false;
    });

    function clearField(element) {
        if ($(element).parents('.expand').size() > 0) {
            $(element).parents('.expand.filled').addClass('open');
        }
        $(element).removeClass('default');
        $(element).siblings(options.closebtn).css({
            opacity: 1
        });
        if ($(element).data("state").touched == 'no') {
            $(element).attr('value', '');
            $(element).data("state").touched = 'yes';
        }
    }

    function revertField(element) {
        if ($(element).attr('value') == '') {
            $(element).attr('value', $(element).data("state").defaulttext);
            $(element).addClass('default');
            $(element).data("state").touched = 'no';
        }
        $.doTimeout(150, function () {
            if ($(element).parents('.expand').size() > 0) {
                $(element).parents('.expand.open').removeClass('open');
            }
            $(element).siblings(options.closebtn).css({
                opacity: 0
            });
        });
    }
    $(options.findclass).each(function () {
        var value = $(this).attr('value');
        $(this).data("state", {
            touched: 'no',
            defaulttext: value
        });
        $(this).addClass('default');
        $(this).click(function () {
            clearField(this);
        }).focus(function () {
            clearField(this);
        }).blur(function () {
            revertField(this);
        });
    });
};

$.fn.checkForm = function () {
	
	var errors = 0;
				
	$('#subscrMail').find('.checkfield').each(function(){
		
		if ($(this).hasClass('email')){
			var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
			if (!filter.test($(this).val())){
				errors = 1;
			}
		}else{
			if ($(this).val() == ''){
				errors = 1;
			}
		}
	});

	if (errors == 1){
		alert('Alle velden verplicht.');
		return false;
	}else{
		return true;
	}	
}

$(document).ready(function(){
	$.fn.searchFieldBehaviour();
	 
	 var options = { 
        target:        '#mailFormContent',
        beforeSubmit: function(){
        	return $.fn.checkForm ();
        }
     }
	 
	 $('#subscrMail').submit(function() { 
        $(this).ajaxSubmit(options); 
        return false; 
    }); 
});
