/**
 * Tool Tip Words highlighting
 * @creator   Thijs Wiersema
 * @date      sept 2008
 */
var tooltipWords = null;
var TooltipWords = new Class( {

  Implements: [Options],

  /**
   * Options
   */
  options: {
    mOverDelay: 600,
    mOutDelay: 1000
  },

  /**
   * Constructor
   *
   * @param {Object} the words for the tips
   * @param {Object} the mainElement to check for the words
   * @param {Object} Options
   */
  initialize: function(theWords, mainElement, options)
  {
    if($chk($('noToolTip'))) {
      return;
    }

    this.theWords     = theWords;
    this.mainElement  = $(mainElement);
    this.setOptions(options);
    this.wordBoundary = '[^a-z0-9]';
    this.wordInfo     = $H();
    this.timeoutMo    = null;
    this.callWordID   = null;
    this.currentWordID = null;
    this.infoRequest  = new Request( {
      method: 'get',
      autoCancel: true,
      url: '/home/',
      onComplete: function(result) {
        $('infoContent').set('html', result);
        this.wordInfo.set(this.currentWordID, result);
      }.bind(this)
    } )

    // loop the words and check if parent element is allowed
    var tipFound = false;
    this.theWords.each( function(word, id) {
      //word.word = word.word.replace ( /_/g, '\\.\\ ?' );
      //word.word = word.word.replace ( /_/g, '[\\.\\-\\ ]{0,2}' );

      var regex = new RegExp(this.wordBoundary+word.word+this.wordBoundary, "im");
      var innerHtml = this.mainElement.get('html');
      var pos = innerHtml.search(regex);
      while( pos >= 0 ) {
        strLength = innerHtml.match ( regex ) [ 0 ].length-2;
        pos++;
        var tags = innerHtml.substr(0, pos).match( /<([^<>\s]*)(\s[^<>]*)?>?/gi ).reverse();

        var inAllowedTag = true;

        for(i=0; (i+1) < tags.length; i++) {
          var tag = tags[i].toLowerCase();

          if(tag == '' || tag == '<br>' || tag == '<br/>') continue;

          if(tag.test("^<[h1|h2|h3|a|option|object|param]", "i")) {
            inAllowedTag = false;
            break;
          }

          if(tag.test("^</[h1|h2|h3|a|option|object|param]", "i")) {
            break;
          }
        }

        if(inAllowedTag) {
          innerHtml = innerHtml.substr(0, pos) + '<span id="tooltipWord-'+id+'" class="tooltipWords" style="display: inline;" onmouseover="tooltipWords.mouseOver('+id+', '+word.type+');" onmouseout="tooltipWords.mouseOut('+id+');">'+innerHtml.substr(pos, strLength)+'</span>' + innerHtml.substr(pos+strLength);
          tipFound = true;
          break;
        } else {
          posNew = innerHtml.substr(pos+word.word.length).search(regex);
          if(posNew >= 0) {
            pos += posNew+word.word.length;
          } else {
            break;
          }
        }

      }

      if(tipFound) {
        this.mainElement.set('html', innerHtml);
      }

    }.bind(this) );
  },


  mouseOver: function(id, type) {
    $('tooltipWord-'+id).addClass('tooltipWords-hover');

    if( this.currentWordID != null && this.currentWordID != id ){
      $clear(this.timeoutMo);
      this.callWordID = id;
      this.showInfo(type);
    } else if( this.currentWordID == null || this.currentWordID != id && this.callWordID != id) {
      $clear(this.timeoutMo);
      this.callWordID = id;
      this.timeoutMo = ( function(){ this.showInfo(type); } ).delay(this.options.mOverDelay, this);
    } else {
      $clear(this.timeoutMo);
    }
  },


  showInfo: function(type)
  {
    $clear(this.timeoutMo);
    if(this.currentWordID != this.callWordID && $chk( wordEl = $('tooltipWord-'+this.callWordID) )) {

      // show info div popup thingy
      if(type == 1) {
        $('libraryDiv').addClass('libraryDivImage');
      } else {
        $('libraryDiv').removeClass('libraryDivImage');
      }
      $('infoContent').set('html', 'loading...');
      var libEl = $('libraryDiv');

      var posWord = wordEl.getPosition();

      libEl.setStyles( {
        'left': posWord.x,
        'top': posWord.y - (type.toInt() == 1 ? 195 : 149),
        'display': 'block'
      } );

      // check if info has already been fetched
      this.currentWordID = this.callWordID;
      this.callWordID = null;
      if($chk( info = this.wordInfo.get(this.currentWordID) )) {
        $('infoContent').set('html', info);
      // do ajax request to get info
      } else {
        this.infoRequest.send('ajaxTooltip='+this.currentWordID);
      }
    }
  },

  mouseOut: function(id)
  {
    $('tooltipWord-'+id).removeClass('tooltipWords-hover');
    $clear(this.timeoutMo);
    this.timeoutMo = this.hideInfo.delay(this.options.mOutDelay, this);
  },

  hideInfo: function()
  {
    $clear(this.timeoutMo);
    this.currentWordID = null;
    $('libraryDiv').setStyle('display', 'none');
  }

} );