/**
 * The Internet Explorer version, or -1 for a different browser.
 *
 * This has a fair amount of redundancy (??), wants fixing.
 */
var ieVersion;

/**
 * Return the Internet Explorer version.
 */
function getInternetExplorerVersion() {
  // see if we've done this before:
  if (ieVersion !== undefined) {
    return ieVersion;
  }
  ieVersion = -1; // Return value assumes failure.
  if (navigator.appName == 'Microsoft Internet Explorer') {
    var ua = navigator.userAgent;
    var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
    if (re.exec(ua) != null) {
      ieVersion = parseFloat(RegExp.$1);
    }
  }
  return ieVersion;
}

/**
 * Add button mouseover and mouseout styles - this must match CSS.
 */
function addButtonStyles(buttons) {
  var buttonStyle = {
    'color': '#00bef3',
    'background-color': '#cbf2ff',
    'border': 'solid 1px #00bef3',
    'font-size': '15px',
    'cursor': 'normal'
  };
  var buttonHoverStyle = {
    'color': '#f738aa',
    'background-color': '#fcd7e5',
    'border-color': '#f738aa',
    'cursor': 'pointer'
  };
  buttons.css(buttonStyle).mouseover(function() {
    $(this).css(buttonHoverStyle);
  }).mouseout(function() {
    $(this).css(buttonStyle);
  });
}

/**
 * Fix IE button styles.
 */
function setIEButtonStyles() {
  if (getInternetExplorerVersion() >= 6) {
    addButtonStyles($('input[type=button]').add('input[type=submit]'));
  }
}

$(setIEButtonStyles);


/**
 * Fix primary nav menu on Ubuntu/Debian.
 */
function fixMenu() {
  var menuHeight;
  var fontSize = parseInt($('#primary-nav td.primary-menu-item').css('font-size'), 10);
  // reduce font size until the menu renders properly:
  do {
    menuHeight = $('#primary-nav td.primary-menu-item a').eq(2).attr('offsetHeight');
    //alert(menuHeight);
    if (menuHeight > 30) {
      fontSize--;
      $('#primary-nav td.primary-menu-item').css('font-size', fontSize + 'px');
    }
  } while (menuHeight > 30);
}

$(fixMenu);

