/*
Sweet Titles (c) Creative Commons 2005
http://creativecommons.org/licenses/by-sa/2.5/
Author: Dustin Diaz | http://www.dustindiaz.com
*/
TipsGenerator = Class.create();
TipsGenerator.prototype = {
  initialize : function(className, url, options) {
    this.xCord = 0;                           // @Number: x pixel value of current cursor position
    this.yCord = 0;                           // @Number: y pixel value of current cursor position
    if ( !document.getElementById || !document.createElement || !document.getElementsByClassName ) {
      return;
    }
    this.options = {
      builder:      this.ajaxRequest.bind(this),
      opacity:      80
    }
    Object.extend(this.options, options || {});

    this.url = url;
    this.canCache = true;
    this.cachedTips = { };
    this.nextId2Request = this.currentRequest = null;
    this.onCompleteAjaxRequest = this.ajaxAnswer.bind(this);
	this.iFrame = document.createElement('iframe');
	document.getElementsByTagName('body')[0].appendChild(this.iFrame);
	this.tip = document.createElement('div');
	this.tip.className = 'toolTip';
	document.getElementsByTagName('body')[0].appendChild(this.tip);
	this.iFrame.style.border = 'none';
	this.iFrame.style.position = 'absolute';
	this.iFrame.style.opacity = '.0';
    this.iFrame.style.filter = "alpha(opacity:0)";
    
	this.iFrame.style.top = this.tip.style.top = '0';
	this.iFrame.style.visibility = this.tip.style.visibility = 'hidden';
	this.tip.style.zIndex = this.iFrame.style.zIndex+1;
	var elemsWithClassName = document.getElementsByClassName(className);
	var curLen = elemsWithClassName.length;
	for (var i=0; i<curLen; i++ ) {
      var elem = elemsWithClassName[i];
      if (elem.id != null) {
        elem.tipsGenerator = this;
        elem.onmouseover = this.tipOver.bindAsEventListener(elem);
        elem.onmouseout = this.tipOut.bindAsEventListener(elem);
      }
    }
  },
  updateXY : function(e) {
    if ( document.captureEvents ) {
	    this.xCord = e.pageX;
	    this.yCord = e.pageY;
	  } else if ( window.event.clientX ) {
	    this.xCord = window.event.clientX + document.documentElement.scrollLeft;
	    this.yCord = window.event.clientY + document.documentElement.scrollTop;
	  }
  },
  clearTip: function(mgr) {
    if (mgr.tID) {
      clearTimeout(mgr.tID);
    }
    if (mgr.opacityID) {
      clearTimeout(mgr.opacityID);
    }
    mgr.tID = mgr.opacityID = null;
    mgr.tip.style.visibility = mgr.iFrame.style.visibility = 'hidden';
  },
  tipOut: function() {
    var mgr = this.tipsGenerator;
    mgr.clearTip(mgr);
  },
  tipOver : function(e) {
    var mgr = this.tipsGenerator;
    e = window.event || e;
    mgr.tID = setTimeout((function() {mgr.tipShow()}).bind(mgr), 500);
    mgr.updateXY(e);
    mgr.options['builder'](this.id);
  },
  getScreenView: function() {
    var scrOfX = 0, scrOfY = 0;
    if( typeof( window.pageYOffset ) == 'number' ) {
      //Netscape compliant
      scrOfY = window.pageYOffset;
      scrOfX = window.pageXOffset;
    } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
      //DOM compliant
      scrOfY = document.body.scrollTop;
      scrOfX = document.body.scrollLeft;
    } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
      //IE6 standards compliant mode
      scrOfY = document.documentElement.scrollTop;
      scrOfX = document.documentElement.scrollLeft;
    }
    var myWidth = 0, myHeight = 0;
    if( typeof( window.innerWidth ) == 'number' ) {
      //Non-IE
      myWidth = window.innerWidth;
      myHeight = window.innerHeight;
    } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
      //IE 6+ in 'standards compliant mode'
      myWidth = document.documentElement.clientWidth;
      myHeight = document.documentElement.clientHeight;
    } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
      //IE 4 compatible
      myWidth = document.body.clientWidth;
     myHeight = document.body.clientHeight;
    }
    return [ scrOfX, scrOfY, scrOfX + myWidth, scrOfY + myHeight ];
  },
  tipShow : function() {
    var scrX = Number(this.xCord);
    var scrY = Number(this.yCord);
    var tp = parseInt(scrY+15);
    var lt = parseInt(scrX+10);
	var addy = '';
	var access = '';

    if (null == this.htmlTip || '' == this.htmlTip) {
      this.tID = setTimeout((function() {this.tipShow()}).bind(this), 100);
      return;
    }
    this.tip.innerHTML = this.htmlTip;
	this.iFrame.style.width = this.tip.offsetWidth; this.iFrame.style.height = this.tip.offsetHeight;
	var screenView = this.getScreenView();
	var	left, top;
	if ( screenView[2] < parseInt(this.tip.offsetWidth)+lt) {
	  //	Cannot go to the right of the mouse, go left
	  left = parseInt(lt-(this.tip.offsetWidth+10));
	} else {
      left = lt;
	}
	this.tip.style.left = this.iFrame.style.left = left + 'px';
    if (screenView[3] < parseInt(this.tip.offsetHeight)+tp ) {
	  //	Cannot go to the bottom of the mouse, go above
       top = parseInt(tp-(this.tip.offsetHeight+10));
	} else {
      top = tp;
	}
	this.tip.style.top = this.iFrame.style.top =  top + 'px';
	// this.tip.innerHTML = this.htmlTip + "screenView[3]: " + screenView[3] + ", "
	this.tip.style.visibility = this.iFrame.style.visibility = 'visible';
	this.tip.style.opacity = '.1';
	this.tipFade(10);
  },
  tipFade: function(opac) {
	var passed = parseInt(opac);
	var newOpac = parseInt(passed+10);
    if ( newOpac < this.options['opacity'] ) {
      this.opacityID = setTimeout((function() {this.tipFade(newOpac)}).bind(this), 20);
	} else {
      newOpac = this.options['opacity'];
	}
    this.setOpacity(newOpac);
  },
  setOpacity: function(opac) {
    this.tip.style.opacity = '.' + opac;
    this.tip.style.filter = "alpha(opacity:"+opac+")";
  },
  ajaxAnswer: function(answer) {
    this.currentRequest = null;
    answer = eval('(' + answer.responseText + ')');
    var id = answer['id'];
    var html = answer['html'];
    if (this.canCache) this.cachedTips[id] = html;
    if (this.requestedId == id) {
      this.htmlTip = html;
    }
    this.requestedId = null;
    if (this.nextId2Request != null) {
      this.callAjax(this.nextId2Request);
    }
  },
  ajaxRequest: function(id) {
    if (this.requestedId == id) { return; }
    this.requestedId = id;
    var html = this.cachedTips[id];
    if (null != html) {
      this.htmlTip = html;
    } else {
      this.htmlTip = null;
      if (this.currentRequest != null) {
        this.nextId2Request = id;
      } else {
        this.callAjax(id);
      }
    }
  },
  callAjax: function(id) {
    this.requestedId = id;
    this.nextId2Request = null;
    this.currentRequest = new Ajax.Request(this.url + id, {method: 'get', onComplete: this.onCompleteAjaxRequest} );
  }
}
