function TableAdmin() {
    this.tables={};
    this.currentBlock=false;

    this.registerTableAdmin=function (adminId) {
	this.tables[adminId]={
	    formElement:  document.getElementById(adminId+'JSEditForm'),
	    iframe:  document.getElementById(adminId+'JSEditIFrame'),						
	    fieldRowId:  document.getElementById(adminId+'JSRowId'),
	    fieldFieldId:  document.getElementById(adminId+'JSFieldId'),
	    fieldEditElementId:  document.getElementById(adminId+'JSEditElementId'),						
	    lastEditedCell: false,
	    saving: false
	};

	// MSIE refuses to load the page when moving the FORM before loaded
	this.addEvent(document, 'load', function () {
		document.body.appendChild(this.tables[adminId].formElement);
	    });
    }

    this.showField=function(cell, adminId, rowId, fieldId, nodeId, initParam) {
	this.hideField();

	var node=document.getElementById(nodeId);
	this.currentBlock=node.parentNode;
				
	this.tables[adminId].lastEditedCell=cell.parentNode;
	this.tables[adminId].fieldRowId.value=rowId;
	this.tables[adminId].fieldFieldId.value=fieldId;
	this.tables[adminId].fieldEditElementId.value=nodeId;
				
	node.taInit(initParam);

	this.alignBlocks(cell, this.currentBlock);
	this.currentBlock.style.display="block";				
    }

    this.hideField=function() {
	if (this.currentBlock) { // Hide old one
	    this.currentBlock.style.display="none";
	}
    }

    this.alignBlocks=function(cell, block) {
	var position=this.getPosition(cell);
	block.style.position='absolute';
	block.style.top=position[0]+'px';
	block.style.left=position[1]+'px';				
    }

    this.getPosition=function (node) {
	var leftPos=topPos=0;
	if (node.offsetParent) {
	    leftPos=node.offsetLeft;
	    topPos=node.offsetTop;
	    while (node = node.offsetParent) {
		leftPos+=node.offsetLeft;
		topPos+=node.offsetTop;
	    }
	}
	return [topPos, leftPos];
    }

    this.onSubmit=function(adminId, event) {
	if (this.saving) { // Being processed now
	    this.stopEvent(event);
	    return false;
	}
				
	this.tables[adminId].saving=true;
	this.hideField();
    }

    this.onSave=function (adminId) {
	this.tables[adminId].saving=false;
	if (this.tables[adminId].lastEditedCell) {
	    var doc=this.tables[adminId].iframe.contentWindow.document;
	    this.tables[adminId].lastEditedCell.innerHTML=doc.body.innerHTML;
	    this.tables[adminId].lastEditedCell=false; // MSIE tends to slide in infinite loop
	}
    }

    /* COMMON METHODS */

    this.stopEvent=function(event) { 
	if (!event) {
	    event=window.event;
	}
	event.preventDefault && event.preventDefault(); 
	event.returnValue=false;
	return false;
    }

    this.addEvent=function( obj, type, fn ) {
	if (obj.addEventListener) {
	    obj.addEventListener( type, fn, false );
	} else if (obj.attachEvent) {
	    obj["e"+type+fn] = fn;
	    obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }
	    obj.attachEvent( "on"+type, obj[type+fn] );
	}
    }

    this.removeEvent=function( obj, type, fn ) {
	if (obj.removeEventListener) {
	    obj.removeEventListener( type, fn, false );
	} else if (obj.detachEvent) {
	    obj.detachEvent( "on"+type, obj[type+fn] );
	    obj[type+fn] = null;
	    obj["e"+type+fn] = null;
	}
    }		
}


if (!window.ta) {
    window.ta=new TableAdmin();
}


