/* Copyright 2002, by Jörg Reuter <jreuter@yaina.de>
 * unauthorized use prohibited.
 *
 * $Id: lykanthrop.js,v 1.1.1.1 2002/07/03 19:15:43 owolf Exp $
 */

function do_open_dictionary(ev)
{
	var refid=this.getAttribute("ref");
	window.open(refid, "dictionary", 
		"depend=yes,scrollbars=yes,height=220,width=220");
}

function do_open_datenschutzhinweis(ev)
{
	window.open("about-datenschutz-popup", "datenschutz",
		"scrollbars=yes,height=400,width=400");
}

function make_visible(nd)
{
	// this is absurd -- while CSS describes style attributes,
	// every browser has it's own idea whether to AND or OR connect
	// attributes whose default values "conflict" with the user
	// supplied...

	nd.style.visibility="visible";
	nd.style.height="auto";  	// Konqueror & Mozilla
	nd.style.overflow="visible";	// Konqueror & IE6
	nd.style.display="block";	// Mozilla & IE6, but not Konqueror
}

function make_invisible(nd)
{
	if (nd == null)
		return;

	nd.style.visibility="hidden";
	nd.style.height=0;
	nd.style.overflow="hidden";
	if (navigator.appName != "Konqueror")	// Konqueror is broken.
		nd.style.display="none";
}

function make_comment_show_link(node, name)
{
	if (!document.getElementById("comment_text_"+name))
		return;

	var anchor=document.createElement("a");
	anchor.setAttribute("id", "comment_link_show_"+name);
	anchor.setAttribute("ref", name);
	anchor.onclick=view_comment;

	var icon=document.createElement("img");
	icon.setAttribute("class", "icon");
	icon.setAttribute("src", "pfeil-unten-icon.png");
	icon.setAttribute("alt", "lesen");
	icon.setAttribute("title", "Kommentar lesen");
//	icon.style.cursor="pointer";
	anchor.appendChild(icon);
	node.appendChild(anchor);
}

