
/* 
behavior.js 
Created 2007/02/25
Updated 2007/05/07
Version 200705080748
Author: Jeffrey Prutsman
Copyright (c)2007-2008 Jeffrey Prutsman ( http://www.signalstrategy.com )
All rights reserved. Reuse and adaptation allowed under MIT License with attribution.
function textResize() adapted from Cameron Adams: See http://www.themaninblue.com/writing/perspective/2003/12/22/
function addEvent() by John Resig: See http://ejohn.org/projects/flexible-javascript-events/ 
eventCache flushing method by Dustin Diaz: See http://www.dustindiaz.com/rock-solid-addevent/
*/

function textSize() {
  var smallestText   = 5;
  var screenConstant = 67;
  if (!document.body.style.fontSize) { 
    // Do not use if(document.cookie) != ""), as other set cookies, PHPSESSID, etc. can cause the script to read an empty cookie and end before the else fires. 
    if (readCookie("cText") > "") { 
      document.body.style.fontSize = readCookie("cText");
      } else if (screen.height > 460 && screen.height < 1200) {
        document.body.style.fontSize = 12 + "px";
        } else { 
          document.body.style.fontSize = (Math.round(screen.height/screenConstant) <= smallestText) ? smallestText + "px" : (Math.round(screen.height/screenConstant)) + "px"; 
          setCookie("cText", document.body.style.fontSize, 365);
          }
    }
  setLayout();
  return true;
  }

function textResize(size) {
  var currentText = parseInt(document.body.style.fontSize);    
  var sizeIncrement  = 2;
  var smallestText   = 5;
  if (size == "smaller" && (currentText - sizeIncrement <= smallestText)) { 
    document.body.style.fontSize = smallestText + "px";
    currentText = parseInt(document.body.style.fontSize);	  
    } else if (size == "smaller") {
	  document.body.style.fontSize = (currentText - sizeIncrement) + "px"; 
      currentText = parseInt(document.body.style.fontSize);	  
	  } else { 
	    document.body.style.fontSize = (currentText + sizeIncrement) + "px"; 
        currentText = parseInt(document.body.style.fontSize);	  
		}
  setCookie("cText", currentText + "px", 365); 
  setLayout();
  return true;
  }

function restoreDefault() {  
  eraseCookie("cText");
  window.location.reload();
  return true;
  }

function setLayout() {
  var ultraLayout = 1250; 
  var wideLayout = 990; 
  var normalLayout = 770; 
  var logoSizeFactor = 2; 
  var logoSizeArray  = [ 18,24,32,43,57,76,100,133 ];
  var windowWidth = !window.innerWidth ? document.body.clientWidth : window.innerWidth; 
  var currentText = parseInt(document.body.style.fontSize);
  var logoSize = objSizeSelect(currentText, logoSizeFactor, logoSizeArray);     
  if (windowWidth >= ultraLayout ) { 
    document.body.className = "ultra";
    } 
	else if (windowWidth >= wideLayout && windowWidth < ultraLayout) { 
      document.body.className = "wide";
	  } 
	  else if (windowWidth >= normalLayout && windowWidth < wideLayout) { 
        document.body.className = "normal";
		} 
        else {
          document.body.className = "basic";		  
          }
  setCookie("cText", currentText + "px", 365); 
  // document.getElementById("header").className = "hc" + logoSize;
  // document.getElementById("logo").className = "hb" + logoSize;  
  document.getElementById("wrapper").style.display = "block";
  matchColumns();
  return true;
  }
  
function objSizeSelect(value, sizeFactor, sizeArray) {
  var objSize;
  var arLength = sizeArray.length;
  for(var i=0; i < arLength; i++) {
    if(sizeArray[i] > sizeFactor*value) {  
      objSize = sizeArray[i];
	  break;
	  }
    }
  return objSize;
  }  

