/**
  * ieSucks.js
  * Yes, it's true.  IE does suck.  This file creates an IESucks object, stored
  * globally as window.ieSucks with all sorts of nifty functions and properties
  * to handle IE's oddities.
  **/
window.ieSucks = new Object();

// isIE
// RETURN: bool if browser is IE or not
window.ieSucks.isIE = function(){
  return navigator.userAgent.indexOf("MSIE") != -1;
}

// isIE6
// RETURN: true if browser is IE 6
window.ieSucks.isIE6 = function(){
  return navigator.userAgent.indexOf("MSIE 6") != -1;
}

// fixOverflowX
// IE has the annoying habit of not obeying overflow properties and will instead
// stretch container elements to fit the content.  This function will set the
// offending element's width to a fixed amount of pixels.  In order to do so,
// it requires the containing element and the offender
// container - containing element, specified as id name or a DOM object
// offender - offending element, specified as id name or a DOM object
window.ieSucks.fixOverflowX = function(container, offender){
  if(!this.isIE()){
    return; // Don't waste time if this isn't IE
  }
  try{
    container = htmlDOM.getElementById(container);
    offender = htmlDOM.getElementById(offender);
    if(!container || !offender){
      return; // Not valid objects
    }
    // Save some properties of the offender
    offender.style.width = ""; // Clear the width so we get "true" dimensions
    var origOffsetLeft = offender.offsetLeft;
    var origOffsetWidth = offender.offsetWidth;
    var origMarginRight = container.clientWidth - offender.offsetLeft
                          - offender.offsetWidth;
    // Now hide the offender so it stops distorting our page
    offender.style.display = "none";
    // Determine if resizing is necessary
    var needed = origOffsetLeft + origOffsetWidth + origMarginRight;
    // IE 6 needs extra spacing - not sure why
    if( container.clientWidth >= needed ){
      offender.style.display = "";
      offender.style.overflow = "";
      offender.style.overflowX = "";
      return; // No resize
    }
    // If we're still here we need to resize
    var setWidth = container.clientWidth - origOffsetLeft - origMarginRight;
    offender.style.width = setWidth + "px";
    offender.style.display = "";
    // Determine if we need to turn on horizontal scrolling
    if(offender.scrollWidth > offender.clientWidth){
      offender.style.overflowX = "scroll";
    }
  }catch(e){
    alert("Error: ieSucks: fixOverflowX");
  }
  return;
}