$(document).ready(function(){
    showThumbs(page);
    prepareLinks(page);
    
});

function changePicture(pic_id){
    picture = $("#small-pictures li#"+pic_id+" a").attr("href");
    $("#big-picture img").attr({"src": picture, "title": pic_id});
    return false;
}

function showThumbs(act_page){ // pokazuje konkretne miniaturki: act_page - aktualna strona
    var t_is = (act_page-1) * images_limit;
    var t_ist = t_is + images_limit;
    var l = 1;
    for(l; l<=images; ++l){
        thumb_id = $("#small-pictures li#"+l);
        thumb_id.removeClass("gallery_hidden");
        if(l<=t_is || l>t_ist){
            thumb_id.addClass("gallery_hidden");
        }
    }
}

function prepareLinks(act_page){ // aktualna strona
    next_p = parseInt(act_page) +1;
    prev_p = act_page -1;
    content = "";
/* linki dla paginacji */
    if(act_page>1){ // wstawiamy strone poczatkowa
        content = "<a href="+prev_p+" title="+prev_p+"><img src=\"/img/arr_niebieska_prev.gif\" /> "+prev+"</a>";
    }else{ // to jest pierwsza strona
        content = "<img src=\"/img/arr_gray2_prev.gif\" /> "+prev;
    }
    $("#gallery-pagination .pag-prev").empty().append(content);
    
    if(act_page<pages){ // maja byc strony poprzednie
        content = "<a href="+next_p+" title="+next_p+">"+next+" <img src=\"/img/arr_niebieska_next.gif\" /></a>";
    }else{ // to jest ostatnia strona
        content = next+" <img src=\"/img/arr_gray2_next.gif\" />";
    }
    $("#gallery-pagination .pag-next").empty().append(content);
    
    // tworzenie linkow dla konkretnych stron paginacji
    for(var i=1; i<=pages; ++i){ // przechodzi po wszystkich linkach i je ustawia
        if(act_page==i){
            $("#gallery-pagination #"+i).replaceWith("<span id=\""+i+"\">"+i+"</span>");
        }else{
            $("#gallery-pagination #"+i).replaceWith("<a href=\""+i+"\" id=\""+i+"\" title=\""+i+"\">"+i+"</a>");
        }
    } 
    /* linki w wyswietlarce */
    var actual_picture = parseInt($("#big-picture img").attr("title")); // id aktualnego obrazka
    prev_p = actual_picture - 1; // poprzedni obrazek
    next_p = actual_picture + 1; // nastepny obrazek
    if(actual_picture>1){ // istnieje poprzedni obrazek
        content = "<a href="+prev_p+" title="+prev_p+">&laquo; "+prev+"</a>";
    }else{ // to jest pierwszy obrazek
        content = "&laquo; "+prev;
    }
    $(".gallery-navigation-by-picture .pag-prev").empty().append(content);
    if(actual_picture<images){ // istnieje nastepny obrazek
        content = "<a href="+next_p+" title="+next_p+">"+next+" &raquo;</a>";
    }else{ // to jest ostatni obraz
        content = next+" &raquo;";
    }
    $(".gallery-navigation-by-picture .pag-next").empty().append(content);
    
    // bindowanie nowych linkow
    bindLinks();
}


function changePage(){
    var pg = parseInt($(this).attr("title"));
    changePicture(pg*parseInt(images_limit)-parseInt(images_limit)+1);
    showThumbs(pg);
    prepareLinks(pg);
    return false;
}
function bindLinks(){
    $(".gallery-navigation-by-picture a").click(changePictureFromLinks);
    $("#small-pictures li a").click(changePictureFromThumbs);
    $("#gallery-pagination a, #gallery-pagination .pag-prev a, #gallery-pagination .pag-next a").click(changePage);
}
function changePictureFromThumbs(){
    changePicture($(this).parent().attr("id"));
    prepareLinks(getPage($(this).parent().attr("id")));
    showThumbs(getPage($(this).parent().attr("id")));
    return false;
}

function changePictureFromLinks(){
    changePicture($(this).attr("title"));
    prepareLinks(getPage($(this).attr("title")));
    showThumbs(getPage($(this).attr("title")));
    return false;
}
function getPage(picture){
    return Math.ceil(picture / images_limit);
}
