﻿var _Menu_HideTimer = null;
var _Menu_Heading = null;

function Menu_Hide(immediate) {
  if (_Menu_HideTimer) { window.clearTimeout(_Menu_HideTimer); }
  
  if (_Menu_Heading) {
    $("ul.main-menu li.menu-heading, ul.filter-panels-container > li[class!=disabled]").removeClass("hover").children("div").hide();
    _Menu_Heading = null;
  }
}

$(function(){
  if ($.browser.mozilla) {
    // Fix firefox's stupid button alignment.
    $("button").css("padding-bottom", "3px");
    $("button > span").css("margin-top", "-1px").css("margin-left", "-1px");
  }
  
  // Hover effects for main menu.
  $("ul.main-menu").disableSelection();
  
  $("ul.main-menu li.menu-heading").hover(
    function(){
      Menu_Hide(true);
      $(this).addClass("hover").children("div").show();
    },
    function(){
      _Menu_Heading = $(this);
      _MenuHideTimer = window.setTimeout("Menu_Hide()", 1000);
    }
  );
  
  $(document).click(function(e){
    //if ($(e.target).closest("ul.main-menu").length == 0) {
      Menu_Hide();
    //}
  });

  // Hover effects for sub-headings of main menu.
  $("div.menu-panel li.sub-heading").hover(
    function(){
      $(this).children("div").show();
    },
    function(){
      $(this).children("div").hide();
    }
  );
  
  // Hover effects for filter panels.
  $("ul.filter-panels-container").disableSelection();
  
  $("ul.filter-panels-container > li[class!=disabled]").click(
    function(){
      Menu_Hide();
      $(this).addClass("hover");
      $(this).children("div").show();
    }
  ).hover(
    function(){
      //
    },
    function(){
      _Menu_Heading = $(this);
      //_MenuHideTimer = window.setTimeout("Menu_Hide()", 500);
    }
  );
  
  // Click handling for filter panels.
  $("a.filter-option").click(function(){
    opt = $(this);
    list = opt.closest("ul").attr("rel");
        
    switch (list) {
      case "Columns" :
        $("ul.main-menu li.menu-heading, ul.filter-panels-container > li[class!=disabled]").removeClass("hover").children("div").hide();
        SelectRowColumns(opt);
        break;
      case "Lot" :
        opt.closest("div").hide();
        gm("xWorkingLotID").value = opt.attr("rel");
        FreezePage("Setting Working Lot...");
        ActionSubmit("NewLot");
        break;
      default :
        opt.closest("div").hide();
        gm("xFilterKey").value = list;
        gm("xFilterValue").value = opt.attr("rel");
        FreezePage("Updating filter...");
        ActionSubmit("NewFilter");
        break;
    }
  });
  
  var tabsId = $("div.ui-tabs").attr("id");
  var selTab = 0;
  if (tabsId) {
    selTab = $.cookie(tabsId + "-selected");
    if (!selTab) { selTab = 0; }
  }
  
  // Display page tabs, which start hidden to avoid showing unstyled content.
  $("div.ui-tabs").tabs({
    select :            function(event, ui) {
                          var url = $.data(ui.tab, "load.tabs");
                          if (url) {
                            SmartClick(url);
                            return false;
                          }
                          return true;
                        },
    selected :          selTab,
    show :              function(event, ui) {
                          if (tabsId) {
                            $.cookie(tabsId + "-selected", $("div.ui-tabs").tabs("option", "selected"));
                          }
                        }
  }).show();
  
  // Display accordions, which start hidden to avoid showing unstyled content.
  $("div.accordion-container").accordion({
    active :            false,
    animated :          false,
    autoHeight :        false,
    collapsible :       true,
    header :            "h4",
    changestart :       function(event, ui){ 
                          var anchor = ui.newHeader.find("a");
                          if (anchor.attr("href") != "#") {
                            ui.newContent.load(anchor.attr("href"));
                            anchor.attr("href", "#");
                          }
    }
  }).show();
  
  // Show first accordion element for forms.
  $("div.accordion-container-fieldset").accordion("activate", 0);
  
  if (!_NoEdit) {
    // Enable sortable behavior for lists.
    $("ul.sortable-container, table.portlet-container").sortable({
      addClasses :        true,
      containment :       "parent",
      cursor :            "move",
      distance :          3,
      forceHelperSize :   true,
      forcePlaceholderSize : true,
      scroll :            true,
      scrollSensitivity : 50,
      scrollSpeed :       25,
      tolerance :         "pointer",
      update :            function(){ SetFieldChanged(); }
    });
    //.disableSelection();

    $(".sortable-selection").sortable("option", "connectWith", ".sortable-selection").sortable("option", "containment", ".sortable-selection-container");
  }
});

