/* enable dropdown menu for IE6 and all others with tab key acess
 * very simple but efficient hack by Yuwei Yu <acevery@gmail.com> */
;(function($) { /* $ will refer to jQuery within this closure */
    /* over and out is for ul#topnav>li.hover function.
     * we don't need any delay here. */
    over = function () {
      $(this).children (":not(.li_con)").css ("display", "block");
      $(this).find ("div.li_bg").css ("display", "block");
    }
    out = function () {
      $(this).children (":not(.li_con)").css ("display", "none");
      $(this).find ("div.li_bg").css ("display", "none");
    }
    /* miliseconds before changing visibility.
     * with this, user can tab throught the submenu */
    var delay = 10;
    /* nav_over and nav_out is for ul#topnav>li.focusin and
     * ul#topnav>li.focusout respectly.
     * When we tab from topnav>li -> subnav>li, the topnav>li first emit
     * the focusout or blur event which will hide the dropdown menu, and
     * make the subnav>li failed to be focusin.
     * Thus, we need to set a little timeout before we actually hide the
     * dropdown menu, so we can tab to the subnav>li.
     * If we find that we still in the same dropdown menu, we can just
     * cancel the last hiding function call. 
     * This is what setTimeout/clearTimeout just do  */
    nav_over = function () {
      /* first cancel the last unapplied changing */
      clearTimeout (this.dropTimer);
      var $$ = $(this);
      this.dropTimer = setTimeout (function () {
        $$.children (":not(.li_con)").css ("display", "block");
        $$.find ("div.li_bg").css ("display", "block");
      }, delay);
    }
    nav_out = function () {
      clearTimeout (this.dropTimer);
      var $$ = $(this);
      this.dropTimer = setTimeout (function () {
        $$.children (":not(.li_con)").css ("display", "none");
        $$.find ("div.li_bg").css ("display", "none");
      }, delay);
    }
    
     /* now link the eventHandlers to the right elements  */
    $("a.topnav_a").each (function () {
      /* for the li of topnav */
      if ($(this).parents ("ul#topnav>li").has("div#dropdown-news").length == 1) {
        return;
      }
      $(this).parents ("ul#topnav>li").hover (over, out);
      $(this).parents ("ul#topnav>li").focusin (nav_over).focusout (nav_out);
    });
    if ($.browser.msie && $.browser.version.substr(0,1)<7) {
      $("div#main_animation").css("margin", "-0.1875em auto 0.75em auto");
      $("div.li_bg").each (function () {
          $(this).html("<img src=\""+filePath+"site-media/images/li_bg_ie6.png\" alt=\"li background\" class=\"bg\" />");
          var bgcss = {
            "width": "80%",
            "margin": "0 10%"
          }
          $(this).css(bgcss);

      });
    }
})(jQuery);

