/**
 * Licensed under the Apache License, Version 2.0 (the 'License');
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an 'AS IS' BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

goog.provide('org.apache.flex.html.beads.controllers.ItemRendererMouseController');

goog.require('goog.events.Event');
goog.require('goog.events.EventType');
goog.require('org.apache.flex.core.IBeadController');



/**
 * @constructor
 * @implements {org.apache.flex.core.IBeadController}
 */
org.apache.flex.html.beads.controllers.ItemRendererMouseController = function() {
};


/**
 * Metadata
 *
 * @type {Object.<string, Array.<Object>>}
 */
org.apache.flex.html.beads.controllers.ItemRendererMouseController.prototype.FLEXJS_CLASS_INFO =
    { names: [{ name: 'ItemRendererMouseController',
                qName: 'org.apache.flex.html.beads.controllers.ItemRendererMouseController' }],
      interfaces: [org.apache.flex.core.IBeadController] };


Object.defineProperties(org.apache.flex.html.beads.controllers.ItemRendererMouseController.prototype, {
    /** @export */
    strand: {
        /** @this {org.apache.flex.html.beads.controllers.ItemRendererMouseController} */
        set: function(value) {
            this.strand_ = value;

            goog.events.listen(this.strand_.element, goog.events.EventType.MOUSEOVER,
                goog.bind(this.handleMouseOver, this));

            goog.events.listen(this.strand_.element, goog.events.EventType.MOUSEOUT,
                goog.bind(this.handleMouseOut, this));

            goog.events.listen(this.strand_.element, goog.events.EventType.MOUSEDOWN,
                goog.bind(this.handleMouseDown, this));

            goog.events.listen(this.strand_.element, goog.events.EventType.MOUSEUP,
                goog.bind(this.handleMouseUp, this));
        }
    }
});


/**
 * @export
 * @param {Object} event The mouse event that triggered the hover.
 */
org.apache.flex.html.beads.controllers.ItemRendererMouseController.prototype.handleMouseOver = function(event) {

  this.strand_.hovered = true;

  var newEvent = new goog.events.Event('rollover');
  newEvent.target = this.strand_;
  this.strand_.itemRendererParent.dispatchEvent(newEvent);
};


/**
 * @export
 * @param {Object} event The mouse-out event.
 */
org.apache.flex.html.beads.controllers.ItemRendererMouseController.prototype.handleMouseOut = function(event) {

  this.strand_.hovered = false;

  var newEvent = new goog.events.Event('rollout');
  newEvent.target = this.strand_;
  this.strand_.itemRendererParent.dispatchEvent(newEvent);
};


/**
 * @export
 * @param {Object} event The mouse-down event.
 */
org.apache.flex.html.beads.controllers.ItemRendererMouseController.prototype.handleMouseDown = function(event) {

  // ??
};


/**
 * @export
 * @param {Object} event The mouse-up event that triggers the selection.
 */
org.apache.flex.html.beads.controllers.ItemRendererMouseController.prototype.handleMouseUp = function(event) {

  var newEvent = new goog.events.Event('selected');

  // normally you do not - and should not - change the target of an event,
  // but these events do not propagate nor bubble up the object tree, so
  // we have to force the event's target to be this item renderer instance.

  newEvent.target = this.strand_;

  // since the event is not going to up the chain, we also have to dispatch
  // it against its final destination.

  this.strand_.itemRendererParent.dispatchEvent(newEvent);
};

@@ -2219,36 +2219,32 @@
 param %7BObject%7D e
-vent
  The mouse event
@@ -2371,36 +2371,32 @@
 ver = function(e
-vent
 ) %7B%0A%0A  this.stra
@@ -2433,36 +2433,29 @@
 Event = new 
-goog.events.
+Mouse
 Event('rollo
@@ -2472,37 +2472,201 @@
 ewEvent.
-target = this.strand_
+initMouseEvent('rollover', false, false,%0A    e.view, e.detail, e.screenX, e.screenY,%0A    e.clientX, e.clientY, e.ctrlKey, e.altKey,%0A    e.shiftKey, e.metaKey, e.button, e.relatedTarget)
 ;%0A  this
@@ -2750,36 +2750,32 @@
 param %7BObject%7D e
-vent
  The mouse-out e
@@ -2884,28 +2884,24 @@
 = function(e
-vent
 ) %7B%0A%0A  this.
@@ -2943,36 +2943,29 @@
 Event = new 
-goog.events.
+Mouse
 Event('rollo
@@ -2981,37 +2981,200 @@
 ewEvent.
-target = this.strand_
+initMouseEvent('rollout', false, false,%0A    e.view, e.detail, e.screenX, e.screenY,%0A    e.clientX, e.clientY, e.ctrlKey, e.altKey,%0A    e.shiftKey, e.metaKey, e.button, e.relatedTarget)
 ;%0A  this

use native event for rollover/rollout