﻿function IsOldIE() {
    return jQuery.browser.msie && jQuery.browser.version < 8;
}

function Move(mc, nextScroll) {
    var lastScroll = -9999;
    var inc = 5;

    function TinyMove() {
        var curScroll = mc.scrollLeft();
        if (curScroll == lastScroll)
            return; // abort move 
        lastScroll = curScroll;
        var dist = nextScroll - curScroll;
        var step = 0;
        if (dist > 0) {
            step = Math.min(dist, inc);
        } else if (dist < 0) {
            step = Math.max(dist, -inc);
        }

        if (step != 0) {
            mc.scrollLeft(curScroll + step);
            setTimeout(TinyMove, 10);
        }
    }

    TinyMove();
}

function goSmooth(right) {
    var mc = jQuery("#menuWindow");
    var mcLeft = mc.offset().left;
    var curScroll = mc.scrollLeft();
    var nextScroll = 0;

    jQuery("#listHolder").children(".listCol").each(function () {
        var e = jQuery(this);
        var left = e.offset().left + curScroll;
        var dist = left - mcLeft;
        if (right) {
            if (nextScroll == 0 && dist > curScroll)
                nextScroll = dist;
        } else {
            if (dist < curScroll)
                nextScroll = dist;
        }
    });

    Move(mc, nextScroll);
}

function goStep(right) {
    var lh = jQuery("#listHolder");
    var cols = lh.children(".listCol");
    var index = 0;
    var firstVisible = 0;
    var lastVisible = 0;
    cols.each(function () {
        index++;
        if (jQuery(this).is(':visible')) {
            lastVisible = index;
            if (firstVisible < 1) firstVisible = index;
        }
    });
    if (right) {
        if (lastVisible < cols.length) {
            firstVisible++;
            lastVisible++;
        } else
            firstVisible = 0;
    } else {
        if (firstVisible > 1) {
            firstVisible--;
            lastVisible = firstVisible + 3;
        } else
            firstVisible = 0;
    }
    if (firstVisible > 0) {
        cols.each(function () {
            jQuery(this).hide();
        });
        index = 0;
        cols.each(function () {
            index++;
            if (index >= firstVisible && index <= lastVisible)
                jQuery(this).show();
        });
    }
}

function go(right) {
    if (IsOldIE())
        goStep(right);
    else
        goSmooth(right);
}

function goLeft() {
    go(true);
}

function goRight() {
    go(false);
}

function InitSubMenu() {
    var lh = jQuery("#listHolder");
    var cols = lh.children(".listCol");
    if (IsOldIE()) {
        var index = 0;
        cols.each(function () {
            index++;
            if (index > 4)
                jQuery(this).hide();
        });
    }
    lh.show(400);
    if (cols.length > 4)
        setTimeout(function () { jQuery(".MoveArrow").show(); }, 400);
    return false;
}
