var minPageHeight = 0;
var iMacContentH = 0;
var iContentY = 0;
var iBracerY = 0;
var iFooterOffset = 0;
var minFooterPaddingY = 350; // 442px - 92px 
	
/* most CMS-pages will have these tags */
var anchorList           = new Object();
anchorList["maclnav"]    = "maclnav";
anchorList["macrnav"]    = "macrnav";
anchorList["maccontent"] = "maccontent";

var bracerNameArr = new Array("maclnav", "macrnav");
var bracerObjArr  = new Array();

function addBracerObj(sLayerName) {
  bracerObjArr[bracerObjArr.length] = new elcLayer(sLayerName);
}

function getContentMaxY() {
  oMacContentObj = new elcLayer("maccontent");
  iMacContentH = (oMacContentObj && oMacContentObj.h)? oMacContentObj.h : iMacContentH;
  for (var i=0; i<bracerNameArr.length; i++) {
    addBracerObj(bracerNameArr[i]);
    if (bracerObjArr[i] && bracerObjArr[i].h && (bracerObjArr[i].h > iContentY)) {
       iContentY = bracerObjArr[i].h;
    }
  }
  return iContentY;
}

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 = iBracerY;
  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)
    anchorCoordinates = getAnchorPosition(name);
    if (anchorCoordinates.y > maxY) {
      maxY = anchorCoordinates.y;
    }
  }
  return maxY;
}

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() {
  // return false;
  findAnchors();
  var maxAnchorY  = getAnchorMaxY();
  var maxContentY = getContentMaxY();
  var maxY        = (maxAnchorY > maxContentY) ? maxContentY : maxAnchorY;
  iFooterOffset   = ((maxY - iMacContentH) > 0) ? (maxY - iMacContentH) : 0;
  // alert(maxAnchorY+":"+macContentY+":"+maxY+":"+iFooterOffset);
  // alert(maxAnchorY);
  // iFooterOffset = (iFooterOffset > minFooterPaddingY) ? iFooterOffset : minFooterPaddingY;

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

}

/* in syndication some browsers do not return the correct anchor coordinates or layer heights prior to the window onload call */
function setFooterYSyndicated() {
/* return false; */
	findAnchors();
	var maxAnchorY = getAnchorMaxY();
	var maxContentY = getContentMaxY();
	var maxH = getLayerMaxH();
	var maxY = (maxAnchorY > maxContentY) ? maxAnchorY : maxContentY;
	iFooterOffset = ((maxY - iMacContentH) > 0) ? (maxY - iMacContentH) : 0;

	// attempt to move the module footer in syndication did not work well
	// in part because of the unreliable anchor and layer stats available
	// prior to complete loading of the module

	/* This was the attempt ...

	// synd ie 6 and possibly others
	// force a minimum height for the footer so that most page loads
	// have a footer initially near the bottom of the module
	if ((maxY > 0) && (maxY <= minPageHeight)) {
		iFooterOffset = (minPageHeight - maxY + 1);
		isUsingMinPageHeight = true;
	}

	if (iFooterOffset > 0) {
		document.write('<table border="0" cellspacing="0" cellpadding="0"><tr><td><img src="/images/1x1.gif" height="' + iFooterOffset + '"></td></tr></table>');
		isSyndicationFooterOK = (isUsingMinPageHeight) ? false : true;
	}

	// subsequent logic in moveFooter looks at the isSyndicationFooterOK var

	*/
}

/* called by a syndicator-supplied window onload handler */
/* in general anchorCoords are not available until this handler is invoked */
function moveFooter() {
	var footerLayer = new elcLayer( "macftr" );
	var moduleWrapperLayer = new elcLayer( "ModuleWrapper" );

	findAnchors();
	var maxAnchorY = getAnchorMaxY();
	var maxContentY = getContentMaxY();
	var maxH = getLayerMaxH();
	var maxY = (maxAnchorY > maxContentY) ? maxAnchorY : maxContentY;

	var relativeMaxContentY = (footerLayer.css && (footerLayer.y > maxContentY)) ? footerLayer.y : maxContentY;
	iFooterOffset = ((maxY - relativeMaxContentY) > 0) ? (maxY - relativeMaxContentY) : 0;

	if (footerLayer != null && footerLayer.css) {
		if (iFooterOffset) {
			footerLayer.moveBy(0, iFooterOffset);
			maxY += iFooterOffset;
		}
		footerLayer.show();
	}

	var candidateSpacerDivHeight = (moduleWrapperLayer.css) ? maxY - (moduleWrapperLayer.y + moduleWrapperLayer.h) : maxY;
	var spacerDivHeight = (candidateSpacerDivHeight < 0) ? iFooterOffset : candidateSpacerDivHeight;
	document.getElementById('spacerDiv').innerHTML = "<div style='height:" + spacerDivHeight + "px;'><img src='" + wsmlMakeResourceUrl('/images/1x1.gif') + "' border='0' alt='' width='1' height='" + spacerDivHeight + "' /></div>";

}	

function reportAnchors() {
	for (var name in anchorList) {
		coordinates = getAnchorPosition(name);
		alert( name + ' x:' + coordinates.x + ' y: ' + coordinates.y );
	}
}

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 );
}
