// simple carousel
var scroll_count = 0;
var this_scroll = 0;
var offset = 0;

function carousel(direction, scroll_count) {
        
    // get dims
    var c_width = $("#carouselContainer").width();
    var c_margin = $('#carousel_viewport').css('margin-left');
    var c_duration = 500;
    
    // disable the scrollbutton
    $('#btnCarousel_' + direction).unbind("click");
        
    if (this_scroll <= scroll_count && this_scroll > -1) {
        // sort out the offset and set the scroll location for this scroll
        if (direction == 'left') {
            if (this_scroll != 0) {
                offset = (parseInt(c_width, "10") + parseInt(c_margin, "10"));
                this_scroll = parseInt(this_scroll, "10") - 1;
            }
        }
        else {
            if (this_scroll != scroll_count) {
                offset = '-' + (parseInt(c_width, "10") - parseInt(c_margin, "10"));
                this_scroll = parseInt(this_scroll, "10") + 1;
            }
        }
    }
    
    $("#carousel_viewport").animate({ 
        marginLeft: offset
    }, c_duration, function () { enable_carousel_button(direction, scroll_count); } );
}

function enable_carousel_button(direction, scroll_count) {
    $("#btnCarousel_" + direction).click(function () {
        carousel(direction, scroll_count);
    });
}

function carousel_auto_run() {
    $('#carousel').everyTime(2000, function () { carousel('right', scroll_count); });
}

$(document).ready(function() {

    jQuery.easing.def = "easeInOutExpo";
    
    // determine the scroll count
    scroll_count = $('#carousel_viewport > li').size() - 1;
    // set left/right buttons on carousel
    enable_carousel_button('left', scroll_count);
    enable_carousel_button('right', scroll_count);
    // set hover on carousel to stop/start
    $('#carousel').hover(function () {
        $('#carousel').stopTime();
    },
    function () {
        carousel_auto_run();
    });

     // kick off the carousel
    carousel_auto_run();
    
});