var popup;
var pictures_list_current = 0;

function zoom(id,width,height) {
    var pW, pH, lW, lH, scroll=1;
    if (popup) popup.close();
    pW = width+25;
    pH = height+100;

    popup=window.open('/zoom/'+id,'zoom','width='+pW+', height='+pH+', scrollbars=1, resizable=0, location=no, status=no');
    popup.focus();
}

function zoom_shutter(url) {
    saveLinks = null;
    saveSets = null;

    if (typeof(shutterLinks) == "undefined") {
        shutterLinks = new Array();
    } else {
        saveLinks = shutterLinks;
    }

    if (typeof(shutterSets) == "undefined") {
        shutterSets = new Array();
    } else {
        saveSets = shutterSets;
    }

    shutterSets[1] = [1];
    shutterLinks[1] = {link:url,num:1,set:1,title:""};
    shutterReloaded.Make(1);

    if (saveLinks != null) {
        window.setTimeout("shutterLinks = saveLinks", 500);
    }
    if (saveSets != null) {
        window.setTimeout("shutterSets = saveSets", 500);
    }

    return false;
}

function shutter_add_link(id, src, set) {

    if (typeof(shutterLinks) == "undefined") {
        return;
    }

    var cnt = shutterLinks.length;
    if (shutterLinks[cnt-1] == 0) cnt--;

    shutterLinks[cnt] = {link:src,num:1,set:set,title:""};
    document.getElementById(id).onclick = new Function("shutterReloaded.Make("+(cnt)+"); return false;");
}

function shutter_gen_set(set) {

    var i=0;
    var k=0;

    if (typeof(shutterLinks) == "undefined") {
        return;
    }

    if (typeof(shutterSets) == "undefined") {
        shutterSets = new Array();
    }
    if (typeof(shutterSets[set]) == "undefined") {
        shutterSets[set] = new Array();
    }
    
    k = 0;
    for (i=0; i<shutterLinks.length; i++) {
        if (shutterLinks[i] == 0) break;
        if (shutterLinks[i].set != set) continue;
        shutterSets[set][k] = i;
        k++;
    }
}

function pictures_list_fw() {

    pictures_list_current++;
    pictures_list_reload();
}

function pictures_list_bw() {

    pictures_list_current--;
    pictures_list_reload();
}

function pictures_list_first() {

    pictures_list_current = 0;
    pictures_list_reload();
}

function pictures_list_last() {

    pictures_list_current = -1;
    pictures_list_reload();
}

function pictures_list_reload() {
    var fw_btn = document.getElementById("fw_btn");
    var bw_btn = document.getElementById("bw_btn");
    var first_btn = document.getElementById("first_btn");
    var last_btn = document.getElementById("last_btn");
    var picture = document.getElementById("pictures_list_view");
    var pages = document.getElementById("pictures_list_pages");

    if (typeof(shutterLinks) == "undefined") return;

    fw_btn.className = bw_btn.className = first_btn.className = last_btn.className = 'active';
    fw_btn.onclick = new Function("pictures_list_fw();");
    bw_btn.onclick = new Function("pictures_list_bw();");
    first_btn.onclick = new Function("pictures_list_first();");
    last_btn.onclick = new Function("pictures_list_last();");

    if (pictures_list_current > (shutterLinks.length - 2)) {
        pictures_list_current = 0;
    }
    if (pictures_list_current == 0) {
        bw_btn.className = "passive";
        bw_btn.onclick = null;
        first_btn.className = "passive";
        first_btn.onclick = null;
    }
    if (pictures_list_current < 0) {
        pictures_list_current = shutterLinks.length-2;
    }
    if (pictures_list_current == (shutterLinks.length - 2)) {
        fw_btn.className = "passive";
        fw_btn.onclick = null;
        last_btn.className = "passive";
        last_btn.onclick = null;
    }

    picture.setAttribute("src", shutterLinks[pictures_list_current].small);
    picture.setAttribute("alt", shutterLinks[pictures_list_current].title);
    picture.onclick = new Function("shutterReloaded.Make("+pictures_list_current+"); return false;");
    picture.style.cursor = "pointer";

    pages.innerHTML = "Изображение "+(pictures_list_current+1)+" из "+(shutterLinks.length-1);
}