﻿var menuTimer = 0;
var menu = 0;
var menuParent = 0;

function closeMenuTimer() {
    if (menu) {
        $(menu).css('visibility', 'hidden');
        menu = 0;
    }
    
    if (menuParent) {
        $(menuParent).removeClass('headerMouseOver');
        menuParent = 0;
    }
}

function cancelTimer() {
    menu = 0;
}

function closeMenu(oldMenuParent, oldMenu) {
    menu = oldMenu;
    menuParent = oldMenuParent;
    menuTimer = setTimeout(closeMenuTimer, 500);
}

function loadNavbar() {
    var data = "method=navbar";
    if (window.location.href.indexOf('/Admin/EditMenu.aspx') > 0) {
        data += "&edit=true";
    }
    
    $.ajax({ type: "GET",
             url: "/PTO/PublicAjax.ashx",
             cache: false,
             dataType: "html",
             data: data,
             success: function(msg){
                $('#nav_bar').remove();
                $('.MenuHeaderContainer').append(msg);
                menuInit();
             }
    });
}

function showMenu(newMenuParent, newMenu) {
    if (menuTimer) {
        clearTimeout(menuTimer);
        menuTimer = 0;
    }
            
    if (menu) {
        $(menu).css('visibility', 'hidden');
    }
    if (menuParent) {
        $(menuParent).removeClass('headerMouseOver');
    }
    
    $(newMenu).css('visibility', 'visible');
    //$(newMenu).css('display', 'block');
    $(newMenuParent).addClass('headerMouseOver');
    menu = newMenu;
    menuParent = newMenuParent;
}

function adjustDropDownMenuWidth() {
    $('#nav_bar dropdownmenu').each(function() {
        if ($(this).width() < 200) {
            $(this).width(200);
        }
    });
}

var max_width = 0;

function adjustMenuWidth(e) {
    $(e).children().each(function() {
        var text = $('a:first', $(this)).text();
        $('#hiddenSpan').html(text);
        var width = $('#hiddenSpan').outerWidth();
        if (width > max_width) {
            max_width = width;
        }        
    });        
    
    $(e).width(parseInt(parseInt(max_width) + 35));
    max_width = 0;
}

function adjustNavBarWidth() {
    var width = 0;
    $('#menu').children().each(function() {
        width += $(this).outerWidth();        
    });
    $('#nav_bar').width(width);
}

function moveMenuUp(id) {
    var data = 'method=moveUp' + 
                   '&menuId=' + id;
        
    $.ajax({
        type: "POST",
        cache: false,
        url: "TestHandler.ashx",
        data: data,
        success: function(msg){                
        }
    });
}

function moveMenuDown(id) {
    var data = 'method=moveDown' + 
                   '&menuId=' + id;
        
    $.ajax({
        type: "POST",
        cache: false,
        url: "TestHandler.ashx",
        data: data,
        success: function(msg){                
        }
    });
}

function newItem(id) {
    var data = 'method=newItem' + 
                   '&menuId=' + id;
        
    $.ajax({
        type: "POST",
        cache: false,
        url: "TestHandler.ashx",
        data: data,
        success: function(msg){
            $(msg).find('result').each(function() {
                //var parent = $(this).attr('parent');
                var newMenuId = $(this).attr('newMenuId');
                var currentMenu = $('#' + $(this).attr('currentMenuId'));
                
                //alert("Parent: " + parent + " current: " + currentMenuId + " new: " + newMenuId);
                
                if (currentMenu.hasClass('top')) {
                    var newElement = "<li class='top_row'><a href='#nogo' id='" + newMenuId + "' class='top tag'>New Item</a><ul class='dropdownmenu' style='width: 81px;'><li><a href='#' class='tag'>Empty</a>";
                }
                else {
                    var newElement = "<li><a href='#nogo' id='" + newMenuId + "' class='tag'>New Item</a><ul class='dropdownmenu' style='width: 81px;'><li><a href='#' class='tag'>Empty</a>";
                }
                var newItem = $(newElement);
                newItem.insertAfter(currentMenu.parent());
                adjustNavBarWidth()
            });
        }
    });
}

function newSubmenu(id) {
    var data = 'method=newSubmenu' + 
               '&menuId=' + id;
        
    $.ajax({
        type: "POST",
        cache: false,
        url: "TestHandler.ashx",
        data: data,
        success: function(msg){
            $(msg).find('result').each(function() {
                var newMenuId = $(this).attr('newMenuId');
                var currentMenuId = $(this).attr('currentMenuId');
                
                var menu = $('#' + currentMenuId);
                var ul = menu.next();
                
                if (menu.hasClass('top')) {
                    ul.css('visibility', 'visible');    
                }
                else {                
                    menu.addClass('tag_sidebar');                    
                    ul.removeClass('dropdownmenu');
                    ul.addClass('sidebar_menu');                    
                    ul.css({ 'width' : '116px', 'left' : '188px', 'visibility' : 'visible' });
                }               
                
                $('a', ul).text('New Item');
            });
        }
    });
}

