jQuery(function(){
    
    //instellingen aanpasbaar
    var automatisch             = 1; // 1=aan / 0=uit
    var beginnenbij                = 0;    //beginnen bij
    var hoelangstilstaan         = 4000; // 1000 = 1sec
    var schuifknopcss            = new Array("background-position","bottom","top"); // css, active, nonactive
    var commentaarinbeeld        = "2px"; // positie binnnen balk
    var commentaarbuitenbeeld    = "50px"; // positie buiten balk
    var resetpositie            = "0px"; // blok left positie bij reset/begin

    //instellingen niet aanpasbaar
    var totaal                     = jQuery(".schuifblok").size();
    var breed                     = jQuery(".schuifblok").width();
    var comheight                 = jQuery(".schuifblokcommentaar").height();
    var pauze                     = false;
    var huidig                     = beginnenbij;
    var timeoutcallback; 

    //start schuiven
    jQuery('.schuifblok:eq('+beginnenbij+')').css("left","0px");
    jQuery('.schuifblokcommentaar:eq('+beginnenbij+')').animate({"top":commentaarinbeeld});
    schuivenactiveknop(huidig);
    
    timeoutcallback = setTimeout(autoschuiven,hoelangstilstaan);

    function schuivenactiveknop($positie)
    {
        jQuery('.schuifnaarpositie').css(schuifknopcss[0],schuifknopcss[2]);
        jQuery('.schuifnaarpositie:eq('+$positie+')').css(schuifknopcss[0],schuifknopcss[1]);
    }
    
    function schuifcommentaar($huidig,$positie){
        jQuery('.schuifblokcommentaar:eq('+$huidig+')').animate({"top":commentaarbuitenbeeld},function(){
            jQuery('.schuifblokcommentaar:eq('+$huidig+')').css("top","-"+comheight+"px");    
        });
        jQuery('.schuifblokcommentaar:eq('+$positie+')').animate({"top":commentaarinbeeld});    

    }

    jQuery("#schuifblokcontainer").hover(function(){
        pauze = true;
        jQuery('#schuifblokpauze').fadeIn();
    },function(){
        pauze = false;   
        jQuery('#schuifblokpauze').fadeOut();  
    });
    
    function autoschuiven()
    {
        if(automatisch==1)
        {
            if(huidig < (totaal-1))
            {
                positie = huidig + 1;
                schuiven(positie);
            } 
            else if(huidig == (totaal-1))
            {
                doorgaannaeinde();
                setTimeout(function() { schuiven(positie); },hoelangstilstaan);
            }
        }
    }
    
    jQuery('.schuifnaarpositie').click(function()
    {
        positie = jQuery(".schuifnaarpositie").index(this); 
        schuiven(positie);
        return false;
    });
    
    function doorgaannaeinde(){
        clearTimeout(timeoutcallback);
        laatste = totaal-1;
        jQuery('.schuifblok:eq(0)').css("left",breed+"px");
        jQuery('.schuifblok:eq('+laatste+')').animate({"left":"-"+breed+"px"});
        jQuery('.schuifblok:eq(0)').animate({"left":resetpositie});
        huidig     = 0;    
        positie = 1;
        schuivenactiveknop(huidig);
        schuifcommentaar(laatste,huidig);
        timeoutcallback = setTimeout(autoschuiven,hoelangstilstaan);
    }

    function teruggaanbijbegin(){
        clearTimeout(timeoutcallback);
        laatste = totaal-1;
        jQuery('.schuifblok:eq('+laatste+')').css("left","-"+breed+"px");
        jQuery('.schuifblok:eq('+laatste+')').animate({"left":resetpositie});
        jQuery('.schuifblok:eq(0)').animate({"left":breed+"px"});
        huidig     = laatste;    
        positie = laatste;
        schuivenactiveknop(positie);
        schuifcommentaar(0,laatste);
        timeoutcallback = setTimeout(autoschuiven,hoelangstilstaan);
    }
    
    function schuiven(positie)
    {
        clearTimeout(timeoutcallback);
        if(!pauze)
        {
            if(positie==huidig){}
            else{
                if(positie>huidig)
                {
                    jQuery('.schuifblok').not('.schuifblok:eq('+huidig+')').css("left",breed+"px");
                    jQuery('.schuifblok:eq('+huidig+')').animate({"left":"-"+breed+"px"});
                    jQuery('.schuifblok:eq('+positie+')').animate({"left":resetpositie});
                    schuivenactiveknop(positie);
                    schuifcommentaar(huidig,positie);
                }
                else
                {
                    jQuery('.schuifblok').not('.schuifblok:eq('+huidig+')').css("left","-"+breed+"px");
                    jQuery('.schuifblok:eq('+huidig+')').animate({"left":breed+"px"});
                    jQuery('.schuifblok:eq('+positie+')').animate({"left":resetpositie});
                    schuivenactiveknop(positie);
                    schuifcommentaar(huidig,positie);
                }
                huidig = positie;

                timeoutcallback = setTimeout(autoschuiven,hoelangstilstaan);   
            }
        }
          
    }
});
