$(window).load(function() {
    $('#slider').nivoSlider({
    	effect:'fade',
    	directionNav:false,
    	controlNav:false,
    	pauseTime:10000
    });
    
    jQuery('#album-slider').tinycarousel({ display: 1, duration: 300, rewind:true });
    jQuery('#vov-slider').tinycarousel({ display: 1, duration: 300, rewind:true });
    jQuery('#thumb-slider').tinycarousel({ display: 13, duration: 300, rewind:false });
    jQuery(".onze-vrienden").tinycarousel({display: 3 ,interval: true, intervaltime: 5000  });
    
    var maxheight = 0;
    $(".sidebar-vov").each(function(){
    	if($(this).height() > maxheight){
    		maxheight = $(this).height();
    	}
    });
    if($(".sidebar-vov").length > 0){	
        $(".sidebar-inner").height(maxheight+100);
    }
    
    $("#thumb-slider .slide a").live("click",function(){
    	$("#thumb-slider .slide").removeClass("active");
   		$(this).parent().addClass("active");
    	
    	var thisEl = $("#thumb-slider .active");
    	
    	var thisIndex = $("li.thumb").index(thisEl);
    	
    	if(thisIndex > 0){
    		$("#large-prev").show();
    	}else{
    		$("#large-prev").hide();
    	}
    	
    	if((thisIndex + 1) == $("li.thumb").length){
			$("#large-next").hide();
		}
    	
   	    	source = $("img",this).attr("src");
    	
    	$(".large-gallery > img").animate({opacity: 0},300, function(){
    		var img = new Image();
    		
    		img.onload = function(){
    			$(".large-gallery > img").attr("src",source.split("__small").join("")).animate({opacity: 1},300);
    			
    		};
    		img.src = source.split("__small").join("");
    	});
		return false;
    });
    
    $("#large-next").live("click",function(){
    	
    	var thisEl = $("#thumb-slider .active");
    	
    	var thisIndex = $("li.thumb").index(thisEl);
		
		
		$("#large-prev").show();
		
		
		if((thisIndex + 2) == $("li.thumb").length){
			$("#large-next").hide();
		}
		
		
		var source = $("#thumb-slider li:eq("+(thisIndex+1)+")").children("a").children("img").attr("src");
		
    	
    	$("#thumb-slider .slide").removeClass("active");
    	
    	$("#thumb-slider li:eq("+(thisIndex+1)+")").addClass("active");
    	
   		    	
    	$(".large-gallery > img").animate({opacity: 0},300, function(){
    		var img = new Image();
    		
    		img.onload = function(){
    			$(".large-gallery > img").attr("src",source.split("__small").join("")).animate({opacity: 1},300);
    			
    		};
    		img.src = source.split("__small").join("");
    	});
		return false;
    });
    
    $("#large-prev").live("click",function(){
    	
    	var thisEl = $("#thumb-slider .active");
    	
    	var thisIndex = $("li.thumb").index(thisEl);
		
		
		$("#large-next").show();
		
		
		if(thisIndex == 1){
			$("#large-prev").hide();
		}
		
		
		var source = $("#thumb-slider li:eq("+(thisIndex-1)+")").children("a").children("img").attr("src");
		
    	
    	$("#thumb-slider .slide").removeClass("active");
    	
    	$("#thumb-slider li:eq("+(thisIndex-1)+")").addClass("active");
    	
   		    	
    	$(".large-gallery > img").animate({opacity: 0},300, function(){
    		var img = new Image();
    		
    		img.onload = function(){
    			$(".large-gallery > img").attr("src",source.split("__small").join("")).animate({opacity: 1},300);
    			
    		};
    		img.src = source.split("__small").join("");
    	});
		return false;
    });

    
    
	if($(".sidebar-inner").height()-100 > ($(".twocol .content-page").height())){
		$(".twocol .content-page").height(($(".sidebar-inner").height()-100));
	}
    var originalTop = $('.nivo-controlNav').css('left');
    $('.nivo-controlNav a').live('click', function(e){
    	
        var i = 0;
        var i_thumbAnimSpeed = 800;
        var topPos;
        var margin = 6;
        var holderHeight = ($('.nivo-controlNav').parent().css('width').replace("px","")*1);
        var picHeight = ($('.nivo-controlNav').css('width').replace("px","")*1);

        if($('.nivo-controlNav').css('width') == "auto") {
            var picHeight = document.getElementById('nivcnt').offsetWidth*1+margin;
        }
        var maxScroll = (holderHeight-picHeight);
        $('.nivo-controlNav a').each(function(el) {
            if(i == 1) {
                firstPos = $(this).position().left;
                topPos = $(this).position().left;
            }
            
            lastPos = $(this).position().left;
            i++;
            if($(this).hasClass("active")){
            	curRel = $(this).attr("rel");
            }
            $(this).removeClass("active");
        });
        $(this).addClass("active");
        var oldPos = $('.nivo-controlNav').position().left;
        var elPos = $(e.target).parent().position().left;
        if(oldPos+elPos == 0){
        	 var newTopPos = firstPos-elPos;
        }else if(oldPos+elPos == 216){
        	 var newTopPos = maxScroll+1;
        }else{
	        var newTopPos = firstPos-elPos; 
        }var goTop;
        if(newTopPos < 0 && i > 3) {
            if(newTopPos > maxScroll) {
            	if(oldPos+elPos == 0){
                	goTop = (topPos-(elPos));
                }else if(oldPos+elPos == 216){
                	goTop = (topPos-(elPos))+72;
                }
            } else {
                //goTop = maxScroll;
            }
        } else {
            goTop = originalTop;
        }
        
        if(goTop < maxScroll){
        	goTop = maxScroll;
}
        if(goTop) {
            $('.nivo-controlNav').animate({
                left: goTop
            }, i_thumbAnimSpeed, function() { });
        }
        $("#slider_"+(parseInt(curRel)+1)).hide();
       	$("#slider_"+(parseInt($(this).attr("rel"))+1)).show();

    });
    
    
    /*$("#gegevens-accord").accordion({
    	autoHeight:false,
    	collapsible: true 
    });*/
    
		$('#gegevens-accord .accord-header').live('click',function() {
			$(this).toggleClass("closed");
			$(this).next().slideToggle();
			return false;
		}).next();

    
    $("#btnAddContact").click(function(){
    	addContact();
    });
    
    
    
    $('#contactform').ajaxForm({  
		  	target:"#hiddenDIV", 
		  	beforeSubmit:function(){
		  		$("#submitbtn").hide();
		     	$("#loading").show();
		  	},
		   	success:function(rtn){ 
		  		$("#submitbtn").show();
		     	$("#loading").hide();
		   	
		   		$("#contactform input").removeClass("error");
		   		$("#contactform select").removeClass("error");
		   		$("#contactform textarea").removeClass("error");
		   		
		    	if(rtn=="success"){
		     		$("#errormsg").hide();
		      		$("#contactform").resetForm();
		      		$("#contactform").html("<p id='successmsg'>"+$("#success_txt").html()+"</p>");
		     	}else{
		     		var errors = Array();
		        	$("#errormsg").html( "Velden met een * zijn verplicht! Controleer of alle velden correct zijn ingevuld." );
		        	$("#hiddenDIV #content ul li").each(function(){
		        		
		        		if($(this).html() == "In order to submit this form, you must complete the captcha field."){
		        			$("#captcha").addClass("error");
		        		}else if($(this).html() == "The email you submitted is not valid"){
		        			$("#email").addClass("error");
		        		}else{
		        			$("#"+$(this).html().toLowerCase().split("&nbsp;")[1]).addClass("error");
		        		}
		        	});
		      		$("#hiddenDIV").empty();
		     		$("#errormsg").show();
		      		//goToByScroll("errormsg");
		     	}
		   	} 
		 });

    
});

function addContact(){

	max = $("#maxcontacts").val();	
	countClass = $(".contact").size();
	newCounter = "";
	if(countClass < max){
		newCounter = countClass + 1;
		$('#newContact').clone().appendTo('#contactsHolder').attr("style","").attr("id", newCounter + "_contact").addClass("contact");
		
		$("#" + newCounter + "_contact input").each(function(e){
			$(this).attr("name", "contact_" + newCounter + "_" + $(this).attr("name"));
		});
		
		$("#contact_amount").val(newCounter);
	}else{
		alert("Max. aantal contacts bereikt.");
	}
}
