// margins.js

// defaults below;  since this is called on resize, we really 
// don't want to recompute these values every time
var minimum_width = "640px"; // ie doesn't like const?!
var min_copy_width = "390px"; 
var margin_percent = 1.55;
var last_margin = margin_percent;
var last_padding = null;  // for ie 5

function adjustMargins(){

	// only for ie5 and ns6 up
	if(document.getElementById){
	
		var brows = document.body.offsetWidth;  // ie
		if ((brows == 0) && (window.innerWidth)) { // netscape
			brows = window.innerWidth;
		}
		margin_percent =  (brows/16) - 41;

		if (margin_percent < 0) {
			margin_percent = 0;	
		}
		else if (margin_percent > 10) {
			margin_percent = 10;	
		}
		//alert ("browser size is " + brows + ";  margin_percent = " + margin_percent);
		
		// adjust margin and content sizes
		if (margin_percent != last_margin) {
			
			// home page
			if (l = document.getElementById('homecontent')){
				l.style.marginRight = margin_percent + "%";
				l.style.marginLeft = margin_percent + "%";
			}
			
			// main content pages
			if (l = document.getElementById('maincontent')){
				l.style.marginLeft = margin_percent + "%";
				l.style.marginRight = margin_percent + "%";
				
				// we have to make extra adjustments to preserve minimum width
				// and keep layers from overlapping;  for very small windows 
				// when there is a sidebar or sideimage
				if (document.getElementById('mainsidebar') ||
					document.getElementById('mainimageleft') ||  
					document.getElementById('mainimageright') ) {
					
					if (margin_percent == 0) {	
						l.style.width = min_copy_width;
						last_padding = l.style.paddingRight;
						l.style.paddingRight = "0";
					}
					else { 
						l.style.width = "auto"; 
						l.style.paddingRight = last_padding;
					}
				
				}
				//alert ("maincontent final marginRight="  +l.style.marginRight + ", marginLeft="  +l.style.marginLeft +  ", width=" + l.style.width);
			}

			// main content page sidebar
			if (l = document.getElementById('mainsidebar')){
				l.style.right = margin_percent + "%";
				// stop the sidebar from moving all the way left in a small window
				if (margin_percent == 0) {
					l.style.left = min_copy_width;
				}
				else l.style.left = "auto";
			}
			
			// main content page side image
			if (l = document.getElementById('mainimageright')){
				l.style.right = margin_percent + "%";
				// stop the image from moving all the way left in a small window
				if (margin_percent == 0) {
					l.style.left = min_copy_width;
				}
				else l.style.left = "auto";
			}
			if (l = document.getElementById('mainimageleft')){
					l.style.left = margin_percent + "%";
			}

			
			// fullheader (entire top nav bar)
			if (l = document.getElementById('fullheader')){
				var content_percent = 100-2*margin_percent;

				l.style.marginRight = margin_percent + "%";
				l.style.marginLeft = margin_percent  + "%";
				l.style.width = content_percent + "%";  // content percent
				// stop the nav bar from wrapping on itself in a small window
				if (margin_percent == 0) {
					l.style.width = minimum_width;
				}
				//else { document.getElementById('fullheader').style.width = "auto"; }
				//alert ("fullheader final marginRight="  +l.style.marginRight + ", marginLeft="  +l.style.marginLeft +  ", width=" + l.style.width);
			}
			last_margin = margin_percent;					
		}
		
		
	}
	document.body.style.display = "block";
}
