var slideInterval;

function cufonBug() {
    /* weird Cufon bug caused by line breaks after some elements */ 
    $("cufon[alt=' ']").remove();
}

function slideSwitch(dir) {
    var dir = (typeof(dir)!='undefined') ? dir : '+';
    var active = $('#slideshow .active');

    /* Reset all slides and run Reset callbacks */
    $('#slideshow').children().each(function() {
        $(this).children().each(function(){
            $(this).queue("fx", []);
            $(this).stop();
        });

        if($(this).hasClass('active') && typeof $(this).attr('callback')=='string') {
            var el = $(this);
            var reset = function() {
                window[el.attr('callback') + 'Reset']();
            }
            setTimeout(reset,1000);
        }
    });

    if ( active.length == 0 ) {
        active = $('#slideshow .slide:last');
    }
    active.siblings().children().attr('style','');
    active.siblings().children('.button').css('opacity',0);

    var nxt;
    if(dir=="+") {
        nxt =  active.next().length ? active.next() : $('#slideshow .slide:first');
    } else {
        nxt =  active.prev().length ? active.prev() : $('#slideshow .slide:last');
    }

    $('.last-active').removeClass('last-active');
    active.addClass('last-active');

    nxt.css({opacity: 0})
        .addClass('active')
        /* Fade in the background */
        .animate({opacity: 1.0}, 1000, function() {
            active.removeClass('active start');
        })
        /* Slide the black bar up */
        .children('.blackBar').animate({bottom: 0}, 1000, function() {
                /* Slide the text right */
                cufonBug();
                $(this).next().animate({left: 0}, 1000, function() {
                        /* Fade in the button */
                        $(this).next().animate({opacity: 1.0}, 1000, function() {
                                if(typeof $(this).parent().attr('callback')!='string') {
                                    return;
                                }
                                clearInterval(slideInterval);
                                slideInterval = 'callback';
                                window[$(this).parent().attr('callback')]($(this).parent());
                            });
                        });
                    });
}

Cufon.replace('#slideshow h1');

$(function() {
    slideSwitch();
    slideInterval = setInterval( "slideSwitch()", 6000);

    $('#slideshow')
        .mouseenter(function() {
                clearInterval(slideInterval);
        })
        .mouseleave(function() {
                if(slideInterval != 'callback') {
                    slideInterval = setInterval( "slideSwitch()", 6000);
                }
        })

    $('<a href="#" id="slideshowPrev"></a>').appendTo('#header').click(function(){
        clearInterval(slideInterval);
        slideSwitch('-');
        return false;
    });
    $('<a href="#" id="slideshowNext"></a>').appendTo('#header').click(function(){
        clearInterval(slideInterval);
        slideSwitch('+');
        return false;
    });
});


/* Custom callbacks for each slide */

function redKap(el) {
    var el = $('span#brand');

    $.each(['Toyota', 'BMW', 'Ford', 'Audi', 'Honda'], function(index, value) {
        el.clone().css({position: 'absolute', opacity: 0}).text(value).appendTo(el.parent());
    });

    cufonBug();
    el.next().nextAll().each(function(index,value) {
        var next = $(this);
        Cufon.replace(next);

        el.animate({opacity: 1},400);
        el.animate({opacity: 0},300);
        el.animate({width: next.innerWidth()}, function() {
            el.text(next.text());
            Cufon.replace(el);
        });
        el.animate({opacity: 1},400);
    });

    el.next().nextAll().remove();

    el.queue(function(){
       slideInterval = setInterval("slideSwitch()", 6000);
    });
}
function redKapReset() {
    var el = $('span#brand');
    el.queue("fx", []);
    el.stop();
    el.attr('style', '');
    el.html('Lexus');
    Cufon.replace(el);
}
