function adjustHeight(columns){
    var tables = this.getTables(columns);
    this.adjustHeightRow(columns,tables);
}

function getTables(columns){
    var tableClasses = Array('infobox','contentbox-outer','productbox');
    var tables = Array();
    var tablesRev = Array();
    var maxLength = 0;
    for(i=0;i<columns.length;i++){
	element = document.getElementById(columns[i]);
	tables[i] = Array();
	var tableCount = 1;
	var tableFound = scanForHTMLNode(element,'TABLE',tableCount);
	while(tableFound){
	    for(c=0;c<tableClasses.length;c++){
		if(tableClasses[c]==tableFound.className) {
		    var index = tableCount - 1;
		    tables[i][index] = tableFound;
		    break;
		}
	    }
	    tableCount++;
	    tableFound = scanForHTMLNode(element,'TABLE',tableCount);
	}
	if(tables[i].length>maxLength) {
	    maxLength = tables[i].length;
	    maxColumn = i;
	}
    }
    //reverse array
    for(i=0;i<tables.length;i++){
	tablesRev[i] = Array();
	for(r=0;r<tables[i].length;r++){
	    var rowIndex = tables[i].length-r-1;
	    tablesRev[i][rowIndex] = tables[i][r];
	}
    }
    return tablesRev;
}

function adjustHeightRow(columns,tables,row){
    if(!row) row = 0;

    var tablesPos = this.getTablePositions(tables);
    var comp = this.scanForSimilarPosition(columns,tablesPos,row);

    var greatest = 0;
    var greatestId = 0;
    var bottomObjs = Array();
    var bottomPos = Array();
    var adjustTr = Array();
    
    var columnLength = 0;
    for(i=0;i<columns.length;i++){
	var boxtype = '';
	if(tables[i][row]) {
	    bottomObjs[i] = tables[i][row];
	    boxtype = bottomObjs[i].className;
	}
	adjustTr[i]=getTableAdjustRow(bottomObjs[i],boxtype);
	if(bottomObjs[i]) {
	    bottomPos[i] = tablesPos[i][row];
	    var _colLength = tables[i].length;
	    if(_colLength > columnLength) columnLength = _colLength;
	    if(columnLength > 1 && _colLength > 1 && comp[i]){
		if(bottomPos[i]>greatest) {
		    greatest = bottomPos[i];
		    greatestId = i;
		}
	    }
	}
    }
    if(greatest){
	bottomValue = bottomPos[greatestId];
	for(i=0;i<bottomObjs.length;i++){
	    if(adjustTr[i] && (bottomPos[i]<bottomValue)) {
		var adjust=bottomValue - bottomPos[i];
		var height=JSLib.Core.getHeight(adjustTr[i])+adjust;
		adjustTr[i].style.height = height+"px";
		var diff=JSLib.Core.getHeight(adjustTr[i])-height;
		adjustTr[i].style.height = (height-diff)+"px";
	    }
	}
    }

    row++;
    if(row<columnLength){
	var adjustedTables = this.getTables(columns);
	this.adjustHeightRow(columns,adjustedTables,row);
    }else{
	this.adjustLastRow(columns);
    }
}

function getTablePositions(tables){
    var tablesPos = Array();
    for(i=0;i<tables.length;i++){
	tablesPos[i] = Array();
	for(t=0;t<tables[i].length;t++){
	    tablesPos[i][t] = JSLib.Core.getTop(tables[i][t]) + JSLib.Core.getHeight(tables[i][t]);
	}
    }
    return tablesPos;
}

function scanForSimilarPosition(columns,tablesPos,row){
    var padding = 40;
    var rowValues = Array();
    var comp = Array();
    
    for(c=0;c<tablesPos.length;c++){
	rowValues[c] = tablesPos[c][row];
    }
    for(i=0;i<rowValues.length;i++){
	for(c=0;c<rowValues.length;c++){
	    var low = rowValues[i]-padding;
	    var high = rowValues[i]+padding;
	    if(rowValues[c]>low && rowValues[c]<high && c!=i) comp[c] = 1;
	}
    }
    return comp;
}

function adjustLastRow(columns){
    var greatest = 0;
    var greatestId = 0;
    var bottomObjs = Array();
    var bottomPos = Array();
    var adjustTr = Array();
    for(i=0;i<columns.length;i++){
	element = document.getElementById(columns[i]);
	bottomObjs[i] = scanForHTMLNode(element,'TABLE',1);
	adjustTr[i] = getTableAdjustRow(bottomObjs[i],bottomObjs[i].className);
	if(bottomObjs[i]) {
	     bottomPos[i] = JSLib.Core.getTop(bottomObjs[i]) + JSLib.Core.getHeight(bottomObjs[i]);
	     if(bottomPos[i]>greatest) {
		 greatest = bottomPos[i];
		 greatestId = i;
	     }
	}
    }
    if(greatest){
	bottomValue = bottomPos[greatestId];
	for(i=0;i<bottomObjs.length;i++){
	    if(adjustTr[i] && (bottomPos[i]<bottomValue)) {
		var adjust=bottomValue - bottomPos[i];
		var height=JSLib.Core.getHeight(adjustTr[i])+adjust;
		adjustTr[i].style.height = height+"px";
		var diff=JSLib.Core.getHeight(adjustTr[i])-height;
		adjustTr[i].style.height = (height-diff)+"px";
	    }
	}
    }
}

function getTableAdjustRow(node,boxtype){
    var lastrow = node;
    switch(boxtype){
    case 'infobox':
    case 'productbox':
	lastrow = scanForHTMLNode(lastrow,'TBODY',1);
	lastrow = scanForHTMLNode(lastrow,'TR',2);
	break;
    case 'contentbox-outer':
	lastrow = scanForHTMLNode(lastrow,'TBODY',1);
	lastrow = scanForHTMLNode(lastrow,'TR',1);
	lastrow = scanForHTMLNode(lastrow,'TD',1);
	lastrow = scanForHTMLNode(lastrow,'TABLE',1);
	lastrow = scanForHTMLNode(lastrow,'TBODY',1);
	lastrow = scanForHTMLNode(lastrow,'TR',1);
	break;
    }
    return lastrow;
}

function scanForHTMLNode(element,tag,num){
    for(j=element.childNodes.length-1;j>=0;j--){
	if(element.childNodes[j].nodeType==1 && element.childNodes[j].nodeName==tag){
	    num--;
	    if(num==0)return element.childNodes[j];
	}
    }
    return false;
}
