// charset=utf-8
// $Id: LexiconDb.js 1831 2009-07-29 14:31:43Z dierker $
// $HeadURL: svn://svnserver/mcmplugins/LexiconDb/trunk/mcm-5.5/scripts/LexiconDb.js $
// +----------------------------------------------------------------------+
// | mcm plugin: LexiconDb                                                |
// | version 1.0                                                          |
// | mcm version 5.5                                                      |
// | (c) 2002-2009 monsun media (http://www.monsun-media.com)             |
// +----------------------------------------------------------------------+


/**
* LexiconDb
*
* @author	hebing
*/
var LexiconDb = {
	/**
	* @var	bool
	*/
	hideLexiconBox : false
	,
	/**
	* @var	Number
	*/
	activeInterval : null
	,
	/**
	* @var	bool
	*/
	Content : null
	,
	/**
	* show a lexicon-entry
	*
	* @param	Event	evt
	* @param	int		lexicon_id
	* @return	void
	*/
	showLexiconEntry : function(evt,lexicon_id){
		if( evt.target ){
			targetElement = evt.target;
		}else if ( evt.fromElement ){
			targetElement = evt.fromElement;
		} /*ie*/

		var LexiconBox;
		LexiconBox = document.getElementById('LexiconBox');
		if( LexiconBox==null ){
			LexiconDb.displayLexiconEntry(evt,lexicon_id);
		}else{
			LexiconDb.hideLexiconEntry(evt);
			LexiconDb.displayLexiconEntry(evt,lexicon_id);
		};
		mcm.cancelEvent(evt);
		targetElement.onmouseout = function(){
			LexiconDb.hideLexiconBox = true;
		}
		LexiconDb.activeInterval = window.setInterval("LexiconDb.checkTimeout('"+evt+"')", 3500);
	}
	,
	/**
	* hide the lexicon entry
	*
	* @param	Event	evt
	* @return	void
	*/
	hideLexiconEntry: function(evt){
		var LexiconBox = document.getElementById('LexiconBox');
		document.body.removeChild(document.getElementById('LexiconBox'));
		if( LexiconDb.activeInterval ){
			window.clearInterval(LexiconDb.activeInterval);
		}
		mcm.cancelEvent(evt);
	}
	,
	/**
	* display a lexicon-entry
	*
	* @param	Event	evt
	* @param	int		lexicon_id
	* @return	void
	*/
	displayLexiconEntry: function(evt,lexicon_id){
		var x = mcm.getPageX(evt);
		var y = mcm.getPageY(evt);

		var LexiconBox = document.createElement('div');
		LexiconBox.setAttribute('id','LexiconBox');
		LexiconBox.style.display = 'none';
		LexiconBox.className = 'LexiconBox';
		document.body.appendChild(LexiconBox);

		var ContentContainer = document.createElement('div');
		// ContentContainer.setAttribute('id','ContentContainer');
		ContentContainer.className = 'ContentContainer';
		LexiconBox.appendChild(ContentContainer);

		var Header = document.createElement('div');
		// Header.setAttribute('id','Header');
		Header.className = 'Header';
		ContentContainer.appendChild(Header);

		var CloseContainer = document.createElement('div');
		// CloseContainer.setAttribute('id','CloseContainer');
		CloseContainer.className = 'CloseContainer';
		ContentContainer.appendChild(CloseContainer);
		CloseContainer.innerHTML = '<a href="#" class="link-close" id="link-close" onclick="LexiconDb.hideLexiconEntry();return false;"><span>schließen</span></a>';

		LexiconDb.Content = document.createElement('div');
		// LexiconDb.Content.setAttribute('id','Content');
		LexiconDb.Content.className = 'Content';
		ContentContainer.appendChild(LexiconDb.Content);

		var Footer = document.createElement('div');
		// Footer.setAttribute('id','Footer');
		Footer.className = 'Footer';
		ContentContainer.appendChild(Footer);

		LexiconDb.Content.innerHTML = '<div id="lexiconWaitStatus"><span>please wait...</span></div>';
		//steuerung = setInterval("LexiconDb.displayText("+lexicon_id+")", 3000);
		LexiconDb.displayText(lexicon_id);

		mcm.cancelEvent(evt);

		LexiconBox.style.left = parseInt(x)-parseInt(LexiconBox.offsetWidth)+'px';
		LexiconBox.style.top  = parseInt(y)+'px';
		LexiconBox.style.display = 'block';
		LexiconBox.style.position = 'absolute';

		LexiconBox.onmouseout = function(){
			LexiconDb.hideLexiconBox = true;
		}
		ContentContainer.onmouseover = function(){
			LexiconDb.hideLexiconBox = false;
		}
	}
	,
	/**
	* check the timeout (?)
	*
	* @return	void
	*/
	checkTimeout : function(evt){
		if( LexiconDb.hideLexiconBox==true ){
			LexiconDb.hideLexiconEntry(evt);
		}
	}
	,
	/**
	* display the text of the lexicon entry
	*
	* @param	int		lexicon_id
	* @return	void
	*/
	displayText : function(lexicon_id){
		var servletUrl = 'plugins/LexiconDb/servlet.php?action=showLexiconEntry&'+'lexicon_id='+lexicon_id;
		var xmlReq = mcm.createXmlHttpRequest();
		xmlReq.open('GET',servletUrl,true);
		xmlReq.send(null);
		xmlReq.onreadystatechange = function(){
			if( xmlReq.readyState==4 ){
				if( xmlReq.status==200 ){
					var lexicon_entry = xmlReq.responseText;
					LexiconDb.Content.innerHTML = lexicon_entry;
				}
			}
		}
	}
}

