/**
 * @author		Peter Fruehwirt
 * @package		de.wbb-security.lexicon.parser
 * @copyright	www.wbb-security.de 2005-2008 
 */
function LexiconIndex() {

	this.name = new Array();
	this.description = new Array();
	this.itemToLexicon = new Array();

	this.language = new Array();
	
	this.lexicon = new Array();
	this.lexiconLanguage = new Array();
	
	this.init = function(language) {
		this.language = language.split(",");
	}

	this.addItem = function(itemID,lexiconID,item,description) {	
		if(typeof this.name[itemID] == "undefined") {
		//alert(itemID+":"+this.name[itemID]+"-"+this.decode_base64(unescape(item))));
		//this.name[itemID] = this.decode_utf8(unescape(item));
		
			this.name[itemID] = this.cleanItemText(unescape(item));
			this.description[itemID] = this.cleanItemText(unescape(description));
			this.itemToLexicon[itemID] = lexiconID;
		}
	}
	
	this.addLexicon = function(lexiconID,name,languageID) {
		if(typeof this.lexicon[lexiconID] == "undefined") {
			//this.lexicon[lexiconID] = this.decode_utf8(unescape(name));
			this.lexicon[lexiconID] = this.cleanItemText(unescape(name));
			this.lexiconLanguage[lexiconID] = languageID;
		}
	}
	
	this.printItem = function(itemID,element) {
		itemID = unescape(itemID) * 1;
		
		if(SHOW_LEXICON_ITEM == 1) {
			if(this.isVisible(itemID)) {
				this.printFormated(itemID,element);
			} else  {
				//document.write(unescape(this.name[itemID]));
				this.print(unescape(this.name[itemID]),element);
			}
		} else {
			//document.write(unescape(this.name[itemID]));
			this.print(unescape(this.name[itemID]),element);
		}
	}
	
	this.printFormated = function(itemID,element) {
		//document.write('[<font color="#FF0000">'+unescape(this.name[itemID])+'</font>]');

		//var description = this.decode_utf8('<div class=\\\'smallFont light\\\' style=\\\'text-align:left !important;\\\'>'+unescape(this.description[itemID])+'</div>');
		var description = '<div class=\\\'lexiconMessageTextWindowDescription\\\'>'+unescape(this.description[itemID])+'</div>';
		var title = '<div class=\\\'containerHead lexiconMessageTextWindowTitle\\\'><h3><img src=\\\''+RELATIVE_WCF_DIR+'icon/lexiconOpenS.png\\\' alt=\\\'\\\' /> <b>'+unescape(this.name[itemID])+'</b></h3></div>';
		
		
		
		//document.write('<a href="index.php?page=LexiconItem&amp;id='+itemID+SID_ARG_2ND+'" class="lexiconIndexItem" onmouseover="return overlib(\''+description+'\', CAPTION, \''+title+'\');" onmouseout="return nd();">'+unescape(this.name[itemID])+'</a>');
	
		var url = 'index.php?page=LexiconItem&amp;id='+itemID+SID_ARG_2ND+'';
		if(typeof document.getElementById(element+'_url') != "undefined") {
			url = document.getElementById(element+'_url').href;
		}
		
		//alert(url);
	
		this.print('<a href="'+url+'" class="lexiconIndexItem" onmouseover="return overlib(\''+description+'\', CAPTION, \''+title+'\');" onmouseout="return nd();" onclick="return nd();">'+unescape(this.name[itemID])+'</a>',element);	
	}
	
	
	this.cleanItemText = function (text) {
		text = text.replace(/<span class="highlight">(.*)<\/span>/gi,'$1');
		return text;
	}
	
	this.print = function (text,element) {
		if(typeof document.getElementById(element) != "undefined") {
			document.getElementById(element).innerHTML = text;
		} else {
			alert("FatalError: Couldn't find HTML element \""+element+"\"");
		}
	}
	
	this.isVisible = function(itemID) {
		var lang = this.lexiconLanguage[this.itemToLexicon[itemID]];
	
		if(typeof lang == "undefined") {
			//alert("ERROR: language of item #"+itemID+" is undefined!");
			return false;
		}
	
		for(i=0;i<this.language.length;i++) {
			if(lang == this.language[i]) {
				return true;
			}
		}
	
		return false;
	}
	
	this.decode_utf8 = function(utftext) {
		var plaintext = ""; var i=0; var c=c1=c2=0;
		while(i<utftext.length) {
			c = utftext.charCodeAt(i);
			if (c<128) {
				plaintext += String.fromCharCode(c);
                i++;
			} else if((c>191) && (c<224)) {
				c2 = utftext.charCodeAt(i+1);
				plaintext += String.fromCharCode(((c&31)<<6) | (c2&63));
				i+=2;
			} else {
				c2 = utftext.charCodeAt(i+1); c3 = utftext.charCodeAt(i+2);
				plaintext += String.fromCharCode(((c&15)<<12) | ((c2&63)<<6) | (c3&63));
				i+=3;
			}
		}
		
		return plaintext;
    }


	this.encode_utf8 = function(string) {
        string = string.replace(/\r\n/g,"\n");
        var utftext = "";

        for (var n = 0; n < string.length; n++) {

            var c = string.charCodeAt(n);

            if (c < 128) {
                utftext += String.fromCharCode(c);
            }
            else if((c > 127) && (c < 2048)) {
                utftext += String.fromCharCode((c >> 6) | 192);
                utftext += String.fromCharCode((c & 63) | 128);
            }
            else {
                utftext += String.fromCharCode((c >> 12) | 224);
                utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                utftext += String.fromCharCode((c & 63) | 128);
            }

        }

        return utftext;
    }
	
	this.decode_base64 = function(utftext) {
       var string = "";
        var i = 0;
        var c = c1 = c2 = 0;

        while ( i < utftext.length ) {

            c = utftext.charCodeAt(i);

            if (c < 128) {
                string += String.fromCharCode(c);
                i++;
            }
            else if((c > 191) && (c < 224)) {
                c2 = utftext.charCodeAt(i+1);
                string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                i += 2;
            }
            else {
                c2 = utftext.charCodeAt(i+1);
                c3 = utftext.charCodeAt(i+2);
                string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
                i += 3;
            }

        }

        return string;
	}

}

LexiconIndex = new LexiconIndex();
	LexiconIndex.init(LEXICON_VISIBLE_LANG);