﻿// Firefly
// new code for dropdown menus
// uses jquery

function setupDynamicMenu(site_id) {

    $(function() {

        var menutimer;
        $('ul.sections-menu>li').hover(function() {
            if (menutimer) clearTimeout(menutimer);
            $('ul.sections-submenu', this.parentNode).css('visibility', 'hidden');
            // mark header selected
            $('>li>a', this.parentNode).removeClass('selected');
            $('>a', this).addClass('selected');
            // show submenu
            $('ul.sections-submenu', this).css('visibility', 'visible');
        }, function() {
            var me = this;
            menutimer = setTimeout(function() {
                // hide submenu
                $('ul', me).css('visibility', 'hidden');
                // restore originally selected header
                $('a.section-toplevel').removeClass('selected');
                $('a.section-toplevel.originalselected').addClass('selected');
            }, 500);
        }).children('a.section-menuonly').click(function() {
            return false;
        });

        $('ul.sections-submenu.editable:not(.section-workspacecontainer)').sortable({
            items: 'li.section',
            update: function() {
                var parent_id = this.id.substring(9);
                $.ajax({
                    url: "Admin/sectionsReorder.aspx",
                    dataType: 'script',
                    type: "POST",
                    data: 'move=ajax&parent_id=' + parent_id + '&site_id=' + site_id + '&' + $(this).sortable("serialize"),
                    error: function() { alert('Error ordering sections.  Check your internet connection and try refreshing the page.'); }
                });
            }
        }).mousedown(function(e) {
            // for IE compatibility
            e.stopPropagation();
        });
        $('ul.sections-menu.editable').sortable({
            items: 'li.section-toplevel',
            update: function() {
                $.ajax({
                    url: "Admin/sectionsReorder.aspx",
                    dataType: 'script',
                    type: "POST",
                    handle: ">li",
                    data: 'move=ajax&parent_id=-1&site_id=' + site_id + '&' + $(this).sortable("serialize"),
                    error: function() { alert('Error ordering sections.  Check your internet connection and try refreshing the page.'); }
                });
            }
        });
        // z-index fix for IE (containers must have high z-index as well)
        var submenuIndex = ($('ul.sections-submenu').css('zIndex'));
        $('ul.sections-menu').each(function() {
            var parent = this.parentNode;
            var count = 0;
            while (parent && (count < 50) && (parent.tagName.toUpperCase() != 'BODY')) {
                $(parent).css('zIndex', submenuIndex);
                parent = parent.parentNode;
                count++;
            }
        });
    });
}
