// 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 integer between 1 and n
function random(n) { return Math.floor(Math.random()* n) + 1; }


function buildName(filename, num)
{
	var 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, random(count)) ;
}

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

	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, random(count) ) );
}


function nextReplace(ad,count,extra)
{
	count = count ? count *1 : 0 ;
	
	var position = getNext(ad,count + (extra ? 1 : 0));
	var href;
	
	if (position < 1) {
		return ;
	}
	else if (position > count) {
		href = extra;
	}
	else {
		href = buildName(location.href, position);
	}
	location.replace(href);
}

/**
 * Retrieves an increments ad counter from a cookie.
 * @param {string} name		Cookie name 
 * @param {integer} count	Number of ads or items
 * @return {integer} 		item number - or zero if no items
 */
function getNext(name,count)
{
	if (!count ) {
		return 0;
	} 
	count = count *1;
	
	var path = location.pathname ;
	var position = getCookie(name) * 1;
	
	if (position < 1) {
		position = random(count);
	}
	else if (position >= count) {
		position = 1;
	}
	else {
		position++;
	}

	setCookie(name, position, path);
	return position;
}




// This only sets session cookies which is fine

	function deleteCookie(name)
	{
		document.cookie = name + "=" + "";
	}
	
	function getCookie(name) 
	{
		var search = name + "=" ;
		if (document.cookie.length > 0) 
		{
			var offset = document.cookie.indexOf(search);
			if (offset != -1) 
			{
				offset += search.length;
				var end = document.cookie.indexOf(";", offset)
				if (end == -1)
					end = document.cookie.length

				return document.cookie.substring(offset,end);
			}
		}
		return null;
	}

	function setCookie(name, value, path)
	{
		var current = getCookie(name);

		if (current == null || current != value) {
			//alert(name + "=" + value);
			document.cookie = name + "=" + value + ( ( path ) ? ";path=" + path : "" );
		}
	}

	
// Custom MiSite Flash caller 
// All srcs already have the .swf extension
function MS_FL_RunContent(){
  var ret = 
    AC_GetArgs
    (  arguments, "", "movie", "clsid:d27cdb6e-ae6d-11cf-96b8-444553540000"
     , "application/x-shockwave-flash"
    );
  AC_Generateobj(ret.objAttrs, ret.params, ret.embedAttrs);
}
