/**
 * forgetting.js
 *
 * Javascript helper functions for The Forgetting website on pbs.org
 *
 * Written by Ronan Dowling, adapted by Drew Gorton, Gorton Studios
 *
 */

$(document).ready(function() {
  $.inputhelp.init( $("#searchterm"), { initText:"Search this site", initClass:"dimmed" } );
  $.textsizer.init( $("#textsizelinks"), { cookieName:"forgetting.font_preference" } );  
});


$.textsizer = {
  
  options : {
    cookieName : "font_preference"
  },
  
  init : function( parentElement, options ) {
    if(options) {
		    $.textsizer.options = $.extend($.textsizer.options, options);
    }
    
      // set the users prefered font size
    var bodyClass = ($.cookie($.textsizer.options.cookieName) != null) ? $.cookie($.textsizer.options.cookieName) : "textsize-sm";
    $("body").attr("class",bodyClass);
    
    // build the resize links unobtrusively
    if( parentElement ) {
      parentElement.append('<span class="label">Text Size:</span>');
      parentElement.append('<a class="textsize-sm" href="#" title="switch to small text">A</a>');
      parentElement.append('<a class="textsize-med" href="#" title="switch to medium text">A</a>');
      parentElement.append('<a class="textsize-lg" href="#" title="switch to large text">A</a>');
    }
    
    // set the onclick for the text resize links
    $("#textsizelinks a").click(
      function() {
        $("body").attr("class",this.className);
        $.cookie($.textsizer.options.cookieName, this.className, {path: '/'});
        return false;
      });

    
  }
}

// add help text into an input box. goes away when focused
$.inputhelp = {
  
  options : {
    initClass : "",
    initText : ""
  },
  
  init : function( inputElement, options ) {
    if(options) {
		    $.inputhelp.options = $.extend($.inputhelp.options, options);
    }
    
    if( inputElement ) {      
      
      if( inputElement ) {
               
        inputElement.focus( 
            function() { 
              if( this.value == $.inputhelp.options.initText ) {
                this.value = "";
              }
              if( $.inputhelp.options.initClass ) {
                $(this).removeClass( $.inputhelp.options.initClass ); 
              }
            } 
          );
        inputElement.blur(
            function() { 
              if( this.value == "" || this.value == $.inputhelp.options.initText ) {
                if( $.inputhelp.options.initClass ) {
                  $(this).addClass( $.inputhelp.options.initClass ); 
                }
                this.value = $.inputhelp.options.initText;             
              }
            }           
          );
        inputElement.blur();
      }
    }
  }
}


