/**********************************************************************
 *	EKSPONENT TABLE AUTO STYLER                                       *
 *	Change history:                                                   *
 *	                                                                  *
 **********************************************************************/
 
var EKSPO_VERBOSE = "";

function ekspoStyling() {
	this.cssClass = null;
	this.cssStyle = null;
	this.cssFunction = null;
}

ekspoStyling.prototype.applyStyle = function(element) {
	if(this.cssClass!=null)
		element.className = this.cssClass;
	
	if(this.cssStyle!=null)
		element.style.cssText = this.cssStyle;
						
	if(this.cssFunction!=null)
		this.cssFunction(element);
}

function ekspoTableLayout() {
	this.overrideTableAttributes = true;
	
	this.table = new ekspoStyling();
	this.allCells = new ekspoStyling();
	
	this.firstRow = new ekspoStyling();
	this.lastRow = new ekspoStyling();

	this.allRows = new ekspoStyling();
	this.oddRows = new ekspoStyling();
	this.evenRows = new ekspoStyling();	
	
	this.rowNumber = new Array();
}

ekspoTableLayout.prototype.applyOnTable = function(tableElement) {
	// alert("applying: "+tableElement.outerHTML);
	
	if(this.overrideTableAttributes) {
		tableElement.setAttribute("border", "0", 0);
		tableElement.setAttribute("cellspacing", "0", 0);
		tableElement.setAttribute("cellpadding", "0", 0);		
	}

	if(this.table!=null) {
		this.table.applyStyle(tableElement);
	}

	if(this.allCells!=null) {
		for(var rc=0; rc<tableElement.rows.length; rc++) {
			for(var cc=0; cc<tableElement.rows[rc].cells.length; cc++) {
				this.allCells.applyStyle(tableElement.rows[rc].cells[cc]);
			}
			
			if(this.rowNumber[rc]!=null) {
				this.rowNumber[rc].applyStyle(tableElement.rows[rc]);
			}
		}
	}

	if(this.allRows!=null && tableElement.rows.length>0) {
		for(var rc=0; rc<tableElement.rows.length; rc++) {
			this.allRows.applyStyle(tableElement.rows[rc]);
		}
	}	
	
	if(this.oddRows!=null && tableElement.rows.length>0) {
		for(var rc=0; rc<tableElement.rows.length; rc+=2) {
			this.oddRows.applyStyle(tableElement.rows[rc]);
		}
	}	

	if(this.evenRows!=null && tableElement.rows.length>0) {
		for(var rc=1; rc<tableElement.rows.length; rc+=2) {
			this.evenRows.applyStyle(tableElement.rows[rc]);
		}
	}	
	
	if(this.firstRow!=null && tableElement.rows.length>0) {
		this.firstRow.applyStyle(tableElement.rows[0]);
	}
	
}

function ekspoTableLayouter() {
	this._tables = new Array();
}

ekspoTableLayouter.prototype.isDescendantOf = function(descendant, element) {
	if(descendant==element)
		return true;
	
	while(descendant!=element && descendant!=null) {
		descendant = descendant.parentNode;
	}
	return (descendant==element);
}

ekspoTableLayouter.prototype.addTablesInElement = function(element,layout) {
	if(!element) {
		EKSPO_VERBOSE += "addTablesInElement: element cannot be null";
		return;
	}
	if(!layout) {
		EKSPO_VERBOSE += "addTablesInElement: layout cannot be null";
		return;
	}

	var tables = document.getElementsByTagName("TABLE");
	for(var foo=0; foo<tables.length; foo++) {
		var table = tables[foo];
		// alert(table.outerHTML);
		if(this.isDescendantOf(table, element)) {
			var tblObj = new Object();
			tblObj.table = table;
			tblObj.layout = layout;
		
			this._tables[this._tables.length] = tblObj;
			
			// return;
		}
	}
}

ekspoTableLayouter.prototype.doLayout = function() {
	for(var foo=0; foo<this._tables.length; foo++) {
		this._tables[foo].layout.applyOnTable(this._tables[foo].table);
	}
}


