jQuery(function () {
    initSlideMenu();
    initSmallPopups();
});

// small popups js
function initSmallPopups() {
    var popup = 'div.drop';
    var holder = jQuery('.services-list > li:has(' + popup + ')');
    var link = '>span, >a';
    var activeClass = 'active';
    var delay = 300;

    holder.each(function () {
        var _holder = jQuery(this);
        var _popup = jQuery(popup, _holder).css({ display: 'block' });
        var _width = _popup.offset().left + _popup.width();
        var _offset = 0;
        _offset += 0;
        var _link = jQuery(link, _holder);

        if (_popup.length && _width > _offset) {
            _popup.addClass('c-alt');
        }

        _link.mouseenter(function () {
            _holder.attr('active', 'true');
            holder.removeClass(activeClass);
            _holder.addClass(activeClass);
        }).mouseleave(function () {
            _holder.removeAttr('active', '');
            var timer = setInterval(function () {
                if (!_holder.attr('flag')) {
                    if (_popup.is(':visible') && !_holder.attr('active')) {
                        _holder.removeClass(activeClass);
                    } else {
                        clearInterval(timer);
                        _holder.removeClass(activeClass);
                    }
                }
            }, delay);
        });

        _popup.mouseenter(function () {
            _holder.attr('flag', activeClass);
        }).mouseleave(function () {
            _holder.removeAttr('flag');
        });
        _holder.removeClass(activeClass);
        _popup.css({ display: '' });
    });
}

function initSlideMenu() {
    var nav = jQuery('#nav li');
    var duration = 300;
    nav.each(function () {
        var _this = jQuery(this);
        var _drop = jQuery('>ul', _this).show();
        var _dropHeight = _drop.height();
        _this.hover(function () {
            _this.addClass('active').parents('li').addClass('active');
            if (jQuery.browser.msie) {
                _drop.css({ height: 0 }).show().stop().animate({ height: _dropHeight }, duration, function () {
                    _drop.css({ overflow: 'visible' });
                });
            } else {
                _drop.css({ height: 0 }).show().stop().animate({ height: _dropHeight, opacity: 1 }, duration, function () {
                    _drop.css({ overflow: 'visible' });
                });
            }
        }, function () {
            _this.removeClass('active').parents('li').removeClass('active');
            var _timer = setInterval(function () {
                if (!_drop.find('ul').is(':visible')) {
                    if (jQuery.browser.msie) {
                        _drop.css({ height: _dropHeight }).stop().animate({ height: 0 }, duration, function () {
                            _drop.hide();
                        });
                    } else {
                        _drop.css({ height: _dropHeight }).stop().animate({ height: 0, opacity: 0 }, duration, function () {
                            _drop.hide();
                        });
                    }
                    clearInterval(_timer);
                }
            }, duration / 2);
        });
    });
    nav.find('>ul').hide();
}
