window.onload = Load;
window.onresize = Resize;

function Load() {
	is.afterLoad(1000,600);
	defineLayerObjs();
	registerEvents();
	MainContent();
	setupLeftButtons();
	checkLeftSide();
}

function Resize() {
	Load();
	Load();
}

function defineLayerObjs() {
	// dObj = new LayerObj('id');
	dContent = new LayerObj('divContent');
	dHeader = new LayerObj('divHeader');
	iHeader = new LayerObj('imgHeader');
	tHeader = new LayerObj('txtHeader');
	dFooter = new LayerObj('divFooter');
	iFooter = new LayerObj('imgFooter');
	dSlogan = new LayerObj('txtFooterSlogan');
	dMenuLeft = new LayerObj('divMenuLeft');
	dMain = new LayerObj('divMain');
	defineOtherLayers();
}

function registerEvents() {
	// dObj.obj.onclick = clickfunction;
	registerOtherEvents();
}

function MainContent() {
	// Main layout code sequence
	dContent.setSize(is.clientWidth, is.clientHeight);
	dHeader.setWidth(dContent.width);
	iHeader.setWidth(dContent.width);
	tHeader.setWidth(dContent.width);
	dFooter.setWidth(dContent.width);
	dFooter.setBottom(0);
	iFooter.setWidth(dFooter.width);
	dSlogan.setWidth(dFooter.width);
	dMenuLeft.setHeight(dContent.height-dMenuLeft.top-iFooter.height-15);
	dMain.setSize(dContent.width-dMain.left,dContent.height-dMain.top-iFooter.height);
	otherMainContent();
}

function menuOverL(e) {
	var evtID = getEventObj(e).id;
	switch(evtID.substr(0,5)) {
	case 'menuL':
		var src = 'images/general/menu_over_l.png';
		break;
	}
	getObj(evtID.substr(0,7)).src = src;
}

function menuOutL(e) {
	var evtID = getEventObj(e).id;
	switch(evtID.substr(0,5)) {
	case 'menuL':
		var src = 'images/general/menu_norm_l.png';
		break;
	}
	getObj(evtID.substr(0,7)).src = src;
}

function getElementsByStyleClass (className) {
  var all = document.all ? document.all :
	document.getElementsByTagName('*');
  var elements = new Array();
  for (var e = 0; e < all.length; e++)
	if (all[e].className == className)
	  elements[elements.length] = all[e];
  return elements;
}


function processLeftClick(){
	if (this.getAttribute("url") == null){
		if (this.getAttribute("submit") == null){
			alert('This button currently has no action.');	
		} else { // end if submit and URL are both null
			var formName = this.getAttribute("submit");
			//alert('attempting to submit to formName: '+formName);
			var formHandler = document.getElementById(formName);
			formHandler.submit();
		}
	} else { // end if url is null
		if (this.getAttribute("newWindow") == null){
		  document.location = this.getAttribute("url");
		} else {
		  window.open(this.getAttribute("url"),"Progressive");	

		}
	} // end if url has a value
} // end function processLeftClick

function setupLeftButtons(){
	var buttonArray = getElementsByStyleClass('leftButton');
	for (var x=0; x < buttonArray.length; x++){
		var theURL = buttonArray[x].getAttribute("url");
		if (theURL == null){
			if (buttonArray[x].getAttribute("submit") == null){
			  alert(buttonArray[x].id + ' has no url or submit attribute. ');	
			} else {
				buttonArray[x].onclick = processLeftClick;	
			}
		} else {
		  //alert('Setting up button: '+buttonArray[x].id);
		  buttonArray[x].onclick = processLeftClick;
		}
	} // end for
}  // end function setupButtons


function findPosX(obj)
  {
    var curleft = 0;
    if(obj.offsetParent)
        while(1) 
        {
          curleft += obj.offsetLeft;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.x)
        curleft += obj.x;
    return curleft;
  }

  function findPosY(obj)
  {
    var curtop = 0;
    if(obj.offsetParent)
        while(1)
        {
          curtop += obj.offsetTop;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.y)
        curtop += obj.y;
    return curtop;
  }


function checkLeftSide(){
	if (document.getElementById('divSearchBox') != null){
		searchBoxObj = document.getElementById('divSearchBox');
		var posY = findPosY(searchBoxObj);
		var boxHeight = document.getElementById('divSearchBox').offsetHeight;
		var boxBottom = posY+boxHeight;
		//alert('bottom of search box: ' + boxBottom);
		var contentBottom = dMain.height + 80; // 80 is the height of the header, that's what I was forgetting
		//alert('bottom of main content: ' + (dMain.height + 80));
		
		if (boxBottom > contentBottom ){
			searchBoxObj.style.display = 'none';	
			searchBoxObj.style.visibility = 'hidden';
		} else {
			//alert('resetting');
			searchBoxObj.style.display = 'block';
			searchBoxObj.style.visibility = 'visible';
		}
		
		var dml = document.getElementById('divMenuLeft');
		dmlTop = findPosY(dml);
		dmlLeft = findPosX(dml);
		dmlHeight = dml.offsetHeight;
		dmlNewHeight = dMain.height - dmlTop;
		dml.style.height = dmlNewHeight + 'px';
	} // end if there is a quickSearchBox at all
}

function bE() { return true; } 

