$(document).ready(function()
{
    $(".tooltip").tooltip({
    	bodyHandler: function() {
    		return $($(this).attr("href")).html();
    	},
    	showURL: false
    });
    
    $('.hasSubmenu').mouseover(function() {
        $(this).children('ul').show();
    })
    
    $('.hasSubmenu').mouseout(function() {
        $(this).children('ul').hide();
    })
    
    $('.portfolioItem').mouseover(function() {
        var src = $(this).children('.imageColor').html();
        $(this).children('.portfolioImageBox').children('.portfolioImage').children('img').attr("src", src);
    });
    
    $('.portfolioItem').mouseout(function() {
        var src = $(this).children('.imageBw').html();
        $(this).children('.portfolioImageBox').children('.portfolioImage').children('img').attr("src", src);
    });
    
	$(".newsOther").click(function() {
        if($(this).hasClass("expanded")) {
            $('.newsContent').slideUp("medium");
        } else {
            $('.newsContent').slideUp("medium");
            $('.newsOther.expanded').toggleClass("expanded").toggleClass("collapsed");
            $(this).parent().find('.newsContent').slideToggle("medium");
        };
        $(this).toggleClass("expanded").toggleClass("collapsed");
        return false;
	});
    
    /* Begin voor slide blokken */
    $('#boxes > li').mouseover(function(){
        $(this).find('.boxClosed').animate({marginTop: "-94px" },{queue: false,duration:200});
    });
    
    $('#boxes > li').mouseout(function(){
        $(this).find('.boxClosed').animate({marginTop: "-6px" },{queue: false,duration:200});
    });
    
    $('.innerfade').innerfade({
		speed: 750,
		timeout: 5000,
		type: 'random'
	});
    
    $('#workshopForm').validate({
        rules: {
            voornaam: {
                notEqual: "Voornaam"
            },
            achternaam: {
                notEqual: "Achternaam"
            },
            email2: {
                notEqual: "E-mail"
            },
            telefoon: {
                notEqual: "Telefoonnummer"
            }
        }
    });
    
    $('#nieuwsbriefForm').validate({
        rules: {
            naam: {
                notEqual: "Naam"
            },
            email: {
                notEqual: "E-mail"
            }
        }
    });
    
    $('#overtuigdForm').validate({
        rules: {
            naam: {
                notEqual: "Naam"
            },
            telefoon: {
                notEqual: "Telefoonnummer"
            }
        }
    });
    
    $('.bottom').vAlign('bottom', 'padding');
    
    $('#contactForm').validate();
    
    $('#vacatureForm').validate();
    
    $('.pollForm').validate();
    
    $.validator.addMethod("notEqual", function(value, element, param) {
      return this.optional(element) || value != param;
    }, "Dit is een verplicht veld.");
    
    
    $('#slider').nivoSlider({
        directionNav: false,
        captionOpacity: 0.7,
        animSpeed: 200,
        pauseTime: 4000,
        effect: 'fade'
    });
    
    $('#portfolioItem').nivoSlider({
        directionNav: false,
        animSpeed: 200,
        pauseTime: 5000,
        effect: 'fade'
    });
    
    $("#headerCarousel").jcarousel({
        scroll: 1,
        auto: 7,
        wrap: 'both',
        animation: 1000,
        initCallback: mycarouselHeader_initCallback,
        itemVisibleInCallback: {
            onAfterAnimation: mycarousel_move,
            onBeforeAnimation: mycarousel_premove
        }
    });
    
    $(".slideCarousel").jcarousel({
        scroll: 1,
        auto: 0,
        wrap: 'last',
        initCallback: mycarousel_initCallback,
        itemVisibleInCallback: {
            onAfterAnimation: mycarousel_move
        }
    });
    
    $('.middle').vAlign('middle');
    
    $("#portfolioCarousel").jcarousel({
        scroll: 1,
        auto: 0,
        animation: 1000,
        easing: 'easeInOutBack',
        initCallback: mycarouselPortfolio_initCallback,
        itemVisibleInCallback: {
            onAfterAnimation: mycarouselPortfolio_move
        }
    });
    
    $(".clearField").focus(function(srcc)
    {
        if ($(this).val() == this.defaultValue)
        {
            $(this).val("");
            $(this).css('color', 'black');
        }
    });
    
    $(".clearField").blur(function()
    {
        if ($(this).val() == "")
        {
            $(this).val(this.defaultValue);
            $(this).css('color', '#c2c2c2');
        }
    });
    
    $(".clearField").blur();
    
    $('.html5video').simpleVideo();
});

function mycarouselHeader_initCallback(carousel) {
    $('.jcarousel-control a').bind('click', function() {
        $('.jcarousel-control a').removeClass('active');
        $(this).addClass('active');
        array = ($(this).attr('id')).split('_');
        id = array[1];
        carousel.scroll($.jcarousel.intval(id));
        return false;
    });
};

function mycarousel_initCallback(carousel) {
    $('.jcarousel-control a').bind('click', function() {
        $('.jcarousel-control a').removeClass('active');
        $(this).addClass('active');
        carousel.scroll($.jcarousel.intval($(this).text()));
        return false;
    });
    
    // Disable autoscrolling if the user clicks the prev or next button.
    carousel.buttonNext.bind('click', function() {
        carousel.startAuto(0);
    });

    carousel.buttonPrev.bind('click', function() {
        carousel.startAuto(0);
    });

    // Pause autoscrolling if the user moves with the cursor over the clip.
    carousel.clip.hover(function() {
        carousel.stopAuto();
    }, function() {
        carousel.startAuto();
    });
};

function mycarousel_premove(carousel, temp1, temp2, temp3)
{
    $('.subtext').hide();
}

function mycarousel_move(carousel, temp1, temp2, temp3)
{
    $('.jcarousel-control a').removeClass('active');
    $('#counter_'+temp2).addClass('active');
    $('.subtext').hide();
    $('#subtext_'+temp2).show();
}

function mycarouselPortfolio_initCallback(carousel) {
    $('#PortfolioPages > li > a').bind('click', function() {
        $('#PortfolioPages > li > a').removeClass('active');
        $(this).addClass('active');
        carousel.scroll($.jcarousel.intval($(this).text()));
        return false;
    });
    
    // Disable autoscrolling if the user clicks the prev or next button.
    carousel.buttonNext.bind('click', function() {
        carousel.startAuto(0);
    });

    carousel.buttonPrev.bind('click', function() {
        carousel.startAuto(0);
    });

    // Pause autoscrolling if the user moves with the cursor over the clip.
    carousel.clip.hover(function() {
        carousel.stopAuto();
    }, function() {
        carousel.startAuto();
    });
};

function mycarouselPortfolio_move(carousel, temp1, temp2, temp3)
{
    $('#PortfolioPages > li > a').removeClass('active');
    $('#counter_'+temp2).addClass('active');
}

(function ($) {
$.fn.vAlign = function($align, $type) 
{
	return this.each(function(i)
    {
    	var ah = $(this).height();
    	var ph = $(this).parent().height();
        
        switch ($align)
        {
            case 'top':
                var mh = 0;
            break;
            case 'middle':
                var mh = Math.ceil((ph-ah) / 2);
            break;
            case 'bottom':
                var mh = Math.ceil(ph-ah);
            break;
        }
        switch ($type)
        {
            case 'padding':
                $(this).css('padding-top', mh);
            break;
            case 'margin':
            default:
                $(this).css('margin-top', mh);
            break;
            
        }
	});
};
})(jQuery);
