// Copyright Marcus Haas
/*
window.onload = init;
function init() {
	if (document.getElementsByTagName("img")[0]) {
		try
		   {document.getElementsByTagName("img")[0].style.cursor = "pointer";}
		catch (e)
		   {document.getElementsByTagName("img")[0].style.cursor = "hand";}
		cursor = document.getElementsByTagName("img")[0].style.cursor;
	}
	if (document.getElementsByTagName("img")[0]) {
		var images = document.getElementsByTagName("img");
		for ( var i=0; i<images.length; i++ ) {
			if (images[i].className != "nopopup") {
				images[i].onclick = function () {showimg(this.src,this.id)};
				images[i].style.cursor = cursor;
			}
		}
	}
}
function showimg(src,id) {
	if (id.indexOf('bild') != -1)
		dir = 'bild';
	else
		dir = 'vita';
	var bild_window = window.open("http://"+window.location.hostname+"/bild.php?"+dir+"ID="+id,"Bild","toolbar=no,width=800,height=550,directories=no,scrollbars,status=no,menubar=no,resizable=yes");
	bild_window.document.close();
	bild_window.focus();
}*/
function fett() {
	var textfield = document.getElementsByTagName('textarea')[0];
	if (textfield.selectionStart || textfield.selectionStart == '0') {
		textfield.focus();
		var startPos = textfield.selectionStart;
		var endPos = textfield.selectionEnd;
		strSelection = "[b]" + textfield.value.substring(startPos, endPos)+ "[/b]";
		textfield.value = textfield.value.substring(0, startPos) + strSelection + textfield.value.substring(endPos, textfield.value.length);
	}
	else if (document.selection && document.selection.createRange().text != '') {
		document.selection.createRange().text = "[b]" + document.selection.createRange().text + "[/b]";
	}
	else {
		if (document.getElementById('fett').style.fontWeight=="bolder") {
			textfield.value += "[b]";
			document.getElementById('fett').style.fontWeight="normal";
		}
		else {
			textfield.value += "[/b]";
			document.getElementById('fett').style.fontWeight="bolder"
		}
	}
}
function italic() {
	var textfield = document.getElementsByTagName('textarea')[0];
	if (textfield.selectionStart || textfield.selectionStart == '0') {
		textfield.focus();
		var startPos = textfield.selectionStart;
		var endPos = textfield.selectionEnd;
		strSelection = "[i]" + textfield.value.substring(startPos, endPos)+ "[/i]";
		textfield.value = textfield.value.substring(0, startPos) + strSelection + textfield.value.substring(endPos, textfield.value.length);
	}
	else if (document.selection && document.selection.createRange().text != '') {
		document.selection.createRange().text = "[i]" + document.selection.createRange().text + "[/i]";
	}
	else {
		if (document.getElementById('italic').style.fontStyle=="italic") {
			textfield.value += "[i]";
			document.getElementById('italic').style.fontStyle="normal";
		}
		else {
			textfield.value += "[/i]";
			document.getElementById('italic').style.fontStyle="italic";
		}
	}
}

function uline() {
	var textfield = document.getElementsByTagName('textarea')[0];
	if (textfield.selectionStart || textfield.selectionStart == '0') {
		textfield.focus();
		var startPos = textfield.selectionStart;
		var endPos = textfield.selectionEnd;
		strSelection = "[u]" + textfield.value.substring(startPos, endPos)+ "[/u]";
		textfield.value = textfield.value.substring(0, startPos) + strSelection + textfield.value.substring(endPos, textfield.value.length);
	}
	else 
	if (document.selection && document.selection.createRange().text != '') {
		document.selection.createRange().text = "[u]" + document.selection.createRange().text + "[/u]";
	}
	else {
		if (document.getElementById('uline')) {
			textfield.value += "[u]";
			document.getElementById('uline').setAttribute('id','line');
			document.getElementById('line').className = 'noline';
		}
		else {
			textfield.value += "[/u]";
			document.getElementById('line').className = 'uline';
			document.getElementById('line').setAttribute('id','uline');
		}
	}
}
function vorschau() {
	var textarea = document.getElementsByTagName('textarea')[0].value.split('[');
	vorschautext = textarea[0];
	for (var i=0; i<textarea.length; i++) {
		bracket = textarea[i].split(']')[0];
		if (bracket == 'b') {
			vorschautext += '<span style="font-weight: bolder;">' + textarea[i].split('b]')[1].split('/b]')[0];
		}
		if (bracket == 'i') {
			vorschautext += '<span style="font-style: italic;">' + textarea[i].split('i]')[1].split('/i]')[0];
		}
		if (bracket == 'u') {
			vorschautext += '<span style="text-decoration: underline;">' + textarea[i].split('u]')[1].split('/u]')[0];
		}
		if (bracket == '/b' || bracket == '/i' || bracket == '/u') {
			vorschautext += '</span>' + textarea[i].split(']')[1];
		}
		else {
			if (document.getElementById(bracket) && bracket != '') {
				vorschautext +=  '<img src="'+document.getElementById(bracket).src+'">'+textarea[i].split(']')[1];
			}
		}
	}
	vorschau_window = window.open('',"Vorschau","toolbar=no,width=500,height=200,directories=no,scrollbars,status=no,menubar=no,resizable=yes");
	vorschau_window.document.open();
	vorschau_window.document.writeln('<html><head><link rel="stylesheet" href="styles.css" type="text/css">');
	vorschau_window.document.writeln('<title>Vorschau</title>');
	vorschau_window.document.writeln('</head><body>');
	vorschau_window.document.writeln('<div>'+vorschautext.replace(/\n/g,'<br />')+'</div>');
	vorschau_window.document.writeln('<div style="clear:bothposition:relative;top:3px;float:right"><a href="javascript:" onclick="self.close()">Schließen</a></div>');
	vorschau_window.document.writeln('</body></html>');
	vorschau_window.document.close();
	vorschau_window.focus();

//	document.getElementById("bbcode").appendChild(vorschau_Element);
}
