var CMSTooltip = Class.create({
  initialize: function(id, options)
  {
    this.tooltip = $(id);
    //if (!this.tooltip)
    //  return false;
    this.options = Object.extend({},options);
    
    if (this.options.before_show)  this.before_show = this.options.before_show.bindAsEventListener(this);
    if (this.options.after_show)   this.after_show  = this.options.after_show.bindAsEventListener(this);
    if (this.options.before_hide)  this.before_hide = this.options.before_hide.bindAsEventListener(this);
    if (this.options.after_hide)   this.after_hide  = this.options.after_hide.bindAsEventListener(this);
    if (this.options.before_close) this.before_close= this.options.before_close.bindAsEventListener(this);
    if (this.options.after_close)  this.after_close = this.options.after_close.bindAsEventListener(this);
    this.e_close = this.close.bindAsEventListener(this);
    
    this.cursor = {left: 0, top: 0};
    this.parent = {element: null, left: 0, top: 0, right: 0, bottom: 0, width: 0, height: 0};
    this.o_parent = {element: null, width: 0, height: 0};
    this.position = {left: 0, top: 0};
    this.dimensions = {width: 260, height: 0};
    this.offset = {top: 0, right: 0, bottom: 0, right: 0};
    
    this.tooltip.observe('click', function(event) { 
      try {
        var link_node = Event.element(event).nodeName.toLowerCase() == 'a';
      }
      catch(e) {
        var link_node = false;
      }
      if (!link_node) { Event.stop(event); } else { return true; }
    }, true);
    if ($(id + '_close')) {
      $(id + '_close').observe('click', this.e_close);	
    }
  },

  show: function(e, parent)
  {
    this.close();
    this.setParent(parent || e.element());
    //this.setCursor(e);
	
    if (this.before_show)  this.before_show();
    this._refreshDimensions();
    this.setPosition(e);
    this.tooltip.setStyle({display: 'block', visibility: 'visible'});
    if (this.after_show) { this.after_show(); }
    if (e) { Event.stop(e); }
  },

  hide: function()
  {
    this.tooltip.setStyle({visibility: 'hidden'});
    if (this.after_hide) this.after_hide();
  },
  
  close: function()
  {
    if (this.before_close) this.before_close();
    this.tooltip.setStyle({display: 'none'});
    if (this.after_close) this.after_close();
  },

  setContent: function(html)
  {
    var c = $(this.tooltip.id + '_content')
    if (c) c.innerHTML = html;
  },

  setCursor: function(e)
  {
    if (e)
    {
      this.cursor.top  = Event.pointerY(e);
      this.cursor.left = Event.pointerX(e);
    }	
  },
    
  setParent: function(parent)
  {
    if (!$(parent)) return false;
    this.parent.element = $(parent);
    var dim = parent.getDimensions();
    var pos = this.parent.element.positionedOffset();
   
    this.parent.width  = (dim.width == 0)  ? parseInt(parent.getStyle('width'))  : dim.width;
    this.parent.height = (dim.height == 0) ? parseInt(parent.getStyle('height')) : dim.height;
    this.parent.left = pos.left;
    this.parent.top = pos.top;
    this.parent.right = this.parent.left + this.parent.width;
    this.parent.bottom = parseInt(this.o_parent.height) - this.parent.top - this.parent.height;
  },

  setOffsetParent: function(offset_parent)
  {
    var offset_parent = $(offset_parent);
    if (!offset_parent) return false;
    this.o_parent.element = offset_parent; 
    var dim = Element.getDimensions(this.o_parent.element);
    this.o_parent.width  = (dim.width == 0)  ? parseInt(this.o_parent.element.getStyle('width'))  : dim.width;
    this.o_parent.height = (dim.height == 0) ? parseInt(this.o_parent.element.getStyle('height')) : dim.height;
  },

  setPosition: function(e)	
  {
    var x_limits = [5, this.o_parent.width - 40];
    var y_limits = [document.viewport.getScrollOffsets().top, document.viewport.getScrollOffsets().top + document.viewport.getHeight()];
    if (this.isIE)
    {
      var co = this.parent.element.cumulativeOffset();
      var layerX = this.cursor.left - co.left;  
      var layerY = this.cursor.top - co.top ;
    } else {
      var layerX = e.layerX ; 
      var layerY = e.layerY ;
    }
    var left = layerX;
    //possible left positioning for tooltip
    //right, left, right of the click, left of the click
    var tt_x = [this.parent.right + 5, this.parent.left - this.dimensions.width - 5, left, left - this.dimensions.width];
    
    for (var i=0; i < tt_x.length; i++)
    {
      if ( tt_x[i] > 0 && tt_x[i] + this.dimensions.width < x_limits[1])
      {
      	left = tt_x[i] - 5;
      	break;
      }
    }
    var top = this.parent.top + layerY - 15;
    var top_a = this.dimensions.height - document.viewport.getHeight() + this.cursor.top - document.viewport.getScrollOffsets().top;
    if (top_a > 0) top = top - top_a;

    this.position = {top: top, left: left};
    //this.tooltip.setStyle({top: top + 'px', left: left + 'px'}); //!!IE problems
    this.tooltip.style.left = left + 'px';
    this.tooltip.style.top =  top + 'px';
  },
    
  _refreshDimensions: function()
  {
    var dim = Element.getDimensions(this.tooltip);
    this.dimensions.width  =  (dim.width == 0)  ?  parseInt(this.tooltip.getStyle('width'))  : dim.width;
    this.dimensions.height =  (dim.height == 0) ?  parseInt(this.tooltip.getStyle('height')) : dim.height;
    return this.dimensions;
  },

  isIE: (!window.opera && navigator.userAgent.indexOf('MSIE') != -1)	  
	  
});