$j(document).ready(function(){
    var $interval = setInterval("slideshow()", 5000);

    $j('#nextbutton').click(function(e){
        e.preventDefault();
        gotoSlide('next');
        clearInterval($interval);
        $interval = setInterval("slideshow()", 5000);
    });
    $j('#prevbutton').click(function(e){
        e.preventDefault();
        gotoSlide('prev');
        clearInterval($interval);
        $interval = setInterval("slideshow()", 5000);
    });

    var $dialog = $j('#dialog').dialog({
        autoOpen: false,
        closeText: 'x',
        width: 400,
        height: 200,
        open: function() {
            $j('#back-button').click(function(){
                $dialog.dialog('close');
            });
        }
    });

    $j('#button').click(function(e){
        e.preventDefault();
        var $url = $j(this).closest('form').attr('action');
        var $email = $j(this).siblings('#newsletter-email').val();

        $j.get($url, {email: $email}, function(data){
            if (data.result === true) {
                $dialog.dialog('open');
            }else{
               $dialog.dialog('open');
                if(data.message === 'incorrect'){         
                    $j('.dialogcontent').html('<p>Uw e-mail is niet correct</p>');
                }else{
                    $j('.dialogcontent').html('<p>Uw e-mail is al geregistreerd.</p>');
                }     
            }
        }, 'json');
    });
    
});

function gotoSlide(direction)
{
    var $active = $j('.active', '#diaslideshow');
    var $next;

    if (direction === 'next') {
        $next = $active.next('div', '#diaslideshow');
        if (!$next.length) {
            $next = $j('div:first', '#diaslideshow');
        }
    } else {
        $next = $active.prev('div', '#diaslideshow');
        if (!$next.length) {
            $next = $j('div:last', '#diaslideshow');
        }
    }
    $active.removeClass('active');
    $next.addClass('active');
}

function slideshow()
{
    gotoSlide('next');
}