var mouseX = 0;
var mouseY = 0;
var curObj = 0;

function showTypeData(type, link) {
  
    if (link == undefined) {
        link = 0;
    }

    switch(type) {
        default:
        case '0':
            $('#menuPages').css('display','none');
            $('#menuControls').css('display','none');
            $('#menuUrl').css('display','none');
            break;
        case '1':
            $('#menuControls').css('display','none');
            $('#menuUrl').css('display','none');
            $('#menuPages').css('display', 'block');

            $('#menuPagesBox').ptoListBox({ data: "page", 
                                            selected: link,
                                            rowClass:"lbRow", 
                                            rowSelectedClass: 'lbRowSelected',
                                            rowOverClass: 'lbRowMouse'});
            
            break;
        case '2':
            $('#menuPages').css('display','none');
            $('#menuUrl').css('display','none');
            $('#menuControls').css('display','block');
            $('#menuControlsBox').ptoListBox({ data: "control", 
                                            selected: link,
                                            rowClass:"lbRow", 
                                            rowSelectedClass: 'lbRowSelected',
                                            rowOverClass: 'lbRowMouse'});
            
            break;
        case '3':
            if (link != 0) {
                $('#menuUrlBox').val(link);
            }
            $('#menuPages').css('display','none');
            $('#menuControls').css('display','none');
            $('#menuUrl').css('display','block');
            break;
    }
}

function deleteMenuItem() {

    var id = $('#currentEditItem').val();
    var data = 'method=deleteMenuItem' + 
                   '&menuId=' + id;
        
    $.ajax({
        type: "POST",
        cache: false,
        url: "TestHandler.ashx",
        data: data,
        success: function(msg){
            $(msg).find('result').each(function() {
                var deletedMenuId = $(this).attr('deletedMenuId');
                $('#' + deletedMenuId).parent().remove();
            });
        }
    });
    
    $(this).dialog('close');
    $('#editDialog').dialog('close');
}

    function editSave() {
        
        var itemGuid = $('#currentEditItem').val();
        
        var linkType = $('#menuType').val();
        var link = 0;
        
        switch (linkType) {
            case '0':
                break;
            case '1':
                 link = $('#menuPagesBox .lbRowSelected').attr('id').split('_')[1];
                break;
            case '2':
                link = $('#menuControlsBox .lbRowSelected').attr('id').split('_')[1];
                break;
            case '3':
                link = $('#menuUrlBox').val();
                break;
            default:
                alert('Invalid selection');
                return;
        }
        
        var data = 'method=updateNavbarItem' + 
                   '&menuId=' + itemGuid +
                   '&name=' + $('#menuName').val() + 
                   '&desc=' + $('#menuDescription').val() +                    
                   '&linkType=' + linkType + 
                   '&link=' + link;
        
        $('#' + itemGuid).text($('#menuName').val());
        $('#editDialog').dialog('option', 'title', $('#menuName').val());
        
        $.ajax({
            type: "POST",
            cache: false,
            url: "TestHandler.ashx",
            data: data,
            success: function(msg){                
            }
        });
        
        $(this).dialog('close');
    }
                              


    