function do_comment_start(ev)
{
//	var sel=document.getSelection();
//	alert(sel);

	var imagenode=this.firstChild; // FIXME
	while(imagenode.nodeName.toLowerCase() != "img" && imagenode.nextSibling)
		imagenode=imagenode.nextSibling;

	imagenode.setAttribute("title", "Formular einklappen");

	var marker_name=this.getAttribute("ref");
	var marker_node=document.getElementById("comment_marker_"+marker_name);
	var form_name="comment_form_"+marker_name;

	var ex_form=document.getElementById(form_name);
	if (ex_form)
	{
		if (ex_form.style.visibility != "hidden") return;
		var tmp=ex_form.parentNode.removeChild(ex_form);
		make_visible(tmp);
	}

	var myform=document.createElement("form");
	myform.id = form_name;
	myform.setAttribute("action",'comment-post.do');
//	myform.setAttribute("target", "_blank");	// illegal in XHTML 1.0
	myform.setAttribute("method", "post");

	var in_para=document.createElement("p");
	myform.appendChild(in_para);


	var in_name=document.createElement("input");
	in_name.setAttribute("size", "20");
	in_name.setAttribute("type", "text");
	in_name.setAttribute("name", "nick");
	in_name.setAttribute("id", "nick");
	in_para.appendChild(in_name);

	var in_name_label=document.createElement("label");
	in_name_label.setAttribute("for", "nick");
	in_name_label.appendChild(document.createTextNode(" Dein Name"));
	in_para.appendChild(in_name_label);

	var in_name_br=document.createElement("br");
	in_para.appendChild(in_name_br);

	var in_email=document.createElement("input");
	in_email.setAttribute("size", "20");
	in_email.setAttribute("type", "text");
	in_email.setAttribute("name", "email");
	in_email.setAttribute("id", "email");
	in_para.appendChild(in_email);

	var in_email_label=document.createElement("label");
	in_email_label.setAttribute("for", "email");
	in_email_label.appendChild(document.createTextNode(" Deine Mailadresse"));
	in_para.appendChild(in_email_label);

	var in_email_br=document.createElement("br");
	in_para.appendChild(in_email_br);

	var in_publish_comment=document.createElement("input");
	in_publish_comment.setAttribute("type", "checkbox");
	in_publish_comment.setAttribute("name", "publish-comment");
	in_publish_comment.setAttribute("id", "publish-comment");
	in_publish_comment.setAttribute("checked", "1");
	in_para.appendChild(in_publish_comment);

	var in_publish_comment_label=document.createElement("label");
	in_publish_comment_label.setAttribute("for", "publish-comment");
	in_publish_comment_label.appendChild(document.createTextNode(" Hier veröffentlichen?"));
	in_para.appendChild(in_publish_comment_label);

	var in_publish_comment_br=document.createElement("br");
	in_para.appendChild(in_publish_comment_br);

	var in_publish_email=document.createElement("input");
	in_publish_email.setAttribute("type", "checkbox");
	in_publish_email.setAttribute("name", "publish-email");
	in_publish_email.setAttribute("id", "publish-email");
	in_publish_email.setAttribute("checked", "1");
	in_para.appendChild(in_publish_email);

	var in_publish_email_label=document.createElement("label");
	in_publish_email_label.setAttribute("for", "publish-email");
	in_publish_email_label.appendChild(document.createTextNode(" Emailadresse veröffentlichen?"));
	in_para.appendChild(in_publish_email_label);

	var in_publish_email_br=document.createElement("br");
	in_para.appendChild(in_publish_email_br);

	var in_publish=document.createElement("input");
	in_publish.setAttribute("type", "checkbox");
	in_publish.setAttribute("name", "publish-anonymous");
	in_publish.setAttribute("id", "publish-anonymous");
	in_para.appendChild(in_publish);

	var in_publish_anonymous_label=document.createElement("label");
	in_publish_anonymous_label.setAttribute("for", "publish-anonymous");
	in_publish_anonymous_label.appendChild(document.createTextNode(" Anonym veröffentlichen?"));
	in_para.appendChild(in_publish_anonymous_label);

	var in_publish_anonymous_br=document.createElement("br");
	in_para.appendChild(in_publish_anonymous_br);

	var tarea=document.createElement("textarea");
	tarea.setAttribute("rows", "10");
	tarea.setAttribute("cols", "60");
	tarea.setAttribute("name", "comment");
	tarea.setAttribute("id", "comment");
	in_para.appendChild(tarea);

	var tarea_br=document.createElement("br");
	in_para.appendChild(tarea_br);

	var in_willig=document.createElement("input");
	in_willig.setAttribute("type", "checkbox");
	in_willig.setAttribute("name", "einwilligung");
	in_willig.setAttribute("id", "einwilligung");
	in_para.appendChild(in_willig);

	var in_willig_label=document.createElement("label");
	in_willig_label.setAttribute("for", "einwilligung");
	in_willig_label.appendChild(document.createTextNode(" Einwilligung zur Erhebung, Verarbeitung und Nutzung gemäß "));
	
	var willig_unterricht_anchor=document.createElement("a");
	willig_unterricht_anchor.setAttribute("title", "Datenschutz-Unterrichtung anzeigen");
	willig_unterricht_anchor.setAttribute("href", "javascript:do_open_datenschutzhinweis()");
//	willig_unterricht_anchor.onclick = do_open_datenschutzhinweis;
	willig_unterricht_anchor.appendChild(document.createTextNode("Datenschutz-Unterrichtung"));
	willig_unterricht_anchor.style.cursor="pointer";
	in_willig_label.appendChild(willig_unterricht_anchor);
	in_para.appendChild(in_willig_label);

	var willig_br=document.createElement("br");
	in_para.appendChild(willig_br);

	willig_br=document.createElement("br");
	in_para.appendChild(willig_br);

	var in_send=document.createElement("input");
	in_send.setAttribute("type", "button");
	in_send.setAttribute("name", "send");
	in_send.setAttribute("value", "abschicken");
	in_send.setAttribute("id", "form_send_"+marker_name);
	in_send.setAttribute("ref", marker_name);
	in_send.onclick=do_comment_send;
	in_para.appendChild(in_send);

	var in_send_space=document.createTextNode(" ");
	in_para.appendChild(in_send_space);

	var in_abort=document.createElement("input");
	in_abort.setAttribute("type", "button");
	in_abort.setAttribute("name", "abort");
	in_abort.setAttribute("value", "lieber nicht");
	in_abort.setAttribute("id", "form_abort_"+marker_name);
	in_abort.setAttribute("ref", marker_name);
	in_abort.onclick=do_comment_abort;
	in_para.appendChild(in_abort);

	var in_abort_br=document.createElement("br");
	in_para.appendChild(in_abort_br);

	var in_paragraph=document.createElement("input");
	in_paragraph.setAttribute("name", "paragraph");
	in_paragraph.setAttribute("id", "paragraph");
	in_paragraph.setAttribute("value", marker_name);
	in_paragraph.setAttribute("type", "hidden");
	in_para.appendChild(in_paragraph);


	var in_chapter=document.createElement("input");
	in_chapter.setAttribute("name", "chapter");
	in_chapter.setAttribute("id", "chapter");
	in_chapter.setAttribute("value", docpath);
	in_chapter.setAttribute("type", "hidden");
	in_para.appendChild(in_chapter);

	marker_node.appendChild(myform);
	make_visible(marker_node);

	this.onclick=do_comment_abort;
}

function do_comment_send(ev)
{
	this.setAttribute("title", "Kommentar schreiben");

	do_comment_remove_form(this.getAttribute("ref"), 1);
	return;
}

function do_comment_abort(ev)
{
	this.setAttribute("title", "Kommentar schreiben");

	do_comment_remove_form(this.getAttribute("ref"), 0);
	return;
}

