// -----------------------------------------------------------------------------------
//
//  Anatips v0.1
//  by Christophe Gimenez - http://www.anaema.com
//  Last Modification: 10/2/2008
//
//  Licensed under the Creative Commons Attribution 2.5 License - http://creativecommons.org/licenses/by/2.5/
//      - Free for use in both personal and commercial projects
//      - Attribution requires leaving author name, author link, and the license info intact.
//
// -----------------------------------------------------------------------------------

if(!ANAEMA) var ANAEMA = {};

ANAEMA.Anatips = Class.create({
  objTip:   undefined,
  tipsArray:  [],

  // Constructor runs on completion of the DOM loading.
  initialize: function() {
    this.tipsArray = [];
    objBody = $$('body')[0];
    var d=document.createElement('div');
    d.id='anatips_tip';
    this.objTip = $(objBody.appendChild(d));
    this.setupTooltips();
  },

  // Scans the DOM to find all elements with an 'anatips' class attribute and bind them to the 3 events handlers
  setupTooltips: function() {
    me = this;
    cnt = 0;
    $$('.l_take','.anatips').each(function(el){
      if(el.title && el.title.length > 0) {
        el.observe('mouseover', (function(event) { event.stop; me.handleMouseOver(event) }).bind(this));
        el.observe('mousemove', (function(event) { event.stop; me.handleMouseMove(event) }).bind(this));
        el.observe('mouseout',  (function(event) { event.stop; me.handleMouseOut(event) }).bind(this));
        el.observe('click',  (function(event) { event.stop; me.handleMouseOut(event) }).bind(this));
        me.tipsArray[cnt] = el.title;
        el.tip_num = cnt++;
        el.removeAttribute('title');
      }
    })
  },

  // Called when mouse enters the target : setup and show container
  handleMouseOver: function(event){
    //target = event.findElement('.l_take');

    target=Event.element(event);
    if (target && this.objTip) {
      this.objTip.innerHTML = this.tipsArray[target.tip_num];
      this.objTip.style.visibility = "visible";
      this.objTip.style.left = (event.pointerX() + 10) + 'px';
      this.objTip.style.top = (event.pointerY() - this.objTip.getHeight()) + 'px';
      image = event.findElement('.anatips img'); // Disable inner images's alt attribute (prevents IE auto alt tooltips)
      if(image) image.alt = '';
      //Shadower.shadow(this.objTip);
    }
  },

  // Called when mouse moves over the target : move the tip container
  handleMouseMove: function(event){
    //target = event.findElement('.l_take,.anatips');
    //return;
    target=Event.element(event);
    if (!(target.hasClassName('l_take') || target.hasClassName('anatips'))) target=false;

    if (target && this.objTip) {
      this.objTip.style.left = (event.pointerX() + 10) + 'px';
      this.objTip.style.top = (event.pointerY() - 40) + 'px';
      //Shadower.shadow(this.objTip);
    }
  },

  // Called when mouse leaves the target : hide the tip container
  handleMouseOut: function(event){
    this.objTip.style.visibility = "hidden";
    //Shadower.hideshadow(this.objTip);
  }

});

document.observe('dom:loaded', function () { ANAEMA.tips = new ANAEMA.Anatips(); });
