﻿//
// ASP.NET 4.0 Menu Fix by Valentin Gushan (feedback: valentin.gushan@gmail.com)
// 2010-06-15
//

$(window).resize(function () {
    $('.menu-dynamic').each(function () {
        // сброс на значения по умолчанию
        if ($(this).attr('leftValue')) {
            $(this).css('left', $(this).attr('leftValue'));
            $(this).css('right', null);
            $(this).removeAttr('leftValue');
        }
    });
});

$().ready(
    function () {
        // IE7 zIndex Fix
        var zIndex = $('.has-popup').length;
        $('.has-popup').each(function () {
            zIndex -= 1;
            $(this).css('z-index', zIndex);
        });
        // bind mouseover handler
        $('.has-popup').bind('mouseover', function () {
            var el = $(this).children('ul');
            // тест был ли отработан случай
            if (el.attr('leftValue')) return;
            //
            var left = el.offset().left;
            var width = el.width();
            var winwidth = $(window).width();
            // проверяем, если справа не умещается, бросаем на лево
            if (left + width > winwidth) {
                el.attr('leftValue', el.css('left'));
                if (width > $(this).offset().left) {
                    // если не умещается слева
                    var offset = winwidth - width - $(this).offset().left - 5;
                    el.css('left', offset + 'px');
                } else {
                    var offset = winwidth - width - $(this).offset().left - 5;
                    el.css('left', (-1 * width) + 'px');
                }
            }
        })
    }
);
