﻿
/* wiki-spezifische Funktionen */

var wiki_button_list = new Array();


function cache_button(id, start_element, end_element, img, sample, tipp, rowId, colName, prefix) {

	wiki_button_list[wiki_button_list.length] = {
			"id" 		: id,
			"start" 	: start_element,
			"end"		: end_element,
			"img"		: img,
			"sample" 	: sample,
			"tipp"		: tipp,
			"rowId"		: rowId,
			"colName"	: colName,
			"prefix"	: prefix
		};
}


function init_wiki_button_element(obj, i, rowId, colName, prefix) {

	var cur = wiki_button_list[i];

	if (cur.rowId != rowId) { return; }

	var new_img = document.createElement("img");
	new_img.src = cur.img;
	new_img.id = cur.id;
	new_img.alt = cur.tipp;
	new_img.title = cur.tipp;
	new_img.onclick = function() {
					insert_wiki_element( cur.start, cur.end, cur.sample, rowId, cur.colName, cur.prefix);
					return false;
				}

	obj.appendChild(new_img);




}

function insert_wiki_element(start, end, sample, rowId, colName, prefix) {

//	alert(start + end + sample);


	var c = document.getElementById('iW.content');	
	if (!c) { c = document.getElementById('eW.content'); }

	if (!c) { c = document.getElementById(prefix + '.' + colName); }

//	alert(prefix + '.' + colName);

	if (!c) { alert('Endgueltig nix'); return; }
	var has_sample = false;

	var thisdoc = null;


	// Sonderfall: Editieren des Editors 

	var cf = document.getElementById(c.id + '_ifr');

	if (cf) {
			// alert(window.document.getElementsByTagName('iframe')[0].contentWindow.document);
			c = cf.contentWindow.document;

			cf.contentWindow.focus();
			thisdoc = c;

	} 
	else { c.focus(); }


	if (document.selection && document.selection.createRange) {

		var scroll = null;

		if (document.documentElement && document.documentElement.scrollTop) { 
			scroll = document.documentElement.scrollTop; }
		else if (document.body) {
			scroll = document.body.scrollTop;
		}

		var r = document.selection.createRange();
		s = r.text;
		remove_ws();
		r.text = start + s + end;

		if (has_sample && r.moveStart) {

			if (window.opera) { end = end.replace(/\n/g,''); }
			r.moveStart('character', - end.length - s.length); 
			r.moveEnd('character', - end.length); 
		}
		r.select();   
		if (document.documentElement && document.documentElement.scrollTop) {
			document.documentElement.scrollTop = scroll; }
		else if (document.body) { document.body.scrollTop = scroll; }


	}

	else 	{ 
		// alert(c);

		if (c.selectionStart || c.selectionStart == '0') { 

			var scroll = c.scrollTop;

			var start_pos = c.selectionStart;
			var end_pos = c.selectionEnd;
			s = c.value.substring(start_pos, end_pos);
			remove_ws();

			// if (sample.indexOf("\n") > -1) { s = '\n \n'; }

			c.value = c.value.substring(0, start_pos) + start + s + end + c.value.substring(end_pos, c.value.length);


			if (has_sample) {
				c.selectionStart = start_pos + start.length; // + ((sample.indexOf("\n") > -1) ? -1 : 0);
				c.selectionEnd = start_pos + start.length + s.length; // + ((sample.indexOf("\n") > -1) ? 1 : 0);

			} else {
				c.selectionStart = start_pos + start.length + s.length + end.length;
				c.selectionEnd = c.selectionStart;
			}


			c.scrollTop = scroll;

		}

		else { // alert('Upps - unten keine Verarbeitung'); 


			t = c.getSelection();

			if (t.length == 0) { t = start + sample + end; }
			else { start + t + end; }

			t = c.execCommand("inserthtml", false, t);

			// alert(t);


		}

	}

	function remove_ws() {

		if (!s) {
			s = sample;
			has_sample = true;
		} else if (s.charAt(s.length - 1) == ' ') { 
			s = s.substring(0, s.length - 1);
			end += ' ';
		} 
	}





}


function insert_wiki_button_list(obj_name, rowId, colName, prefix) {

	var obj = document.getElementById(obj_name);



	if (!obj) { return; }



	cache_button("w_bold_" + rowId, "'''", "'''", "/images/mail/bold.gif", "fetter Text", "fett", rowId, colName, prefix);
	cache_button("w_italic_" + rowId, "''", "''", "/images/mail/italic.gif", "kursiver Text", "kursiv", rowId, colName, prefix);
	cache_button("w_link_" + rowId, "[[", "]]", "/images/mail/plus-link.gif", "Link", "Link", rowId, colName, prefix);
	cache_button("w_elink_" + rowId, "[[", "!Linktext]]", "/images/mail/plus-ext-link.gif", "Link (new Window)", "Link (new Window)", rowId, colName, prefix);
	cache_button("w_ul_" + rowId, "* ", "", "/images/mail/unordered-list.gif", "Liste", "einfache Liste", rowId, colName, prefix);
	cache_button("w_ol_" + rowId, "# ", "", "/images/mail/ordered-list.gif", "Aufzählung", "nummerierte Liste", rowId, colName, prefix);
	cache_button("w_h1_" + rowId, "== ", " ==", "/images/mail/header1.gif", "Überschrift 1", "Überschrift 1", rowId, colName, prefix);
	cache_button("w_h2_" + rowId, "=== ", " ===", "/images/mail/header2.gif", "Überschrift 2", "Überschrift 2", rowId, colName, prefix);
	cache_button("w_h3_" + rowId, "==== ", " ====", "/images/mail/header3.gif", "Überschrift 3", "Überschrift 3", rowId, colName, prefix);

	cache_button("w_left_" + rowId, "[left]", "[/left]", "/images/mail/left.gif", "linksbündig", "linksbündig", rowId, colName, prefix);
	cache_button("w_center_" + rowId, "[center]", "[/center]", "/images/mail/center.gif", "zentriert", "zentriert", rowId, colName, prefix);
	cache_button("w_right_" + rowId, "[right]", "[/right]", "/images/mail/right.gif", "rechtsbündig", "rechtsbündig", rowId, colName, prefix);

	cache_button("w_table_" + rowId, "{|border='0'\n|", "\n|}", "/images/mail/table.gif", " ", "Tabelle", rowId, colName, prefix);
	cache_button("w_table_" + rowId, "\n|-\n|", "", "/images/mail/tablerow.gif", " ", "neue Zeile", rowId, colName, prefix);
	cache_button("w_table_" + rowId, "\n|", "", "/images/mail/tablecell.gif", " ", "neue Zelle", rowId, colName, prefix);




	for (i = 0; i < wiki_button_list.length; i++) {
		init_wiki_button_element(obj, i, rowId, colName, prefix);
	}



}



