﻿$(document).ready(function () {
    //Add rounded corners LeftMenu first item
    $('.topMenuSubSubCMSListMenuUL > LI:first-child,.topMenuSubSubCMSListMenuUL > LI:first-child a,.topMenuSubSubCMSListMenuUL > LI:first-child span').css('border-top-right-radius', '7px');
    //Add rounded corners LeftMenu last item
    $('.topMenuSubSubCMSListMenuUL > LI:last-child,.topMenuSubSubCMSListMenuUL > LI:last-child a,..topMenuSubSubCMSListMenuUL > LI:last-child span').css('border-bottom-right-radius', '7px');

    //Add different backgrounds
    $('.topMenuSubCMSListMenuLI:odd').addClass('topMenuSubCMSListMenuLIodd');
    $('.topMenuSubSubCMSListMenuLI:odd').addClass('topMenuSubSubCMSListMenuLIodd');

    //Add rounded corners TopMenu last item
    $('.topMenuSubCMSListMenuUL > LI:last-child').css({ 'border-bottom-left-radius': '7px', 'border-bottom-right-radius': '7px' });
    $('.topMenuSubCMSListMenuUL > LI:last-child a,.topMenuSubCMSListMenuUL > LI:last-child span').css({ 'border-bottom-left-radius': '8px', 'border-bottom-right-radius': '8px' });
    $('.topMenuSubCMSListMenuLI').add('.topMenuSubCMSListMenuHighlightedLI').hover(function () {
        //Retrieve the index of the highlighted listitem
        var listItemIndex = $(this).children('ul').parent().index();
        var topMenuSubItemCount = $(this).parent().children('LI').size();
        $(this).find('li').each(function (index) {
            var topMenuSubSubItemCount = $(this).siblings('LI').size();
            if (listItemIndex + index >= topMenuSubItemCount - 1) {
                //Add rounded corner only if it is the last listItem
                if (index == topMenuSubSubItemCount) {
                    $(this).css({ 'border-bottom-left-radius': '7px', 'border-bottom-right-radius': '7px' });
                }
            }
        });
    });
    $('.topMenuCMSListMenuLI').add('.topMenuCMSListMenuHighlightedLI').has('.topMenuSubCMSListMenuUL').each(function (index) {
        $(this).find('.topMenuCMSListMenuLink,.topMenuCMSListMenuLinkHighlighted').addClass('topMenuBracket');
    });
    $('.topMenuSubCMSListMenuLI').add('.topMenuSubCMSListMenuHighlightedLI').has('.topMenuSubSubCMSListMenuUL').each(function (index) {
        $(this).find('.topMenuSubCMSListMenuLink,.topMenuSubCMSListMenuLinkHighlighted').addClass('subMenuBracket');
        $(this).parent().width($(this).parent().width() + 15);
    });

    $('.topMenuCMSListMenuLI,.topMenuCMSListMenuHighlightedLI').hover(function () {
        if ($('.topMenuSubCMSListMenuUL', this).is(':animated')) {
            $('.topMenuSubCMSListMenuUL', this).stop(true, true).animate({ height: 'toggle' }, {
                queue: false,
                duration: 0
            });
        }
        else {
            $('.topMenuSubCMSListMenuUL', this).stop(true, true).animate({ height: 'toggle' }, {
                queue: false,
                duration: 400
            });
        }
    },
    function () {
        if ($('.topMenuSubCMSListMenuUL', this).is(':animated')) {
            $('.topMenuSubCMSListMenuUL', this).stop(true, true).animate({ height: 'toggle' }, {
                queue: false,
                duration: 0
            });
        }
        else {
            $('.topMenuSubCMSListMenuUL', this).stop(true, true).animate({ height: 'toggle' }, {
                queue: false,
                duration: 200
            });
        }
    });

    $('.topMenuSubCMSListMenuLI,.topMenuSubCMSListMenuHighlightedLI').hover(function () {
        $('.topMenuSubSubCMSListMenuUL').each(function (index) {
            var parentLIwidth = $(this).parent().width();
            $(this).css('left', parentLIwidth - 1);
            $(this).css('top', $(this).parent().position().top);
        });
        if ($('.topMenuSubSubCMSListMenuUL', this).is(':animated')) {
            $('.topMenuSubSubCMSListMenuUL', this).stop(true, true).animate({ width: 'toggle' }, {
                queue: false,
                duration: 0
            });
        }
        else {
            $('.topMenuSubSubCMSListMenuUL', this).stop(true, true).animate({ width: 'toggle' }, {
                queue: false,
                duration: 400
            });
        }
    },
    function () {
        if ($('.topMenuSubSubCMSListMenuUL', this).is(':animated')) {
            $('.topMenuSubSubCMSListMenuUL', this).stop(true, true).animate({ width: 'toggle' }, {
                queue: false,
                duration: 0
            });
        }
        else {
            $('.topMenuSubSubCMSListMenuUL', this).stop(true, true).animate({ width: 'toggle' }, {
                queue: false,
                duration: 200
            });
        }
    });
});
