﻿﻿$(document).ready(function () {
    if ($("#carrousel").length > 0) carrousel();

    //make entire woonlocatie block clickable
    $(".woonlocatie").click(function () {
        window.location = $(this).find("a").attr("href");
        return false;
    });

    if ($(".slideshow").length > 0) loadSlideShow();

    $(".subnav").prepend('<div class="subnavOpener"></div>').append('<div class="subnavCloser"></div>');

    //alternate rows on newslists
    $(".nieuwslist ol>li, table tr").each(function (i) {
        if (i % 2 == 0) {
            $(this).addClass("alt");
        };
    });

    if ($(".ontmoetBegeleiders").length > 0) ontmoetBegeleiders();

    if ($.browser.msie && $.browser.version == 6) {
        $(".plaatsen ul li").mouseover(function () {
            $(this).addClass("hover");
            $(".popin", this).show();
        });
        $(".plaatsen ul li").mouseleave(function () {
            $(this).removeClass("hover");
            $(".popin", this).hide();
        });
    }


    if ($.browser.msie && $.browser.version < 8) {
        $("<div class='clearfix'></div>").insertBefore(".leesmeerR");
    }


    $("a[rel]").overlay({
        closeOnClick: true,
        top: "center",
        mask: {
            color: '#000000',
            opacity: 0.8
        }
        
    });

    // Dynamic text resizing
	var size = readCookie('textSize');
	if (size != null) {
		setTextSize(size);
	};
	
	
	// Webwinkel
	$("select.nrProducts").each(function(){
	  $(this).change(function(){
	    if ($(this).val() == "more") {
	        var parent = $(this).parent();
	    	  $(this).replaceWith('<input class="nrProducts" type="text" name="quantity" />');
	    	  setTimeout( function() {
	    		  parent.children('.nrProducts').focus();
	    		} , 0); 
	    }
	  });
	});

});

var activePhoto = new Array();
var totalPhotos = new Array();

var activeStep;
var totalSteps;

var settings = {
    "timeout": 10000 // carrousel timeout
};

var interval = -1;
var wait = -1;
var timeRemaining = settings.timeout;

function carrousel() {
    totalSteps = $("#steps li").length - 1; // start counter = 0
    activeStep = -1;
    initCarrousel();

    interval = setInterval(function () {
        initCarrousel();
    }, settings.timeout);

    $("#steps li").each(function (i) {
        // force width for ie < 8
        $(this).width($(this).width());
        $(this).find("a").addClass("li" + i);
    });
    $("#steps a").click(function () {
        clearInterval(interval);
        activeStep = $(this).attr("class");
        activeStep = activeStep.substr(2) - 1;
        initCarrousel(true);
    });

}

function initCarrousel(clicked) {
    activeStep++;
    if (activeStep > totalSteps) activeStep = 0;

    // we know what the next step will be so let's do it!
    $("#steps li .counter").removeClass("activecounter");
    $($("#steps li").removeClass("active")[activeStep]).addClass("active").find(".counter").addClass("activecounter");


    // Fix for opacity problem in IE7 (and lower)
    if ($.browser.msie && $.browser.version < 8) {
    	$("#controlItems li").stop().hide();
    } else {
    	$("#controlItems li").stop().fadeOut();
    }
    
    $($("#controlItems li")[activeStep]).stop().fadeIn();

    $("#steps li .activecounter, #steps li .counter").css({
        "margin-top": 5
    });

    if (clicked === undefined || clicked === null || clicked === false) {
        $("#steps li .activecounter").stop().animate({
            marginTop: "35px"
        }, settings.timeout, 'linear');
    }

    
    // pause on mouseover, resume on mouseleave
    $("#steps li, #steps li.active, #controlItems .item").unbind();
    $("#steps li, #controlItems .item").mouseover(function () {
        $("#steps li .activecounter, #steps li .counter").stop();
        clearInterval(interval);
        
    });

    $("#steps li, #controlItems .item").mouseleave(function () {
        // calculate how much time has passed in the Interval
        var currentMargin = $("#steps li .activecounter").css("margin-top");
        var currentMargin = currentMargin.substring(0, currentMargin.length - 2) - 5;
        timeRemaining = settings.timeout - (currentMargin / (30 / settings.timeout));

        $("#steps li .counter, #steps li .activecounter").stop();

        $("#steps li .counter").css({
            "margin-top": 5
        });
        $("#steps li .activecounter").css({
            "margin-top": currentMargin + 5
        });

        // complete animation within remaining time of the Interval
        $("#steps li .activecounter").stop().animate({
            marginTop: "35px"
        }, timeRemaining, 'linear', function () {
            initCarrousel();

            interval = setInterval(function () {
                initCarrousel();
            }, settings.timeout);
        });
    });
    
}

