var can_autoMove = true;

$(document).ready(function() {
    closeAllKatMenu();
    autoStartMove();
})

function closeAllKatMenu()
{
    $('.katalogmenu').each(function(i) {
        var temp = getCookie('open['+i+']');
        if (temp != 1) {
            $(this).addClass('act');
        }
    })
}

function katMenu(_this)
{
    var div = $(_this).parent();

    if (div.hasClass('act')) {
        div.removeClass('act');
        document.cookie = "open["+$('.katalogmenu').index(div)+"]=1; path=/";
    } else {
        div.addClass('act');
        document.cookie = "open["+$('.katalogmenu').index(div)+"]=0; path=/";
    }
}

function getCookie(name) {
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1) {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    } else
    begin += 2;
    var end = document.cookie.indexOf(";", begin);
    if (end == -1) {
        end = dc.length;
    }
    return unescape(dc.substring(begin + prefix.length, end));
}

var frameTimerID = null;
var autoFrameTimerID = null;
var auto_course = 'next';
function movePhotoFrame(course, step)
{
    var step = (step ? step : 30);
    var elem = document.getElementById('photo_frame');
    var start = elem.scrollLeft;

    if (course == 'next') {
        elem.scrollLeft = start + step;
    }

    if (course == 'prev') {
        elem.scrollLeft = start - step;
    }
}

function startMovephotoFrame(course, speed)
{
    if (autoAutostart) {
        clearInterval(autoAutostart)
    }
    autoAutostart = null;

    autoStopMove();
    stopMovePhotoFrame();
    auto_course = course;
    frameTimerID = setInterval(function() {
        movePhotoFrame(course, speed);
    }, 40);
}

function stopMovePhotoFrame()
{
    if (frameTimerID) {
        clearInterval(frameTimerID);
    }
    frameTimerID = null;
}

function moveNext(course)
{
    course = (course ? true : false);
    if (course) {
        startMovephotoFrame('next');
    } else {
        startMovephotoFrame('prev');
    }
}

var autoAutostart = null;
function moveStop()
{
    stopMovePhotoFrame();
    autoAutostart = setTimeout(autoStartMove, 5000);
}


function autoStartMove ()
{
    if (can_autoMove) {
        autoStopMove();
        frameTimerID = setInterval(function() {
            movePhotoFrame(auto_course, 1);
        }, 40);
    }
}

function autoStopMove()
{
    if (autoFrameTimerID) {
        clearInterval(autoFrameTimerID);
    }
    autoFrameTimerID = null;
}

