//
// 2006 by bitart; Author: Alexander Graf
//
// Copyright by bitart - Furch & Graf GbR, Gartenstr. 5 56220 St. Sebastian,
// Germany. All rights reserved.
// Copyright in this software is owned by bitart, unless otherwise indicated.
//
// You are not allowed to distribute this software in any form or use it as
// part of another project without asking the owner for permission to use.
//

var panel_popup = null;
var panel_word  = null;
var panel_timer = null;
var panel_pos   = {'x':0,'y':0};

function initPanel()
{
	try{
		var content = document.createDocumentFragment();

		var query = document.createElement('div');
		query.className = 'words';
		query.id = 'panel_word';
		query.appendChild(document.createTextNode(''));

		var addbut = document.createElement('div');
		addbut.id = 'panel_add';
		addbut.className = 'BigButton';
		addbut.onclick = clickPanel;
		addbut.appendChild(document.createTextNode('»Text« zur Suche hinzufügen.'));
		var newbut = document.createElement('div');
		newbut.id = 'panel_new';
		newbut.className = 'BigButton';
		newbut.onclick = clickPanel;
		newbut.appendChild(document.createTextNode('Direkt nach »Text« suchen.'));
		initButtons(addbut, newbut);

		content.appendChild(query);
		content.appendChild(addbut);
		content.appendChild(newbut);

		var panel = createBox(content, 'panel');

		panel.onmouseout = closePanel;
		panel.onmousemove = holdPanel;

		document.body.appendChild(panel);

		panel_popup = panel;
		panel_word  = query;

		document.onmouseup = openPanel;
		document.onmousemove = posPanel;
	}catch(e){}
}

function posPanel(e)
{
	if (typeof e == 'undefined'){
		panel_pos.x = window.event.clientX + document.body.scrollLeft;
		panel_pos.y = window.event.clientY + document.body.scrollTop;
	}else{
		panel_pos.x = e.pageX;
		panel_pos.y = e.pageY;
	}
	return true;
}

function openPanel(e)
{
	var nope = false;
	try{
		var node = (typeof e == 'undefined')?window.event.srcElement:e.target;
		while (node && node.tagName.toLowerCase() != 'body'){
			if (node.className.indexOf('panel')!=-1){
				nope = true;
				break;
			}
			node = node.parentNode;
		}
	} catch(e) {}
	var text = '';
	try{
		if (window.getSelection){
			text = window.getSelection().toString();
		} else if (window.document.getSelection){
			text = window.document.getSelection().toString();
		} else if (window.document.selection){
			text = window.document.selection.createRange().text.toString();
		}
	} catch(e) {}
	if (text && highlight_clean){
		text = text.replace(highlight_clean,' ').replace(/^ /g,'').replace(/ $/,'');
		if (!nope && text){
			closePanel(true);

			panel_word.words = text;
			panel_word.replaceChild(document.createTextNode(text), panel_word.firstChild);

			var xp = panel_pos.x-2;
			var yp = panel_pos.y-2;
			var xo = document.body.scrollLeft || window.pageXOffset || 0;
			var yo = document.body.scrollTop || window.pageYOffset || 0;
			var iw = (document.documentElement.clientWidth || window.innerWidth || document.body.clientWidth || 0)-3;
			var ih = (document.documentElement.clientHeight || window.innerHeight || document.body.clientHeight || 0)-10;

			if (xp-xo + panel_popup.offsetWidth > iw) xp = iw - panel_popup.offsetWidth;
			if (yp-yo + panel_popup.offsetHeight > ih) yp = panel_pos.y - panel_popup.offsetHeight + 2;

			panel_popup.style.left = xp+'px';
			panel_popup.style.top  = yp+'px';

			panel_popup.style.visibility = 'visible';

			panel_word.focus();
		}
		try{
			if (window.getSelection){
				window.getSelection().removeAllRanges();
			} else if (window.document.getSelection){
				window.document.getSelection().removeAllRanges();
			} else if (window.document.selection){
				window.document.selection.empty();
			}
		} catch(e) {}
	}else if (!nope){
		closePanel(true);
	}
	return true;
}

function closePanel(now)
{
	if (panel_popup){
		holdPanel();
		if (now === true){
			panel_popup.style.visibility = 'hidden';
		}else{
			panel_timer = window.setTimeout('closePanel(true)', 1000);
		}
	}
	return true;
}

function holdPanel()
{
	if (panel_timer) panel_timer = window.clearTimeout(panel_timer);
	return true;
}

function clickPanel()
{
	switch (this.id){
		case 'panel_add':
			top.frames.search.setSearch(panel_word.words, true);
			break;
		case 'panel_new':
			top.frames.search.setSearch(panel_word.words);
			break;
	}
	return closePanel(true);
}


