/*
 *@author: Dennis Nissen (post@dennisnissen.de)
 *@description: Highlights a list of word in given elements and displays a tooltip with description of this word.
 *Words and Descriptions are either fetched via Ajax-call or submitted statically by using different helper Objects.
 *
 *@depends: jquery.tooltip.js
 *
 *@todo : let User choose, which tooltip-class to use
 *@todo : globaly highlight words only once - not just in Element
 *@todo : ability to provide a template for tooltip content
 *
 */

jQuery.fn.LiveGlossar = function(options){
   //extend default options with user defined options
   settings = jQuery.extend({
      highlightStyle: "LiveGlossarHighlight", //set css-class of highlighted Word
      replaceAll: false,                      //replace all words in element, not just the first
      caseSensitive: true,                    //by default replacing is done case sensitive
      glossarWords: new Array(),              //list of words
      descriptionFetcher: function(){         //function to fetch description for a word - override it !?
         return this.innerHTML;
      },
      tooltipOptions:{}                       //options for tooltip-plugin
   },options);

   //same for tooltipOptions to provide user the ability to change behaviour of jquery.tooltip
   tooltipOptions = jQuery.extend({
     bodyHandler: settings.descriptionFetcher
   },settings.tooltipOptions);

   //if user specified a function to fetch words, execute it
   if (typeof settings.glossarWords == 'function'){
      settings.glossarWords = settings.glossarWords();
   }

   

   
   //surround glossar words with span to identify, style and display tooltip
   $(this).each(function(){
      var content = $(this).html();
      if (content != null){
         var opt = settings.replaceAll == true ? "g":""; //set casesensitive and "all" flags for regexp
         opt += settings.caseSensitive == false ? "i":"";
         $(settings.glossarWords).each(function(){
            word = decodeURI(this);
            content = content.replace(eval('/(^|[^\\wäöüßÄÖÜß]+)'+word+'($|[^\\wäöüßÄÖÜß]+)/'+opt),"$1<span LiveGlossar=1>"+word+"</span>$2");
         });
         $(this).html(content);
      }
   });
   //style words, add tooltip and return all of them
   return $("span[LiveGlossar]").each(function(){
                                 this.className = settings.highlightStyle;
                           }).tooltip(tooltipOptions);
   //return $("span[LiveGlossar]");
}

//collection of "helper"-classes to fetch Words / Description either with Ajax-Request or Offline via static call




