//////////////////////////////////////
// CAROUSEL
//////////////////////////////////////
$(function(){
    $('div.banners:first').carousel();
}); 

// homepage banner navigation
$.fn.carousel = function () {

    var buttons   = $('ul li a', this),
		slides  = $('div.slide img', this),
		texts   = $('div.slide strong', this),
        current = null,
        last    = null,
        total   = 0,
        index   = 0;

    slides.css('opacity', 0);

    $.each(buttons, function (i, el) {
        try {
            $(el).data('slide', $(slides.get(i)));
            total++;
        } catch (e) {
            return false;
        }
    });

    buttons.click(function (e) {
        var t = $(this)
            ;
        e.preventDefault();
        if (last != null) {
            last.data('slide').parent().parent().css('z-index', 1);
        }
        if (current != null) {
            current.data('slide').stop()
                .parent().parent().css('z-index', 50);
            current.removeClass('current');
            last = current;
        }
        if (t.data('slide')) {
            if (current) {
                t.data('slide').stop()
                    .css('opacity', 0);
            } else {
                t.data('slide').stop();
            }
            t.data('slide')
                .animate({opacity: 1})
                .parent().parent().css('z-index', 200);
            current = t.addClass('current');
        }
    });

    next = function () {
        $(buttons.get(index++ % total)).click();
        setTimeout(next, 3000);
    }

    next();

    return this;
    
}
