//<![CDATA[
function FoldingControl(openedImageUrl, closedImageUrl) {
	this.objectHash = new Hash();
	this.openedImageUrl = openedImageUrl;
	this.closedImageUrl = closedImageUrl;
}

FoldingControl.prototype.addControl = function(buttonObj, imageObj, contentObj) {
	var initOpenFlag = false;
	if (arguments.length > 3)
		initOpenFlag = arguments[3];
	
	buttonObj = $(buttonObj);
	imageObj = $(imageObj);
	contentObj = $(contentObj);
	
	initOpenFlag ? contentObj.show() : contentObj.hide();
	imageObj.src = initOpenFlag ? this.openedImageUrl : this.closedImageUrl;
	
	var observeFunc = Event.observe(buttonObj, 'click', FoldingControl.toggleContent.bindAsEventListener(this, buttonObj));
	var item = {'button': buttonObj, 'image': imageObj, 'content': contentObj, 'isOpened': initOpenFlag, 'oFunc': observeFunc};
	this.objectHash.set(buttonObj.identify(), item);
	
	buttonObj.setStyle({cursor: 'pointer'});
};

FoldingControl.toggleContent = function(event) {
	var args = $A(arguments);
	args.shift();

	buttonObj = $(args[0]);
	var statusItem = this.objectHash.get(buttonObj.identify());
	if (statusItem == null)
		return;
	
	statusItem.isOpened = !statusItem.isOpened;
	statusItem.isOpened ? statusItem.content.show() : statusItem.content.hide();
	
	statusItem.image.src = statusItem.isOpened ? this.openedImageUrl : this.closedImageUrl;
	this.objectHash.set(buttonObj.identify(), statusItem);
};

FoldingControl.prototype.unload = function() {
	this.objectHash.each(function(pair) {
		Event.stopObserving($(pair.value.button), 'click', pair.value.oFunc);
	});
};
//]]>