function fshow(elem) {
    elem.style.display = 'block';
}

function fhide(elem) {
    elem.style.display = 'none';
}
 
function fcenter(ramec, imgh, imgw, img) {
    var wheight = document.body.clientHeight;
    var wwidth  = document.body.clientWidth;
    var rate = 1;
    if (imgh + 84 > wheight) {
        rate = (wheight-84) / imgh;
        imgh *= rate;
        imgw *= rate;
    }
    if (imgw + 24 > wwidth) {
        rate = (wwidth-24) / imgw;
        imgh *= rate;
        imgw *= rate;
    }
    
    imgh = Math.floor(imgh);
    imgw = Math.floor(imgw);

    img.height = imgh;
    img.width = imgw;

    var nh = Math.floor((wheight - imgh - 84)/2);
    var nw = Math.floor((wwidth - imgw - 24)/2);

    ramec.style.left = nw+'px';
    ramec.style.top = (nh+document.body.scrollTop)+'px';
    ramec.style.width = (imgw+2)+'px';
    ramec.style.height = (imgh+64)+'px';
}

var obrazky, obrindex, timer, slide=false;

function initObrazky(obr) {
    obrazky = obr;
}

function fzapni(fotosrc, height, width, oi) {
    var ramec = document.getElementById('fotopopup');
    var img = document.getElementById('fotopopupimg');
    img.src = fotosrc;
    fcenter(ramec, height, width, img);
    fshow(ramec);
    obrindex = oi;
}

function ffirst() {
    fhide(document.getElementById('fotopopup'));
    next = obrazky[0];
    fzapni(next[0],next[1],next[2],0);
}
function fprevious() {
    fhide(document.getElementById('fotopopup'));
    if (--obrindex >= 0) {
        next = obrazky[obrindex];
        fzapni(next[0],next[1],next[2],obrindex);
    } else {
        fstopslideshow();
    }
}
function fnext() {
    fhide(document.getElementById('fotopopup'));
    if (++obrindex <= obrazky.length-1) {
        next = obrazky[obrindex];
        fzapni(next[0],next[1],next[2],obrindex);
    } else {
        fstopslideshow();
    }
}
function flast() {
    fhide(document.getElementById('fotopopup'));
    next = obrazky[obrazky.length-1];
    fzapni(next[0],next[1],next[2],obrazky.length-1);
}
function fslideshow() {
    if (slide) {
        clearInterval(timer);
        document.getElementById('slideshowcontrol').src = '/img/play.png';
        slide = false;
    } else {
        timer = setInterval(fnext, 3000);
        document.getElementById('slideshowcontrol').src = '/img/pause.png';
        slide = true;
    }
}
function fstopslideshow() {
    if (slide) {
        clearInterval(timer);
        document.getElementById('slideshowcontrol').src = '/img/play.png';
        slide = false;
    }
}



