// ==============================================================================
// = vertical.fill.js -- automatic element resizing to fit a confined space     =
// = ----------------------------------------------------------------------     =
// = Copyright 2007 Dan Drinkard, http://displayawesome.com                     =
// = Download: http://displayawesome.com/resources/js/vertical.fill.js          =
// = CREDIT: GetElementsByClassName taken from Robert Nyman's                   =
// ="Ultimate getElementsByClassName,"                                          =
// = http://www.robertnyman.com/2005/11/07/the-ultimate-getelementsbyclassname/ =
// =                                                                            =
// = This program is free software: you can redistribute it and/or modify       =
// = it under the terms of the GNU General Public License as published by       =
// = the Free Software Foundation, either version 3 of the License, or          =
// = (at your option) any later version.                                        =
// =                                                                            =
// = This program is distributed in the hope that it will be useful,            =
// = but WITHOUT ANY WARRANTY; without even the implied warranty of             =
// = MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              =
// = GNU General Public License for more details.                               =
// =                                                                            =
// = You should have received a copy of the GNU General Public License          =
// = along with this program.  If not, see <http://www.gnu.org/licenses/>.      =
// =                                                                            =
// ==============================================================================

//  Configuration

//  Font Size: The starting point. If your space is huge and you typically have
//  a small amount of text, start with a bigger number to reduce cycles, etc.
var vf_fontSize     = 1.6;

//  Units: px, em, %, or pt. Don't make up units, it won't work :)
var vf_unit         = 'em';

//  Increment: How fine-tuned you want your sizing to be... chances are that
//  most browsers won't even render less than 1px at a time, but why not try?
//  IMPORTANT! if you use ems, divide this by 10 (so .1 instead of 1)!!
var vf_increment    = .1;

//  Tolerance: If you need a little fudge room (too much recursion error or 
//  text ends up too big), increase this number.
var vf_tolerance    = 0;

//  Trigger: The className you want to make resizable
var vf_trigger      = 'verticalFill';

//DO NOT CHANGE ANYTHING BELOW THIS LINE!
//==================================================================================

function vf(el, finalheight, curSize){
  if(el.innerHTML == '') return;
  var currentheight = el.offsetHeight;
  var fontsize      = curSize; 
  
  if(currentheight > finalheight)
  {
    fontsize -= vf_increment;
    el.style.fontSize = fontsize+vf_unit;
    vf_last = '-';
    vf(el, finalheight, fontsize);
  }
  else if(currentheight < finalheight && vf_last!='-')
  {
    fontsize += vf_increment;
    el.style.fontSize = fontsize+vf_unit;
    vf_last = '+';
    vf(el, finalheight, fontsize);
  }
  else
  {
      vf_last = '';
      return null;
  }
}

function vf_getElementsByClassName(className, tag, el){
  var testClass = new RegExp("(^|\\\\s)" + className + "(\\\\s|$)");
  var tag = tag || "*";
  var el = el || document;
  var elements = (tag == "*" && el.all)? el.all : el.getElementsByTagName(tag);
  var returnElements = [];
  var current;
  var length = elements.length;
  for(var i=0; i<length; i++){
      current = elements[i];
      if(testClass.test(current.className)){
          returnElements.push(current);
      }
  }
  return returnElements;
}

function vf_init()
{
  var vf_elements = vf_getElementsByClassName(vf_trigger);
  vf_last = '';
  for(i=0;i<vf_elements.length;i++)
  {
      el=vf_elements[i];
      if(el.currentStyle){
        var pt = el.parentNode.currentStyle['paddingTop'];
        var pb = el.parentNode.currentStyle['paddingBottom'];
        var mt = el.currentStyle['marginTop'];
        var mb = el.currentStyle['marginBottom'];
      }else if(window.getComputedStyle || document.defaultView.getComputedStyle){
        var pt = document.defaultView.getComputedStyle(el.parentNode,null).getPropertyValue('padding-top');
        var pb = document.defaultView.getComputedStyle(el.parentNode,null).getPropertyValue('padding-bottom');
        var mt = document.defaultView.getComputedStyle(el,null).getPropertyValue('margin-top');
        var mb = document.defaultView.getComputedStyle(el,null).getPropertyValue('margin-bottom');
      }else{
        var pt = 0;
        var pb = 0;
        var mt = 0;
        var mb = 0;
      }
      var finalHeight = el.parentNode.offsetHeight - ( parseFloat(pt) + parseFloat(pb) + parseFloat(mt) + parseFloat(mb) + parseFloat(vf_tolerance) );
      el.style.fontSize = vf_fontSize+vf_unit;
      vf(el, finalHeight, vf_fontSize);
  }
}

window.onload = vf_init;
