﻿	function elementWidth( obj ){
//		var retVal = 0;
//		if( obj.offsetParent ){
//			if( obj.offsetWidth == 0 ){
//				retVal += elementWidth( obj.offsetParent );
//			}else{
//				retVal = obj.offsetWidth;
//			}
//		}
//    
//
//		return retVal;
        return Element.getWidth(obj);
	}

	function elementHeight( obj ){
//		var retVal = 0;
//		if( obj.offsetParent ){
//			if( obj.offsetHeight == 0 ){
//				retVal += elementHeight( obj.offsetParent );
//			}else{
//				retVal = obj.offsetHeight;
//			}
//		}
//
//		return retVal;
        return Element.getHeight(obj);
	}
	
   function getAbsolute(el) { //From Calendar
	var SL = 0, ST = 0;
	var is_div = /^div$/i.test(el.tagName);
	if (is_div && el.scrollLeft)
		SL = el.scrollLeft;
	if (is_div && el.scrollTop)
		ST = el.scrollTop;
	var r = { x: el.offsetLeft - SL, y: el.offsetTop - ST };
	if (el.offsetParent) {
		var tmp = getAbsolute(el.offsetParent);
		r.x += tmp.x;
		r.y += tmp.y;
	}
	return r;
    };	
	

	function layoutTabContainer( _tabContainer ){
		var tabContainer = $(_tabContainer);
   	    tabContainer.style.visibility="hidden";

		if((tabContainer._wrapperTable==undefined)||(tabContainer._wrapperTable == null)){
			var wrapperTable = document.createElement("table");
			wrapperTable.visibility="hidden";
			wrapperTable.border=0;
			wrapperTable.cellPadding=0;
			wrapperTable.cellSpacing=0;
			wrapperTable.style.width="100%";

			var wrapperBody = document.createElement("tbody");
			wrapperTable.appendChild( wrapperBody );
			var wrapperrow = document.createElement("tr");
			wrapperBody.appendChild( wrapperrow );
			var wrappercol = document.createElement("td");
			wrapperrow.appendChild( wrappercol );
			var tabParent = tabContainer.parentNode;
			tabParent.replaceChild( wrapperTable, tabContainer );
			wrappercol.appendChild( tabContainer );
			tabContainer._wrapperTable = wrapperTable;
		}

		if((tabContainer._tmpContainer==undefined)||(tabContainer._tmpContainer==null)){
			tabContainer._tmpContainer = new Array();
			while( tabContainer.firstChild != null ){
				var thisFirstChild = tabContainer.firstChild;
				if( thisFirstChild.style ){
					if( thisFirstChild.style.display!="block" ){
						thisFirstChild.style.display="block";
					}
				}
				tabContainer._tmpContainer.push(tabContainer.removeChild(tabContainer.firstChild));
			}
		}

		if((tabContainer._prevElems!=undefined)&&(tabContainer._prevElems!=null)){
			for( var i = 0; i < tabContainer._prevElems.length; i++ ){
				oldElem = tabContainer._prevElems[i];
				if( oldElem.style ){
					oldElem.parentNode.removeChild( oldElem );
					try{delete oldElem;}catch(noError){}
				}
			}
			try{delete tabContainer._prevElems;}catch(noError){}
		}

		while( tabContainer.firstChild != null ){
			tabContainer.removeChild( tabContainer.firstChild );
		}

		var maxWidth = elementWidth( tabContainer );


		tabContainer._containerDiv = document.createElement("div");
		tabContainer._containerDiv.style.margin = "0px";
		tabContainer._containerDiv.style.padding = "0px";
		tabContainer.appendChild(tabContainer._containerDiv);

		var newTable = document.createElement( "table" );
		newTable.border=0;
		newTable.cellPadding=0;
		newTable.cellSpacing=0;

		var tbody = document.createElement("tbody");
		newTable.appendChild( tbody );
		var trow = document.createElement("tr");
		tbody.appendChild( trow );

		var fillerCol = document.createElement("td");
		fillerCol.innerHTML = "&nbsp;";
		fillerCol.width="100%";
		trow.appendChild( fillerCol );
		tabContainer._containerDiv.appendChild(newTable);
		var toAdd = tabContainer._tmpContainer;

		var rows = new Array();
		var rowIdx = 0;
		rows[rowIdx] = new Array();
		for( var i = 0 ; i < toAdd.length; i++ ){
			var toAddClone = toAdd[i].cloneNode(true);
			var tcol = document.createElement("td");
			tcol.style.whiteSpace="nowrap";
			tcol.appendChild(toAddClone);
			trow.insertBefore(tcol,fillerCol);

			var checkWidth = elementWidth( tabContainer );
			if(checkWidth>maxWidth){
				rowIdx++;
				rows[rowIdx] = new Array();
				trow.removeChild(tcol);
				trow.removeChild(fillerCol);
				var newRowTable = document.createElement("table");
				newRowTable.border=0;
				newRowTable.cellPadding=0;
				newRowTable.cellSpacing=0;
				tbody = document.createElement("tbody");
				newRowTable.appendChild(tbody);
				trow = document.createElement("tr");
				tbody.appendChild(trow);
				tabContainer._containerDiv.insertBefore(newRowTable,newTable);
				newTable=newRowTable;
				trow.appendChild(tcol);
				trow.appendChild(fillerCol);
			}
			rows[rowIdx].push(toAddClone);
			if( toAddClone.style ){
				toAddClone.style.zIndex = 100-rowIdx;
			}
		}
		trow.removeChild(fillerCol);

		tabContainer._prevElems = new Array();

		for( var i = 0 ; i < rows.length; i++ ){
			for( var j = 0 ; j < rows[i].length; j++ ){
				var tabElem = rows[i][j];
				if( tabElem.style ){
//					tabElem._position = Position.positionedOffset( tabElem );
					tabElem._position = getAbsolute(tabElem);

					tabElem._height = elementHeight( tabElem );
					tabElem._width = elementWidth( tabElem );
					tabContainer._prevElems.push(tabElem);
				}
			}
		}

		tabContainer._containerDiv.style.height = elementHeight( tabContainer )+"px";
		var spacerDiv = document.createElement("div");
		spacerDiv.innerHTML = "&nbsp;";
		spacerDiv.style.width = elementWidth( tabContainer ) + "px";
		spacerDiv.style.height = elementHeight( tabContainer ) +"px";
		var bottomPx = undefined;
		var diffHeight = undefined;
		for( var i = 0 ; i < rows.length; i++ ){
			for( var j = 0 ; j < rows[i].length; j++ ){
				var tabElem = rows[i][j];
				if( tabElem.style ){
					if( bottomPx == undefined ){
						bottomPx = (tabElem._position.y+tabElem._height);
					}

					tabElem.parentNode.removeChild( tabElem );
					document.body.appendChild( tabElem );
					tabElem.style.position = "absolute";
					tabElem.style.top = tabElem._position.y+"px";
					tabElem.style.left = tabElem._position.x+"px";
					tabElem.style.height = (bottomPx - tabElem._position.y)+"px";
					tabElem.style.width = tabElem._width +"px";
					var tmpHeight = elementHeight( tabElem );
					if( diffHeight == undefined ){
						diffHeight = tmpHeight - tabElem._height;
					}
					tabElem.style.height = (tmpHeight - diffHeight)+"px";
					tabElem.style.visibility="visible";
				}
				try{delete rows[i][j];}catch(noError){}
			}
			try{delete rows[i];}catch(noError){}
		}
		tabContainer.replaceChild( spacerDiv, tabContainer._containerDiv );
		try{delete tabContainer._containerDiv}catch(noError){}
		tabContainer.style.visibility="visible";
	}


