
var headerCounter = 0;
onBodyLoadFunctions.push('replaceHeaders()');

function replaceHeaders()
{
	allHeaders = document.getElementsByTagName('h1');
	while(allHeaders.length > 0)
	{
		replaceElWithHeader(allHeaders[0], 300);
	}
	allHeaders = document.getElementsByTagName('h2');
	while(allHeaders.length > 0)
	{
		replaceElWithHeader(allHeaders[0], 300);
	}
	allHeaders = document.getElementsByTagName('h3');
	while(allHeaders.length > 0)
	{
		replaceElWithHeader(allHeaders[0], 200);
	}
}


function replaceElWithHeader(old, maxWidth)
{
	// grab title
	title = old.innerHTML;

	// create a span which we hide at the bottom of our body
	headerSpan = newEl('span',{innerHTML: title});
	headerSpan.style.fontSize = '13px';
	headerSpan.style.visibility = 'hidden';
	document.body.appendChild(headerSpan);

	// if the headerSpan it to big
	flashHeight = 20;
	if(headerSpan.offsetWidth > maxWidth+10)
	{
		flashHeight = 50;
		// init the loop
		firstLine = "";
		secondLine = "";
		wordNotDone = false;
		c = 1;
		// keep cutting off letters untill our span is smaller then 210px and we're not in the middle of a word
		while((headerSpan.offsetWidth > maxWidth+10 || wordNotDone) && headerSpan.offsetWidth > 0)
		{
			wordNotDone = false;
			secondLine = title.substr(title.length-c, 1) + secondLine;
			firstLine = title.substr(0, title.length-c);
			if(title.substr(title.length-c, 1) != " ") wordNotDone = true;
			headerSpan.innerHTML = firstLine;
			c++;
		}
		if(firstLine == "")
		{
			headerSpan.innerHTML = title;
			firstLine = "";
			secondLine = "";
			c = 1;
			while((headerSpan.offsetWidth > maxWidth+10) && headerSpan.offsetWidth > 0)
			{
				secondLine = title.substr(title.length-c, 1) + secondLine;
				firstLine = title.substr(0, title.length-c);
				headerSpan.innerHTML = firstLine;
				c++;
			}
		}
		delEl(headerSpan);
		// append with a linebreak between
		title = firstLine + "\n" + secondLine;
	}


	// create a div and write the flash into it
	headerDiv = newEl('div',{innerHTML: title, id: 'flashheader_'+headerCounter});
	repEl(old, headerDiv);
	var o_swfObject = swfobject.createSWF({data:"/images/flash/heading.swf", width:maxWidth, height:flashHeight }, {flashvars:"txt="+title,wmode: "transparent",quality: "high"}, 'flashheader_'+headerCounter);
	headerCounter++;
}



