// слайдер фильтров на главноiй странице
var oldFrame_ = 1;
var sl_t = 7000;
var a_sp = 1000;
var img_num;
var animate = true;
var sl_timer;
var prvspl;
var oldTime = new Date().getTime();

function showFrameNext() {
    var nf = oldFrame_ + 1// = Math.ceil(Math.random() * img_num);
    if (oldFrame_ == nf) nf++;
    if (nf > img_num) nf = 1;
    showFrame2(oldFrame_, nf);
}

function sliderStop() {
    clearTimeout(sl_timer);
    animate = false;
}

function sliderStart() {
    animate = true;
    showFrameNext(oldFrame_);
}

function showFrame(n, m) {
    if (n == m) return;
    var ban = $('#ban_m_m_' + n);
    var ban2 = $('#ban_m_m_' + m);
    oldFrame_ = m;
    clearTimeout(sl_timer);
    if (animate) sl_timer = setTimeout(function(){showFrameNext(m)}, sl_t);
    ban2.css({'left': 0, 'opacity': 0}).show();

    $('#prvspl').remove();
    $('#prv' + m).append(prvspl);

    ban2.css({'left': 950}).show();
    ban.animate({'left': -950}, a_sp);
    ban2.animate({'left': 0}, a_sp);
}

function chromeTimeoutHack() {
    if ((new Date().getTime() - oldTime) > sl_t) {
        clearTimeout(sl_timer);
        oldTime = new Date().getTime();
        if (animate) showFrameNext();
    } else sl_timer = setTimeout(chromeTimeoutHack, 1000);
}

function showFrame2(n, m) {
    if (n == m) return;
    var ban = $('#ban_m_m_' + n);
    var ban2 = $('#ban_m_m_' + m);
    oldFrame_ = m;
    clearTimeout(sl_timer);
    sl_timer = setTimeout(chromeTimeoutHack, sl_t);
    if($.browser.msie && $.browser.version == "6.0") {
        ban.hide();
        ban2.show();
    } else {
        $('#prvspl').remove();
        $('#prv' + m).append(prvspl);
        ban.css({'z-index': 0}).fadeOut(a_sp);
        ban2.css({'z-index': 1}).fadeIn(a_sp);
    }

}

$(document).ready(function(){
    img_num = $('.top_slider > div').length;
    prvspl = $('<div id="prvspl"></div>');
    prvspl.css({'width': '100%', 'height': '100%', 'background': '#FFF', 'opacity': '0.3'});
    setTimeout(showFrameNext, sl_t);
    $.idleTimer(sl_t * 3);
    $(document).bind("idle.idleTimer", function(){ animate = false; });
    $(document).bind("active.idleTimer", function(){
        animate = true;
        setTimeout(chromeTimeoutHack, sl_t);
    });
});
