Event.observe(window, "load", function()
{
	relTags()
});


// makes external links out of anchors with a rel="external" attribute (beacause target is not xhtml compatible
function relTags()
{
	// popup
	var popup_options = 'scrollbars=yes,resizable=yes,width=700,wheight=700';
	if (document.getElementsByTagName)
	{
		var anchors = document.getElementsByTagName("a");
		for (var i = 0; i < anchors.length; i++)
		{
			var anchor = anchors[i];
			if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external")
			{
				anchor.target = "_blank";
			}
			if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "popup")
			{
				anchor.onclick = function()
				{
					var mylink = this.getAttribute("href");
					window.open(mylink, 'popup', popup_options);
					return false;
				};
			}
		}
	}
}



function tabnavig(position, tab, container, content)
{
	var myElements = document.getElementsByClassName(container);
	for (i = 0; i < myElements.length; ++i)
	{
		$(content + i).style.display = "none";
		$(tab + i).className = "";

		Element.show(content + position);
		$(tab + position).className = "active";
		$(content + position).style.display = "block";

	}
}

function top()
{
	scroll(0, 0);
}

langtoggle = "off";
function languageselect()
{
	if ($('langmenu'))
	{
		$('langmenu').onclick = function()
		{
			if (langtoggle == "off")
			{
				$('language-menu').addClassName('active');
				langtoggle = "on";
			}
			else
			{
				$('language-menu').removeClassName('active');
				langtoggle = "off";
			}
		}
	}
}

/* content padding-bottom verkleinern wenn kein abbinder da ist */
function abbindercheck()
{
	if (!$('abbinder'))
	{
		$('content').style.paddingBottom = "100px";
	}
}

/* Popups */
function openPopup(URL, WIDTH, HEIGHT)
{
	sBaseHref = document.getElementsByTagName("base")[0].getAttribute("href");

	if (!WIDTH)
		WIDTH = 600;
	if (!HEIGHT)
		HEIGHT = 600;
	popup = window.open(URL, "popup", "width=" + WIDTH + ", height=" + HEIGHT + ", scrollbars=yes");
}

//Image Popup mit Bildangabe zum einladen
function showImage(img, WIDTH, HEIGHT)
{
	imagepopup = window.open('', 'Popup', 'height=' + HEIGHT + ',width=' + WIDTH)
	content = '<body topmargin="0" leftmargin="0" marginwidth="0" marginheight="0" bgcolor="#FFFFFF">' +
	'<table cellpadding="0" cellspacing="0" border="0" width="100%" height="100%"><tr><td>' +
	'<a href="javascript:window.close()"><img name="myImage" src="' +
	img +
	'" border="0" alt="Auf das Bild klicken um es zu schliessen"></a>' +
	'</td></tr></table></body>';
	imagepopup.document.write(content);
	imagepopup.document.close();
}

/* treeedit */
function sitemapstyler(id, closed)
{
	var sitemap = id ? $(id) : $("sitemap-list");
	if (closed)
	{
		ul_style = "none";
		span_classname = "collapsed";
	}
	else
	{
		ul_style = "block";
		span_classname = "expanded";
	}
	if (sitemap)
	{
		this.listItem = function(li)
		{
			if (li.getElementsByTagName("ul").length > 0)
			{
				var ul = li.getElementsByTagName("ul")[0];
				ul.style.display = ul_style;
				var span = document.createElement("span");
				span.className = span_classname;
				span.onclick = function()
				{
					ul.style.display = (ul.style.display == "none") ? "block" : "none";
					this.className = (ul.style.display == "none") ? "collapsed" : "expanded";
				};
				li.appendChild(span);
			};
					};
		var items = sitemap.getElementsByTagName("li");
		for (var i = 0; i < items.length; i++)
		{
			listItem(items[i]);
		};
			};
	};

Event.observe(window, 'load', function()
{
	sitemapstyler("edit_tree", "closed");
	sitemapstyler("sitemap-list");
});



// to hide/show field id_contacttype in forum (add entry)
function showHideContactType()
{
	if ($('firstcontact') && $('id_contacttype'))
	{
		if ($F('firstcontact') == "no")
		{
			Element.hide('id_contacttype');
		}
		else
		{
			Element.show('id_contacttype');
		}
	}
}



/*
 glossary begin
 expects predefined glossary array:
 var glossary = new Array();
 glossary[4] = new Object();
 glossary[4]["name"] = "Google";
 glossary[4]["link"] = "http://google.de";
 */
