var Timer_b, Timer_d, Timer_p;
var Timer_scroll = false;
var oSet = {
    "timer" : 7000
};
var active = 0;
var b_active = 0;
var aFile = new Array();
window.onscroll = J_scroll;

function Jb_sign() {
    var pop_sign_div = document.getElementById('pop_sign_div');
    var pop_is_sign = document.getElementById('pop_is_sign');
    if (pop_sign_div.style.visibility == 'hidden') {
        pop_sign_div.style.visibility = 'visible';
        pop_is_sign.innerHTML = 'убрать подписи';
    } else {
        pop_sign_div.style.visibility = 'hidden';
        pop_is_sign.innerHTML = 'показать подписи';
    }
}

function J_position() {
    var pop = document.getElementById('pop_back');
    var b_buttons_line = document.getElementById('b_buttons_line');
    var pop_sign_div = document.getElementById('pop_sign_div');
    pop.style.width = document.body.scrollWidth;
    pop.style.height = document.body.scrollHeight;
    b_buttons_line.style.left = Math.round(document.body.clientWidth / 2 - 237) + document.body.scrollLeft;
    if ((document.body.clientHeight - 50) + document.body.scrollTop > parseInt(b_buttons_line.style.top)) {
        if ((document.body.clientHeight - 50) + document.body.scrollTop - parseInt(b_buttons_line.style.top) > 20) {
            pop_sign_div.style.top = parseInt(b_buttons_line.style.top) + 10 - 40;
            b_buttons_line.style.top = parseInt(b_buttons_line.style.top) + 10;
        } else {
            pop_sign_div.style.top = parseInt(b_buttons_line.style.top) + 1 - 40;
            b_buttons_line.style.top = parseInt(b_buttons_line.style.top) + 1;
        }
    } else if ((document.body.clientHeight - 50) + document.body.scrollTop < parseInt(b_buttons_line.style.top)) {
        pop_sign_div.style.top = parseInt(b_buttons_line.style.top) - 10 - 40;
        b_buttons_line.style.top = parseInt(b_buttons_line.style.top) - 10;
    } else {
        clearInterval(Timer_scroll);
        Timer_scroll = false;
    }
}

function J_scroll() {
    var pop = document.getElementById('pop_back');
    if (pop && pop.style.visibility == 'visible' && !Timer_scroll) {
        J_position();
        Timer_scroll = window.setInterval(J_position, 10);
    }
}

function J_pop() {
    b_active = active;
	var oDIV = document.getElementById('div_pop_img');
    var oIMG = document.getElementById('pop_img_' + aFile[active]);
	var pop = document.getElementById('pop_back');
    var pop_sign_div = document.getElementById('pop_sign_div');
    var b_buttons_line = document.getElementById('b_buttons_line');
    L_pause();
	if (oDIV.style.visibility == 'hidden') {
        oDIV.style.opacity = 0;
        oDIV.style.filter = 'alpha(opacity=0)';
        pop.style.visibility = 'visible';
        oDIV.style.visibility = 'visible';
        b_buttons_line.style.visibility = 'visible';
        pop_sign_div.style.visibility = 'visible';
        J_scroll();
        Jb_img();
        Jb_play();
    } else {
        pop_sign_div.style.visibility = 'hidden';
        b_buttons_line.style.visibility = 'hidden';
        pop.style.visibility = 'hidden';
        oDIV.style.visibility = 'hidden';
        oIMG.style.display = 'none';
    }
}

function Jb_next(pause) {
    clearInterval(Timer_b);
    clearInterval(Timer_d);
    if (pause) Jb_pause();
    b_active = (b_active + 1 > aFile.length - 1) ? 0 : b_active + 1;
    Timer_d = window.setInterval(Jb_dim, 50);
}

function Jb_prev() {
    clearInterval(Timer_b);
    clearInterval(Timer_d);
    Jb_pause();
    b_active = (b_active - 1 < 0) ? aFile.length - 1 : b_active - 1;
    Timer_d = window.setInterval(Jb_dim, 50);
}

function Jb_pause() {
    var play = document.getElementById('b_play');
    clearInterval(Timer_p);
    play.className = 'b_img_play';
    play.title = 'Воспроизведение';
    play.href = 'javascript:Jb_play();';
}

function Jb_dim() {
    var img = document.getElementById('div_pop_img');
    var opacity = Math.floor(parseFloat(img.style.opacity) * 100);
    if (opacity <= 0) {
        clearInterval(Timer_d);
        Jb_img();
    } else {
        img.style.opacity = opacity / 100 - 0.1;
        img.style.filter = "alpha(opacity=" + (opacity - 10) + ")";
    }
}

