/* adds a tooltip */
Tooltip = Behavior.create({
  initialize: function() {
    this.text = this.element.getAttribute('title');
    this.tip = $div({ id : 'case-tooltip' }, this.text);
  },
  onmousemove: function(e) {
   this.tip.style.left = e.pointerX()-55 + 'px';
   this.tip.style.top = e.pointerY()-70 + 'px';
  },
  onmouseout: function(e) {
  	this.tip.remove();
    this.element.title = this.text;
  },
  onmouseover: function(e) {
    document.body.appendChild(this.tip);
    // remove title to disable browser's default tooltip
    this.element.title = '';
  }
});

/* manages links */
Link = Behavior.create({
  initialize: function() {
  	this.url = this.element.getAttribute('href');
  	this.rel = this.element.getAttribute('rel');
  },
  onclick: function(e) {
    // open link in opener window
  	if (this.rel == 'opener') {
      if (opener) {
        opener.location.href = this.url;
        e.stop();
      }
    // popup
	  } else if (this.rel == 'popup') {
      window.open(this.url, 'case_popup', 'width=532, height=580, scrollbars=yes, menubar=yes, location=no, resizable=yes');
      e.stop();
    } else {
      // normal link
    }
  }
});

/* add tooltip rule to all case links */
Event.addBehavior({ 
  '.cases a.case' : Tooltip
});

/* add link rule to all case links */
Event.addBehavior({ 
  '.cases a' : Link
});

/* add link rule to all links in case popup */
Event.addBehavior({ 
  '.case-popup a' : Link
});
