var currentimage = 0;
var timerid = null;
var timerrunning = false;

function previmage( post_id, picCount ) {
    var id;
    for (i=0; i<picCount; i++) {
        id=post_id+'picture'+i;
        var elem = document.getElementById(id);
        elem.style.display = 'none';
        elem.style.visibility = 'hidden';
    }
    currentimage--;
    if (currentimage < 0)
        currentimage = picCount - 1;
    id=post_id+'picture'+currentimage;
    var elem = document.getElementById(id);
    elem.style.display = 'block';
    elem.style.visibility = 'visible';
}

function nextimage( post_id, picCount ) {
    var id;
    for (i=0; i<picCount; i++) {
        id= post_id+'picture'+i;
        var elem = document.getElementById(id);
        elem.style.display = 'none';
        elem.style.visibility = 'hidden';
    }
    currentimage++;
    if (currentimage >= picCount)
        currentimage = 0;
    id= post_id+'picture'+currentimage;
    var elem = document.getElementById(id);
    elem.style.display = 'block';
    elem.style.visibility = 'visible';
}

function gotoimage( num, post_id, picCount ) {
    var id;
    for (i=0; i<picCount; i++) {
        id=post_id+'picture'+i;
        var elem = document.getElementById(id);
        elem.style.display = 'none';
        elem.style.visibility = 'hidden';
    }
    currentimage = num;
    id=post_id+'picture'+currentimage;
    var elem = document.getElementById(id);
    elem.style.display = 'block';
    elem.style.visibility = 'visible';
}

function timertick(post_id, picCount) {
    nextimage(post_id, picCount);
    timerid = setTimeout("timertick("+post_id+","+picCount+")", 2000);
}

function slideshow(toggle, post_id, picCount) {

    if (toggle == 0 && timerrunning) {
        clearTimeout(timerid);
        timerrunning = false;
        ele = document.getElementById( 'pauseslideshow_' + post_id );
        if( ele != null ) ele.style.display = 'none';
        ele = document.getElementById( 'playslideshow_' + post_id );
        if( ele != null ) ele.style.display = '';
    }
    if (toggle == 1 && !timerrunning) {
        nextimage( post_id, picCount );
        timerid = setTimeout("timertick("+post_id+","+picCount+")", 2000);
        timerrunning = true;
        ele = document.getElementById( 'playslideshow_' + post_id );
        if( ele != null ) ele.style.display = 'none';
        ele = document.getElementById( 'pauseslideshow_' + post_id );
        if( ele != null ) ele.style.display = '';
    }
}

function launch_middle(name,url,w,h) {

    var l=window.screenX + (window.outerWidth-w)/2;
    var t=window.screenY + (window.outerHeight-h)/2;
    w+=20;
    h+=20;
    window[name]=open(url,name,'width='+w+',height='+h+',left='+l+',top='+t+',status,scrollbars');
    if (window[name] && !window[name].closed) window[name].focus();
}