function Jb_img() {
    var i;
    var count = aFile.length;
    if (count < 1) return;
    clearInterval(Timer_b);
    clearInterval(Timer_d);
    for (i = 0; i < count; i++) {
        document.getElementById('pop_img_' + aFile[i]).style.display = 'none';
    }
    var oIMG = document.getElementById('pop_img_' + aFile[b_active]);
    var oDIV = document.getElementById('div_pop_img');
    var pop_sign_text = document.getElementById('pop_sign_text');
    pop_sign_text.innerHTML = oIMG.alt;
    oIMG.style.display = 'block';
    oDIV.style.width = oIMG.width + 'px';
    oDIV.style.height = oIMG.height + 'px';
    oDIV.style.top = (document.body.clientHeight > parseInt(oIMG.height))
        ? Math.round(document.body.clientHeight / 2) - Math.round(parseInt(oIMG.height) / 2) + document.body.scrollTop
        : document.body.scrollTop;
    oDIV.style.left = Math.round(document.body.clientWidth / 2) - Math.round(parseInt(oIMG.width) / 2) + document.body.scrollLeft;

    J_scroll();
    Timer_b = window.setInterval(Jb_bright, 50);
}

function Jb_bright() {
    var img = document.getElementById('div_pop_img');
    var opacity = Math.floor(parseFloat(img.style.opacity) * 100);
    if (opacity >= 100) {
        clearInterval(Timer_b);
    } else {
        img.style.opacity = opacity / 100 + 0.1;
        img.style.filter = "alpha(opacity=" + (opacity + 10) + ")";
    }
}

function Jb_timer(operator) {
    if (operator == 'plus') {
        if (oSet['timer'] < 30000) {
            oSet['timer'] += 1000;
        }
    } else {
        if (oSet['timer'] > 3000) {
            oSet['timer'] -= 1000;
        }
    }
    if (document.getElementById('b_play').className == 'b_img_pause') {
        clearInterval(Timer_p);
        Timer_p = window.setInterval(function () { Jb_next(false); }, oSet['timer']);
    }
    document.getElementById('pop_timer_text').innerHTML = (oSet['timer'] / 1000) + ' сек';
}

function Jb_play() {
    if (aFile.length <= 1) return;
    var play = document.getElementById('b_play');
    clearInterval(Timer_p);
    play.className = 'b_img_pause';
    play.title = 'Пауза';
    play.href = 'javascript:Jb_pause();';
    Timer_p = window.setInterval(function () { Jb_next(false); }, oSet['timer']);
}

function L_play() {
    if (aFile.length <= 1) return;
    var play = document.getElementById('play');
    clearInterval(Timer_p);
    play.className = 'img_pause';
    play.title = 'Пауза';
    play.href = 'javascript:L_pause();';
    Timer_p = window.setInterval(function () { L_next(false); }, 7000);
}

function L_pause() {
    var play = document.getElementById('play');
    clearInterval(Timer_p);
    if (play) {
        play.className = 'img_play';
        play.title = 'Воспроизведение';
        play.href = 'javascript:L_play();';
    }
}

function L_next(pause) {
    clearInterval(Timer_b);
    clearInterval(Timer_d);
    if (pause) L_pause();
    active = (active + 1 > aFile.length - 1) ? 0 : active + 1;
    Timer_d = window.setInterval(L_dim, 50);
}

function L_prev() {
    clearInterval(Timer_b);
    clearInterval(Timer_d);
    L_pause();
    active = (active - 1 < 0) ? aFile.length - 1 : active - 1;
    Timer_d = window.setInterval(L_dim, 50);
}

function L_dim() {
    var img = document.getElementById('img_div');
    var pub_sign = document.getElementById('pub_sign');
    var opacity = Math.floor(parseFloat(img.style.opacity) * 100);
    if (opacity <= 0) {
        clearInterval(Timer_d);
        L_img();
    } else {
        pub_sign.style.opacity = opacity / 100 - 0.1;
        pub_sign.style.filter = "alpha(opacity=" + (opacity - 10) + ")";
        img.style.opacity = opacity / 100 - 0.1;
        img.style.filter = "alpha(opacity=" + (opacity - 10) + ")";
    }
}

function L_img() {
    var i;
    var count = aFile.length;
    if (count <= 1) return;
    clearInterval(Timer_b);
    clearInterval(Timer_d);
    for (i = 0; i < count; i++) {
        document.getElementById('pub_img_' + aFile[i]).style.display = 'none';
    }
    document.getElementById('pub_img_' + aFile[active]).style.display = 'block';
    document.getElementById('pub_sign').innerHTML = document.getElementById('pub_img_' + aFile[active]).alt;

    Timer_b = window.setInterval(L_bright, 50);
}

function L_bright() {
    var img = document.getElementById('img_div');
    var pub_sign = document.getElementById('pub_sign');
    var opacity = Math.floor(parseFloat(img.style.opacity) * 100);
    if (opacity >= 100) {
        clearInterval(Timer_b);
    } else {
        pub_sign.style.opacity = opacity / 100 + 0.1;
        pub_sign.style.filter = "alpha(opacity=" + (opacity + 10) + ")";
        img.style.opacity = opacity / 100 + 0.1;
        img.style.filter = "alpha(opacity=" + (opacity + 10) + ")";
    }
}