function __doHighlight(haystack, needle, p_all, title, link, highlight_start_tag, highlight_end_tag)
{
	if ((!highlight_start_tag) || (!highlight_end_tag))
	{
		highlight_start_tag = "<span style='color:blue; background-color:yellow;'>";
		highlight_end_tag = "</span>";
	}

	var all = p_all ? true : false;
	var new_text = "";
	var i = -1;
	var lc_needle = needle.toLowerCase();
	var lc_haystack = haystack.toLowerCase();

	while (haystack.length > 0)
	{
		i = lc_haystack.indexOf(lc_needle, i + 1);
		if (i < 0)
		{
			new_text += haystack;
			haystack = "";
		}
		else
		{
			// skip anything inside an HTML tag
			if (haystack.lastIndexOf(">", i) >= haystack.lastIndexOf("<", i))
			{
				if (lc_haystack.lastIndexOf("/script>", i) >= lc_haystack.lastIndexOf("<script", i) &&
				lc_haystack.lastIndexOf("/a>", i) >= lc_haystack.lastIndexOf("<a", i))
				{
					new_text += haystack.substring(0, i) + highlight_start_tag + haystack.substr(i, needle.length) + highlight_end_tag;
					haystack = haystack.substr(i + needle.length);
					lc_haystack = haystack.toLowerCase();
					i = -1;
				}
			}
		}
	}
	return new_text;
}

function doHighlight(haystack, needle, p_count, title, link, highlight_start_tag, highlight_end_tag)
{
	if ((!highlight_start_tag) || (!highlight_end_tag))
	{
		//		highlight_start_tag = "<span style='background-color:#e6e8ef;border-bottom:1px dotted #000;cursor:pointer;' title='" + needle + "' onclick=\"document.location.href='" + link + "'\">";
		highlight_start_tag = "<span style='background-color:#e6e8ef;border-bottom:1px dotted #000;cursor:pointer;' title='" + needle + "' onclick=\"openPopup(\'" + link + "\')\">";
		highlight_end_tag = "</span>";
	}

	var count = p_count ? p_count : false;

	var my_reg_exp = new RegExp('\\b(' + needle + ')\\b', 'gi');
	haystack = haystack.replace(my_reg_exp, '##replace_span_start##$1##replace_span_end##');

	erg = haystack.match(/(<a.*?>)(.*?)(<\/a>)/gi);

	// alle highlight_tags zwischen <a.*> und </a> rausschmeissen
	if (erg)
		for (var i = 0; i < erg.length; ++i)
		{
			var neu = erg[i].replace(/(##replace_span_start##|##replace_span_end##)/g, '');
			my_reg_exp = new RegExp('' + erg[i] + '', 'gi');
			haystack = haystack.replace(my_reg_exp, neu);
		}
	if (!count)
	{
		haystack = haystack.replace(/##replace_span_start##/gi, highlight_start_tag);
		haystack = haystack.replace(/##replace_span_end##/gi, highlight_end_tag);
	}
	else
	{
		for (var i = 0; i < count; ++i)
		{
			haystack = haystack.replace(/##replace_span_start##/i, highlight_start_tag);
			haystack = haystack.replace(/##replace_span_end##/i, highlight_end_tag);
		}
		haystack = haystack.replace(/##replace_span_start##/gi, '');
		haystack = haystack.replace(/##replace_span_end##/gi, '');
	}

	return haystack;
}


function buildGlossary(p_element, p_count)
{
	var el = $(p_element);
	var haystack = el.innerHTML;
	var count = p_count ? p_count : false;
	var debug = "";

	if (!glossary)
		return false;
	if (!el)
		return false;
	if (el.hasClassName('glossary_replaced'))
		return false;

	//	var Links = KeyWordGetLinks();

	for (var i = (glossary.length - 1); i >= 0; i--)
	{
		if (glossary[i])
		{
			var needle = glossary[i]["name"] ? glossary[i]["name"] : "";
			var link = glossary[i]["link"] ? glossary[i]["link"] : "";
			var title = glossary[i]["body"] ? glossary[i]["body"] : "";
			haystack = doHighlight(haystack, needle, count, title, link);
		}
	}
	el.addClassName('glossary_replaced');
	el.innerHTML = haystack;

	//	KeyWordCleanLinks(Links);
}

function KeyWordGetLinks()
{
	var Links = new Array();
	for (i = 0; i < document.body.getElementsByTagName('A').length; i++)
	{
		Links[i] = new Array(document.body.getElementsByTagName('A')[i].href, document.body.getElementsByTagName('A')[i].innerHTML);
	}
	return Links;
}

function KeyWordCleanLinks(Links)
{
	for (i = 0; i < document.body.getElementsByTagName('A').length; i++)
	{
		document.body.getElementsByTagName('A')[i].href = Links[i][0];
		document.body.getElementsByTagName('A')[i].innerHTML = Links[i][1];
	}
}





Event.observe(window, 'load', function()
{
	// divs mit class=getfullquestion
	$$('div.fachcontent').each(function(el)
	{
		buildGlossary(el, 1);
	});
	// alle elemente mit id=getfull
	/*
	 $$('#getfull').each(function(el){
	 buildGlossary(el, false);
	 });
	 */
});

/*

 glossary end

 */

