/* Inklap functies */

function toggle_rowdisplay(img)
{
	var table = img.parentNode, display;

	if (img.src == plus.src)
	{
		img.src =  min.src;
		display = ''
		if (typeof klipklapcookie[img.id] != 'undefined')
			delete klipklapcookie[img.id];
	}
	else if (img.src == min.src)
	{
		img.src = plus.src;
		display = 'none'
		klipklapcookie[img.id] = 1;
	}

	while ( table.tagName.toLowerCase() != 'table' && table != document )
		table = table.parentNode;
	if (table == document)
		return;

	var rowcollection = table.rows, i = rowcollection.length, rel;

	while (--i)
	{
		rel = rowcollection[i].getAttribute('relation');
		if ( rel != null && rel == img.id )
			rowcollection[i].style.display = display;
	}

	if (!skipcookie)
	{
		var temp = [], i = 0;
		for (var category in klipklapcookie)
			temp[i++] = category;

		SetCookie('klipklapcookie', temp.join(','));
	}
}

function toggle_cat_show(img)
{
	toggle_rowdisplay(img);

	var row = img.parentNode;
	while ( row.tagName.toLowerCase() != 'tr' && row != document )
		row = row.parentNode;
	if (row == document)
		return;

	var cellcollection = row.cells, i = cellcollection.length, celltype;

	while (--i)
	{
		celltype = cellcollection[i].getAttribute('celltype');
		switch (celltype)
		{
			case 'title':
				cellcollection[i].colSpan = cellcollection[i].colSpan == 1 ? 4 : 1;
				break;
			case 'hide':
				cellcollection[i].style.display = cellcollection[i].style.display == 'none' ? '' : 'none';
				break;
		}
	}
}

function toggle(name)
{
	var e = document.getElementById(name);
	if (e != null && typeof e.onclick == 'function') document.getElementById(name).onclick();
}

function readCookies()
{
	var cookiecontent = GetCookie('klipklapcookie');
	if (cookiecontent !== null)
	{
		var temp = cookiecontent.split(','), i = temp.length;
		while (i--)
			klipklapcookie[temp[i]] = 1;

		i = categories.length;
		while (i--)
			if ( typeof klipklapcookie[categories[i]] != 'undefined' )
				toggle(categories[i]);
	}
	skipcookie = false;
}

function toggle_display(which)
{
	var el = document.getElementById(which);
	el.style.display = el.style.display == 'none' ? '' : 'none';
	if (which == 'topicsearch' && el.style.display == '') document.forms['tsearch'].elements['data[filter_keywords]'].focus();
}

/* Generic Cookie functies */

function GetCookie(sName)
{
	var aCookie = document.cookie.split('; '), i = aCookie.length, aCrumb;
	while (i--)
	{
 		aCrumb = aCookie[i].split('=');
		if (sName == aCrumb[0])
			return typeof aCrumb[1] != 'undefined'? unescape(aCrumb[1]) : null;
	}
	return null;
}

function SetCookie(sName, sValue)
{
	document.cookie = sName + '=' + escape(sValue) + '; expires=Fri, 31 Dec 2099 23:59:59 GMT; path=/';
}

/* message box editing functions */

function storeCursor(element)
{
	if ( typeof element.createTextRange != 'undefined' )
		element.cursorPos = document.selection.createRange().duplicate();
}

function det_replace(type, text)
{
	if (text != '')
	{
		switch (type)
		{
			case 'AND':
				text = text.replace(/(\w+)\s+/g, '$1 AND ');
				break;
			case 'OR':
				text = text.replace(/(\w+)\s+/g, '$1 OR ');
				break;
			case 'brackets':
				text = '('+text+')';
				break;
			case 'title':
				text = 'title:('+text+')';
				break;
			case 'start':
				text = 'start:('+text+')';
				break;
			default:
			text = type;
		}
	}
	else
	{
		switch (type)
		{
			case 'AND':
				text = ' AND ';
				break;
			case 'OR':
				text = ' OR ';
				break;
			case 'brackets':
				text = '()';
				break;
			case 'title':
				text = 'title:';
				break;
			case 'start':
				text = 'start:';
				break;
			default:
				text = type;
		}
	}
	return text;
}

function putStr( type )
{
	var target = document.getElementById( 'messageBox' );

	if ( target != null )
	{
		if ( typeof target.cursorPos != 'undefined' )
		{
			var cursorPos = target.cursorPos;
			cursorPos.text = det_replace(type, cursorPos.text);
		}
		else if ( typeof target.selectionStart != 'undefined' )
		{
			var sStart = target.selectionStart;
			var sEnd = target.selectionEnd;
			var text = det_replace(type, target.value.substr(sStart, sEnd));
			target.value = target.value.substr(0, sStart) + text + target.value.substr(sEnd, target.value.length);
			var nStart = sStart == sEnd ? sStart + text.length : sStart;
			var nEnd = sStart + text.length;
			target.setSelectionRange(nStart, nEnd);
		}
		else
		{
			target.value += det_replace(type,'');
		}
		target.focus();
		storeCursor(target);
	}
}

function winopen(url, winname, width, height)
{
	var popup = window.open(url,winname,'toolbar=no,location=no,menubar=no,scrollbars=yes,width='+width+',height='+height+',resizeable=no,status=no');
	popup.focus();
}

function mark_topicstarter(user_id)
{
	var i = messageids.length, img;
	while (i--)
	{
		if (messageids[i][1] == user_id)
		{
			img = document.getElementById('online_icon_'+messageids[i][0]);
			if (img.src == online.src) img.src = online_ts.src;
			else img.src = offline_ts.src;
			img.width = '14';
			img.title = 'Topicstarter ' + img.title;
		}
	}
}

/* Image preload */

var plus = new Image(13,13);
plus.src = board_template_url + 'images/icons/explode_plus_b.gif';
var min = new Image(13,13);
min.src = board_template_url + 'images/icons/explode_min_b.gif';

var offline = new Image(8,10);
offline.src = board_template_url + 'images/icons/offline.gif';
var offline_ts = new Image(14,10);
offline_ts.src = board_template_url + 'images/icons/offline_ts.gif';
var online = new Image(8,10);
online.src = board_template_url + 'images/icons/online.gif';
var online_ts = new Image(14,10);
online_ts.src = board_template_url + 'images/icons/online_ts.gif';

/* Initialisation */

var categories = [];
var messageids = [];
var klipklapcookie = [], skipcookie = true;