var COLLAPSABLE_PARENT_NAME = "collapsable";
var COLLAPSABLE_PARENT_TYPE = "div";
var COLLAPSABLE_CHILD_TYPE 	= "p";

var COLLAPSABLE_EXPAND = "/_public/js/expand/plus_ik.jpg";
var COLLAPSABLE_SHRINK = "/_public/js/expand/minus_ik.jpg";

init = function() {
	if(document.getElementById && document.createTextNode) {
		var entries = document.getElementsByTagName(COLLAPSABLE_PARENT_TYPE);
		for(i=0;i<entries.length;i++)
		{
			if (entries[i].className==COLLAPSABLE_PARENT_NAME)
				assignCollapse(entries[i]);
			if (entries[i].className=="collapsableDiv")
				assignCollapseDiv(entries[i]);
		}
	}
}

assignCollapse = function (div) {
	var button 	= div.getElementsByTagName('h2')[0];
	button.style.cursor='pointer';

	button.setAttribute('expand', '<img src=' + COLLAPSABLE_EXPAND +' style="float: right; margin-top: 5px;"/>' + button.innerHTML);
	button.setAttribute('shrink', '<img src=' + COLLAPSABLE_SHRINK +' style="float: right; margin-top: 5px;" />' + button.innerHTML);

	button.setAttribute('state', -1);
	button.innerHTML='dsds';
	div.insertBefore(button, div.getElementsByTagName(COLLAPSABLE_CHILD_TYPE)[0]);

	button.onclick=function(){ 
		var state = -(1*this.getAttribute('state'));
		this.setAttribute('state', state);
		
		this.parentNode.getElementsByTagName(COLLAPSABLE_CHILD_TYPE)[0].style.display=state==1?'none':'block';
		this.innerHTML = this.getAttribute(state==1?'expand':'shrink');
	};					
	button.onclick();
}
assignCollapseDiv = function (div) {
	var button 	= div.getElementsByTagName('h2')[0];
	button.style.cursor='pointer';

	button.setAttribute('expand', '<img src=' + COLLAPSABLE_EXPAND +' style="float: right; margin-top: 5px;"/>' + button.innerHTML);
	button.setAttribute('shrink', '<img src=' + COLLAPSABLE_SHRINK +' style="float: right; margin-top: 5px;" />' + button.innerHTML);

	button.setAttribute('state', -1);
	button.innerHTML='dsds';
	div.insertBefore(button, div.getElementsByTagName("div")[0]);

	button.onclick=function(){ 
		var state = -(1*this.getAttribute('state'));
		this.setAttribute('state', state);
		
		this.parentNode.getElementsByTagName("div")[0].style.display=state==1?'none':'block';
		this.innerHTML = this.getAttribute(state==1?'expand':'shrink');
	};					
	button.onclick();
}

window.onload=init;