function do_comment_remove_form(ref, dosubmit)
{
	var myform=document.getElementById("comment_form_"+ref);
	if (!myform) return;

	make_invisible(myform);
	make_invisible(myform.parentNode);

	var mylink=document.getElementById("comment_link_form_"+ref);
	if (mylink) mylink.onclick=do_comment_start;

	if (dosubmit)
		myform.submit();
/*
 *	great, this crashes Konqueror:
 */

//	save_object=parent.removeChild(myform);
}

function view_comment(ev)
{
	var marker_name=this.getAttribute("ref");
	var icon=this.firstChild;	// FIXME
	while(icon.nodeName.toLowerCase() != "img" && icon.nextSibling)
		icon=icon.nextSibling;

	var anchors=document.getElementsByTagName("a"); // bke
	var k;

	for (k=0; k < anchors.length; k++)
	{
		var anchor=anchors[k];
		if (anchor.getAttribute("rev") != marker_name)
			continue;

		var comment_text=anchor.parentNode;

		if (comment_text.style.visibility == "hidden")
		{
			icon.setAttribute("src", "pfeil-oben-icon.png");
			icon.setAttribute("title", "Kommentar einklappen");
			icon.style.cursor="pointer";
			make_visible(comment_text);
		} else {
			icon.setAttribute("src", "pfeil-unten-icon.png");
			icon.setAttribute("title", "Kommentar lesen");
			icon.style.cursor="pointer";
			make_invisible(comment_text);
		}
	}
}

function init()
{

	// no DOM capable browser
	if (!document.getElementById)
		return;

	// Opera
	if (window.opera)
	{
		// Opera below 7.x does not support DOM3
		if (navigator.appName == "Opera")
		{
			var vers=navigator.appVersion;
			vers=vers.substring(0, vers.indexOf(" "));
			if (vers < 7)
				return;
		} else {
			// we don't know the Opera version when using faked identification
			return;
		}
	} 

	// Konqueror 3

	/* 
	 * DOM-GENERATED FORMS ARE BROKEN  A G A I N  IN THAT FUCKING
	 * THING!
	 *
	 * Haven't these dimwits ever heard the term "regression testing"?!
	 *
	 * And I'm so fucking stupid to work around this crap.
	 *
	 * There goes another weekend fiddling around with this bullshit
	 * instead of writing content. Or creating the PHP3 script.
	 * Or the comment-inclusion script. Or doing *something* useful.
	 * Morons.
	 */

	/* They fixed it in KDE 3.1... Let's see what they broke instead... */

	var is_konqui_300=0;
	if (navigator.appName == "Konqueror" &&
		(navigator.appVersion.match(/Konqueror\/3\.0/) ||
		 navigator.appVersion.match(/Konqueror\/3\;/)
//		 || navigator.appVersion.match(/Konqueror\/3\.1/)
		)
	   )
	{
		is_konqui_300=1;
	}

	var compatibility_section=document.getElementById("no_js_browser");
	make_invisible(compatibility_section);

	// brain-damaged Konqueror 3.0.0-RC3
	if (!is_konqui_300)
	{
		var compatibility_form=document.getElementById("comment_form");
		make_invisible(compatibility_form);
	}

	var comment;
	var k;

	for(k=0; (comment=comment_list[k]); k++) 
	{
		var comment_link_show=document.getElementById("comment_link_show_"+comment);
		var comment_link_form=document.getElementById("comment_link_form_"+comment);

		// IE <= 6.0 (at least) does not know addEventListener,
		// OTOH all HTML 4.01 conforming browsers are required to
		// support the onclick virtual method.

		// furthermore, take brain-damaged Konqueror 3.0.0-RC3 into account
		if (comment_link_form && !is_konqui_300)
		{
			comment_link_form.onclick=do_comment_start;
			comment_link_form.removeAttribute("href");
			comment_link_form.setAttribute("ref", comment);
		}

		if (comment_link_show)
		{
			comment_link_show.onclick=view_comment;
			comment_link_show.removeAttribute("href");
			comment_link_show.setAttribute("ref", comment);
		}
	}

	for (k=0; (comment=user_comment_list[k]); k++)
	{
		if (comment == "") continue;

		var comment_marker=document.getElementById("comment_marker_"+comment);
		var anchors=document.getElementsByTagName("a");
		var a;

		for (a=0; a < anchors.length; a++)
		{
			var anchor=anchors[a];
			if (anchor.getAttribute("rev") != comment)
				continue;


			var comment_text=anchor.parentNode;
			if (comment_text && comment_marker)
			{
				comment_marker.parentNode.insertBefore(comment_text, comment_marker.nextSibling);
				make_invisible(comment_text);
			}
		}
	}

	var anchors=document.getElementsByTagName("a");
	var a;

	for (a=0; a < anchors.length; a++)
	{
		var anchor=anchors[a];
		var arel=anchor.getAttribute("rel");
		if (arel == "Glossary")
		{
			var target=anchor.getAttribute("href");
			anchor.removeAttribute("href");
			anchor.setAttribute("ref", target);
			anchor.onclick=do_open_dictionary;
		}
	}

	// var unwichtiges=document.getElementById("klugscheisser");
	// make_invisible(unwichtiges);
}

// vim:tabstop=4

