// JavaScript Document
$(document).ready(function() {
    var images = new Array();
    images.push('uploads/header/1.jpg');
    images.push('uploads/header/2.jpg');
    images.push('uploads/header/3.jpg');
    images.push('uploads/header/4.jpg');
    images.push('uploads/header/5.jpg');
    images.push('uploads/header/6.jpg');
    images.push('uploads/header/7.jpg');
    var max = $(images).length;
    var currentFoto = 0;
    var intv;
    var target = $("#topimagecontainer");
    var setSwitch = false;
    var isSwitching = false;

    if (max > 0) {
        intv = setInterval(function(currentFoto, max) {
            LoadImage(currentFoto, max, true, false)
        }, 4000)
    }

    function LoadImage(index, max, isAutoSwitchCall, isInitCall) {

        clearInterval(intv);
        intv = setInterval(function(currentFoto, max) {
            LoadImage(currentFoto, max, true, false)
        }, 4000);

        if (isAutoSwitchCall) {
            index = currentFoto + 1;
            if (index == $(images).length) {
                index = 0;
            }
        }

        if (currentFoto != index || isInitCall == true) {
            currentFoto = index;
            $(target).css({ 'background': 'none' });

            if (setSwitch) {
                var img = $('#topimagecontainer #topgalleryimage1');
                var img2 = $('#topimagecontainer #topgalleryimage2');
                setSwitch = false;
            } else {
                var img = $('#topimagecontainer #topgalleryimage2');
                var img2 = $('#topimagecontainer #topgalleryimage1');
                setSwitch = true;
            }

            //$(target).fadeTo(1000, 0, PlaceImage(index));			

            $(img).load(function() {
                $(img2).fadeOut(1000, function() {
                    $(img).css({ 'z-index': '5' });
                    $(img2).css({ 'z-index': '0', 'display': 'block' });
                });
            }).error(function() {
                $(target).remove();
            }).attr('src', images[index]);
        }
    }

    //---- Einde gallery ----//

    $("#fotos a, #fotoslarge a, #detailpaginafotos a, a.fotobox").fancybox({
        'zoomSpeedIn': 300,
        'zoomSpeedOut': 300,
        'overlayShow': true,
        'overlayColor': '#000000',
        'padding': 2,
        'overlayOpacity': 0.8
    });

    //--- Jquery sliding menu ---//
    $('.subtop ul').hide();
    $('.subtop li a').click(
        function() {
            var checkElement = $(this).next();
            if((checkElement.is('ul')) && (checkElement.is(':visible'))) {
                return false;
            }
            if((checkElement.is('ul')) && (!checkElement.is(':visible'))) {
                $('.subtop ul:visible').slideUp('normal');
                checkElement.slideDown('normal');
                return false;
            }
        }
    );
});