function defineImage(imgNum, imgName, imgAlt) {
  var rowWidth   = 48;
  var rowImages  = 3;
  var textSize   = readCookie('cText');
  var imgWidth   = Math.round((parseInt(textSize)*rowWidth)/rowImages)
  this.imgNum    = imgNum;
  this.imgName   = imgName;
  this.imgAlt    = imgAlt;
  this.imgWidth  = imgWidth;
  this.showImage = displayImage;
  }
  
function displayImage( ) {
  document.write('<img src="' + this.imgName + '" width="' + this.imgWidth + '" alt="' + this.imgAlt + '" />');
  }


function addEvent(obj, type, func) { 
  if (obj.addEventListener) { 
    obj.addEventListener( type, func, false );
    }
    else if (obj.attachEvent) { 
      obj["e"+type+func] = func; 
      obj[type+func] = function() { 
	    obj["e"+type+func]( window.event ); 
        } 
      obj.attachEvent("on"+type, obj[type+func]); 
      } 
  } 

var eventCache = function() {
  var listEvents = [];
  return {
    listEvents : listEvents,
    add : function(node, sEventName, fHandler) {
      listEvents.push(arguments);
      },
    flush : function() {
      var i, item;
      for(i = listEvents.length - 1; i >= 0; i = i - 1) {
        item = listEvents[i];
        if(item[0].removeEventListener) {
          item[0].removeEventListener(item[1], item[2], item[3]);
        };
        if(item[1].substring(0, 2) != "on") {
          item[1] = "on" + item[1];
        };
        if(item[0].detachEvent) {
        item[0].detachEvent(item[1], item[2]);
        };
        item[0][item[1]] = null;
        };
      }
    };
  }();

addEvent(window, "load", textSize);
addEvent(window, "resize", textSize);  
addEvent(window, "unload", eventCache.flush);  

function setCookie(name,value,days) {
  if(days) {
    var date = new Date();
    date.setTime(date.getTime() + (days*86400000));
    var expires = "; expires=" + date.toGMTString();
    } else var expires = "";
      document.cookie = name + "=" + value+expires + "; path=/";
      }

function readCookie(name) {
  var cKey = name + "=";
  var cArray = document.cookie.split(";");
  for(var i=0; i < cArray.length; i++) {
    var c = cArray[i];
    while (c.charAt(0)==" ") c = c.substring(1,c.length);
      if (c.indexOf(cKey) == 0) return c.substring(cKey.length,c.length);
    }
  return null;
  }

function eraseCookie(name) {
  setCookie(name,"",-1);
  }

// RESIZE TWO OR MORE DIV ELEMENTS TO THE SAME HEIGHT
// Each divGroup is an array of div elements with the same class name. Each code section in the matchColumns function sets every div in a divGroup, i.e., every div with the same class name, to the same height. Multiple divGroups, each with a different resized height, can be defined. 
// ONE resizable parent divGroup can contain one or more child divGroups, each with a different resized height than the parent divGroup. For example, the parent divGroup might define the main content section of a Web page and have left, center and right column div elements. The center column div element could contain one or more resizable child divGroups, where each child divGroup would typically be a row containing two or more adjacent div elements. A parent divGroup that contains one or more child divGroups MUST be referenced LAST in the matchColumns function. 
    
function matchColumns() { 
  var divs, divGroup, maxHeight, divHeight, d; 
  divs = document.getElementsByTagName('div'); 
  divGroup = []; maxHeight=0; 
  for(var i=0; i < divs.length; i++){ 
    if(/\bmatch\b/.test(divs[i].className)){ 
      d = divs[i]; 
      divGroup[divGroup.length] = d; 
      if(d.offsetHeight){ 
        divHeight = d.offsetHeight;
        } 
        else if(d.style.pixelHeight){ 
          divHeight = d.style.pixelHeight;
          } 
      maxHeight = Math.max(maxHeight,divHeight);
      }
    } 
    for(var i=0; i<divGroup.length; i++){
      divGroup[i].style.height = maxHeight + "px";
      } 		  
  // Can add additional match classes (class names) here		  
  }  