/* ************************************************************************

   Image-loading mixin based on qooxdoo - the new era of web development

   http://qooxdoo.org

   Copyright:
     2004-2008 1&1 Internet AG, Germany, http://www.1und1.de
     2009 Nexst4 GmbH

   License:
     LGPL: http://www.gnu.org/licenses/lgpl.html
     EPL: http://www.eclipse.org/org/documents/epl-v10.php
     See the LICENSE file in the project's top-level directory for details.

   Authors:
     * Fabian Jakobs (fjakobs)
     * Sebastian Werner (wpbasti)
     * adapted by Heiko Stuebner (heiko.stuebner@nexst4.de)

************************************************************************ */

/**
 * Mixin um die Verwendung von Bildern zu erleichtern.
 * Basiert auf den Routinen aus dem Qooxdoo Image-Widget.
 */
qx.Mixin.define("nx4.webwidgets.ui.core.MImageHandling",
{
  events :
  {
    /**
     * Map mit den Feldern: element, width. height
     */
    "imageLoadComplete" : "qx.event.type.Data"
  },

  /*
  *****************************************************************************
     MEMBERS
  *****************************************************************************
  */

  members :
  {

    /**
     * Applies the source to the clipped image instance or preload
     * a image to detect sizes and apply it afterwards.
     *
     * @return {void}
     */
    _styleImageSource : function(el, source)
    {
      if (!source)
      {
        el.resetSource();
        return;
      }

      //FIXME: compat-Layer nach qx 1.0 Umstellung entfernen
      var il = (qx.io2 && qx.io2.ImageLoader) ? qx.io2.ImageLoader : qx.io.ImageLoader;

      // Detect if the image registry knows this image
      if (qx.util.ResourceManager.getInstance().has(source))
        this.__setManagedImage(el, source);
      else if (il.isLoaded(source))
        this.__setUnmanagedImage(el, source);
      else
        this.__loadUnmanagedImage(el, source);
    },


    /**
     * Use the ResourceManager to set a managed image
     *
     * @param el {Element} image DOM element
     * @param source {String} source path
     * @return {void}
     */
    __setManagedImage : function(el, source)
    {
      var ResourceManager = qx.util.ResourceManager.getInstance();

      // Try to find a disabled image in registry
      if (!this.getEnabled())
      {
        var disabled = source.replace(/\.([a-z]+)$/, "-disabled.$1");
        if (ResourceManager.has(disabled))
        {
          source = disabled;
          this.addState("replacement");
        }
        else
        {
          this.removeState("replacement");
        }
      }

      // Optimize case for enabled changes when no disabled image was found
      if (el.getSource() === source)
        return;

      // Apply source
      el.setSource(source);
    },


    /**
     * Use the infos of the ImageLoader to set an unmanaged image
     *
     * @param el {Element} image DOM element
     * @param source {String} source path
     * @return {void}
     */
    __setUnmanagedImage : function(el, source)
    {
      // Apply source
      el.setSource(source);

      // Compare with old sizes and relayout if necessary
      //FIXME: compat-Layer nach qx 1.0 Umstellung entfernen
      var il = (qx.io2 && qx.io2.ImageLoader) ? qx.io2.ImageLoader : qx.io.ImageLoader;
      this.fireDataEvent("imageLoadComplete", 
      {
        element : el,
        width   : il.getWidth(source),
        height  : il.getHeight(source)
      });
    },


    /**
     * Use the ImageLoader to load an unmanaged image
     *
     * @param el {Element} image DOM element
     * @param source {String} source path
     * @return {void}
     */
    __loadUnmanagedImage : function(el, source)
    {
      //FIXME: compat-Layer nach qx 1.0 Umstellung entfernen
      var il = (qx.io2 && qx.io2.ImageLoader) ? qx.io2.ImageLoader : qx.io.ImageLoader;

      // only try to load the image if it not already failed
      if(!il.isFailed(source))
        il.load(source, qx.lang.Function.curry(this.__loaderCallback, el), this);
      else
      {
        if (el != null)
          el.resetSource();
      }
    },


    /**
     * Event handler fired after the preloader has finished loading the icon
     *
     * @param source {String} Image source which was loaded
     * @param imageInfo {Map} Dimensions of the loaded image
     * @return {void}
     */
    __loaderCallback : function(el, source, imageInfo)
    {
      // Output a warning if the image could not loaded and quit
      if (imageInfo.failed)
        this.warn("Image could not be loaded: " + source);

      // Update image (again)
      this.__setUnmanagedImage(el, source);
    }

  }
});
