/*******************************************************************
*
*   Squint - javascript text resizer library
*
*   Useage: include this file in the header, then use 
*     renderSquintControls to render buttons, or tie a button to 
*     squint(1) to increase size and squint(-1) to decrease size.
*     Bind initSquint to document.onload in order to allow cookies
*     to remember which size is chosen
*
*******************************************************************/

var strCookieName = "squintStartSize";
var arrTags = new Array( 'div','td','tr','button','span','label','input','select');
var arrSizes = new Array( 'xx-small','x-small','small','medium','large','x-large','xx-large' );

var strTrace = "";
var intDefaultSize = 2;
var intStartSize = intDefaultSize;
var strDomain = "";

// Tie to the document onload event - sets the start size if it's in a cookie
function initSquint(){
  strTrace += "Init\n";
  strDomain = document.domain;
  strTrace += "set domain to " + strDomain + "\n";
  i = getCookieVal( strCookieName );
  if( i ){
    strTrace += "Cookie found with value " + i + "\n";
    intStartSize = i;
    squint(0);
  }
  else
  {
    //No cookie value - need to prefix any ie browser b4 7
    if (navigator.appName == "Microsoft Internet Explorer")
    {
        if (getIEVersion() < 7)
        {
            setCookieVal( strCookieName, 1);
            intStartSize = 1; 
            squint(0);
        }
    }
  }
  
}

function getIEVersion()
{
  var rv = -1; // Return value assumes failure.
  if (navigator.appName == 'Microsoft Internet Explorer')
  {
    var ua = navigator.userAgent;
    var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
    if (re.exec(ua) != null)
      rv = parseFloat( RegExp.$1 );
  }
  return rv;
}

// Function to resize text
function squint( intInc ){
  strTrace = "";
  if (!document.getElementById) return
  if( squint.arguments.length > 1 ){
    strTarget = squint.arguments[1];
  }else{
    strTarget = "body";
  }
  var doc = document;
  var el = null;
  var intSize = intStartSize;
  var i,j,arr;
  
  intSize = eval( intSize + " + intInc" );
  if ( intSize < 0 ) intSize = 0;
  if ( intSize > 6 ) intSize = 6;
  intStartSize = intSize;
  
  // Save intStartSize to cookie
  setCookieVal( strCookieName, intSize );
  
  if ( !( el = doc.getElementById( strTarget ) ) ) el = doc.getElementsByTagName( strTarget )[ 0 ];

  el.style.fontSize = arrSizes[intSize];

  for ( i=0; i<arrTags.length; i++ ) {
    cTags = el.getElementsByTagName( arrTags[i] );
    
    for ( j=0; j<cTags.length; j++ )
    {     
        cTags[j].style.fontSize = arrSizes[intSize];
    }
  }
}

// Store a cookie
function setCookieVal( cookieName, cookieValue, lifeTime, path, domain, isSecure ) {
  if( !cookieName ) { return false; }
  document.cookie = escape( cookieName ) + "=" + escape( cookieValue ) +
    ( lifeTime ? ";expires=" + ( new Date( ( new Date() ).getTime() + ( 1000 * lifeTime ) ) ).toGMTString() : "" ) +
    ( path ? ";path=" + path : "") + 
    ( domain ? ";domain=" + domain : "") + 
    ( isSecure ? ";secure" : "");

  if( lifeTime < 0 ) { if( typeof( getCookieVal( cookieName ) ) == "string" ) { return false; } return true; }
  if( typeof( getCookieVal( cookieName ) ) == "string" ) { return true; } return false;
}

// Get a cookie's value
function getCookieVal(strName){
  cki = document.cookie;
  strTrace += "Cookie: " + cki + "\n";
  if( cki.indexOf(strName + "=") > -1 ){
    arrCookie = cki.split("; ");
    for( i=0; i<arrCookie.length; i++ ){
      arrPair = arrCookie[i].split("=");
      if( arrPair[0] == strName ){
        strTrace += strName + " found at position " + i + ", value " + arrPair[1] + "\n";
        return unescape( arrPair[1] );
      }
    }
  }else{
    strTrace += strName + " not found in cookie\n";
  }
  return null;
}

//<SCRIPT>renderSquintControls();</SCRIPT>
