var text = '';
var expire = 0;
var windowWidth = window.innerWidth || document.documentElement.clientWidth;


function pasteQuote(form, username, text) {
	addText("[quote]Zitat von "+username+"\n"+text+"\n[/quote]", form);
}
  
function openProfile(UID) {
	var profile = window.open(
		'./profile.php?UID='+UID, '_blank' /*'profileWindow'*/,
		'width=455,height=270,location=no,menubar=no,resizable=yes,scrollbars=yes,status=no,toolbar=no');
}
function openProfile2(dest) {
	var profile = window.open(
		'http://my.mods.de/'+dest, '_blank' /*'profileWindow'*/,
		'width=780,height=540,location=no,menubar=no,resizable=yes,scrollbars=yes,status=no,toolbar=no');
}

function makeFocus(form) {
	form.message.blur();
	form.message.focus();
}

function makeImage(form) {
	var url = window.prompt('URL zum Bild: ', 'http://');
	if(url != '' && url != null)
		return addText('[img]'+url+'[/img]', form);
	makeFocus(form);
}

function makeLink(form) {
	var url = window.prompt('URL: ', 'http://');
	if(url != '' && url != null)
		return addText('[url]'+url+'[/url]', form);
	makeFocus();
}

function makeNamedLink(form) {
	var name = window.prompt('Linktext: ', '');
	if(name != '' && name != null) {
		var url  = window.prompt('URL: ', 'http://');
		if(url != '' && url != null)
			return addText('[url='+url+']'+name+'[/url]', form);
	}
	makeFocus();
}

function addToList(form) {
	var list = '';
	var entry = '-';
	var start = '';
	var type = window.prompt("Listtyp:\n[ a , 1 , leer ]", '');
	if(type == 'a' || type == '1') start = '[list=' + type + "]\n";
	else start = "[list]\n";
	while(entry != '' && entry != null) {
		var entry = window.prompt('Listeneintrag:', '');
		if(entry != '' && entry != null)
			list += '[*]' + entry + "\n";
	}
	var toAdd = start + list + '[/list]';
	addText(toAdd, form);
}

function addText(toAdd,form) {
	addCode(toAdd, '', form);
}

function addCode (tagStart, tagEnd, form) {
	var input = form.message;
	input.focus();

	if (typeof document.selection != 'undefined') {
		var range = document.selection.createRange();
		var insText = range.text;

		range.text = tagStart + insText + tagEnd;
		range = document.selection.createRange();

		
		if (insText.length == 0) {
			range.move('character', -tagEnd.length);
		}
		else
			range.moveStart('character', tagStart.length + insText.length + tagEnd.length);
		range.select();

	} else if (typeof input.selectionStart != 'undefined') {
		var start = input.selectionStart;
		var end = input.selectionEnd;
		var insText = input.value.substring(start, end);

		input.value = input.value.substr(0, start) + tagStart + insText + tagEnd + input.value.substr(end);

		var pos = (insText.length == 0) ? (start + tagStart.length) : (start + tagStart.length + insText.length + tagEnd.length);
		input.selectionStart =
		input.selectionEnd = pos;
	}
}

function jumpTo(BID){
	if (BID != 0)
		window.location.href = 'board.php?BID='+BID;
}

function openModTagWindow (tid) {
	window.open('list-modtags.php?TID='+tid, '_blank', 'width=250,heigth=350,resizable=yes,status=yes,scrollbars=yes,dependent=yes');
}
function insertThreadTag (tag) {
	opener.fSetThreadTag.thread_tag.value = tag;
	opener.fSetThreadTag.submit();
	window.close();
}

function $ (id) { return document.getElementById(id) }

function toggleVisibility (id) {
	var s = document.getElementById(id).style;
	s.display = (s.display == 'none') ? 'inline' : 'none';
}
function toggleCat (el, id) {
	if (expire == 0) {
		expire = new Date();
		expire.setTime(expire.getTime() + (365 * 24 * 60 * 60 * 1000));
		expire = expire.toGMTString();
	}
	var s = document.getElementById(id).style;
	if (s.display == 'none') {
		s.display = 'inline';
		el.innerHTML = '&raquo;';
		document.cookie = 'hide_'+id+'=0; expires=Thu, 01-Jan-70 00:00:01 GMT';
	} else {
		s.display = 'none';
		el.innerHTML = '&laquo;';
		document.cookie = 'hide_'+id+'=1; expires='+expire;
	}
}

function setBookmark (pid, token) {
	async_get(
		'async/set-bookmark.php?PID='+pid+'&token='+token, null,
		function (xml) {
			switch(parseInt(xml.responseText)) {
				case 1: alert('OK, Lesezeichen gesetzt.'); break;
				case 2: alert('Du hast zuviele Lesezeichen und kannst daher kein neues setzen!'); break;
				default: alert('Unbekannter Fehler // '+xml.responseText); break;
			}
		}
	);
	return;
}
function removeBookmark (bmid, token, thread, force) {
	var msg = thread
		? 'Soll das Lesezeichen \''+thread+'\' wirklich gelöscht werden?'
		: 'Soll dieses Lesezeichen wirklich gelöscht werden?';
	if (force || confirm(msg)) {
		async_get('async/remove-bookmark.php?BMID='+bmid+'&token='+token, null, null);
		return true;
	} else
		return false;
}
function updateBookmark (bmid, token) {
	async_get(
		'async/update-bookmark.php?BMID='+bmid+'&token='+token, null,
		function (xml) { if (parseInt(xml.responseText) == 1) alert('OK, das Lesezeichen wurde auf den letzten Post in diesem Thread gesetzt.') } );
	return;
}

function async_get (url, post, retfunc) {
	var xmlact;
	try { xmlact = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHTTP'); }
	catch (e) { xmlact = null; }
	if (! xmlact) return;
	xmlact.open((post && post.length) ? 'POST' : 'GET', url);
//	xmlact.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	xmlact.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
	xmlact.onreadystatechange = function () { if (xmlact && xmlact.readyState==4 && xmlact.status==200) { retfunc(xmlact); xmlact = null; } };
	xmlact.send(post);
}

function resizeImages () {
	var elems = document.getElementsByTagName('img');
	for (var i=elems.length; i>=0; i--)
		if (elems[i] && re_imgclass.test(elems[i].className))
			resizeImage(elems[i]);
}

var re_imgclass = /\bp\b/i;
function resizeImage (e) {
	var p = e.parentNode,
	    maxw = (windowWidth * 0.95) - 15;

	if (!e || !p)
		return;

	while( p ) {
		maxw -= p.offsetLeft;
		p = p.offsetParent;
	}

	var w = e.offsetWidth,
	    r = e.offsetHeight / w;

	if (w <= maxw)
		return;
	
	e.style.width = maxw;
	e.style.height = maxw * r;
}

function deleteElem (e) {
	if (e && e.parentNode) {
		e.parentNode.removeChild(e);
		return true;
	}
	return false;
}

