
function reloadLayout(init) {
	// Reloads the window if Nav4 resized
	if (init==true) with (navigator) {
		if ((appName=="Netscape")&&(parseInt(appVersion)==7)) {
    			document.paramPageW=innerWidth;
    			document.paramPageH=innerHeight;
    			onresize=reloadLayout;
    		}
    	}
  	else if (innerWidth!=document.paramPageW || innerHeight!=document.paramPageH) {
  		history.go(0);
  	}
}

reloadLayout(true);

function docEl(n,d) {
	var p,i,x;
	if (!d) {
		d=document;
	}
	if ((p=n.indexOf("?"))>0&&parent.frames.length) {
		d=parent.frames[n.substring(p+1)].document;
		n=n.substring(0,p);
	}
	if (!(x=d[n])&&d.all) {
		x=d.all[n];
		for (i=0;!x&&i<d.forms.length;i++) {
			x=d.forms[i][n];
		}
	}
	for (i=0;!x&&d.layers&&i<d.layers.length;i++) {
		x=docEl(n,d.layers[i].document);
	}
	if (!x && d.getElementById) {
		x=d.getElementById(n);
	}
	return x;
}

function menuDims() {
	if (document.getElementById) {
		var el = document.getElementById('navmenu');
		if (el) {
			navmenuTop = xTop(el);
			navmenuH = xHeight(el);
		}
	}
}

function vupDims() {
	prevVupH = vupH;
	prevVupW = vupW;
	if (window.innerWidth) {
		vupW= window.innerWidth;
		vupH = window.innerHeight;
	}
	else if (document.body) {
		vupW=document.body.clientWidth;
		vupH=document.body.clientHeight;
		if (document.body.offsetWidth == vupW && document.documentElement && document.documentElement.clientWidth) {
			vupW=document.documentElement.clientWidth;
		}
    		if (document.body.offsetHeight == vupH && document.documentElement && document.documentElement.clientHeight) {
     		vupH=document.documentElement.clientHeight;
     	}
     }
     if (vupW < 600) { vupW = 600; }
     if (vupH < 440) { vupH = 440; }
     viewportWidth = vupW;
     viewportHeight = vupH;
     minVupH = vupH;
}

// element id, left, top, width, height (these are in pixels or the word 'auto')
// then, left, right, top, bottom margins (i.e., space for sidebars and bottom elements)

function stretchEl(id,pL,pT,pW,pH,mL,mR,mT,mB) {
	var el,posType,elX,elY,elW,elH;
	var units='';
	if ((el=docEl(id))==null) {
		return;
	}
	if ((parseInt(navigator.appVersion)>4 || navigator.userAgent.indexOf("MSIE")>-1)&& !window.opera) {
		units="px";
	}
	if (!el.paramStretch) {
		el.paramStretch=true;
		el.paramStretchW=0;
		el.paramStretchH=0;
	}
	if (vupW==el.paramStretchW && vupH==el.paramStretchH) {
		return;
	}
	if (pW=='auto') {
		if (pL=='auto') {
			elW=vupW-mL-mR;
		}
		else {
			elW=vupW-pL-mR;
		}
	}
	else {
		elW=pW;
	}
	if (pH=='auto') {
		if (pT=='auto') {
			elH=vupH-mT-mB;
		}
		else {
			elH=vupH-pT-mB;
		}
	}
	else {
		elH=pH;
	}
	if (pL=='auto') {
		if (pW=='auto') {
			elX=mL;
		}
		else {
			elX=vupW-pW-mR;
		}
	}
	else {
		elX=pL;
	}
	if (pT=='auto') {
		if (pH=='auto') {
			elY=mT;
		}
		else {
			elY=vupH-pH-mB;
		}
	}
	else {
		elY=pT;
	}
	posType=(document.layers)?el:el.style;
	posType.left=elX+units;
	posType.top=elY+units;
	if (document.layers) {
		if (elH != '0') { el.clip.height=elH; } 
		if (elW != '0') { el.clip.width=elW; }
	}
	else if (window.opera) {
		if (elH != '0') { el.style.pixelHeight=elH; }
		if (elW != '0') { el.style.pixelWidth=elW; }
	}
	else {
		if (elW != '0') { posType.width=elW+units; }
		if (elH != '0') { posType.height=elH+units; }
	}
	el.paramStretchW=vupW;
	el.paramStretchH=vupH;
}

function setDefaults() {
	bannerH = showBannerH;
	contentT = (6 + bannerH + 8);
	bottomH = 20;
	sidebarW = 0;
	if (bannerState == '0') {
		bannerH = 0;
		contentT = (9 + 6 - 1);
		bottomH = 6;
	}
	if (sidebarState == '1') {
		sidebarW = 200;
	}
}

function stretch() {
	clearTimeout(layoutTimer);
	stretchEl('backdrop','6','6','auto',vupH-12,0,scrollerW,0,0);
	stretchEl('pageTop','0','0','auto',20,0,scrollerW,0,0);
	stretchEl('pageLeft','0','20','6','auto',0,0,0,20);
	stretchEl('pageBanner','6','6','auto',bannerH,0,scrollerW,0,0);
	stretchEl('pageRight','auto','20','6','auto',0,scrollerW,0,20);
	stretchEl('pageBottom','0','auto','auto','20',0,scrollerW,0,0);
	stretchEl('content','220',contentT,'auto','auto',0,(sidebarW+6+scrollerW),0,bottomH);
	setContainerW('content');
	if (sidebarState == '1') {
		stretchEl('sidebar','auto',contentT,sidebarW,'auto',0,(6+scrollerW),0,bottomH);
	}
	if (bannerState == '1') {
		stretchEl('sliders','6',(bannerH + 6),'auto',9,0,(6+scrollerW),0,0);
	}
	else {
		stretchEl('sliders','6','6','auto',9,0,(6+scrollerW),0,0);
	}
	layoutTimer = setTimeout('saveLayout();', 1000);
}

