var slideshow_semaphore = true;
var slideshow_speed = 100;
var timeout_int;


$(document).ready(function(){
    $(".slide a").each(function(){
        var pattern = /url\(|\)|"|'/g;
        var image_url = $(this).css("background-image").replace(pattern, "");        
        pic = new Image();
        pic.src = image_url;
    });
    
    $("#next").click(function(e){
        e.preventDefault();
        if(timeout_int)
            clearTimeout(timeout_int);
        if(slideshow_semaphore){
            slideshow_semaphore = false;
            $(".slide:visible h1, .slide:visible h2, .slide:visible h3").fadeOut("fast");
            $(".slide:visible").hide('slide', {direction: 'left'}, slideshow_speed, function(){
                var next = null;                
                if($(this).next(".slide").length>0)
                    next = $(this).next(".slide");
                else
                    next = $(".slide:first");
                $(next).show('slide', {direction: 'right'}, slideshow_speed, function(){
                    slideshow_semaphore = true;
                    timeout_int = setTimeout(function(){$("#next").click()}, 4000);
                });
                $(next).find("h1").fadeIn();
                $(next).find("h2").fadeIn();
                $(next).find("h3").fadeIn();
            });
        }
    
    });

    $("#prev").click(function(e){
        e.preventDefault();
        if(timeout_int)
            clearTimeout(timeout_int);
        if(slideshow_semaphore){
            slideshow_semaphore = false;
            $(".slide:visible h1, .slide:visible h2, .slide:visible h3").fadeOut("fast");
            $(".slide:visible").hide('slide', {direction: 'right'}, slideshow_speed, function(){
                var prev = null;                
                if($(this).prev(".slide").length>0)
                    prev = $(this).prev(".slide");
                else
                    prev = $(".slide:last");
                $(prev).show('slide', {direction: 'left'}, slideshow_speed, function(){
                    slideshow_semaphore = true;
                    timeout_int = setTimeout(function(){$("#next").click()}, 4000);
                });
                $(prev).find("h1").fadeIn();
                $(prev).find("h2").fadeIn();
                $(prev).find("h3").fadeIn();
                
            });
        }
    });

    timeout_int = setTimeout(function(){$("#next").click()}, 4000);
});

