/*------------------------------------------------------------------------------
Function:       FigureHandler()
Author:         Aaron Gustafson (aaron at easy-designs dot net)
                Initial port to jQuery by Remy Sharp (http://remysharp.com)
                Port updated by Aaron Gustafson
Creation Date:  20 August 2007
Version:        0.1
Homepage:       http://code.google.com/p/easy-designs/wiki/FigureHandler
License:        MIT License (see homepage)
Note:           If you change or improve on this script, please let us know by
                emailing the author (above) with a link to your demo page.
------------------------------------------------------------------------------*/
function FigureHandler ( id, sizes )
{ 
  // basic configuration
  if ( typeof(sizes) !== 'object' )
  { 
    var sizes = {
      '75-100': 'full-col', 
      '67-75':  'three-quarters-col', 
      '50-67':  'two-thirds-col', '34-50' : 'half-col', 
      '25-34':  'third-col', 
      '0-25':   'quarter-col'
    }; 
  } 
  var selector = '.figure'; 
  if ( typeof( id ) == 'string' )
  {
    selector = '#' + id + ' ' + selector; 
  }
  
  function init()
  { 
    $( selector ).each(function(){
      $figure = $(this);
      var img_w = $figure.find('img').width;
      var col_w = parseInt( $figure.parent().css('width') ); 
      var percent = Math.ceil( img_w / col_w * 100 ); 
      var range, col_class; 
      for ( var size in sizes )
      { 
        range = size.split('-'); 
        if ( percent > range[0] &&
             percent <= range[1] )
        { 
          col_class = sizes[size];
          break; 
        } 
      } 
      $figure.addClass(col_class); 
      $figure.find('p').css('width', img_w + 'px');
    }); 
  } 
  init(); 
}