
$(document).ready(function(){
    /**
    * add tab functionality
    */
    $(function() {
        if($(".routeMap").length)
            $(".routeMap").tabs();
    });

    // add classes .first and .last
    $('.routeMap ul.tab li:first a').addClass('first');
    $('.routeMap ul.tab li:last a').addClass('last');
});


/**
 * 
 * @param gallery_id
 * @param height
 */
function nrandshadowbox_next(gallery_id, height)
{
    document.getElementById(gallery_id).scrollTop += height;
    nrandshadowbox_update_counter(gallery_id, height);
}

/**
 * 
 * @param gallery_id
 * @param height
 */
function nrandshadowbox_prev(gallery_id, height)
{
    document.getElementById(gallery_id).scrollTop -= height;
    nrandshadowbox_update_counter(gallery_id, height);
}

/**
 * 
 * @param gid gallery ID
 * @param height
 */
function nrandshadowbox_update_counter(gid, height)
{
    var gallery = document.getElementById(gid);
    var count = Math.ceil(gallery.scrollTop / height + 1);
    
    $('#' + gid + '_counter, #' + gid + '_counter_b, .' + gid + '_counter')
        .html(count);
}

/**
 * 
 * @param gid gallery ID
 * @param height
 */
function nrandshadowbox_init_counter(gid, height)
{
    _nrandshadowbox_init_counter(gid, height);
    _nrandshadowbox_init_counter(gid + '_p', height);
    _nrandshadowbox_init_counter(gid + '_v', height);
}

/**
 * 
 * @param gid gallery ID
 * @param height
 */
function _nrandshadowbox_init_counter(gid, height)
{
    var gallery = document.getElementById(gid);
    if (! gallery) {
        return;
    }
    var count = Math.ceil(gallery.scrollHeight / height);
    
    if (count == 0) {
        $('.routeMap').bind('tabsshow', function(event) {
            _nrandshadowbox_init_counter(gid, height);
            $(this).unbind(event);
        });
        return;
    }
    
    gid = gid + '_counter_max';
    
    $('#' + gid + ', #' + gid + '_b, .' + gid).html(' / ' + count);
}

/**
 * 
 * @param gallery_id
 */
function nrandshadowbox_start(gallery_id)
{
    Shadowbox.open(document.getElementById(gallery_id + '_media_1'));
}

/**
 * 
 * @param id
 */
function nrandshadowbox_start_gallery(id)
{
    Shadowbox.applyOptions({
        slideshowDelay: 4,
        
        onClose: function(img){
            // NR-SME 20100520
            // i do not know why, but having only the first line does not work
            Shadowbox.applyOptions({slideshowDelay: 0});
            // so here comes a second line
            Shadowbox.applyOptions({slideshowDelay: 0});
        }
    });
    Shadowbox.open(document.getElementById(id));
}

