/*
	$Id:$

	Description: Manipulating M2/M3Design ads layout
				Requires whatbrowser.js
*/

/* Javascript for _m2_pageheader.cfm 
* (C) Copyright Trinity Mirror 2005-2007
*/
// The above notice to be retained after compression

/*
DOM helpers
*/
function findPosY(obj) 
{	var curtop = 0;
	if (obj.offsetParent) 
	{	curtop = obj.offsetTop;
		while (obj = obj.offsetParent) 
		{	curtop += obj.offsetTop;
		}								
	}
	return curtop;
}
function hasBigDescendant(el)
{	var pxHeightReqd = 100;
	if (el.offsetHeight && el.offsetHeight >= pxHeightReqd) 
	{	return true;
	}
	if (el.childNodes.length)
	{	for (var i=0;i<el.childNodes.length;i++)
		{	if (hasBigDescendant(el.childNodes[i])) {return true;}
		}
	} 
	return false;
}

/*
Fix ad display.  Should be run post content load as much as possible
*/
function displayTopAds()
{	/* 	MPU ad div can have up to three different IDs
	*/
	setTimeout('removeEmptyMPU(\'mpuad\')',1500); //wait for content to arrive
	setTimeout('removeEmptyMPU(\'mpu1\')',1500); 
	setTimeout('removeEmptyMPU(\'mpu2\')',1500); 
	correctRightSkyPosition();
	removeEmptyEspottingDiv();
}

/*
Move the right sky */
function correctRightSkyPosition()
{ 	var rightAdPlacement = document.getElementById("rightAdPlacement");
	var rightad = document.getElementById("rightad");
	var rightAdOffset = null;

	//move right ad level with sky ad
	if (rightAdPlacement) {rightAdOffset = findPosY(rightAdPlacement);}
	else return;

	if (rightAdOffset || rightAdOffset==0) 
	{	if (browser == 'ie') {rightad.style.top = rightAdOffset;}
		else if (browser == 'Safari') {rightad.style.top = (rightAdOffset + 2) + "px" ;}
		else {rightad.style.top = rightAdOffset + "px";}
	}

}

/*
Might not serve any links
*/
function removeEmptyEspottingDiv()
{ 	var espottinglinks = document.getElementById('espottinglinks');
	if (espottinglinks && espottinglinks.innerHTML.toLowerCase().indexOf("href=") != -1)
	{ 	//served a link
		espottinglinks.style.display = 'block';
	}
}

function removeEmptyMPU(divId)
{	//check browser supported
	if (typeof document.getElementById == 'undefined' || (OS && browser && OS=='Mac' && browser=='ie')) {return;}
	var mpuad = document.getElementById(divId);
	if (!mpuad) {return;}

	//show now, so that offsetHeight works
	mpuad.style.display = 'block';	
	
	// Rule: a header element in the MPU, if any, should have the id of the mpu
	// plus suffix 'header'
	var mpuadheader = document.getElementById(divId+'header');
	//hide the header anyway if it's a FALK ad
	if  (mpuadheader && mpuad.innerHTML.indexOf("falkag") != -1)
	{ 	mpuadheader.style.display = 'none';	
	}

	/*
	Look for a descendant of mpuad with an offsetHeight of at least 200px.
	If none found, hide the ad.
	*/
	if (hasBigDescendant(mpuad) )
	{	//continue to show the mpu
		if  (mpuadheader)
		{ 	mpuadheader.style.display = 'block';	
		}
		/*
		IE/win only: fix M3 layout
		Classname 'ad-mpu' is taken as M3 design style
		*/
		if (browser && browser=='ie' && mpuad.className.match(/ad-mpu/))
		{	IECorrectM3MPU(mpuad);	
		}
	} else 
	{ 	//hide the mpu
		mpuad.style.display = 'none';
		if  (mpuadheader)
		{ 	mpuadheader.style.display = 'none';	
		}
	}
}

/*
IE/win only: fix M3 layout by moving paragraphs (using DOM) until the
paragraph before the mpu is less than 40px distant from its predecessor
paragraph.

Degrade gracefully (ie. hack around edge cases) by processing only up to 8
paragraphs.
*/
function IECorrectM3MPU(mpuad)
{	var hasCorrectLayout=false;
	var siblingsMoved=0;
	function isParagraphNode(node)
	{	return (el_nextPara!=null && (node.nodeName.toUpperCase() == 'P' || node.nodeName.toUpperCase() == 'H3' ))
	}
	do 
	{	var el_nextPara = mpuad.nextSibling;
		if (!isParagraphNode(el_nextPara))
		{	//abort, MPU is at bottom of article
			break;
		} 
		//look for the gap with previous para
		var el_previousPara = mpuad.previousSibling;
		if (!isParagraphNode(el_previousPara))
		{	//abort, MPU is at top of article (??)
			break;
		}
		//check distance between bottom of previouos para, and top of next
		hasCorrectLayout = findPosY(el_nextPara) - (findPosY(el_previousPara) + el_previousPara.offsetHeight) < 40;
		if (!hasCorrectLayout)
		{	//reparent the next paragraph, so it becomes el_previousPara on next iteration
			mpuad.parentNode.insertBefore(el_nextPara,mpuad);
			siblingsMoved++;
		}
	} while (!hasCorrectLayout && siblingsMoved < 9)
}

