var tableWriter=Class.create();
tableWriter.prototype={
	
	nullpatterns:[/^\s*$/i,/^<p>&nbsp;<\/p>$/i,/^<p>\s*<\/p>$/i,/^\s*<br(\s\/)?>\s*$/i],
	parentBlockTag:"tr",
	childBlockTag:"td",
	hideClass:"tableWriter_hide",
	
	initialize:function(targetClass,options){
		this.targetClass=targetClass;
		
		if(typeof options=="object"){
			for(var prop in options){
				this.prop=options[prop];
			}
		}
		document.write("<style>."+targetClass+"{visibility:hidden}."+targetClass+" ."+this.hideClass+"{display:none}<\/style>");
		Event.observe(document, "dom:loaded",this.load.bind(this),false);
	},
	load:function(){
		var classes=$$("."+this.targetClass);

		classes.each(this.inspectClass.bind(this));
	},
	/* tableの検査 */
	inspectClass:function(_class){
		this.allparentEmpty=true;
		var parents=$A(_class.getElementsByTagName(this.parentBlockTag));
		parents.each(this.inspectParent.bind(this));
		
		if(this.allparentEmpty){
			_class.style.display="none";
		} else {
			_class.style.visibility="visible";
		}		
	},
	/* trの検査 */
	inspectParent:function(_parent){
		//if(this.ifinspectChild){
			/*
				孫検査モード
				孫要素が全部空だったら、子要素を隠す
			*/
			var childs=$A(_parent.getElementsByTagName(this.childBlockTag));
			this.allchildEmpty=true;
			childs.each(this.inspectChild.bind(this));
			if(this.allchildEmpty){
				
				Element.addClassName(_parent, this.hideClass);
			} else {
				this.allparentEmpty=false;
			}
		
	},
	inspectChild:function(_child){
		if(!this.allchildEmpty) return false;
		
		if(!this.ifNodesAreEmpty(_child)){
			this.allchildEmpty=false;
		}
	},
	ifNodesAreEmpty:function(node){
		var html=node.innerHTML;
		for(var j=0,jL=this.nullpatterns.length;j<jL;j++){
			if(this.nullpatterns[j].exec(html)) {
				return true;
			}
		}
		return false;

	}
}

function miniToggleBlock(targetElement){
	if(!targetElement.parentNode) return false;
	var inner=targetElement.innerHTML;
	if(inner==="" || inner==="<p>&nbsp;</p>") {
		return 	targetElement.parentNode.removeChild(targetElement);
	}
}