var Global = {

	init: function(){
        //Global.initLightBox();
            
            if( $('.search, .search-inline').length != 0 ){ 
                Global.initSearch();
            }
            
            if( $('#main table').length != 0 ) {
                $("#main table tr:nth-child(odd)").not('#main .announcements table tr, #content .bestuur tr').addClass("odd");
            }
        },

        initNewWindowLinks: function(){
            $(function(){
                $('a.twitter-link').click(function(){
                    window.open(this.href);
                    return false;
                });
            });
        
        }, initSearch: function(){
        
             $('form .submit').attr("value","");
			$(".search").attr("value","Zoeken");
			
            $(".search").focus(function(){
            	if($(".search").attr("value") == "Zoeken"){
            		$(".search").attr("value","");
            	}
            });
            $(".search").focusout(function(){
            	if($(".search").attr("value") == ""){
            		$(".search").attr("value","Zoeken");
            	}
            });
        }
    } //end of global

$(document).ready(function() { 
    Global.init();
	setNav();
	$("#faq").accordion({ autoHeight: false });
	blurAnchors();
	
	$('#nieuwsbrief').ajaxForm({  
	  	target:"#hiddenDIV", 
	  	beforeSubmit:function(){
	  		$("#submitbtn").hide();
	     	$("#loading").show();
	  	},
	   	success:function(rtn){ 
	  		$("#submitbtn").show();
	     	$("#loading").hide();
	   	
	   		$("#freeform input").removeClass("error");
	   		$("#freeform select").removeClass("error");
	   		$("#freeform textarea").removeClass("error");
	   		
	    	if(rtn=="success"){
	      		$("#gegevenswijzigen").resetForm();
	      		$("#freeform").html("<p id='successmsg'>Uw inschrijving is succesvol verstuurd en wordt in behandeling genomen.</p>");
	     	}else{
	     		var errors = Array();
	        	$("#errormsg").html( "E&eacute;n of meer velden zijn niet correct ingevuld." );
	        	$("#hiddenDIV #content ul li").each(function(){
	        		$("#"+$(this).html().toLowerCase()).addClass("error");
	        	});
	      		$("#hiddenDIV").empty();
	     		$("#errormsg").show();
	      		goToByScroll("errormsg");
	     	}
	   	} 
	 });
	
	$('#gegevenswijzigen').ajaxForm({  
	  	target:"#hiddenDIV", 
	  	beforeSubmit:function(){
	  		$("#submitbtn").hide();
	     	$("#loading").show();
	  	},
	   	success:function(rtn){ 
	  		$("#submitbtn").show();
	     	$("#loading").hide();
	   	
	   		$("#freeform input").removeClass("error");
	   		$("#freeform select").removeClass("error");
	   		$("#freeform textarea").removeClass("error");
	   		
	    	if(rtn=="success"){
	      		$("#gegevenswijzigen").resetForm();
	      		$("#freeform").html("<p id='successmsg'>Uw wijziging is succesvol verstuurd en wordt in behandeling genomen.</p>");
	     	}else{
	     		var errors = Array();
	        	$("#errormsg").html( "E&eacute;n of meer velden zijn niet correct ingevuld." );
	        	$("#hiddenDIV #content ul li").each(function(){
	        		$("#"+$(this).html().toLowerCase()).addClass("error");
	        	});
	      		$("#hiddenDIV").empty();
	     		$("#errormsg").show();
	      		goToByScroll("errormsg");
	     	}
	   	} 
	 });
	 
	 $('#vertelhetons').ajaxForm({  
	  	target:"#hiddenDIV", 
	  	beforeSubmit:function(){
	  		$("#submitbtn").hide();
	     	$("#loading").show();
	  	},
	   	success:function(rtn){ 
	  		$("#submitbtn").show();
	     	$("#loading").hide();
	   	
	   		$("#freeform input").removeClass("error");
	   		$("#freeform select").removeClass("error");
	   		$("#freeform textarea").removeClass("error");
	   		
	    	if(rtn=="success"){
	      		$("#vertelhetons").resetForm();
	      		$("#freeform").html("<p id='successmsg'>Uw bericht is succesvol verstuurd en wordt in behandeling genomen.</p>");
	     	}else{
	     		var errors = Array();
	        	$("#errormsg").html( "E&eacute;n of meer velden zijn niet correct ingevuld." );
	        	$("#hiddenDIV #content ul li").each(function(){
	        		$("#"+$(this).html().toLowerCase()).addClass("error");
	        	});
	      		$("#hiddenDIV").empty();
	     		$("#errormsg").show();
	      		goToByScroll("errormsg");
	     	}
	   	} 
	 });
});


function goToByScroll(id){
	$('html,body').animate({scrollTop: $("#"+id).offset().top},'fast');
}

function setNav(){
	var trigger=$('#submenu > ul > li');
	if(trigger.length!=0){
		$(trigger).each(
			function(i){
				$(this).mouseenter(function(){
					if($(this).hasClass("parent")){
					var next=$(this).children('ul');
						next.css('height','');
						next.stop(true,false).slideDown('fast');
						$(this).toggleClass("down");
					}
				}).mouseleave(function(){
					if(!$(this).hasClass('active') && !$(this).hasClass('parent_active')  && $(this).hasClass('parent')){
						$(this).find('ul').stop(true,false).delay(20).slideUp('fast',function(){$(this).removeAttr("style");$(this).css("display","none");});
						$(this).toggleClass("down");
					}
				});
			}
		);
	}
	
	
	$('li').has('ul').addClass("parent");
}

function blurAnchors(){
	if(document.getElementsByTagName) {
		var a = document.getElementsByTagName("a");
		//collect all anchors A
		for(var i = 0; i < a.length; i++){
			// mouse onfocus, blur anchors
			a[i].onfocus = function(){this.blur();};
		}
		var a = document.getElementsByTagName("input");
		//collect all anchors A
		for(var i = 0; i < a.length; i++){
			// mouse onfocus, blur anchors
			if(a[i].type == 'submit')
			a[i].onfocus = function(){this.blur();};
		}
	}
}

