var useBSNns;

if (useBSNns)
{
	if (typeof(bsn) == "undefined")
		bsn = {}
	var _bsn = bsn;
}
else
{
	var _bsn = this;
}




_bsn.commentSystem = {
	isOn:	false
}

_bsn.commentSystem.init = function ($div)
{
	if (!document.getElementById)
		return false;
	
	this.postEle = document.getElementById($div)
	this.isOn = false;
	this.selectEle = null;
	
	var children = this.postEle.childNodes;
	var id = 1;
	
	for (var i=0;i<children.length;i++)
	{
		if (children[i].nodeType != 3)
		{
			children[i].id = "bsnID"+id;
			children[i].style.position = "relative";
			id++;
		}
	}
	
}

_bsn.commentSystem.toggle = function ()
{
	if (!document.getElementById)
		return false;
		
	if (!this.isOn)
	{
		this.activate();
	} else {
		this.deactivate();
	}
}




_bsn.commentSystem.activate = function ()
{
	var children = this.postEle.childNodes;
	var me = this;
	
	
	for (var i=0;i<children.length;i++)
	{
		if (children[i].appendChild)
		{

			if (children[i].nodeType != 3)
			{
				children[i].onmouseover = function ()
				{
					this.className += " csOver";
				}
				children[i].onmouseout = function ()
				{
					this.className = this.className.substring(0,-7);
				}
				children[i].onclick = function ()
				{
					me.select(this);
				}
			}
				
		}
	}
	
	
	// get number of comments for each block
	//
	var comments = document.getElementById('prevcomments').childNodes;
	this.aNumComments = new Array();
	var up, bl;
	
	for (var i=0;i<comments.length;i++)
	{
		if (comments[i].id)
		{
			up = comments[i].id.indexOf("_");
			if (up != -1)
			{
				bl = comments[i].id.substring(up+1);
				
				if (this.aNumComments[bl] == undefined)
					this.aNumComments[bl] = 1;
				else
					this.aNumComments[bl]++;
			}
		}
	}
	
	// display number of comments for each block
	//
	var div;
	
	for (var key in this.aNumComments)
	{

		div = document.createElement("div");
		div.appendChild( document.createTextNode( this.aNumComments[key] ) );
		div.className = "csCCount";
		document.getElementById("bsnID"+key).appendChild( div );
	}
	

	this.isOn = true;
	var btn = document.getElementById('csButton');
	btn.value = "deactivate block comments";

}




_bsn.commentSystem.deactivate = function ()
{
	var children = this.postEle.childNodes;
	var me = this;
	
	
	for (var i=0;i<children.length;i++)
	{
		if (children[i].appendChild)
		{

			if (children[i].nodeType != 3)
			{
				children[i].onmouseover = null;
				children[i].onmouseout = null;
				children[i].onclick = null;
				
				children[i].className = children[i].className.substring(0,-7);
			}
			
		}
	}
	
	
	// remove number of comments
	//
	for (var key in this.aNumComments)
	{
		document.getElementById("bsnID"+key).removeChild( document.getElementById("bsnID"+key).lastChild );
	}
	
	

	this.isOn = false;
	var btn = document.getElementById('csButton');
	btn.value = "activate block comments";

}



_bsn.commentSystem.select = function ($ele)
{
	var children = this.postEle.childNodes;
	var me = this;
	var id = $ele.id.substring(5);
	
	for (var i=0;i<children.length;i++)
	{
		if (children[i].id != $ele.id  &&  children[i].nodeType != 3)
		{
			children[i].style.display = "none";
		}
	}
	
	$ele.className = $ele.className.substring(0,-7);
	
	var btn = document.getElementById('csButton');
	btn.value = "show all";
	btn.onclick = function () { me.deselect() }
	
	
	
	// show only comments for this block
	//
	var comments = document.getElementById('prevcomments').childNodes;
	
	for (var i=0;i<comments.length;i++)
	{
		if (comments[i].id)
		{
			up = comments[i].id.indexOf("_");
			if (up != -1)
			{
				bl = comments[i].id.substring(up+1);
				
				if (bl != id)
					comments[i].style.display = "none";
			} else {
				comments[i].style.display = "none";
			}
		}
	}
	
	
	// write id into form
	//
	document.getElementById('blockid').value = id;
	
	
	// adjust comment header
	// (using innerHTML because it's late and I'm lazy)
	//
	document.getElementById('commenthead').innerHTML = "Write a comment about block "+id;
}






_bsn.commentSystem.deselect = function ()
{
	var children = this.postEle.childNodes;
	var me = this;
	
	for (var i=0;i<children.length;i++)
	{
		if (children[i].nodeType != 3)
			children[i].style.display = "block";

	}
	
	// show only comments for this block
	//
	var comments = document.getElementById('prevcomments').childNodes;
	
	for (var i=0;i<comments.length;i++)
	{
		if (comments[i].id)
		{
			up = comments[i].id.indexOf("_");
			if (up != -1)
			{
				comments[i].style.display = "block";
			}
		}
	}
	
	var btn = document.getElementById('csButton');
	btn.value = "deactivate block comments";
	btn.onclick = function () { me.toggle() }
	
	
	// delete id from form
	//
	document.getElementById('blockid').value = "";
	
	// adjust comment header
	// (using innerHTML because it's late and I'm lazy)
	//
	document.getElementById('commenthead').innerHTML = "Write a comment";
}