var current_index = -1;
var slides_count = 1;
var stageID = '';

function slidestage_init(id) {
    // start
    current_index = 0;
    slides_count = Math.max(1, $('#' + id + ' .list li').size());
    stageID = id;

    // hide all items
    $('#' + stageID + ' .list li').hide();

    // hook up navigation buttons
    $('#' + stageID + ' .navigation a.next').click(function() {  slidestage_next(); return false; });
    $('#' + stageID + ' .navigation a.prev').click(function() {  slidestage_prev(); return false; });

    // show first item
    slidestage_goto(1);
}
function slidestage_goto(index) {
    // retrieve new index
    var new_index = Math.max((index % (slides_count + 1)), 1);

    // hide current index
    if(current_index >= 0) {
        $('#' + stageID + ' .list li:nth-child(' + current_index + ')').hide();
    }

    // show new index
    $('#' + stageID + ' .list li:nth-child(' + new_index + ')').show();
    current_index = new_index;

    // toggle buttons
    if(slides_count > 1) {
        $('#' + stageID + ' .navigation a.next').show();
        $('#' + stageID + ' .navigation a.prev').show();
        $('#' + stageID + ' .navigation span.count').show();
    } else {
        $('#' + stageID + ' .navigation a.next').hide();
        $('#' + stageID + ' .navigation a.prev').hide();
        $('#' + stageID + ' .navigation span.count').hide();
    }

    // update counter
    $('#' + stageID + ' .navigation span.count').html(current_index + ' &#47; ' + slides_count);
}
function slidestage_next() {
    // goto next
    slidestage_goto(current_index + 1);
}
function slidestage_prev() {
    // goto previous
    slidestage_goto(current_index - 1);
}