function loadSlideShow() {

    $(".slideshow").each(function (i) {

        totalPhotos[i] = $("ul.fotoContainer li", this).length - 1;
        activePhoto[i] = -1;
        activePhoto[i]++;

        // set currentphoto & totalphoto number in overlays
        if ($(this).siblings("p.counter").length > 0) {
            $(this).siblings("p.counter").find(".fotoId").html(activePhoto[i] + 1);
            $(this).siblings("p.counter").find(".totalfotoId").html(totalPhotos[i] + 1)
        }

        $($("ul.fotoContainer", this).find("li").addClass("hidden")[activePhoto[i]]).removeClass("hidden");

        $(".previous", this).click(function () {
            activePhoto[i]--;
            if (activePhoto[i] < 0) activePhoto[i] = totalPhotos[i];
            $($(this).siblings("ul.fotoContainer").find("li").addClass("hidden")[activePhoto[i]]).removeClass("hidden");

            // decrease current photo number in overlays
            if ($(this).parent().siblings("p.counter").length > 0) {
                $(this).parent().siblings("p.counter").find(".fotoId").html(activePhoto[i] + 1);
            }
        });
        $(".next", this).click(function () {
            activePhoto[i]++;
            if (activePhoto[i] > totalPhotos[i]) activePhoto[i] = 0;
            $($(this).siblings("ul.fotoContainer").find("li").addClass("hidden")[activePhoto[i]]).removeClass("hidden");

            // increase current photo number in overlays
            if ($(this).parent().siblings("p.counter").length > 0) {
                $(this).parent().siblings("p.counter").find(".fotoId").html(activePhoto[i] + 1);
            }
        });
    });
}


// ontmoetBegeleiders

function ontmoetBegeleiders() {
    $(".dock-item").each(function (i) {
        $(this).attr("id", "dock-item" + i);
        $(".details").each(function (i) {
            $(this).attr("id", "details" + i);
        });
    });

    activeItemNumber = $(".dock-item.active").attr("id");
    activeItemNumber = activeItemNumber.substr(activeItemNumber.length - 1);
    $($(".detailsContainer .details")[activeItemNumber]).show();

    $(".dock-item").mouseover(function () {
        itemNumber = $(this).attr("id");
        itemNumber = itemNumber.substr(itemNumber.length - 1);


        $(".detailsContainer .details").hide();
        $($(".detailsContainer .details")[itemNumber]).show();
        	
        // IF statement added by kkrabben to prevent flickering
        if ($("img.medewerker", this).height() == 137) {		
	        $("img.medewerker").stop().css({
	            height: "137px",
	            width: "150px"
	        });
	
	        $("img.medewerker", this).animate({
	            height: "160px",
	            width: "176px"
	        }, 150);
        };

    });

    
    $(".dock").mouseleave(function () {
        $(".detailsContainer .details").hide();
        $($(".detailsContainer .details")[activeItemNumber]).show();
        $(".dock-item img.medewerker").stop().css({
            height: "137px",
            width: "150px"
        });
        $(".dock-item.active img.medewerker").css({
            height: "160px",
            width: "176px"
        });
    });
	
};

function setTextSize(textSize) {
	if (textSize == 'normal') {
		$('body').css('font-size', '0.75em');
		$('#toolbar *').removeClass('active');
		$('#toolbar .normal').addClass('active');
	}
	else if (textSize == 'big') {
		$('body').css('font-size', '0.9em');
		$('#toolbar *').removeClass('active');
		$('#toolbar .big').addClass('active');
	}
	else if (textSize == 'bigger') {
		$('body').css('font-size', '1em');
		$('#toolbar *').removeClass('active');
		$('#toolbar .bigger').addClass('active');
	}
	document.cookie = 'textSize=' + textSize;
};

/* Copyright: http://www.quirksmode.org/js/cookies.html */
function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
};
