/** AppCMSDictionary.class.js
*
* @desc cms dictionary functions
**/

var AppCMSDictionary = function()
{
    return {
        //creates the dictionary
        createDictionary : function()
        {
            var aWords = new Array();
            
            var aAlreadyWords = new Array();
            for(i = 0; i < aCMSDictionaryWords.length; i++)
            {
                var oWordToAdd = null;
                aCMSDictionaryWords[i] = aCMSDictionaryWords[i].replace('"[""]"', '"[]"');
                try
                {
                    oWordToAdd = $.evalJSON(aCMSDictionaryWords[i]);
                    
                    for(j = 0; j < oWordToAdd.WB_AlternativeWoerter.length; j++)
                    {
                        var oWord = {
                            sWord : $("<div/>").html(oWordToAdd.WB_AlternativeWoerter[j]).text(),
                            sNormalWord : oWordToAdd.WB_Wort,
                            sDescription : oWordToAdd.WB_Beschreibung
                        }
                        
                        if(aAlreadyWords.indexOf(oWord.sWord) == -1)
                        {
                            aWords.push(oWord);
                            aAlreadyWords.push(oWord.sWord);
                        }
                    }
                }
                catch(e) {}
                
                var oWord = {
                    sWord : $("<div/>").html(oWordToAdd.WB_Wort).text(),
                    sNormalWord : oWordToAdd.WB_Wort,
                    sDescription : oWordToAdd.WB_Beschreibung
                }
                
                if(aAlreadyWords.indexOf(oWord.sWord) == -1)
                {
                    aWords.push(oWord);
                    aAlreadyWords.push(oWord.sWord);
                }
            }
            
            aWords.sort(compareStringLengths);
            
            for(i = 0; i < aWords.length; i++)
            {
                $('body').words(aWords[i]);
            }
            
            $('body span.cms_dictionary_word').mouseover(function(e) {
                $('body').append('<div class="cms_dictionary_layer">' + $(e.currentTarget).find(".cms_dictionary_description").html() + '</div>');
                
                $('.cms_dictionary_layer').css("top", ($(e.currentTarget).offset().top + 16) + "px")
                $('.cms_dictionary_layer').css("left", $(e.currentTarget).offset().left + "px")
            })
            
            $('body span.cms_dictionary_word').mouseout(function(e) {
                $('.cms_dictionary_layer').remove();
            })
        }
    }
}();

$.fn.words = function(oWord) {
    var rx = new RegExp('(?![^<]+>)\\b(' + oWord.sWord + ')\\b', 'gi');
    
    if($('body :not(script)').html().indexOf(oWord.sWord) != -1)
    {
        var aFields = $('#content, #left .content.gartentipp');
        
        for(iNumber = 0; iNumber < aFields.length; iNumber++)
        {
            if($(aFields[iNumber]).parents("#subnav").length == 0)
            {
                aFields[iNumber].innerHTML = aFields[iNumber].innerHTML.replace(rx, '<span class="cms_dictionary_word">$1<span class="cms_dictionary_description"><span class="cms_dictionary_title">' + oWord.sNormalWord + '</span>' + oWord.sDescription + '</span></span>')
            }
        }
    }
}

function compareStringLengths ( a, b )
{
    if ( a.sWord.length > b.sWord.length )
        return -1;
    if ( a.sWord.length < b.sWord.length )
        return 1;
    return 0; // a and b are the same length
}

/**
* Array.indexOf
* 
* function to create the index-of function in older browsers
*/
if(!Array.indexOf){
    Array.prototype.indexOf = function(obj, start){
        var ret = -1;
        for(var i=(start||0); i<this.length; i++){
            if(this[i]==obj){
                ret = i;
            }
        }
        return ret;
    }
}

$(document).ready(function() {
    setTimeout("AppCMSDictionary.createDictionary()", 300);
});