function clearDimParams(i) {
	if (document.getElementById) {
		var el = document.getElementById(i);
		if (el) {
			if (el.paramStretch) {
				el.paramStretchW=-99;
				el.paramStretchH=-99;
			}
		}
	}
}

function setContainerW(c) {
	if (document.getElementById) {
		var elOuter = document.getElementById(c);
		var elInner = document.getElementById(c + 'Container');
		if (elOuter && elInner) {
			var outerW = xWidth(elOuter);
			var innerW = outerW - 30;
			if (innerW > 0) {
				xWidth(elInner, innerW);
			}
		}
	}
}

function resetStretch() {
	clearTimeout(resizTimer);
	clearTimeout(layoutTimer);
	menuDims();
	vupDims();
	fitStretch();
}

function resizWait() {
	clearTimeout(resizTimer);
	resizTimer = setTimeout('resizeNomo();', 500);
}

function resizeNomo() {
	clearTimeout(resizTimer);
	resetStretch();
	resizTimer = setTimeout('resetStretch();', 50);
}

function setBanner() {
	if (bannerState == '1') {
		bannerState = '0';
		if (sidebarState =='0') {
			enableAltCSS('noBanner2');
		}
		else {
			enableAltCSS('noBanner');
		}
		if (document.getElementById) {
			var el = document.getElementById('setBanner');
			if (el) {
				el.className = 'showBanner';
			}
		}
		setDefaults();
		fitStretch();
	}
	else {
		bannerState = '1';
		saveLayout();
		bannerState = '0';
		if (document.getElementById) {
			var el = document.getElementById('setBanner');
			if (el) {
				el.className = 'hideBanner';
				refresh();
			}
		}
	}
}

function setSidebar() {
	if (sidebarState == '1') {
		sidebarState = '0';
		if (document.getElementById) {
			var el = document.getElementById('sidebar');
			if (el) {
				el.style.display = 'none';
			}
			var el = document.getElementById('setSidebar');
			if (el) {
				el.className = 'showSidebar';
			}
		}
		if (bannerState == '0') {
			enableAltCSS('noBanner2');
		}
		else {
			enableAltCSS('noSidebar');
		}
	}
	else {
		sidebarState = '1';
		if (document.getElementById) {
			var el = document.getElementById('sidebar');
			if (el) {
				el.style.display = 'block';
			}
			var el = document.getElementById('setSidebar');
			if (el) {
				el.className = 'hideSidebar';
			}
		}
		disableAltCSS('noSidebar');
		disableAltCSS('noBanner2');
		if (bannerState == '0') {
			enableAltCSS('noBanner');
		}
		else {
			disableAltCSS('noBanner');
		}
	}
	setDefaults();
	resetStretch();
}

function clearStretch() {
	clearDimParams('backdrop');
	clearDimParams('pageTop');
	clearDimParams('pageLeft');
	clearDimParams('pageBanner');
	clearDimParams('pageRight');
	clearDimParams('pageBottom');
	clearDimParams('content');
	clearDimParams('sidebar');
	clearDimParams('sliders');
}

function fitStretch() {
	clearTimeout(layoutTimer);
	var glideWait = 0;
	clearStretch();
	navmenuH = menuH + menuOtherH;
	var testH = navmenuTop + navmenuH + bottomH;
	//alert('testH = '+testH + '\n\nnavmenuH = '+navmenuH+'\nnavmenuTop = '+navmenuTop+'\nbottomH = '+bottomH+'\nmenuH='+menuH+'\nmenuOtherH='+menuOtherH+'\n\nvupH='+vupH+'\nprevVupH='+prevVupH+'\nminVupH='+minVupH);
	prevVupH = vupH;
	if ((testH >= vupH) || (testH >= minVupH)) {
		vupH = testH;
		stretch();
		glideWait = 100;
	}
	else {
		vupH = minVupH;
		stretch();
	}
	return glideWait;
}

function enableAltCSS(title) {
   var i, a, main;
   for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
     if(a.getAttribute("rel").indexOf("style") != -1
        && a.getAttribute("title")) {
       a.disabled = true;
       if(a.getAttribute("title") == title) a.disabled = false;
     }
   }
}

function disableAltCSS(title) {
   var i, a, main;
   for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
     if(a.getAttribute("rel").indexOf("style") != -1
        && a.getAttribute("title")) {
       a.disabled = true;
       if(a.getAttribute("title") == title) a.disabled = true;
     }
   }
}

function initStretch() {
	initLayout=false;
	getLayout();
	if (document.body) {
		document.body.className = 'fontsize' + fontSizeIndex;
	}
	fontSizerStyles();
	if (document.getElementById) {
		var el = document.getElementById('pageBanner');
		if (el) {
			showBannerH = xHeight(el);
			if (showBannerH == 0) {
				el.style.display = 'block';
				showBannerH = xHeight(el);
				if (bannerState == '0') {
					el.style.display = 'none';
				}
			}
		}
	}
	if (!navigator.userAgent.indexOf("MSIE")>-1) {
		if (scrollerW == 0) {
			scrollerW = getScrollerWidth();
		}
	}
	setDefaults();
	menuDims();
	vupDims();
	var testH = navmenuTop + navmenuH + bottomH;
	if ((testH >= vupH) || (testH >= minVupH)) {
		prevVupH = vupH;
		vupH = testH;
	}
	stretch();
	initLayout=true;
	hideWorking();
}

addLoadEvent(initStretch);
addResizeEvent(resizWait);


