var numImages = 0;
var current = 1;
var currentImage;
var timer;

$(document).ready(function()
{
    numImages = $("div#home-presentatie img.presentatie-home-image").length;
    current = 1;

    $("div#home-presentatie img#navleft").bind("click", navLeft);
    $("div#home-presentatie img#navright").bind("click", navRight);
    $("div#home-presentatie div#submenu-merken").hoverIntent(merkenMouseover, merkenMouseout);
    $("div#home-presentatie div#submenu-merken img").bind("mouseover", merkenMouseover2);
    $("div#home-presentatie div#submenu-merken img").bind("mouseout", merkenMouseout2);

    timer = new Timer(5000, navRight);
    if(numImages > 1)
        timer.startTimer();

    function navLeft()
    {
        timer.stopTimer();

        var previousImage = currentImage;
        if(current == 1)
        {
            current = numImages;
        }
        else
        {
            current--;
        }
        currentImage = $("div#home-presentatie img#presentatie-home-image-" + current);

        previousImage.fadeOut(1000, function(){currentImage.fadeIn(1000)});

        //previousImage.css("display", "none");

        if(numImages > 1)
            timer.startTimer();
    }

    function navRight()
    {
        timer.stopTimer();

        var previousImage = currentImage;
        if(current >= numImages)
        {
            current = 1;
        }
        else
        {
            current++;
        }
        currentImage = $("div#home-presentatie img#presentatie-home-image-" + current);

        previousImage.fadeOut(1000, function(){currentImage.fadeIn(1000)});

        //previousImage.css("display", "none");

        if(numImages > 1)
            timer.startTimer();
    }

    function merkenMouseover()
    {
        $("div#home-presentatie img#navleft").fadeOut(1000);
        $("div#home-presentatie img#navright").fadeOut(1000);
    }

    function merkenMouseout()
    {
        $("div#home-presentatie img#navleft").fadeIn(1000);
        $("div#home-presentatie img#navright").fadeIn(1000);
    }

    function merkenMouseover2()
    {
        var id = $(this).attr("id");
        var previousImage = currentImage;
        currentImage = $("div#home-presentatie img#presentatie-home-" + id);

        if(currentImage.length > 0)
        {
            previousImage.fadeOut(1000, function(){currentImage.fadeIn(1000)});
            timer.stopTimer();
        }
    }

    function merkenMouseout2()
    {
        var previousImage = currentImage;
        currentImage = $("div#home-presentatie img#presentatie-home-image-1");

        if(previousImage.length > 0)
        {
            previousImage.fadeOut(1000, function(){currentImage.fadeIn(1000)});
            timer.startTimer();
        }
    }
    

    currentImage = $("div#home-presentatie img#presentatie-home-image-" + current);
    currentImage.fadeIn(2000);
    $("div#submenu-merken").fadeIn(2000);
});