//////////////////////////////////////////////
 // This part is for window.onload operations//
//////////////////////////////////////////////

window.onload = function() {
  //Reduce the font size on buttons etc. if they don't fit in, works on given classes.
  //If you want to activate it on your tag, then add the class 'font_adjust' to it. 
  //Warning. Element's font-size must be in pixels! (At least at this moment...)   
  FontAdjustAll(new Array('font_adjust','button_big', 'button_small'));
};

//gets all elements of certain class/classes
function getElementsByClass(searchClass) {
	var classElements = new Array();
	var els = document.getElementsByTagName('*');
	var elsLen = els.length;
	var pattern = new RegExp("\\b"+searchClass+"\\b");
	for (i = 0; i < elsLen; i++) {
		if (pattern.test(els[i].className)) {
			classElements.push(els[i]);
		}
	}
	return classElements;
}
	
	
function FontAdjustAll(theClass) {
  if(!(theClass instanceof Array)){
	  var firstSearch = theClass;
	  theClass = new Array();
	  theClass[0] = firstSearch;
	}
	for (k = 0; k < theClass.length; k++){
    var adjustedClass = getElementsByClass(theClass[k]);
    for(var i = 0; i < adjustedClass.length; i++){
      FontAdjust(adjustedClass[i]);
    }
	}
}
	
function FontAdjust(pEl){
  if(document.defaultView){
    var MaxHeight = Math.round(document.defaultView.getComputedStyle(pEl, '').getPropertyValue('height').replace('px', '') *0.95);
    if(navigator.appName == 'Opera'){
      MaxHeight = Math.round(MaxHeight * 0.8);
    }
  }else if(pEl.currentStyle){
    var MaxHeight = Math.round(pEl.currentStyle.height.replace('px', '') * 0.95);
  }
  var d = document.createElement('div');
  document.body.appendChild(d);
  if(document.defaultView){
    d.style.fontSize = document.defaultView.getComputedStyle(pEl, '').getPropertyValue('font-size');
    d.style.fontWeight = document.defaultView.getComputedStyle(pEl, '').getPropertyValue('font-weight');
    d.style.width = document.defaultView.getComputedStyle(pEl, '').getPropertyValue('width').replace("px", "") -2 + "px";
  }else if(pEl.currentStyle){
    d.style.fontSize = pEl.currentStyle.fontSize;
    d.style.fontWeight = pEl.currentStyle.fontWeight;
    d.style.width = pEl.currentStyle.width.replace("px", "") - 2 +'px';
  }
  if('' !== pEl.innerHTML){
    d.innerHTML = pEl.innerHTML;
  }else if(pEl.type == 'button' || pEl.type == 'submit'){
    d.innerHTML = pEl.value;
  }
  while(d.clientHeight > MaxHeight){
    //alert(d.style.fontSize);
    d.style.fontSize = (d.style.fontSize.replace("px", "") -1)+'px';
  }
  pEl.style.fontSize = d.style.fontSize;
  document.body.removeChild(d);
}