// text fitting functions

	var fitTextInBox_maxWidth = false;
	var fitTextInBox_maxHeight = false;
	var fitTextInBox_currentWidth = false;

	var div= false;
	var span = false;
	function fitTextInBox(boxID,maxHeight)
	{
		var div = document.getElementById(boxID);
		if (!div) return;
		if (maxHeight)
			fitTextInBox_maxHeight = Math.min(maxHeight, div.clientHeight);
		else
			fitTextInBox_maxHeight = div.clientHeight;

		fitTextInBox_maxWidth = div.clientWidth ;
		span = div.getElementsByTagName('SPAN')[0];
		span.style.fontSize = '1px';
		fitTextInBox_currentWidth = span.offsetWidth;

		var wrap = true;
		/// - this test breaks IE :`
		//if (div.style.whiteSpace ) {
		//	wrap = div.style.whiteSpace != 'pre' ;
		//} else {
			// Don't know what it is so force to default
		//	div.style.whiteSpace = 'pre-wrap';
		//}
		fitTextInBoxAutoFit(1, wrap);
		//barf(boxID,span.style.fontSize, span.offsetWidth, fitTextInBox_maxWidth , span.offsetHeight, fitTextInBox_maxHeight );
	}


	function barf(a,b,c,d,e,f)
	{
		alert(a+' ~ '+b+' ~ '+c+' ~ '+d+' ~ '+e+' ~ '+f+' ~ ');
	}

	function fitTextInBoxAutoFit(inc, wrap)
	{
		var tmpFontSize = span.style.fontSize.replace('px','')/1;
		span.style.fontSize = (tmpFontSize + inc) + 'px';

		if (inc < 0 )
		{
			if ( span.offsetHeight  > fitTextInBox_maxHeight  || span.offsetWidth > fitTextInBox_maxWidth )
			{
				// Use this to shrink back down into the box
				fitTextInBoxAutoFit(inc, wrap);
			}
			else
			{
				//barf('done shrink',span.style.fontSize, span.offsetWidth, fitTextInBox_maxWidth , span.offsetHeight, fitTextInBox_maxHeight );
			}
		}
		else
		{
			if (span.offsetWidth  >= fitTextInBox_currentWidth &&
				span.offsetWidth  <  fitTextInBox_maxWidth &&
				span.offsetHeight <  fitTextInBox_maxHeight &&
				tmpFontSize < 300 )
			{
				// uses this to get fontsize without wrapping
				fitTextInBox_currentWidth = span.offsetWidth;
				fitTextInBoxAutoFit(inc, wrap);
			}
			else
			{
				span.style.fontSize = tmpFontSize + 'px';
				//barf('maybe grow',span.style.fontSize, span.offsetWidth, fitTextInBox_maxWidth , span.offsetHeight, fitTextInBox_maxHeight );

				if ( wrap )
				{
					var maxFontSize = tmpFontSize * Math.sqrt( fitTextInBox_maxHeight / span.offsetHeight )  ;
					if ( maxFontSize - tmpFontSize > 1 )
					{
						span.style.fontSize =  maxFontSize +'px';
						//barf('grown',span.style.fontSize, span.offsetWidth, fitTextInBox_maxWidth , span.offsetHeight, fitTextInBox_maxHeight );
					}
				}

				if ( span.offsetHeight > fitTextInBox_maxHeight  || span.offsetWidth > fitTextInBox_maxWidth)
				{
					fitTextInBoxAutoFit(-1, wrap);
				}
			}
		}
		//span.style.lineHeight = '110%';


	}

	var box_minHeight = false;
	function fitBoxToText(boxID,minHeight)
	{
		if (! minHeight ) minHeight = 0;

		var div = document.getElementById(boxID);
		if (!div) return 0;

		var span = div.getElementsByTagName('SPAN')[0];
		if (!span) return 0;

		var divOffsetHeight = div.offsetHeight;
		var baselineTweak = 0;
		//barf(div.offsetHeight , div.clientHeight , span.offsetHeight , span.clienttHeight);

		if ( span.offsetWidth == 0 ) {
			div.style.height = minHeight + 'px';
		}
		else if ( span.offsetHeight < div.clientHeight && span.offsetHeight >= minHeight )
		{
			// TBD not sure about the adjustment factor here
			div.style.height = ( span.offsetHeight + baselineTweak) + 'px' ;
		}
		//barf(div.offsetHeight , div.clientHeight , span.offsetHeight , span.clienttHeight);
		return divOffsetHeight - div.offsetHeight;
	}

	function giveSpace(toID, fromID, minHeight)
	{
		if (! minHeight ) minHeight = 0;

		var to = document.getElementById(toID);

		to.style.height = to.style.height.replace('px','')/1 + fitBoxToText(fromID, minHeight) + 'px';
	}



// Ad rotation

function zeroPad(n, digits)
{
	n = n.toString();
	while (n.length < digits) {
		n = '0' + n;
	}
	return n;
}


// generate interger between 1 and n
function random(n) { return Math.floor(Math.random()* n) + 1; }


function buildName(filename, num, fixed)
{
	if (!fixed || !num || num <1)
		num = random(num);

	iterator = "_" + zeroPad(num ,3);

	 if ( filename.length == 0 )
 		return "";

	var dot = filename.lastIndexOf(".") ;

 	if (dot == -1 )
 		return filename + iterator;

 	return filename.substring(0,dot)  + iterator + filename.substr(dot,filename.length);
}

function randomSrc(id, count)
{
	if (!count || count < 1) return ;
	var obj = document.getElementById(id);
	obj.src  = buildName(obj.src, count) ;
}

// extra is an additional url to include in the rotation - acts as default if 0 count
function randomReplace(count, extra)
{
	if (!count ) {
		return ;
	}

	if ( count < 1)
	{
		if(!extra) {
			return ;
		} else {
			location.replace(extra);
			return ;
		}
	}

	if ( extra)
		if ( random( count/1 + 1) > count)
		{
			location.replace(extra);
			return ;
		}

	location.replace(buildName(location.href, count) ) ;
}

