var submenu = {};

// show menu
submenu.show = function(self,left,e)
{
  var self = $(self);

  this.hide_stop();

  // fetch menu wrapper and menu items
  var menu = $('#'+self.parent().attr('class')+'_submenu');
  var items = $('#'+self.parent().attr('class')+'_submenu li');

  // if we're already hovering over a menu hide the other
  if(this.active)
  {
    // don't hide if it is the same one
    if(this.active.attr('id') == menu.attr('id')) return true;
    this.hide();
  }
  // store active menu and menu button
  this.active = menu;
  this.button = self;
  
  // get position of menubar positioning submenu
  var bar = $('#menu').offset();

  // position menu wrapper
  menu.css({marginTop:-15,paddingLeft:left});

  // set up menu items hover to keep menu open
  // this is a bit hack but IE won't respect mouseover/out on the wrapper
  $('.t').hover(function(){submenu.hide_stop();},function(){submenu.hide_schedule();});
  items.hover(function(){submenu.hide_stop();},function(){submenu.hide_schedule();});
  
  // sticky menu button as hovered
  this.button.addClass('active');
  
  if(document.all) $('#sortby').hide();
  menu.show();

  return false;
};

// hide menu and remove active class from button
submenu.hide = function(e)
{
  if(!this.active) return;
  this.active.hide();
  this.active = false;
  this.button.removeClass('active');
  this.button = false;
  if(document.all) $('#sortby').show();
}

// schedule a hide for .75 seconds from now
submenu.hide_schedule = function()
{
  clearTimeout(this.hidetimer);
  if(this.active) this.hidetimer = setTimeout(function(){submenu.hide()},750);
};

// stop the hide operation if we're hovering over a menu item
submenu.hide_stop = function() { clearTimeout(this.hidetimer); }

submenu.showtext = function(ob)
{
  var parent = $(ob).parents('.submenu').attr('id');
  $('.over').removeClass('over');
  $(ob).addClass('over');
  $('#'+parent+' .subtext').html($(ob).children('.hide').html());
}

// close this if we click anywhere
$(document).ready(function(){$(document).bind('mouseup',function(){submenu.hide()});});
