/**
 * This is responsible for resizing a center area as the user expands/contracts a page.
 *
 * @param customFunction a function that you want called with the new height, the new width and the element that will be resized.
 */
UIB.resizer.Resizer = function(elemsToResize, elemsToTakeIntoAccount, extraHeight, extraWidth, customFunction)
{
    this.init(elemsToResize, elemsToTakeIntoAccount, extraHeight, extraWidth, customFunction);
};

UIB.resizer.Resizer.prototype = {
  // This will make sure that if multiple resize requests come in, that only 1 per second is obeyed.
  askToResizeContent: function()
  {
    now = (new Date()).getTime();
    if((now - this.lastResizeTime) > 1000)
    {
      // Call it directly - no need to wait since it's been a while
      this.resizeContent();
    } else if((now - this.nextResizeTime) > 0)
    {
      // We need to resize in a second
      var thisResizer = this;
      new PeriodicalExecuter(function(pe) { pe.stop(); thisResizer.resizeContent(); }, 1);
      this.nextResizeTime = now + 1000;
    }
  },

  resizeContent: function()
  {
      this.lastResizeTime = (new Date()).getTime();

      // Figure out how much height is remaining after we subtract the existing elements on the page.
      var heightRemaining = YAHOO.util.Dom.getClientHeight();
      var footerHeight = $('footer') == null ? 0 : $('footer').clientHeight;
      heightRemaining -= $('header').clientHeight + footerHeight;
      heightRemaining -= this.extraHeight;

      this.elemsToTakeIntoAccount.each(function(elementParameter) {
          var elem = $(elementParameter);
          if(elem)
          {
              var height = elem.offsetHeight == 0 ? elem.clientHeight : elem.offsetHeight;
              YAHOO.log("Height of " + elem.id + " is " + height + "px", "debug", "resizer");
              heightRemaining -= height;
          }
      });

      if(this.extraWidth)
      {
          var widthRemaining = YAHOO.util.Dom.getClientWidth();
          widthRemaining -= this.extraWidth;
      }
    
      var resizer = this;
      this.elemsToResize.each(function(elemToResize) {
          if($(elemToResize))
          {
              YAHOO.log("Client Height = " + YAHOO.util.Dom.getClientHeight()
                      + ", header height = " + $('header').clientHeight
                      + ", footer height = " + footerHeight
                      + ". Resizing " + $(elemToResize).id  + " to " + heightRemaining + "x" + (resizer.extraWidth ? widthRemaining : 0) + "px.", "debug", "resizer");

              $(elemToResize).style.height = heightRemaining + "px";
              if(resizer.extraWidth)
              {
                  $(elemToResize).style.width = widthRemaining + "px";
              }

              if(resizer.customFunction)
              {
                  resizer.customFunction(widthRemaining, heightRemaining, elemToResize);
              }
          }
      });
  }
};

UIB.resizer.Resizer.prototype.init = function(elemsToResize, elemsToTakeIntoAccount, extraHeight, extraWidth, customFunction)
{
    this.elemsToResize = $A(elemsToResize);
    this.elemsToTakeIntoAccount = $A(elemsToTakeIntoAccount);
    this.extraHeight = extraHeight;
    this.extraWidth = extraWidth;
    this.nextResizeTime = 0;
    this.lastResizeTime = 0;
    this.customFunction = customFunction;


    this.askToResizeContent();
    YAHOO.util.Event.addListener(self, "resize", this.askToResizeContent, this, true);
};


