/*
 * Lettrine - jQuery plugin
 *
 *	http://www.zen-in-progress.com/plugin-jquery-effet-lettrine/
 *
 * Copyright (c) 2009 BAREAU Stéphane
 *
 * Dual licensed under the MIT and GPL licenses:
 *   http://www.opensource.org/licenses/mit-license.php
 *   http://www.gnu.org/licenses/gpl.html
 *
 */

 (function($) {
  // plugin definition
  $.fn.lettrine = function(options) {
    debug(this);
    var opts = $.extend({}, $.fn.lettrine.defaults, options);
    
    return this.each(function() {
      $this = $(this);
      var letter = $this.text().substr(0,1);
      var index = $this.html().indexOf(letter,0);
      var lettrine = '<img src="'+opts.img_path+letter+'.'+opts.img_ext+'" style="'+opts.img_style+'">';
      if( 0 != index )
      {
        var pos1 = $this.html().indexOf('<',0);
        var pos2 = $this.html().indexOf('>',0);
        while( ( pos1 < index) && ( pos2 > index) )
        {
          
          index = $this.html().indexOf(letter,pos2);
          pos1 = $this.html().indexOf('<',pos1+1);
          pos2 = $this.html().indexOf('>',pos2+1);        
        }
        index = $this.html().indexOf(letter,pos2);
        $this.html( $this.html().substr(0,index) + lettrine + $this.html().substr(index+1));
      }
      else 
      {
        $this.html(lettrine+$this.html().substr(1));
      }
    });
  };

  function debug($obj) {
    if (window.console && window.console.log)
      window.console.log('lettrine selection count: ' + $obj.size());
  };

  // plugin defaults
  $.fn.lettrine.defaults = {
      img_ext: 'png',
      img_path: './',
      img_style: 'vertical-align:-5px;'
  };

})(jQuery);



