/*******************************************************************
*
*   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
*
*   Iain Wallace, bit10 ltd 2004/11/11
*
*******************************************************************/

var strCookieName = "squintStartSize";
var arrTags = new Array( 'div','td','tr','button','a','span');
//var arrSizes = new Array( 'xx-small','x-small','small','medium','large','x-large','xx-large' );
var arrSizes = new Array( '10px','11px','12px','13px','14px','15px','16px' );
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);
  }
}

// Draw the controls onto the page
function renderSquintControls(){
    document.write( "<span class=\"dark\"><b>CHANGE TEXT SIZE:</b></span>&nbsp;" ); 
    document.write( "<img align=\"absmiddle\" src=\"images/button_minus.gif\" alt=\"Decrease text size\" onclick=\"javascript:squint(-1);\">&nbsp;" );
    document.write( "<img align=\"absmiddle\" src=\"images/button_plus.gif\" alt=\"Increase text size\" onclick=\"javascript:squint(1);\">" );
}

// 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;
}

renderSquintControls();