$(document).ready(function() {
    
    loadNavbar();
    
    $('#confirmDelete').dialog ({ autoOpen: false,
                                  modal: true,
                                  title: "Confirm Delete",
                                  buttons: { "OK": deleteMenuItem,
                                             "Cancel": function() { $(this).dialog('close'); } },
                                  width: 450 });
    
    $('#editItemDialog').dialog( { autoOpen: false,
                                   modal: true,
                                   buttons: { "Save": editSave,
                                              "Cancel": function() { $(this).dialog('close'); } },
                                   width: 450 });

    
    $('#editDialog').dialog({ autoOpen: false,                              
                              modal: true,
                              beforeclose: function() { loadNavbar(); } });
                              
    $('#menuType').change(function() {
        showTypeData($(this).val());
        
    });
    
    $('#editDialog a').mouseover(function() {
        $(this).css('color', '#ff953d');
    });
    
    $('#editDialog a').mouseout(function() {
        $(this).css('color', '');
    });                       
    
    
    ///////
    
    $('#moveUp').click(function() {
        var id = $('#currentEditItem').val();
        var parent = $('#' + id).parent();
        var prev = parent.prev();
        if (prev.length) {
            moveMenuUp(id);
            parent.swap(prev);
        }
    });
    
    $('#moveDown').click(function() {
        var id = $('#currentEditItem').val();
        var parent = $('#' + id).parent();
        var next = parent.next();
        if (next.length) {
            moveMenuDown(id);
            parent.swap(next);
        }
    });
    
    $('#deleteItem').click(function() { 
        $('#confirmDelete span').text($('#' + $('#currentEditItem').val()).text());
        $('#confirmDelete').dialog('open');
    });
    
    $('#newItem').click(function() {
        var id = $('#currentEditItem').val();
        newItem(id);        
    });
    
    $('#newSubmenu').click(function() {
        var id = $('#currentEditItem').val();
        newSubmenu(id);        
    });
        
    $('#clickEditLink').click(function() {
        
        var id = $('#currentEditItem').val();
                     
        $('#menuPages').css('display','none');
        $('#menuControls').css('display','none');
        $('#menuUrl').css('display','none');
        $('#editItemDialog').dialog('option', 'title', "Edit " + $('#editDialog').dialog('option', 'title'));
        $('#editItemDialog').dialog('open');
        
        var data = "method=navbarItems";
        $.ajax({
            type: "GET",
            url: "TestHandler.ashx",
            cache: false,
            dataType: "xml",
            data: data,
            success: function(msg){
                //alert('got data');
                $(msg.documentElement).find('MenuItem').each(function() {
                    if ($(this).attr('id') == id) {
                        var item = $(this);
                        $('#menuName').val(item.children('name').text());
                        $('#menuDescription').val(item.children('description').text());
                        $('#menuType').val(item.children('linkTypeId').text());
                        showTypeData(item.children('linkTypeId').text(), item.children('link').text());
                    }                      
                });                      
            }
        });
    });
});

function menuInit() {

    $('#moveUp').html("Move Up");
    $('#moveDown').html("Move Down");
    
    $('#nav_bar .dropdown').each(function() {
        var text = $(this).text();
        $('#hiddenSpan').html(text);
        $(this).width($('#hiddenSpan').outerWidth() + 1);
    });
    
    $('#nav_bar .no_dropdown').each(function() {
        var text = $(this).text();
        $('#hiddenSpan').html(text);
        $(this).width($('#hiddenSpan').outerWidth());
    });
    
    $('#nav_bar .no_dropdown').mouseover(function() {
        closeMenuTimer(); 
        $(this).addClass('headerMouseOver');
    });
    
    $('#nav_bar .no_dropdown').mouseout(function() {
        $(this).removeClass('headerMouseOver');
    });
    
    adjustNavBarWidth();
    
    $('#nav_bar .sidebar_menu').each(function() {
        adjustMenuWidth($(this));
    });
    
    $('#nav_bar .dropdownmenu').each(function() {
        adjustMenuWidth($(this));
    });
    
    $('#nav_bar .sidebar_menu').each(function() {
        $(this).css('left', $(this).parent().outerWidth());
        
        $(this).parent().mouseover(function() { 
            $('ul:first', $(this)).css('visibility','visible');
        });
        
        $(this).parent().mouseout(function() { 
            var ed = $('#editDialog');
            if (!ed.length || ed.dialog('isOpen') == false) {
                $('ul:first', $(this)).css('visibility','hidden');
            }
        });        
    });    
    
    $('#nav_bar .dropdown').mouseover(function() {
        showMenu($(this), $(this).next());
    });
    
    $('#nav_bar .dropdown').mouseout(function() {            
        closeMenu($(this), $(this).next());
    });
    
    $('#nav_bar .dropdownmenu').mouseover(function() {
        showMenu($(this).parent(), $(this));
    });
    
    $('#nav_bar .dropdownmenu').mouseout(function() {
        var ed = $('#editDialog');
        if (!ed.length || ed.dialog('isOpen') == false) {
            closeMenu($(this).parent(), $(this));
        }
    });
    
    /////////////////////////////////////
    
    $('#nav_bar a').click(function() {
    
        if ($(this).hasClass('top')) {
            $('#moveUp').html("Move Left");
            $('#moveDown').html("Move Right");
        }
        
        $('#currentEditItem').val($(this).attr('id'));
        
        $('#' + $(this).attr('id')).css({'background-color' : '#c28452',
                                         'color' : '#fff' });        
        
        $('#editDialog').dialog('option', 'title', $(this).text());        
        $('#editDialog').dialog('open');
        
        $('#newSubmenu').css('display','inline');
        if ($(this).hasClass('tag_sidebar')) {
            $('#newSubmenu').css('display','none');
        }
    });
    
    adjustDropDownMenuWidth();
    
    $('#nav_bar .top').each(function() {
        if ($(this).hasClass('dropdown')) {
            $(this).css('position','relative');
            var downArrow = $("<img class='down_arrow' src='/PTO/Graphics/drop_down.png' />");
            $(this).append(downArrow);
            var width = $(this).parent().width() - 17;
            downArrow.css({'top' : '9px', 'left' : width + 'px' });
        }
    }); 
}    
