/*
	17/3/2009
	Caption
	Jquery plugin for image captions
	Copyright (C) 2009 Peter Törnstrand

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
jQuery.fn.caption = function(options)
{
	var defaults = {
		placement: 'below',
		attribute: 'alt'
	}
	var config = $.extend(defaults, options);
	
	return $(this).each(function()
	{
    $self = $(this);
    
    if($self.attr(config.attribute).length > 20 && $self.attr('width') > 200)
    {
      var align = $self.attr('align');
      var classes = '';
      switch(align)
      {
        case 'left' :
          classes = ' float-left';
          break;
        case 'right' :
          classes = ' float-right';
          break;
      }
      
      var style = 'style="width:'+$self.attr('width')+'px;"';
      
  		$self.wrap('<div class="image-caption'+classes+'" '+style+'></div>');

  		var html = '<h4>' + $self.attr(config.attribute) + '</h4>';

  		switch(config.placement)
  		{
  			case 'below' :
  				$self.after(html);
  				break;

  			case 'above' :
  				$self.before(html);
  				break;
  		}
    }
	});
}