function get_directory() {

    dir = $("SPAN.directory").text();

    if (dir.lastIndexOf("/") != (dir.length-1)) {
        dir = dir + "/";
    }

    return dir;

}

function get_size() {
    size = parseInt($("SPAN.size").text());

    return size

}

function set_background(jObject, image, directory) {

    if (directory == null) {
        directory = "background/";
    }

    jObject.css('background', "#fff url('/images/" + directory  + image + "') center center no-repeat");

}

$(document).ready(function() {

    set_background($('DIV#page'), "back1.jpg", get_directory());

    if (get_size() > 1) {
        a = 2;
    } else {
        a = get_size();
    }

    $('IMG#prev').click(function(cl) {

        set_background($('DIV#page'), "back" + a + ".jpg", get_directory());

        if (a <= 1) {
            a = get_size();
        } else {
            a--;
        }

    });

    $('IMG#next').click(function(cl) {

        set_background($('DIV#page'), "back" + a + ".jpg", get_directory());

        if (a >= get_size()) {
            a = 1;
        } else {
            a++;
        }

    });

});