FOOTER_DEBUG = (window.location.search.indexOf('footer_check=1') > -1) ? true : false;
var minPageHeight = 0;
var iBBUKContentH = 0;
var iFooterOffset = 0;
var iPreSyndicationFooterOffset = 0;

var anchorList = new Object();

function findAnchors() {
	var tags = document.getElementsByTagName("div");
	for (var j = 0; j < tags.length; j++) {
		var thisObj = tags[j];
		if (thisObj != null && thisObj.id) {
			// our naming convention for tagging anchors is to append 'ATag' to the div id.
			var tagName = new String (thisObj.id + "ATag");
			var tagElement = new elcElement(tagName);
			if (tagElement && tagElement.obj) {
				anchorList[tagName] = thisObj.id;
			}
		}
	}
}

function getAnchorMaxY() {
	var maxY = 0;
	for (var name in anchorList) {
		 // getAnchorPosition(obj) will return {0,0} if anchor not found (or in synd. with some browsers prior to complete page load)
		 // getAnchorPosition is now returning the tuple {x,y,t,l,r,b}
		 // getAnchorPosition was modified to look a anchors inside the content div
		 // IE (5.2) on a MAC does not report the bottom (returns 0)
		anchorCoordinates = getAnchorPosition(name);
                if (FOOTER_DEBUG) {
                    alert( name + " " + anchorCoordinates.t + " " + anchorCoordinates.b );
                }
                // why were we adding offset top and offset bottom?
		//var derivedBottom = anchorCoordinates.t + anchorCoordinates.b;

                var derivedBottom = (anchorCoordinates.t > anchorCoordinates.b) ? anchorCoordinates.t : anchorCoordinates.b;
		if (derivedBottom > maxY) {
			maxY = derivedBottom;
		}
	}
	return maxY;
}

// needed for MAC IE browsers
function getLayerMaxH() {
	var maxH = 0;
	for (var name in anchorList) {
		var layerName = anchorList[name];
		var thisLayer = new elcLayer( layerName );
		if ((thisLayer.y + thisLayer.h) > maxH) {
			maxH = thisLayer.y + thisLayer.h;
		}
	}
	return maxH;
}

function setFooterY() {
	findAnchors();
	var maxAnchorY = getAnchorMaxY();
	var maxY = maxAnchorY;
	var maxH = getLayerMaxH();

	// get height of the content div
	oBBContentObj = new elcLayer("BBContent");
	iBBUKContentH = (oBBContentObj && oBBContentObj.h) ? oBBContentObj.h : 0;

	// for MAC IE5 workaround
	contentMaxH = (oBBContentObj && oBBContentObj.y) ? maxH - oBBContentObj.y : maxH;

	if (bw.ie && bw.win) {
		iFooterOffset = ((maxY - iBBUKContentH) > 0) ? (maxY - iBBUKContentH) : 0;
	} else if (bw.ie && bw.mac) {
		//iFooterOffset = (contentMaxH > maxY) ? contentMaxH : (maxY) ? maxY : 0;
		var macIEMaxH = (contentMaxH > maxY) ? contentMaxH : maxY;
		iFooterOffset = ((macIEMaxH - iBBUKContentH) > 0) ? (macIEMaxH - iBBUKContentH) : 0;
	} else {
		//iFooterOffset = (maxY) ? maxY : 0;
		var macIEMaxH = (contentMaxH > maxY) ? contentMaxH : maxY;
		iFooterOffset = (macIEMaxH) ? macIEMaxH : 0;
	}
        if (FOOTER_DEBUG) {
	    alert( 'maxH: ' + maxH + '\tmaxY: ' + maxY + "\tiBBUKContent: " + iBBUKContentH + "\tiFooterOffset: " + iFooterOffset + "\tcontentMaxH: " + contentMaxH);
        }

	if (iFooterOffset > 0) {
		document.write('<table border="0" cellspacing="0" cellpadding="0"><tr><td><img id="pgContentHeight" src="/images/1x1.gif" height="' + iFooterOffset + '" width="1" /></td></tr></table>');
	}
	iPreSyndicationFooterOffset = iFooterOffset;
}

function moveFooter() {
	findAnchors();
	var maxAnchorY = getAnchorMaxY();
	var maxY = maxAnchorY;
	// note: MAC IE 5 workaround does not appear to be necessary in syndication

	oBBContentObj = new elcLayer("BBContent");
	oBBSyndContentFooterObj = new elcLayer("BBSyndicationContentFooter");
	iBBUKContentH = (oBBContentObj && oBBContentObj.h) ? oBBContentObj.h : 0;

	if (bw.ie && bw.win) {
		iFooterOffset = ((maxY - iBBUKContentH) > 0) ? (maxY - iBBUKContentH) : 0;
	} else {
		iFooterOffset = (maxY) ? maxY : 0;
	}

	// if we've already injected content ...
	if (iFooterOffset < iPreSyndicationFooterOffset) {
		iFooterOffset = 0;
	} else {
		iFooterOffset -= iPreSyndicationFooterOffset;
	}
	//alert( 'maxY: ' + maxY + "\tiBBUKContent: " + iBBUKContentH + "\tiFooterOffset: " + iFooterOffset );

	var spacerImage = wsmlMakeResourceUrl( '/images/1x1.gif' );

	// just for ie browsers
	if (iFooterOffset > 0) {
		var sBBContentPadding = '<table border="0" cellspacing="0" cellpadding="0"><tr><td><img src="' + spacerImage + '"" height="' + iFooterOffset + '"></td></tr></table>';
		oBBSyndContentFooterObj.writeThis( sBBContentPadding );
	}
}

function reportAnchors() {
	for (var name in anchorList) {
		coordinates = getAnchorPosition(name);
		alert( name + ' x:' + coordinates.x + ' y: ' + coordinates.y + "\nt: " + coordinates.t + "\tl: " + coordinates.l + "\tr: " + coordinates.r + "\tb: " + coordinates.b );
	}
}

function reportLayerStats() {
	var tags = document.getElementsByTagName("div");
	for (var j = 0; j < tags.length; j++) {
		var thisObj = tags[j];
		if (thisObj != null && thisObj.id) {
			var layer = new elcLayer( thisObj.id );
			alert( thisObj.id + "\tx: " + layer.x + "\ty: " + layer.y + "\th: " + layer.h + "\tw: " + layer.w );
		}
	}
}
function reportThisLayerStats(id) {
	var layer = new elcLayer( id );
	alert( id + "\tx: " + layer.x + "\ty: " + layer.y + "\th: " + layer.h + "\tw: " + layer.w );
}
