function change_page(index) {

    if  (pics.h == 300) {
        var start_index = index * 8;
        var end_index = (index + 1) * 8;
    } else {
        var start_index = index * 8 + 4;
        var end_index = (index + 1) * 8 + 4;
    }

    var box = $('.entry .content .thumbs');
    box.empty();
    
    if(pics.h == 500 && (pics.l.length<=4)) {
        box.hide();
        return false;
    }

    for (i=start_index; i<end_index; i++) {
        var pic = pics.l[i];
        if(!pic)
            var c = '<div class="false_thumb">&nbsp;</div>';
        else
            var c = '<div class="thumb"><img onclick="return show_image('+i+')" src="'+pic.thumb+'"></div>';
        box.append(c);
    }
    return false;
}

function show_image(index) {

    var box = $('.entry .content .pics .pic');
    var desc = $('.entry .content .pics .desc');
    box.addClass('h'+pics.h);
    box.empty();

    var pic = pics.l[index];
    i_prev = index - 1;
    w_prev = '<h5><a href="#" onclick="return show_image('+i_prev+')">&laquo; anterior</a> |</h5>';
    if(i_prev<0)
        w_prev = '<h5>&nbsp;</h5>';
    i_next = index + 1;
    w_next = '<h5>| <a href="#" onclick="return show_image('+i_next+')">próxima &raquo;</a></h5>';
    if(i_next==pics.l.length)
        w_next = '<h5>&nbsp;</h5>';
    var c = w_prev+'<h2>'+pic.cred+'</h2>'+w_next+'<h3 style="background-image: url('+pic.pic+')">&nbsp;</h3>'+w_prev+'<h4>&nbsp;</h4>'+w_next;
    box.append(c);
    desc.text(pic.desc);

    return false;

}

function show_first_images() {
    var box = $('.entry .content .extra_pics');
    for(i=0; i<4; i++) {
        var pic = pics.l[i];
        var c = '<img onclick="return show_image('+i+')" src="'+pic.thumb+'">';
        box.append(c);
    }
    box.show();
    return false;
}

function paginate() {

    var paginator = $('.entry .content .paginator');
    if(pics.h == 300)
        var pags = Math.ceil((pics.l.length - 1) / 8.);
    else
        var pags = Math.ceil((pics.l.length - 5) / 8.);
    if(pags<=1)
        return false;

    for(i=0; i<pags; i++) {
        paginator.append('<a href="#" onclick="return change_page('+i+')">'+i+'</a> ');
    }

    paginator.show();
    return false;

}

$(document).ready(function() {

    show_image(0);
    if(pics.h == 500)
        show_first_images();
    change_page(0);
    paginate();
    $('.entry .pics').show();

});
