
var dw_fontSizerDX = {
  sizeUnit:    "px",
  defaultSize: 12,
  maxSize:     28,
  minSize:     8,
 
  adjustList: [],
  set: function (sel,dflt,mn,mx) {
    this.adjustList[this.adjustList.length] = arguments;
  },

  init: function() {
    var size, sizerEl, i;
    if ( !document.getElementById || !document.getElementsByTagName ) return;
    size = window.location.search? window.location.search.slice(1): getCookie("fontSize");
    size = !isNaN( parseFloat(size) )? parseFloat(size): this.defaultSize;
    // in case default unit changed or size passed in url out of range
    if ( size > this.maxSize || size < this.minSize ) size = this.defaultSize;
    this.curSize = this.defaultSize;  // create curSize property to use in calculations 
    sizerEl = document.getElementById('sizer');
    if (sizerEl) sizerEl.style.display = "block";
    // hold ratio of this element's default size to this.defaultSize for calcs in adjust fn 
    for (i=0; this.adjustList[i]; i++) 
      this.adjustList[i][4] = this.adjustList[i][1] / this.defaultSize;
    if ( size != this.defaultSize ) this.adjust( size - this.defaultSize );
  },

  adjust: function(n) {
    var alist, size, list, i, j;
    // check against max/minSize
  
    if ( n > 0 && dw_fontSizerDX.maxSize ) {
      if ( dw_fontSizerDX.curSize + n > dw_fontSizerDX.maxSize )
	  n = dw_fontSizerDX.maxSize - dw_fontSizerDX.curSize;
    } else if ( n < 0 && dw_fontSizerDX.minSize ) {
	if ( dw_fontSizerDX.curSize + n < dw_fontSizerDX.minSize )
	  n = dw_fontSizerDX.minSize - dw_fontSizerDX.curSize;
    }
    //	alert(n)
    if ( n == 0 ) return false;
    dw_fontSizerDX.curSize += n;
    // loop through adjustList, calculating size, checking max/min
    alist = dw_fontSizerDX.adjustList;
    for (i=0; alist[i]; i++) {
      size = dw_fontSizerDX.curSize * alist[i][4]; // maintain proportion 
      size = Math.max(alist[i][2], size); size = Math.min(alist[i][3], size);
      list = dw_getElementsBySelector( alist[i][0] );
      for (j=0; list[j]; j++) { list[j].style.fontSize = size + dw_fontSizerDX.sizeUnit; }
    }
  //  alert(dw_fontSizerDX.curSize);
//    setCookie( "fontSize", dw_fontSizerDX.curSize, 180, "/" );
    setCookie( "fontSize", dw_fontSizerDX.curSize, 1, "/" );
  },

  // Reset adjustList elements to their default sizes
  reset: function() {
    var alist = dw_fontSizerDX.adjustList, list, i, j;
	
    for (i=0; alist[i]; i++) {
      list = dw_getElementsBySelector(alist[i][0] );
	  //alert(alist[i][0]+"-"+alist[i][1]);
	  //alert(list[j]+"-"+alist[i][1])
      for (j=0; list[j]; j++) { list[j].style.fontSize = alist[i][1] + dw_fontSizerDX.sizeUnit; }
    }
    dw_fontSizerDX.curSize = dw_fontSizerDX.defaultSize;
    deleteCookie("fontSize", "/");
  }

}

// resource: simon.incutio.com/archive/2003/03/25/getElementsBySelector
function dw_getElementsBySelector(selector) {
	var nodeList = [document], tokens, bits, list, els, i, j, k;
  if (!document.getElementsByTagName) return [];
  tokens = selector.split(' ');
  for (i=0; tokens[i]; i++) {
    if ( tokens[i].indexOf('#') != -1 ) {  // id
      bits = tokens[i].split('#'); 
      nodeList = [ document.getElementById( bits[1] ) ];
      continue; 
    } else if ( tokens[i].indexOf('.') != -1 ) {  // class
      bits = tokens[i].split('.');
      for (j=0; nodeList[j]; j++) 
	list = dw_getElementsByClassName( bits[1], bits[0], nodeList[j]);
      nodeList = [];
      for (j=0; list[j]; j++) { nodeList.push(list[j]); }
      continue; 
    } else {  // element 
      els = []; 
      for (j = 0; nodeList[j]; j++) {
	list = nodeList[j].getElementsByTagName(tokens[i]);
	for (k = 0; list[k]; k++) { els.push(list[k]); }
      }
      nodeList = els;
    }
  }
  return nodeList;
}

function dw_getElementsByClassName(sClass, sTag, oCont) {
  var result = [], list, i;
  oCont = oCont? oCont: document;
  if ( document.getElementsByTagName ) {
    if ( !sTag || sTag == "*" ) {
      list = oCont.all? oCont.all: oCont.getElementsByTagName("*");
    } else list = oCont.getElementsByTagName(sTag);
    
    for (i=0; list[i]; i++) 
      if ( list[i].className.match( new RegExp("\\b" + sClass + "\\b", "i") ) ) result.push( list[i] );
  }
  return result;
}

if (!Array.prototype.push) {  // for ie5.0
	Array.prototype.push =	function() {
		for (var i=0; arguments[i]; i++) this[this.length] = arguments[i];
		return this.length;
	}
}  


function setCookie(name,value,days,path,domain,secure) {
  var expires, date;
  if (typeof days == "number") {
    date = new Date();
    date.setTime( date.getTime() + (days*24*60*60*1000) );
		expires = date.toGMTString();
  }
  document.cookie = name + "=" + escape(value) +
    ((expires) ? "; expires=" + expires : "") +
    ((path) ? "; path=" + path : "") +
    ((secure) ? "; secure" : "");
//    ((domain) ? "; domain=" + domain : "") +

}

// Modified from Jesse Chisholm or Scott Andrew Lepera ?
// (found at both www.dansteinman.com/dynapi/ and www.scottandrew.com/junkyard/js/)
function getCookie(name) {
  var nameq = name + "=";
  var c_ar = document.cookie.split(';');
  for (var i=0; i<c_ar.length; i++) {
    var c = c_ar[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameq) == 0) return unescape( c.substring(nameq.length, c.length) );
  }
  return null;
}

// from Bill Dortch's Cookie Functions (hidaho.com) 
function deleteCookie(name,path,domain) {
  if (getCookie(name)) {
    document.cookie = name + "=" +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}
