var myTempArgs = {};
var myInterval = null; //for interval
var hideDelayTimer = null; //for hover delay settimeout
var interValTimer = 300;

function delaytest() {
    myInterval = setInterval('intervalway()', interValTimer);
}

function intervalway() {
    var uporDown = '';
    var functionTemp = myTempArgs.whichBackButton.parent('div.nb_sunmenu').find('div.allin').children('ul.showing:first');

    if (myTempArgs.whichBackButton.hasClass('goup')) {
        uporDown = '+=';
    } else if (myTempArgs.whichBackButton.hasClass('godown')) {
        uporDown = '-=';
    }

    //if (functionTemp.not(':animated')) {
    functionTemp.animate({
        top: uporDown + myTempArgs.unitHeight
    }, { queue: false, duration: interValTimer, easing: 'linear', complete: countmenu(functionTemp) });

    //}
}

function menuupdown(e) {
    e.stop(false, true); //for fixbug un and down
    var tempNu = e.data('countMenuNu');
    var howManyLi = e.data('childrenNu');

    var goupBu = myTempArgs.whichBackButton.parent('div.nb_sunmenu').find('div.goup');
    var godownBu = myTempArgs.whichBackButton.parent('div.nb_sunmenu').find('div.godown');

    if (((howManyLi + tempNu) > 8) && ((howManyLi + tempNu) < howManyLi)) {
        goupBu.show();
        godownBu.show();
    } else if (howManyLi + tempNu <= 8) {

        goupBu.show();
        godownBu.hide();

        if (myInterval) {
            e.stop(true, true);
            clearInterval(myInterval)
        }
    } else {
        goupBu.hide();
        godownBu.show();

        if (myInterval) {
            e.stop(true, true);
            clearInterval(myInterval);
        }
    }
}

function hasbackbutton(e, chagetext) {
    var whichMenu = e.parents('div.nb_sunmenu:first');
    whichMenu.find("div.backbutton:first").removeClass("hide").find('a').text(chagetext);
};

function showMenuButton(e) {
    //e must be ul.product_sub_nav
    var scope = e.parents('.nb_sunmenu:first');
    var upButton = scope.find('div.goup');
    var downButton = scope.find('div.godown');
    var allLi = e.data('childrenNu');
    var currentNu = e.data('countMenuNu');

    if (allLi > 8) {
        if (currentNu == 0) {
            upButton.hide()
            downButton.show();
        } else if ((allLi + currentNu) <= 8) {
            upButton.show();
            downButton.hide();
        } else {
            upButton.show();
            downButton.show();
        }
    } else {
        upButton.hide();
        downButton.hide();
    }
};

function resetall(e) {
    var which_sunmenu = e
    var all_menu = e.find('ul.product_sub_nav');
    var firstMenu = e.find('.li_item_1');
    var whichSlid = e.find('div.allin')
    var backBtn = e.find('div.backbutton');

    if (hideDelayTimer) { clearTimeout(hideDelayTimer); };
    if (myInterval) { clearInterval(myInterval) };
    all_menu.removeClass('showing');
    firstMenu.addClass('showing');

    whichSlid.css({ left: 0 });

    all_menu.each(function() {
        $(this).css({ 'top': 0, 'left': $(this).data('originalLeft') })
        $(this).data('countMenuNu', 0);
    })

    backBtn.addClass('hide');
    showMenuButton(firstMenu);

};

