// $Id: standards.js 28 2009-04-22 21:00:12Z esmith $
/**
* This function is used to decode email addresses that have been
* obfuscated using Tim William's Email Obfuscator Script 2.1 
* Obfuscated addresses are generated here http://www.jottings.com/obfuscator/
*
* @input coded The value generated @ http://www.jottings.com/obfuscator/
* @input key The value generated @ http://www.jottings.com/obfuscator/
* @version 1
* @return String An unscrambled mailto link.
*/
function timWilliamsObfuscator(coded, key) {
  
  var shift=coded.length
  var link=""
  
  for (i=0; i<coded.length; i++) {
    if (key.indexOf(coded.charAt(i))==-1) {
      ltr = coded.charAt(i)
      link += (ltr)
    }
    else {     
      ltr = (key.indexOf(coded.charAt(i))-shift+key.length) % key.length
      link += (key.charAt(ltr))
    }
  }
  return link;
}


/**
 * Checks that a variable is defined and not null.
 *
 * @param Variable Any input.
 * @version 1
 * @return Boolean
 */
function hasValue(variable) {
  if (typeof(variable) == "undefined") {
    return false;
  }
  if (variable == '') {
    return false;
  }
  if (variable == null) {
    return false;
  } 
  return true;
}




/**
* A function to preload images.
*
* Pass images to be preloaded to the function like so 
*
* $.preloadImages("image1.gif", "/path/to/image2.png", "some/image3.jpg");
*
* Source: http://www.mattfarina.com/2007/02/01/preloading_images_with_jquery
* 
* @param A list of image paths.
* @version 1
* @return Void
*/
jQuery.preloadImages = function() {
  for(var i = 0; i<arguments.length; i++)
  {
    jQuery("<img>").attr("src", arguments[i]);
  }
} // jQuery.preloadImages()





/** 
* Implement a simple rollover effect.
*
* Images of the class "rollover" (img.rollover) will be swapped on 
* mouseover. The file names are expected to formatted like so
*
*   image-off.ext // The default state image to display.
*   image-on.ext  // The mouseover state image to display.
*		iamge-act.ext // The active (current) page state image to display.
*
* @version 1
* @return Void 
*/
jQuery.simpleRollover = function(){	  
  
  // Add a class to stick an item as active and swap to hot image.
  var navItem = "";
  if (typeof activenav != 'undefined') {
    len = activenav.length;
    for (i in activenav) {
      navItem = $(activenav[i] + ' img.rollover');
      navItem.addClass('active');
      navItem.attr('src', navItem.attr('src').replace('-off', '-on'));
      navItem = '';
    }
  }  
  // Preload rollover images
  $('img.rollover').each(
    function(){$.preloadImages( 
      $(this).attr('src'),
      $(this).attr('src').replace('-off', '-on')
    );
  });

  $('img.rollover').hover(
    function(){
      if (!$(this).hasClass('active')) {
        $(this).attr('src', $(this).attr('src').replace('-off', '-on'));
      }
    }, 
    function() {
      if (!$(this).hasClass('active')) {
        $(this).attr('src', $(this).attr('src').replace('-on', '-off'));
      }
    }
  );
} // jQuery.simpleRollover()



/**
* Generates a debugger console.
* 
* @param A list of debugger messages.
* @return Void
*/
jQuery.debuggerConsole = function() {

  // Create an array to contain debugger messages.
  if (typeof debuggerConsoleMessages == 'undefined') {
    debuggerConsoleMessages = new Array();
  } 


  if (arguments.length > 0) {
    // @note I originally tried using for (i in arguments) but it wouldn't work.
    for (i=0; i<arguments.length;i++) {
      debuggerConsoleMessages.push(arguments[i]);      
    }
  }
  
  // Create the console if it is not already present.
  debuggerConsole = $('#debuggerConsole');
  if (debuggerConsole.length < 1 ) {
    $('body').append('<div id="debuggerConsole">DEBUGGER CONSOLE<br /></div>');
    // Style the consol for visibility.
    debuggerConsole = $('#debuggerConsole');
    debuggerConsole.css('padding', '20px')
           .css('color', 'white')
           .css('background', 'black')
           .css('border', '2px solid red')
           .css('text-align', 'left');
  }

  // Add messages to the console.
  debuggerConsoleStack = '';
  for (message in debuggerConsoleMessages) {
    debuggerConsoleStack += debuggerConsoleMessages[message] + '<br />';
  }
  debuggerConsoleStack = debuggerConsole.html() + debuggerConsoleStack;
  debuggerConsole.html(debuggerConsoleStack);
} // jQuery.debuggerConsole()




/**
* Functions for setting, clearing, and reading cookies.
*/
function createCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else var expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return null;
}

function eraseCookie(name) {
  createCookie(name,"",-1);
}



/**
* Invoke standard extensions as determined by an array of function names. 
* 
* Pass the jQuery extension names as an array and they will be called in 
* first in, first out order. Do not include the $. prefix or closing 
* parantheses ( ), just the name, e.g.,
*
* someFunctions = ['func1', 'func2', 'func2'];
* $.runStandards(someFunctions);
*
* @param arguments An array containing the names of functions to be called
* on init.
* @return Void
*/
jQuery.runStandards = function(){
  if (arguments.length > 0) {
    for (i=0;i<arguments.length;i++) {
      eval('$.'+ arguments[i] +'()');
    }
  }
} // jQuery.runStandards()