﻿var _Menu_HideTimer = null;
var _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();
      $("object").addClass("menu-hide");
      $(this).addClass("hover");
      $(this).children("div").show();
    },
    function(){
      _Menu_Heading = $(this);
      _MenuHideTimer = window.setTimeout("Menu_Hide()", 500);
    }
  );

  // 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();
      $("object").addClass("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;
    }
  });
  
  // Show filter bar if it contains something.
  if (_IsMasterPage) {
    if ($("#divFilterBar").html().trim() != "") {
      $("#divFilterBar").show();
    }
  };
  
  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"
	}).show();
	
	// Show first accordion element for forms.
	$("div.accordion-container-fieldset").accordion("activate", 0);
	
	// 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,
	  scroll :            true,
	  tolerance :         "pointer",
	  update :            function(){ SetFieldChanged(); }
	}).disableSelection();

  $(".sortable-selection").sortable("option", "connectWith", ".sortable-selection").sortable("option", "containment", ".sortable-selection-container");
});

function Menu_Hide(menu) {
  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;
  }
  
  $("object").removeClass("menu-hide");
}