// JavaScript Document
function Browser() {

  var ua, s, i;

  this.isIE    = false;  // Internet Explorer
  this.isOP    = false;  // Opera
  this.isNS    = false;  // Netscape
  this.isFF    = false;  //FireFox
  this.version = null;

  ua = navigator.userAgent;

  s = "Opera";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isOP = true;
    this.version = parseFloat(ua.substr(i + s.length));
    return;
  }

  s = "Netscape6/";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isNS = true;
    this.version = parseFloat(ua.substr(i + s.length));
    return;
  }

  // Treat any other "Gecko" browser as Netscape 6.1.

  s = "Gecko";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isNS = true;
    this.version = 6.1;
    return;
  }

  s = "MSIE";
  if ((i = ua.indexOf(s))) {
    this.isIE = true;
    this.version = parseFloat(ua.substr(i + s.length));
    return;
  }
  
  s = "FireFox"
  if((i = ua.indexOf(s)) >= 0){
	  this.isFF = true;
	  this.version = parseFloat(ua.substr(i + s.length));
	  return;
  }
}

var browser = new Browser();

function adjustLayout()
{
  // Get natural heights
 // var cHeight = xHeight("contentcontent");
  var lcolHeight = xHeight("left_column");
  var rcolHeight = xHeight("right_2column");
  var lHeight = xHeight("leftCol_text_wrapper");
  var rHeight = xHeight("right_2content");
  var lfHeight = xHeight("leftCol_footer");
  //get the leftCol_text_wrapper element and leftCol_footer
  var leftColEl = xGetElementById("left_column");
  var leftTopEl = xGetElementById("leftCol_text_wrapper");
  var leftTextEl = xGetElementById("leftCol_text");
  var leftFooterEl = xGetElementById("leftCol_footer");
  var footerWrapEl = xGetElementById("footer_wrapper");
  //now get the position of each element
  var leftColPos = findPos(leftColEl);
  var leftTextPos = findPos(leftTopEl);
  var leftFootPos = findPos(leftFooterEl);
  var footerPos = findPos(footerWrapEl);
  //set the marAdj value to 0
  var marAdj = 0;
  //if this is not IE See if there is a bottom margin on the leftCol_text element
  if(!browser.isIE){
	//get the integer value of the bottom margin on the text and text wrapper
	var textMarginBottom = parseInt(getStyle(leftTextEl,"margin-bottom").slice(0,-2));
	var tWrapperMarginBottom = parseInt(getStyle(leftTopEl,"margin-bottom").slice(0,-2));
	//alert("text bottom: "+textMarginBottom+"\nwrapper bottom: "+tWrapperMarginBottom);
	marAdj += textMarginBottom + tWrapperMarginBottom;
  }
  
  // Find out if the right column is taller than the left
  if(rcolHeight > lcolHeight){ 
    var maxHeight = rcolHeight;
    // Assign maximum height to the left column
    xHeight("left_column", maxHeight);
	//calculate the amount needed to push the image to the bottom of the left column
	var newTop = (footerPos[1] - lfHeight) - ((leftTextPos[1] - leftColPos[1]) + lHeight + leftColPos[1] + marAdj);
	
	//alert("left height: "+lHeight+"\nleft footer height:"+lfHeight+"\nnewTop:"+newTop+"\nright col height: "+rcolHeight+"\nnew padding: "+newPadding+"\nfooter total: "+(newTop+lfHeight));
	
	//set the relative top position of the footer image to the calculate newTop value
	xStyle("top",newTop+"px","leftCol_footer");
	
  }else{
	  xHeight("right_2column",xHeight("left_column"));
  }

  // Show the footer
  xStyle("visibility","visible","leftCol_footer");
}

/*
*http://www.quirksmode.org/js/findpos.html#
*
* I use this to get the page positions for the 
* left column, left footer and page footer.
*
*/
function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		do {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		} while (obj = obj.offsetParent);
	}
	return [curleft,curtop];
}

/*
* http://www.robertnyman.com/2006/04/24/get-the-rendered-style-of-an-element/
* This function will get the computed styles for page elements. I use it to 
* get the margin-bottom for the left column text and wrapper because it isn't 
* included in the element height for FF,Saf and OP.
*/
function getStyle(oElm, strCssRule){
	var strValue = "";
	if(document.defaultView && document.defaultView.getComputedStyle){
		strValue = document.defaultView.getComputedStyle(oElm, "").getPropertyValue(strCssRule);
	}
	else if(oElm.currentStyle){
		strCssRule = strCssRule.replace(/\-(\w)/g, function (strMatch, p1){
			return p1.toUpperCase();
		});
		strValue = oElm.currentStyle[strCssRule];
	}
	return strValue;
}