// 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
 */

/* ---------------------------------------------------------------------------
   Base class for all Aspects
   All aspects should inherit from this class.
   --------------------------------------------------------------------------- */
Gaia.Aspect = Class.create({

  // Override this one in derived classes
  initialize: function(parentId, options){
    this.initializeAspect(parentId, options);
  },


  // Actual implementation of "constructor"
  initializeAspect: function(parentId, options){
    this.parentId = parentId;
    this.options = options || {};
  },


  // Returns the control the aspect is attached to
  getWrappedControl: function(){
    if( !this._parent )
      this._parent = $G(this.parentId);
    return this._parent;
  },

  // Called when forceAnUpdate is called on the owning widget
  forceAnUpdate: function(){
  },


  // Called when widget is "re-initialized" after a forceAnUpdate
  // Pairs together with the Aspect.forceAnUpdate method
  reInit: function(){
  },


  // Override in derived aspects (MUST override)
  destroy: function(){
    throw "Must override method in Aspect not overridden!";
  }
});

/* ---------------------------------------------------------------------------
   AspectWithEvents, contains event lists with automatic initialization 
   --------------------------------------------------------------------------- */ 
Gaia.AspectWithEvents = Class.create(Gaia.Aspect.prototype, {
  
  initialize: function(parentId, options){
    this.initializeAspectWithEvents(parentId, options);
  },

  initializeAspectWithEvents: function(parentId, options){
    this.initializeAspect(parentId, options);
    this.eventList = $H({});
    this.initEvents();
  },
  
  // adding the event with handler and storing the reference in the eventlist
  addEvent : function(evtName, evtHandler) {
    if (!this.eventList.get(evtName)) {
      var idx = this.eventList.set(evtName, evtHandler);
      Element.observe($(this.parentId), evtName, this.eventList.get(evtName));  
    }
  }, 
  
  // cleaning up event handlers
  clearEvents : function() {
   var keys = this.eventList.keys();
   for (var i=0, length = keys.length; i< length; ++i) {
      var evt = keys[i];
      Element.stopObserving($(this.parentId), evt, this.eventList.get(evt));
      this.eventList.unset(evt)
    }
  }, 
  
  // retrieve mouse event data from event. 
  getMouseEventData : function(event) {
    
    var scrollOffset = document.viewport.getScrollOffsets();
    var viewportOffset = Element.viewportOffset(this.getWrappedControl().element);
    var keys = (event.shiftKey ? 1 : 0) | (event.ctrlKey ? 2 : 0) | (event.altKey ? 4 : 0);
    
    return {
      x : Event.pointerX(event), 
      y : Event.pointerY(event),
      offsetLeft : viewportOffset.left + scrollOffset.left, 
      offsetTop  : viewportOffset.top + scrollOffset.top, 
      controlKeys : keys
    }
  },

  
  // override this function to initialize your own events and add them to the event array
  initEvents : function() {
  }, 
  
  reInit: function(){
    this.initEvents();
  },

  destroy: function(){
    this.clearEvents();
  }

}); 


Gaia.Aspect.browserFinishedLoading = true;
