
function slider_initCallback(carousel) {

    // Build dot navigation and insert
    var numItems = $('#slider li').length;

    carousel.container.append('<div id="jcarousel-control"></div>');

    dots = '';
    
    $('#slider li').each(function(index) {
        dots += '<a href="#">' + (index+1) + '</a>';
    });

    $('#jcarousel-control').html(dots);

    jQuery('#jcarousel-control a').bind('click', function() {
        carousel.scroll(jQuery.jcarousel.intval(jQuery(this).text()));
        $('#jcarousel-control a').removeClass('active');
        $(this).addClass('active');
        return false;
    });

}

function slider_itemVisibleCallback(carousel, obejctli,liindex,listate) {

    if (listate != 'init') {
        var size = jQuery('#slider').jcarousel('size');
    }
    
    else {
        var size = 5;
    }

    $('#jcarousel-control a').removeClass('active');

    // really tricky to find the current item as circular carousel creates extra ones
    var num = 0;

    // how many times have we gone round (minus small amount so floor works)
    var divide = (liindex / size) - 0.0001;
    var times = Math.floor(divide);

    // calculate what actual index would have been if no extra items created
    num = (liindex - (size*times));

    // mark as active
    jQuery('#jcarousel-control a:nth-child('+ num +')').attr("class","active");
}

$(document).ready(function() {


    // Button Text
    $('button').click( function() {
        $(this).html('Please Wait');
        return true;
    });


    if (document.getElementById("slider")) {
        $('#slider').jcarousel({
            initCallback: slider_initCallback,
            wrap: 'circular',
            itemFirstInCallback: {
                onBeforeAnimation: slider_itemVisibleCallback
            },
            scroll: 1
        });
    }

    if ($('ul.multi_images').length) {
        $('ul.multi_images a').colorbox({
            opacity: .6
        });
    }

    if (document.getElementById("comment_added")) {
        var $target = $("#comment_added");
        var targetOffset = ($target.offset().top)-20;
        $('html,body').animate({scrollTop: targetOffset}, 500);
    }
    if (document.getElementById("formErrors")) {
        var $target = $("#formErrors");
        var targetOffset = ($target.offset().top)-20;
        $('html,body').animate({scrollTop: targetOffset}, 500);
    }


});

