/*
  File: CBEvent.js
  Date: 20050524
  Author: nils@onsite.no
  Description: cross browser event object
  Version: 0.1
  Note: this js needs to evolve...... to be big and strong....hahahahahhohohooooo
*/

function CBEvent(obj,evt) {
  if (evt && evt.evt) { // evt is a CBEvent already
    return evt;
  }
  // XXX 200070202 nilschd this is not a good detection routine, should use object detection. But I'm in a hurry...
  this.safari = (navigator.userAgent.indexOf('Safari')>-1?true:false);

  if (evt == null) {
    if (obj == null) {
      alert("CBEvent: no obj given"); return;
    }
    evt = obj.event;
    // XXX this is not perfect, nilschd. 
    if (evt == null) {
      evt = window.event;
    }
  }

  this.evt = evt;

  return this;
}

// returns the clicked button id on the mouse
CBEvent.prototype.getButton = function() {
  var buttonId;
  if (this.evt.which) {
    buttonId = this.evt.which - 1;
    // XXX: See "Mouse button ID values in various browser" table at http://unixpapa.com/js/mouse.html
  }
  else if (this.evt.button) {
    switch (this.evt.button) {
      case 1:  buttonId = 0; break; // Left   mouse button
      case 2:  buttonId = 2; break; // Right  mouse button
      case 4:  buttonId = 1; break; // Middle mouse button
      default: buttonId = null;
    }
  }
  return buttonId;
}

CBEvent.prototype.getKeyCode = function() {
  return this.evt.which || this.evt.keyCode;
}

CBEvent.prototype.getType = function() { 
  return this.evt.type;
}

CBEvent.prototype.getTarget = function() {
  var t = this.evt.target || this.evt.srcElement || "";
  
  if (t.nodeType == 3) { // defeat Safari bug, ref: http://www.quirksmode.org/js/events_properties.html
    t = t.parentNode;
  }
  return t;
}

// Returns the x position where the event occurred relative to the left visible edge of the current frame/window
CBEvent.prototype.getX = function() {
  return this.evt.clientX;
}

// Returns the y position where the event occurred relative to the visible top of the current frame/window
CBEvent.prototype.getY = function() {
  return this.evt.clientY;
}

CBEvent.prototype.preventDefault = function() {
  if (this.evt.preventDefault) {
    this.evt.preventDefault();
  }
  else {
    this.evt.returnValue = false;
  }
}

CBEvent.prototype.stopPropagation = function() {
  if (this.evt.stopPropagation) {
    this.evt.stopPropagation();
  }
  else {
    this.evt.cancelBubble = true;
  }
}

// Returns the x position where the event occurred relative to the "physical" edge of the current window (getX + scrollWidth)
CBEvent.prototype.getLayerX = function() {
  return this.evt.layerX || this.evt.offsetX;
}

// Returns the y position where the event occurred relative to the "physical" top of the current window (getY + scrollHeight)
CBEvent.prototype.getLayerY = function() {
  return this.evt.layerY || this.evt.offsetY;
}

CBEvent.prototype.getRelatedTarget = function() {
  return this.evt.relatedTarget || this.evt.toElement || this.evt.fromElement;
}

/* Returns the window where the event occurred */
CBEvent.prototype.getWindow = function() {
  var doc = this.getDocument();
  var win = doc.defaultView || doc.parentWindow;
  if (!win) {
    return alert("Browser doesn't support defaultView or parentWindow");
  }
  return win;
}

/* Returns the document where the event occurred */
CBEvent.prototype.getDocument = function() {
  return this.getTarget().ownerDocument;
}

CBEvent.prototype.test = function() {
  return "test";
}

