/* $Id: fontsizer.js,v 1.2 2008/03/17 05:27:17 redbox2000drupalorg Exp $ */
/**
* Purpose:   Font sizer class, handles increasing and decreasing font size of a page.
*            It increases the font in 10% increments. By getting the level / 10 + 1.
*            i.e. level 2 is .2 + 1 so 1.2 or 120%.
*
* Requires:  JQuery and the JQuery cookies plugin.
*
* Use:       Setup the fontsizer $.FontSizer.Init(options); the two options are
*            min and max, for the min level and max level.
*            Defaults are min: -3 and max: 5.
*
* Author:    Stefan Sedich (stefan.sedich@gmail.com)
*
*
*
* ---- [ Additions and Extension ] ---------------------------------------------
*
* Author:    Martin Lanser
* 
* Changes:   Added new options:
*             - obj  : name of object that is affected by FontSizer (e.g. 'body', '.scale', '#section') 
*             - min  : MIN level. This must be an INTEGER.
*             - max  : MAX level. This must be an INTEGER.
*             - step : STEP size. This must be an INTEGER and defines the step-size of each increment.
*             - unit : fontsize unit. This must be a STRING (e.g. 'px', 'em', '%').
*             - def  : DEFAULT level. This must be an INTEGER. THis value is used for 'Reset()'
*/
$.FontSizer = {
  
  level: 0,
  
  options: {
    obj:  'body',
    min:  -5,
    max:  5,
    step: 1,
    unit: 'em',
    def:  0
  },

  Clean: function(obj) {
    var obj = obj || 'def';
    var clean = obj.replace(/[.# ]/gi, '');
    
    return clean;
  },
  
  GetLevel: function(obj) {
    var clean = $.FontSizer.Clean(obj);
    var name  = (clean == '') ? 'jqry_font_level' : 'jqry_font_level_' + clean;
    var level = parseInt(($.cookie(name) != null) ? $.cookie(name) : $.FontSizer.level);
    
    return level;
  },
  
  Init: function(optns) {
    if(optns != undefined) {
      $.FontSizer.options = $.extend($.FontSizer.options, optns);
    }
    
    // Get current LEVEL and UNIT from cookies.
    var level = ($.cookie('jqry_font_level') != null) 
                  ? $.cookie('jqry_font_level') 
                  : $.FontSizer.level;
    var unit = ($.cookie('jqry_font_unit') != null) 
                  ? $.cookie('jqry_font_unit') 
                  : $.FontSizer.options.unit;
    
    // Set font size to current level.
    $.FontSizer.SetSize(level, unit);
  },
  
  IncreaseSize: function(obj) {
    var level = $.FontSizer.GetLevel(obj);
    
    if((level + $.FontSizer.options.step) <= $.FontSizer.options.max) {            
      // If we have not exceded MAX level, increase LEVEL and set the size.
      $.FontSizer.SetSize(level + $.FontSizer.options.step, null, obj);  
    }
  },
  
  DecreaseSize: function(obj) {
    var level = $.FontSizer.GetLevel(obj);

    if((level - $.FontSizer.options.step) >= $.FontSizer.options.min) {
      // If we have not exceded MIN level, deccrease LEVEL and set the size.
      $.FontSizer.SetSize(level - $.FontSizer.options.step, null, obj);  
    }      
  },      
  
  Reset: function(obj) {
    // Reset LEVEL back to default
    $.FontSizer.SetSize($.FontSizer.options.def, null, obj);
  },
  
  SetSize: function(level, unit, obj) {

    var obj   = obj || $.FontSizer.options.obj;
    
    var clean = $.FontSizer.Clean(obj);
    var name  = (clean == '') ? 'jqry_font_level' : 'jqry_font_level_' + clean;
    var level = level || 0;
    var unit  = unit || $.FontSizer.options.unit;
    
    //Set new current LEVEL and UNIT
    $.FontSizer.level = level;
    $.FontSizer.options.unit = unit;
    
    //Save updated values to the cookie
    $.cookie(name, level);
    $.cookie('jqry_font_unit',  unit);
    
    //Work out the new size value and set it.
    var newSize = (unit == 'em') ? (level / 10) + 1 : level;
    $(obj).css('fontSize', newSize + unit);    
  }
}; 