/*  dw_sizefont.js version date: Jan 2004  */

/*************************************************************************
  This code is from Dynamic Web Coding at http://www.dyn-web.com/
  Copyright 2003-4 by Sharon Paine 
  See Terms of Use at http://www.dyn-web.com/bus/terms.html
  regarding conditions under which you may use this code.
  This notice must be retained in the code as is!
*************************************************************************/

dw_sizeFont = {
  defaultSize: 10,  // Default size for body font
  sizeUnit: "px",   // Unit of measurement for body font-size 
  bodyOnly: false,  // change body font-size rule only (set true if you use %'s or em's in any font-size spec's)
  incAmount: 1,     // amount to increment/decrement per click of buttons
  maxSize: 24,      // maximun size for body font (number or null)
  minSize: 4,      // minimum size for body font (number or null)
  
  // button text (buttons generated below in code)
  // put empty strings here to place linked text/images in sizer div yourself
  increaseBtn: "", // Increase
  decreaseBtn: "", // Decrease
  resetBtn:    "",   // Reset Restores font sizes to a original settings
  
  skipList:  ["search", "add2ndItemHere"],  // holds optional selector text list to leave untouched (can add items here or just before call to init)
  rulesList: [],   // to hold style rules that have font-size setting  
  controlList: [],  // selector text list to set min/max sizes on (populated using setControls)
  
  // check for selectorText support below
  standards: [document.getElementById, document.styleSheets, document.createElement, typeof document.body != "undefined" && typeof document.body.setAttribute != "undefined"],

  setControls: function (sel,mn,mx) {
    this.controlList[this.controlList.length] = arguments;
  },

  init: function () {
    var rules = [], sizediv, size, i, j;
    for ( i=0; i<this.standards.length; i++ ) { if ( !this.standards[i] ) return; }

    // loop through all the style sheets and collect their rules
    for ( i=0; i<document.styleSheets.length; i++ ) {
      if (document.styleSheets[i].rules) {
        rules.push( document.styleSheets[i].rules );
      } else if (document.styleSheets[i].cssRules) { 
        rules.push( document.styleSheets[i].cssRules );
        // check if cssRule is import, if so get its rules too 
        for (j=0; j<rules[i].length; j++) {
          if ( rules[i][j].type == 3 ) // type 3 is @import rule
            rules.push( rules[i][j].styleSheet.cssRules );
        }
      }
  
      if (document.styleSheets[i].imports) {  // get ie imports
        for (j=0; j<document.styleSheets[i].imports.length; j++) 
          rules.push( document.styleSheets[i].imports[j].rules );
      }
    } 
    
    // add buttons or just display sizediv
    sizediv = document.getElementById("sizer");
    if (!sizediv) { alert("Don't forget the sizer div!"); return; }
    if ( this.increaseBtn ) {
      var frm = sizediv.appendChild( document.createElement("FORM") );
      var incbtn = frm.appendChild( document.createElement("BUTTON") );
      var decbtn = frm.appendChild( document.createElement("BUTTON") );
      var resetbtn = frm.appendChild( document.createElement("BUTTON") );      
      incbtn.appendChild( document.createTextNode(dw_sizeFont.increaseBtn) );
      decbtn.appendChild( document.createTextNode(dw_sizeFont.decreaseBtn) );
      resetbtn.appendChild( document.createTextNode(dw_sizeFont.resetBtn) );      
      incbtn.setAttribute( "id", "inc");  decbtn.setAttribute( "id", "dec");
      incbtn.setAttribute( "accessKey", "i");  decbtn.setAttribute( "accessKey", "r");
      incbtn.onclick = this.adjust;  decbtn.onclick = this.adjust; resetbtn.onclick = this.reset;
    } 
    sizediv.style.display = "block";
    
    // send each style sheet's rules for further processing and storage in rulesList
    for ( i=0; i<rules.length; i++ ) { this.holdSizeRules( rules[i] ); }
    
    // size passed in url or contained in cookie?
    size = (window.location.search)? window.location.search.slice(1): getCookie("fontSize")? getCookie("fontSize"): null;
    // check for bodyRule (set in holdSizeRules) to avoid error if no support for selectorText
    if ( size && !isNaN( parseFloat(size) ) && this.bodyRule ) 
      this.adjust( parseFloat(size) - parseFloat(this.bodyRule.style.fontSize) );

  },
  
  // called from init, passed each style sheet's rules
  // hold the ones that have font-size rules if they are not in the skipList
  holdSizeRules: function (rules) {
    var i, j;
    rulesCheck:
    for ( i=0; i<rules.length; i++) {
      if ( rules[i].style && rules[i].style.fontSize ) { 
        // earliest opportunity to check support for selectorText
        if ( !rules[i].selectorText ) { document.getElementById("sizer").style.display = "none"; return; }
        
        if ( rules[i].selectorText.match( new RegExp("\\bbody\\b", "i") ) ) {
          this.bodyRule = rules[i]; // hold body rule for use with cookies
          // if only adjusting size of body font, hold its rule and return
          if ( this.bodyOnly ) { this.rulesList.push(rules[i]); return; }
        }
        
        // check skipList 
        for ( j=0; j<this.skipList.length; j++) {
          if ( rules[i].selectorText.match( new RegExp("\\b" + this.skipList[j] + "\\b", "i") ) ) 
            continue rulesCheck;
        }
        this.rulesList.push(rules[i]);

        // for controlList, hold size ratio to body font-size 
        for ( j=0; j<this.controlList.length; j++) {
          if ( rules[i].selectorText.match( new RegExp("\\b" + this.controlList[j][0] + "\\b", "i") ) ) 
            this.controlList[j][3] = parseFloat(rules[i].style.fontSize) / parseFloat(this.bodyRule.style.fontSize);
        }
      }
    }  
  
  },
  
  // manages font size changes, called onload if size set in cookie or url, and onclick of buttons/links
  adjust: function (n) {
    var bodySize = parseFloat(dw_sizeFont.bodyRule.style.fontSize);
    var rules = dw_sizeFont.rulesList;  
    if ( typeof n != "number" ) // if called onclick and buttons generated, no arg passed
      n = dw_sizeFont.incAmount * ( (this.id == "inc")? 1: -1 );

    // check against max/minSize
    if ( dw_sizeFont.maxSize ) {
      if ( n > 0 && bodySize + n > dw_sizeFont.maxSize )
        n = dw_sizeFont.maxSize - bodySize;
    }
    if ( dw_sizeFont.minSize ) {
      if ( n < 0 && bodySize + n < dw_sizeFont.minSize )
        n = dw_sizeFont.minSize - bodySize;
    }
    if ( n == 0 ) return false;
    
    for (var i=0; i<rules.length; i++) {
      if ( parseFloat(rules[i].style.fontSize) + n > 0 ) { // triggers error if decrease to < 0
        // check if rule member of controlList, if so it's sizing is handled there and returns true
        if ( !dw_sizeFont.handleControlList( rules[i] ) )
          rules[i].style.fontSize = parseFloat( rules[i].style.fontSize) + n + dw_sizeFont.sizeUnit;
      }
    }
    setCookie( "fontSize", dw_sizeFont.bodyRule.style.fontSize, 180, "/" );
    return false; // so no form submission message
  },

  // called from dw_sizeFont.adjust 
  // checks controlList items, returns true if rule sent is in controlList, false if not
  // handles sizing of controlList items, checks against min/max settings
  handleControlList: function (rule) {   
    var sz, cl = this.controlList; 
    for (var i=0; i<cl.length; i++) {  
      if ( rule.selectorText.match( new RegExp("\\b" + cl[i][0] + "\\b", "i") ) ) {
        sz = Math.round( parseFloat(this.bodyRule.style.fontSize) * cl[i][3] );
        sz = Math.max(cl[i][1], sz); sz = Math.min(cl[i][2], sz);
        rule.style.fontSize = sz + this.sizeUnit;
        return true;
      } 
    }
    return false;
  },
  
  reset: function() {
    dw_sizeFont.adjust( dw_sizeFont.defaultSize - parseFloat( dw_sizeFont.bodyRule.style.fontSize ) );
    deleteCookie("fontSize", "/");
    return false;
  }
  
}  

if (!Array.prototype.push) {
	Array.prototype.push =  function() {
		for (var i=0; arguments[i]; i++) this[this.length] = arguments[i];
		return this.length;
	}
}  

dw_sizeFont.init();