/**
 * license-display helper
 *
 * @since   2008-11-12
 * @require jQuery
 * @bugs    support only img at present
 */
var LicenseDisplay = {
  /**
   * license info
   *
   * { 'license name': [ 'term', 'uri' ],
   *   'license name': [ 'term', 'uri' ],
   *   ...
   * }
   */
  licenses: {
    'cc-by-sa': [
      'Creative Commons : Attributes, Share Alike',
      'http://creativecommons.org/licenses/by/3.0/deed.ja'
    ]
  },
  common_display: undefined,
  config: { 'format': '<ul><li></li></ul>',
            'speed':  'normal',
            'delay':  2500
  },

  /**
   * main
   */
  run: function( konfig ) {
    var self = this;
    jQuery(function($) {
      if ( typeof konfig != 'undefined' ) {
        for ( var i in konfig ) {
          if ( i in self.config ) {
            self.config[i] = konfig[i];
          }
        }
      }
      self.find_licensed_img().each( function() {
        var obj   = $(this);
        var term  = self.license_term( self.extract_license( obj.attr( 'class' ) ) );
        var src   = obj.attr( 'src' );
        var d     = self.find_one_display( src );
        var added = false;
        if ( typeof d == 'object' ) {
          obj.bind("mouseover", function() {
              if ( !self.common_display && !added ) {
                d.append( term );
                self.wrap_term( term, self.config['format'] );
                added = true;
              }
              d.slideDown( self.config['speed'] );
            });
          obj.bind("mouseout", function() {
            setTimeout( function() {
              d.slideUp( self.config['speed'], function() {
                if ( self.common_display ) {
                  d.empty();
                }
                });
              }, self.config['delay']
            );
          });
        }
      });
    });
  },

  /**
   * wrap term text with `format'
   *
   * format accepts either HTML element name or HTML string
   *
   * @since 2008-11-13
   * @param string  term jQuery object
   * @param string  element
   * @return string jQuery object
   */
  wrap_term: function( term, format ) {
    if ( format.match( /[<>]/ ) ) {
      return term.wrap( format );
    } else {
      return term.wrap( document.createElement( format ) );
    }
  },

  /**
   * extract license name from attribute 'class'
   *
   * @since  2008-11-12
   * @param  string klass
   * @return string
   */
  extract_license: function( klass ) {
    return klass.split( ';', 2 )[1];
  },
    
  /**
   * extract src path from attribute 'id'
   *
   * @since  2008-11-13
   * @param  string id
   * @return string
   */
  extract_src: function( id ) {
    return id.split( '=', 2 )[1];
  },

  /**
   * return license term object specified by license name
   *
   * @since  2008-11-14
   * @param  array info
   * @return object
   */
  license_term: function( license ) {
    var self = this;

    if ( license in self.licenses ) {
      var info = self.licenses[license];
      if ( info.length == 2 ) {
        return jQuery( "<a rel='license' href='" + info[1] + "'>" + info[0] + "</a>" );
      } else {
        return jQuery( info[0] );
      }
    }
  },

  /**
   * @since  2008-11-13
   * @return object
   */
  find_licensed_img: function() {
    return $("img[class^=licensed;]");
  },

  /**
   * @since  2008-11-13
   * @param  string
   * @return object
   */
  find_one_display: function( src ) {
    var self = this;

    if ( self.common_display ) {
      return self.common_display;
    }

    var obj = undefined;
    if ( src ) {
      var id = "[class=license-display][id=src=" + src + "]";
      obj = $("[class=license-display][id=src=" + src + "]" );
      if ( !obj.length ) {
        obj = undefined;
      }
    }
    if ( !obj ) {
      obj = self.find_common_display();
      if ( obj ) {
        self.common_display = obj;
      }
    }

    return obj;
  },
  
  /**
   * return one class="license-display" object
   *
   * @since  2008-11-13
   * @return object
   */
  find_common_display: function() {
    var displays = $(".license-display");
    if ( displays.length == 1 ) {
      return displays[0];
    }
  }
};
