/*
Credits : Peter Paul Koch - http://www.quirksmode.org/dom/getstyles.html
*/

function getOff(id)
{
	if (!document.createElement)
	{
		alert('This script won\'t work in your browser.');
		return;
	}
	x = document.getElementById(id);
	return x.offsetHeight;
}

/*
Credits : Peter Paul Koch - http://www.quirksmode.org/dom/getstyles.html
*/
function changeOff(amount)
{
	var y = getOff();
	x.style.width = y + amount;
}

/*
Credits : Peter Paul Koch - http://www.quirksmode.org/js/findpos.html
*/
function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}


// Pos 1 : ID du bloc à tester
// Pos 2 : ID du bloc à cumuler / tableau d'IDs de blocs à cumuler
/*var bottomBlocks;
bottomBlocks = new Array(
	new Array('colArow3_outerContainer', null),
	// --------------------------------------------------
	new Array('colBrow3block1_outerContainer', 'colBrow3block2_outerContainer'),
	// --------------------------------------------------
	new Array('colCrow2_innerContainer', null)
);
// --------------------------------------------------
*/

function alignPageBottomBlocks()
{
	var nbBlocks, i, pageBottom, pageBottomBlockId;
	var blockId = new Array();
	var blockHandle = new Array();
	var blocksBlw = new Array();
	var blockBlwHandle = new Array();
	var blockTop = new Array();
	var blockHeight = new Array();
	var blockSetBottom = new Array();
	var blockBlwHeight = new Array();
	var blockHeightIncr = new Array();
	var blockBottom = new Array();
	var blockBlwHeightArr = new Array();

	nbBlocks = arguments.length;
	
	// Get infos, evaluate page bottom

	/* ============================================
		Evaluation des bas et hauteurs respectives de chaque
		bloc (ou "jeu de blocs"), et du plus bas parmi ceux-ci
	   ============================================*/

	// Pour chaque bloc passé en argument
	for (i = 0; i < nbBlocks; i++)
	{
		var nbBlocksBlw = 0;

		blockId[i] = arguments[i][0];
		blockHandle[i] = document.getElementById(blockId[i]);
		blocksBlw[i] = arguments[i][1];

			/* Cas : il faut ajouter à la hauteur du "bloc set" la hauteur d'un ou 
			plusieurs autres bloc(s) présent en dessous du bloc qu'il faut incrémenter. */
		if (blocksBlw[i] != null && blocksBlw[i] != '')
		{
			argType = typeof(blocksBlw[i]);
				//alert('argType = '+argType);

				// Cas un autre bloc
			if (argType == 'string')
			{	
				nbBlocksBlw = 1;
				blockBlwHandle[i] = document.getElementById(blocksBlw[i]);
			}
				// Cas plusieurs autres blocs
			if (argType == 'object') // Tableau d'IDs de blocs
			{
				alert('Cas tableau de blocs : anormal !');
				/*
				nbBlocksBlw = blocksBlw[i][1].lengh;
				if (nbBlocksBlw > 1) 
				{
					for (j = 1; j < nbBlocksBlw; j++)
						blockBlwHandle[i][j] = document.getElementById(blocksBlw[i][j]);
				}*/
			}
		}
		

		// -------------------------------------------
		// Evaluation du bas de chaque "jeu de blocs"
		// -------------------------------------------

		coords = findPos(blockHandle[i]);
		blockTop[i] = coords[1];

		blockHeight[i] = getOff(blockId[i]);
			
			//alert('blockHeight[i] = '+ blockHeight[i]);
		blockSetBottom[i] = blockBottom[i] = blockTop[i] + blockHeight[i];
			//alert('blockId[i] = '+blockId[i]+'\nblockTop[i] = '+ blockTop[i]+'\nblockHeight[i] = '+ blockHeight[i]+'\nblockSetBottom[i] = '+ blockBottom[i]);

			/* Cas il faut cumuler la hauteur avec celle d'un
				autre bloc au-dessus ou au-dessous. */
		if (nbBlocksBlw === 1)
		{
			blockBlwHeight[i] = getOff(blocksBlw[i]);
			blockSetBottom[i] += blockBlwHeight[i];
		}
			/* Cas il faut cumuler la hauteur avec celle de plusieurs
				autres blocs au-dessus ou au-dessous. */
		if (nbBlocksBlw > 1) // Tableau de blocs
		{
			alert('Cas tableau de blocs : anormal !');
			/*for (j = 0; j < nbBlocksBlw - 1; j++)
			{
				blockBlwHeightArr[i][j] = getOff(blockBlwHandle[i][j]);
				if (j = 0) blockSetBottom[i] = blockBottom[i];
				blockBlwHeight[i] += blockBlwHeightArr[i][j];
				blockSetBottom[i] += blockBlwHeightArr[i][j];
			}
			*/
		}		

		// -------------------------------------------
		// Evaluation du bas de la page (le plus bas des "jeux de blocs")
		// -------------------------------------------

		if (i == 0)
		{
			pageBottom = blockSetBottom[i];
			pageBottomBlockId = blockId[i];
		}
		else
		{
			if (blockSetBottom[i] > pageBottom)
			{
				pageBottom = blockSetBottom[i];
				pageBottomBlockId = blockId[i];
			}
		}

			// alert('Blocset le plus bas : '+pageBottomBlockId+ '\nBas de page : ' +pageBottom);

	}

	/* ============================================
		Assignation aux blocs descendant moins bas
		(que celui descendant le plus bas) d'une 
		hauteur suplémentaire pour compenser 
		la différence (et ainsi les aligner)
	   ============================================*/

	for (i = 0; i < nbBlocks; i++)
	{
		if (blockId[i] == pageBottomBlockId) continue;

		blockHeightIncr[i] = pageBottom - blockSetBottom[i];

			// Correction paddings Mozilla
		if (navigator.appName === 'Netscape')
		{	
				//alert(blockHeightIncr[i]);
			topPad = getStyle(blockId[i], 'padding-top');
			topPad = topPad.substr(0, topPad.length - 2);
				//alert(topPad);
			bottomPad = getStyle(blockId[i], 'padding-bottom');
			bottomPad = bottomPad.substr(0, bottomPad.length - 2);
				//alert(bottomPad);
			blockHeightIncr[i] = blockHeightIncr[i] - parseInt(topPad) - parseInt(bottomPad);
				//alert(blockHeightIncr[i]);
		}

			// Correction "forcée" (comportement Mozilla inexpliqué)
		if (arguments[i].length == 4 && arguments[i][3] === true)
			blockHeightIncr[i] += arguments[i][2];

			//alert('Dans la boucle corrective :\nblockId[i] = '+ blockId[i]+'\nbBlocksBlw = '+nbBlocksBlw+'Dans la boucle corrective :\npageBottom = '+ pageBottom+'\nblockSetBottom[i] = '+blockSetBottom[i]+'\nblockHeightIncr[i] = '+blockHeightIncr[i]);

		if (nbBlocksBlw > 0) 
		{
				//alert('Avant correction de l\'incrément : blockHeightIncr[i] = '+blockHeightIncr[i]);
			blockHeightIncr[i] -= blockBlwHeight[i];	
				//alert('Après correction de l\'incrément : blockHeightIncr[i] = '+blockHeightIncr[i]);
		}

		blockHandle[i].style.height = blockHeight[i] + blockHeightIncr[i];
	}
	
	return true;
}