
var glossary_entries = new Array();

	
var locHref = window.location.href;

if (locHref.search(/\?/) > -1) {
	locHref += '&';
}
else {
	locHref += '?';
}

$(document).ready( function(){



	// get the glossary-list
	$.get( locHref  + 'type=' + gmfinlineglossary_type_num , { glossary: "_getlist" },
	  function(data){
	    glossary_entries = data.split(',');

		// empty "data" results in a one item array, this must be emptied
		if (glossary_entries.length == 1){
			if (glossary_entries[0].length == 0) glossary_entries.pop();
		}

		if (glossary_entries.length > 0){
			gmfinlineglossary_searchForGlossaryWords();	
		}

	  }
	);


	// add the glossary content as hidden element
	$('body').append('<div id="gmfinlineglossary_info_div" class="gmfinlineglossary_info_box">glossary</div>');
	gmfinlineglossary_hideInfoBox();

});


function gmfinlineglossary_setInfoBoxPosition(element){
	var elementPosition = $(element).offset();
	//var elementWord = element.innerHTML;
	var elementWord = $(element).text();
	elementPosition.left += $(element).width() + 3;
		
	$('#gmfinlineglossary_info_div').text('Lade Glossar ...')
		.load( locHref + 'type=' + gmfinlineglossary_type_num , 'glossary='+elementWord , setInfoDivPosition)
		.show();
	
	if ( ($(window).width() + $(window).scrollLeft()) <  (elementPosition.left + $('#gmfinlineglossary_info_div').width()) ){
		elementPosition.left = $(element).offset().left - 15 - $('#gmfinlineglossary_info_div').width();
	}


	if ( ($(window).height() + $(window).scrollTop()) < (elementPosition.top + $('#gmfinlineglossary_info_div').height())){
		elementPosition.top = $(element).offset().top - $('#gmfinlineglossary_info_div').height();
	}

	//$('#gmfinlineglossary_info_div').append( 'element.top: ' + $(element).offset().top + ' / ' + ' div.height:' +  $('#gmfinlineglossary_info_div').height());

	$('#gmfinlineglossary_info_div').css(elementPosition);	
	
}

function setInfoDivPosition(responseText, textStatus, XMLHttpRequest){
	var elementPosition = $('#gmfinlineglossary_info_div').offset();

	if ( ($(window).height() + $(window).scrollTop()) < (elementPosition.top + $('#gmfinlineglossary_info_div').height())){
		elementPosition.top = elementPosition.top - 10 - $('#gmfinlineglossary_info_div').height();
	}
	
	$('#gmfinlineglossary_info_div').css(elementPosition);
}

function gmfinlineglossary_hideInfoBox(){
	$('#gmfinlineglossary_info_div').hide();
}

function gmfinlineglossary_searchForGlossaryWords(){
	
	//$('p').each(function(item){

	$('p').contents().filter(function() { return this.nodeType == 3; }) //Node.TEXT_NODE is 3
	.each(function(item){
		
		var textContent = this.nodeValue;
		var resultText = '';
		
		var wordStart = 0;

		while (textContent.charAt(wordStart).search(/\s/) > -1) wordStart++;

		var collectChars = '';

		for (var i=0; i<=textContent.length; i++){
			
			
			
			if (textContent.charAt(i).search(/[^\w]/) > -1 ){
			
				// the char is no letter
				
				collectChars = checkIfWordIsInGlossary(collectChars);
				
				resultText += collectChars + textContent.charAt(i);
				
				collectChars = '';
			}
			else {
				collectChars += textContent.charAt(i);
			}
			

			
			/*
			// add the last word
			if (i>=textContent.length){
				
				var checkWord = textContent.substring(wordStart,textContent.length );
				
				checkWord = checkIfWordIsInGlossary(checkWord);
				
				resultText += ' ' + checkWord;
				wordStart = i;
				while (textContent.charAt(wordStart) == ' ') wordStart++;
				
			}
			else if (textContent.charAt(i).search(/[\s\.<>]/) > -1 && i>wordStart ){
			
				var checkWord = textContent.substring(wordStart,i);
			
				checkWord = checkIfWordIsInGlossary(checkWord);
				
				resultText += ' ' + checkWord;
				
				wordStart = i;
				while (textContent.charAt(wordStart).search(/[^\w]/) > -1) wordStart++;
			}
			*/
			
		}
		resultText += collectChars;	
		
		$(this).replaceWith(resultText);

		//this.innerHTML = resultText;
		
		//$(this).html( resultText );
	});
	
}

function checkIfWordIsInGlossary(word){
	for (var j=0; j<glossary_entries.length; j++){
		if (glossary_entries[j] == word.toLowerCase()){
			word = '<span class="gmfinlineglossary_highlight" onmouseover="gmfinlineglossary_setInfoBoxPosition(this)" onmouseout="gmfinlineglossary_hideInfoBox()">' + word + '</span>';
			break;
		}
	}
	return word;
}
