var global_variable = 1;
var mouse_x = 0;
var mouse_y = 0;
var Tooltip = Class.create();
Tooltip.prototype = {
  initialize: function(element, tool_tip) {
    var options = Object.extend({
      default_css: false,
      margin: "0px",
      padding: "5px",
      backgroundColor: "#d6d6fc",
      min_distance_x: 5,
      min_distance_y: 5,
      delta_x: 0,
      delta_y: 0,
      zindex: 1000,
      delay: 2000
      
    }, arguments[2] || {});

    this.element      = $(element);
    this.options      = options;

    // use the supplied tooltip element or create our own div
    if($(tool_tip)) {
      this.tool_tip = $(tool_tip);
    } else {
      this.tool_tip = $(document.createElement("div")); 
      document.body.appendChild(this.tool_tip);
      this.tool_tip.addClassName("tooltip");
      this.tool_tip.appendChild(document.createTextNode(tool_tip));
    }

    // hide the tool-tip 
    //this.tool_tip.hide();

    this.eventMouseOver = this.showTooltip.bindAsEventListener(this);
    this.eventMouseOut   = this.hideTooltip.bindAsEventListener(this);
    this.eventMouseMove  = this.moveTooltip.bindAsEventListener(this);
    this.registerEvents();
  },

  destroy: function() {
    // Event.stopObserving(this.element, "mouseover", this.eventMouseOver);
    // Event.stopObserving(this.element, "mouseout", this.eventMouseOut);
    // Event.stopObserving(this.element, "mousemove", this.eventMouseMove);
  },

  registerEvents: function() {
    Event.observe(this.element, "mouseover", this.eventMouseOver);
    Event.observe(this.element, "mouseout", this.eventMouseOut);
    Event.observe(this.element, "mousemove", this.eventMouseMove);
  },

  moveTooltip: function(event){
  Event.stop(event);
  // get Mouse position
    if(global_variable == 1) {
        mouse_x = Event.pointerX(event);
        mouse_y = Event.pointerY(event);
        global_variable++;
    }
    
    this.setStyles(mouse_x-325,mouse_y+20); 

  // decide if wee need to switch sides for the tooltip
  // var dimensions = Element.getDimensions( this.tool_tip );
  //  var element_width = dimensions.width;
  //  var element_height = dimensions.height;
  // 
  //  if ( (element_width + mouse_x) >= ( this.getWindowWidth() - this.options.min_distance_x) ){ // too big for X
  //    mouse_x = mouse_x - element_width;
  //    // apply min_distance to make sure that the mouse is not on the tool-tip
  //    mouse_x = mouse_x - this.options.min_distance_x;
  //  } else {
  //    mouse_x = mouse_x + this.options.min_distance_x;
  //  }
  // 
  //  if ( (element_height + mouse_y) >= ( this.getWindowHeight() - this.options.min_distance_y) ){ // too big for Y
  //    mouse_y = mouse_y - element_height;
  //    // apply min_distance to make sure that the mouse is not on the tool-tip
  //    mouse_y = mouse_y - this.options.min_distance_y;
  //  } else {
  //    mouse_y = mouse_y + this.options.min_distance_y;
  //  } 
   //  // _Mtip=setTimeout("buildTips('"+_ttext+"',1)",_menuOpenDelay+M_ToolTipDelay)
  //  // now set the right styles
  //  // changed..
  //  this.setStyles(mouse_x-320,mouse_y-20)
  //  // this.setStyles(150, 250);
},
  
    
  showTooltip: function(event) { 
    Event.stop(event);
    this.moveTooltip(event);
    new Element.show(this.tool_tip);
  },
  
  setStyles: function(x, y){
    // set the right styles to position the tool tip
    Element.setStyle(this.tool_tip, { position:'absolute',
                      top:y + this.options.delta_y + "px",
                      left:x + this.options.delta_x + "px",
                      zindex:this.options.zindex
                    });
  
    // apply default theme if wanted
    if (this.options.default_css){
        Element.setStyle(this.tool_tip, { margin:this.options.margin,
                                          padding:this.options.padding,
                                          backgroundColor:this.options.backgroundColor,
                                          zindex:this.options.zindex
                        }); 
    } 
  },

  hideTooltip: function(event){
    for (var i=0;i<=300000000;i++)
    {
      
    }
    new Element.hide(this.tool_tip);

  },


  
  getWindowHeight: function(){
    var innerHeight;
    if (navigator.appVersion.indexOf('MSIE')>0) {
      innerHeight = document.body.clientHeight;
    } else {
      innerHeight = window.innerHeight;
    }
    return innerHeight; 
  },
 
  getWindowWidth: function(){
    var innerWidth;
    if (navigator.appVersion.indexOf('MSIE')>0) {
      innerWidth = document.body.clientWidth;
    } else {
      innerWidth = window.innerWidth;
    }
    return innerWidth;  
  }

}

