// Gaia Ajax Copyright (C) 2008 - 2009 Gaiaware AS. details at http://gaiaware.net/

/* 
 * Gaia Ajax - Ajax Control Library for ASP.NET
 * Copyright (C) 2008 - 2009 Gaiaware AS
 * All rights reserved.
 * This program is distributed under either GPL version 3 
 * as published by the Free Software Foundation or the
 * Gaia Commercial License version 1 as published by
 * Gaiaware AS
 * read the details at http://gaiaware.net
 */

/* ---------------------------------------------------------------------------
   AspectClickable, adds up server-side Click Event support for Controls
   --------------------------------------------------------------------------- */
Gaia.AspectClickable = Class.create(Gaia.AspectWithEvents, {

  initialize: function(parentId, options){
    this.initializeClickable(parentId, options);
  },
  
  // Contructor
  initializeClickable: function(parentId, options){
    
    // Calling base class constructor
    this.initializeAspectWithEvents(parentId, options);
    
    this.options = Object.extend({ 
        enableBubbling: false, 
        evaluate: function(){return true;}
    }, this.options);
    
  },
  
   // overridden to initialize the events specified for AspectClickable
  initEvents : function() {
    for (var i=0, length = this.options.evts.length; i< length; ++i){
      this.addEvent(this.options.evts[i], this.dispathClickEvent.bindAsEventListener(this, this.options.evts[i]));
    } 
  }, 

  // Called when wrapped widget is clicked or double clicked
  // Note that this one might be very useful since it also passes the x and y coordinates
  // 
  // back to the server...
  dispathClickEvent: function(event, evtName){
    if( !this.options.evaluate.bind(this)() )
      return;

    var d = this.getMouseEventData(event);
    Gaia.Control.callAspectMethod.bind(this.getWrappedControl())(
      'ClickMethod', [evtName, d.x, d.y, d.offsetLeft, d.offsetTop, d.controlKeys]);
      
    if (!this.options.enableBubbling) {
      Event.stop(event);
    }
  }

});

Gaia.AspectClickable.browserFinishedLoading = true;
