/**
 * $ addCaptions 0.1
 * 
 * Adds the ability to add captions around images
 */
/**
 * addCaptions
 * 
 * Function to wrap an image in a styled div containing the image caption
 * 
 * @param object options Object containing any options for processing
 * @return void
 * @author Dom Hastings
 */
(function($) {
  $.fn.addCaptions = function(options) {
    // if the options aren't set, set them to an empty object
    options = options || {};

    // the default settings
    var settings = {
      // attributes to check for caption (these are checked in order)
      attributes: [
        'longdesc',
        'title',
        'alt'
      ],

      // whether or not to add the caption box to images with no caption
      allowBlankCaption: true,

      // fins options (change these to match other elements eg a.fullSizeImage or whatever)
      findElement: 'img',
      findClass: '',

      // remove the float directly on the image?
      removeFloat: true,

      // options for respective elements
      containerElement: 'div',
      containerClass: 'caption',
      // 
      titleElement: 'p',
      titleClass: 'caption-title',
      // 
      imageClass: ''
      //
    }

    // update the settings
    $.extend(settings, options);

    // get all the images in the current element
    if (settings.findClass) {
      var images = $(this).find(settings.findElement + '.' + settings.findClass);

    } else {
      var images = $(this).find(settings.findElement);
    }

    // if there are some images
    if (images.length) {
      // loop through each
      $.each(images, function() {
        // get the parent object
        var parent = $(this).parent().get(0);

        // check it's not already got a caption
        if (parent.tagName.toLowerCase() == settings.containerElement && $(parent).hasClass(settings.containerClass)) {
          return true;
        }

        // initialize the caption variable
        var caption = '';

        // determine the image caption
        for (var i = settings.attributes.length - 1; i >= 0; i--) {
          caption = $(this).attr(settings.attributes[i]);
          
          if (caption) {
            break;
          }
        }

        // if the caption is empty, return unless explicitly allowed
        if (!caption && !settings.allowBlankCaption) {
          return true;
        }

        // get the float
        var location = $(this).css('float');

        // remove the float
        if (settings.removeFloat) {
          $(this).css('float', 'none');
        }

        // if we;ve got to add a class...
        if (settings.imageClass) {
          $(this).addClass(settings.imageClass);
        }

        // wrap the image in a div with the correct styling
        $(this).wrap('<' + settings.containerElement + ' class="' + settings.containerClass + '" style="float: ' + location + ';"></' + settings.containerElement + '>');

        if (caption) {
          // add the caption in after the current element
          $(this).after('<' + settings.titleElement + ' class="' + settings.titleClass + '">' + caption + '</' + settings.titleElement + '>');
        }
      });
    }
  }
})(jQuery);
