/* 
	Custom functions 
	----------------
	
	Relies on prototype.js
*/

var mainMinHeight = 450;

function LCG_init()
{
	if ($("main"))
	{
		setMinHeight($("main"), mainMinHeight);
	}
	
	if ($('columnBody'))
	{
		matchGreaterHeight($('columnBody'), $('content'));
	}
	
	setBoxGroupHeights();

/*
	if ($('rfxOuterTable'))
	{
		var numTries = 0;
		
		while ((numTries < 100) && ($('rfxImage_Views').getDimensions().width > 10))
		{
		    var tableDimensions = $('rfxOuterTable').getDimensions();
		    alert("Cell Width is: " + $('rfxImage_Views').getDimensions().width);
		    self.resizeTo(tableDimensions.width + 15, tableDimensions.height + 15);
		    
		    numTries++;
		}

	}
	*/
}

function setMinHeight(elementToSet, minHeightPixels)
{
	if (elementToSet.getHeight() < minHeightPixels)
	{
		elementToSet.style.height = minHeightPixels + "px";
	}	
}

function matchGreaterHeight(elementOne, elementTwo)
{
	var oneHeight = elementOne.getHeight();
	var twoHeight = elementTwo.getHeight();
	
	if (oneHeight > twoHeight)
	{
		var twoBottomPadding = elementTwo.getStyle('padding-bottom');
		twoBottomPadding = twoBottomPadding.replace('px', '');

		elementTwo.style.height = (oneHeight - twoBottomPadding) + 'px';
	}
	else
	{
		var oneBottomPadding = elementOne.getStyle('padding-bottom');
		oneBottomPadding = oneBottomPadding.replace('px', '');

		elementOne.style.height = (twoHeight - oneBottomPadding) + 'px';
	}
}

function setBoxGroupHeights()
{
	var boxGroups = $$('.boxGroup');
	
	if (boxGroups.length > 0)
	{
		for (var i = 0; i < boxGroups.length; i++)
		{
			var thisGroupBox = boxGroups[i];
			var childTextBoxes = thisGroupBox.getElementsByClassName('boxText');
			var childEditBoxes = thisGroupBox.getElementsByClassName('editBox');

			if (childTextBoxes.length > 0)
			{
				setMaxHeight(childTextBoxes);
			}
			
			if (childEditBoxes.length > 0)
			{
				var contentBoxes = new Array();
								
				for (var j = 0; j < childEditBoxes.length; j++)
				{
					contentBoxes[j] = childEditBoxes[j].down('.content');
				}
				
				setMaxHeight(contentBoxes);
			}
		}
	}
}

function setMaxHeight(boxesToSet)
{
	var maxHeight = 0;
	
	for (var i = 0; i < boxesToSet.length; i++)
	{
		maxHeight = Math.max(boxesToSet[i].getHeight(), maxHeight);
	}
	
	maxHeight = maxHeight - getVertPadding(boxesToSet[0]);
	
	for (var j = 0; j < boxesToSet.length; j++)
	{
		var thisBox = boxesToSet[j];
		thisBox.style.height = maxHeight + "px";
	}
}
	

function getVertPadding(elementToCheck)
{
	var bottomPadding = elementToCheck.getStyle('padding-bottom');
	bottomPadding = parseInt(bottomPadding.replace('px', ''));
	var topPadding = elementToCheck.getStyle('padding-top');
	topPadding = parseInt(topPadding.replace('px', ''));

	return topPadding + bottomPadding;
}

function clickClear(fieldToClear, defaultText)
{
	if (fieldToClear.value == defaultText)
	{
		fieldToClear.value = "";
	}
}

function clickRecall(fieldToRecall, defaultText)
{
	if (fieldToRecall.value == "")
	{
		fieldToRecall.value = defaultText;
	}
}
