
/** toCamelCase(input)
 * Converts string input to a camel cased version of itself.
 * For example:
 * toCamelCase("z-index"); // returns zIndex
 * toCamelCase("border-bottom-style"); // returns borderBottomStyle.
 */
function toCamelCase(sInput)
{
	var oStringList = sInput.split('-');

	if(oStringList.length == 1)
	{
		return oStringList[0];
	}

	var ret = sInput.indexOf("-") == 0 ? oStringList[0].charAt(0).toUpperCase() + oStringList[0].substring(1) : oStringList[0];

	for(var i = 1, len = oStringList.length; i < len; i++)
	{
		var s = oStringList[i];
		ret += s.charAt(0).toUpperCase() + s.substring(1)
	}

	return ret;
}



function GetObjectStyle(oObject, strStyle)
{
	var value = null;
	if(typeof(document.defaultView) != 'undefined')
	{
		if(typeof(document.defaultView.getComputedStyle) != 'undefined')
		{
			value = document.defaultView.getComputedStyle(oObject, '').getPropertyValue(strStyle);
//window.alert(value + ' = document.defaultView.getComputedStyle(' + oObject + ', \'\').getPropertyValue(' + strStyle + ')');
		}
		else
		{
			window.alert('Error retrieving style from object.');
			value = '';
		}
	}
	else
	{
		value = oObject.currentStyle[toCamelCase(strStyle)];
//window.alert(value + ' = ' + oObject + '.currentStyle[' + strStyle + ']');
	}
	
	return value;
}


function ObjectWidth(oObject)
{
	var iWidth = -1;

	if(typeof(oObject) != 'object')
		return iWidth;

	if(typeof(oObject.getBoundingClientRect) != 'undefined')
	{
		iWidth = oObject.getBoundingClientRect().right - oObject.getBoundingClientRect().left;
	}
	else
	{
		iWidth = GetObjectStyle(oObject, 'width');
		iWidth = parseInt(iWidth, 10);

		if(isNaN(iWidth))
		{
			iWidth = -1
		}
	}

	return iWidth;
}



function ResizeIMG(oIMG, minSize, maxSize)
{
	if(minSize >= maxSize)
		return;

	if(maxSize != -1)
	{
		if(ObjectWidth(oIMG) >= maxSize)
		{
			oIMG.style.width = maxSize;
//			oIMG.style.border = 'solid 2px red';
		}
	}

	if(minSize != -1)
	{
		if(ObjectWidth(oIMG) <= minSize)
		{
			oIMG.style.width = minSize;
//			oIMG.style.border = 'solid 2px green';
		}
	}
}



function ShrinkToParent(oIMG)
{
	if(typeof(oIMG) == 'undefined')
		return true;

	var oImages = Array();
	var oParent = null;
	
	if(typeof(oIMG.parentNode) != 'undefined')
	{
		oParent = oIMG.parentNode;
	}
	else
	{
		if(typeof(oIMG.parentElement) != 'undefined')
		{
			oParent = oIMG.parentElement;
		}
		else
		{
			return true;
		}
	}

	var ParentSize = ObjectWidth(oParent);
	var ParentPadding = parseInt(GetObjectStyle(oParent, 'padding-left')) + parseInt(GetObjectStyle(oParent, 'padding-right'));
	if(isNaN(ParentPadding))
	{
		ParentPadding = 0;
	}

	ParentSize -= (2 * ParentPadding);

	var i = 0;

	if(typeof(oParent.KR_ImagesHandeled) != 'undefined')
	{
		if(oParent.KR_ImagesHandeled)
		{
			return;
		}
	}

	for(oIMG = oParent.firstChild; oIMG != null; oIMG = oIMG.nextSibling)
	{
		if(oIMG.tagName == 'IMG')
		{
			oIMG.style.display= 'inline';
			oImages[oImages.length] = oIMG;
		}
	}

	oParent.KR_ImagesHandeled = true;

	for(i = 0; i < oImages.length; i++)
	{
		ResizeIMG(oImages[i], -1, ParentSize);
	}
}

function ShrinkAllImagesToParent(strImageName)
{
	if(typeof(window.document.getElementsByName) == 'undefined')
	{
		return true;
	}

	var oImages = window.document.getElementsByName(strImageName);
	var oImage = null;
	var i = 0;
	
	if(null != oImages)
	{
		for(i = 0; i < oImages.length; i++)
		{
			oImage = oImages[i];
			oImage.style.display = 'none';
		}
		
		for(i = 0; i < oImages.length; i++)
		{
			oImage = oImages[i];
			ShrinkToParent(oImage);
		}
	}
}