//Pop-up menu timer helper function
var menu_tool_tip = [];
function menu_hide(o)
{ 
    if (document.dispatchEvent) 
    {
        var evt = document.createEvent("MouseEvents");
        evt.initMouseEvent("mouseout", true, true, window, 0, 0, 0, 0, 0, 
            false, false, false, false, 0, o.parentNode);
        o.dispatchEvent(evt);
    }
    else 
        o.fireEvent('onmouseout');
    /*
     W3C
     event.initMouseEvent(type, canBubble, cancelable, view, 
         detail, screenX, screenY, clientX, clientY, 
         ctrlKey, altKey, shiftKey, metaKey, 
         button, relatedTarget);
     */
}
//Added a pop-up menu object class. [2009-07-22, A.Lewis]
var MenuToolTip = Class.create(Tooltip, {
    hideTooltip: function($super, event) {
        //get this.tool_tip's form (if any) 
        //and set the form's "onsubmit" 
        //and this.tool_tip's "onmouseleave"
        //to hide this.tool_tip
        var d = this.tool_tip;
        var f = d.select('form')[0];
        if(f)f.onsubmit = d.hide;
        if(d.onmouseleave)
            d.onmouseleave = d.hide;
        else //mimic onmouseleave
            d.onmouseout = function(event){
                if (!event) var event = window.event;
                var tg = (window.event) ? event.srcElement : event.target;
                if (tg.nodeName != 'DIV') return;
                var reltg = (event.relatedTarget) ? event.relatedTarget : event.toElement;
                while (reltg != tg && reltg.nodeName != 'BODY')
                    reltg = reltg.parentNode;
                if (reltg == tg) return;
                d.hide();
            }
    },
    
    showTooltip: function($super, event) {
        $super(event);
        this.moveTooltip(event); //IE needs this here again to refresh layout of the div
        this.tool_tip.focus();
        try{f.elements[0].focus()}
        catch(err){/*fail silently*/}
        // timer on menu
        this.delay = 3000;
        menu_tool_tip[0] = this.tool_tip;
        menu_tool_tip[1] = setTimeout("menu_hide(menu_tool_tip[0])", this.delay);
        menu_tool_tip[0].onmousemove = function(event){ window.clearTimeout(menu_tool_tip[1]); }
    },
    
    moveTooltip: function(event){ 
        Event.stop(event);
        // get Mouse position
        if(global_variable == 1) {
            mouse_x = Event.pointerX(event);
            mouse_y = Event.pointerY(event);
            global_variable++;
        }
    
        var dimensions = Element.getDimensions( this.tool_tip );
        var menu_width = dimensions.width;
        var menu_height = dimensions.height;
        var shift_left = 0;
        var shift_up = 0;
        /*
         * TODO: detect if the menu is over the right side viewport
         * limit or below the bottom viewport limit and shift it left
         * or above the mouse pointer if need be
         */
        this.setStyles(mouse_x - shift_left, mouse_y + shift_up); 
    },
    
    setStyles: function(x, y){
        // set the right styles to position the tool tip
        Element.setStyle(this.tool_tip, {
            position: 'absolute',
            zindex: this.options.zindex
        })
    }
});



