/*
if (window.captureEvents) {
	window.captureEvents(Event.RESIZE);
	if (isOpera) {
		window.onresize = reloadPage;
	} else {
		window.onresize = positionElements;
	}
} else {
	if (isOpera) {
		window.onresize = reloadPage;
	} else {
		window.onresize = positionElements;
	}
}
*/
if (window.captureEvents) {
	window.captureEvents(Event.RESIZE);
}
window.onresize = reloadPage;


function reloadPage(e) {
	if (typeof checkHorizontalScroll != 'undefined') {
		checkHorizontalScroll = clearInterval(checkHorizontalScroll);
	}
	location.reload();
}

var switchSize = 1024;
var minContentWidth = 780;
var prevOuterWidth = -1;
var prevScrollLeft, stableScrollLeft = 0;
var iInterval = 0;
var pollIntervals = new Array(5, 10, 50, 100, 250, 500, 750, 1000);
var nIntervals = pollIntervals.length;

function positionElements() {

	var stakeholderArea = document.getElementById('stakeholderarea');
	var stakeholderAreaChildNode;
	var divs;
	var div;
	var navElt;
	var numNavElts;
	var navEltWidth;
	var images;
	var numImg;
	var navEltImg;
	var segmentDivider
	// var backgroundURL;
	// var backgroundColor;
	// var backgroundColor0 = "#ffffff";
	// var backgroundColor1 = "#d6e0eb";
	// var backgroundColor2 = "#e6eaee";
	// var hasTransparentBackground = false;

	var outerWidth = getOuterWidth();

	var contentWidth = getContentWidth();
	var mainContentWidth = getMainContentWidth();
	var stakeholderAreaWidth = getStakeholderAreaWidth();
	var businessChannelWidth = getBusinessChannelWidth();
	

	if (outerWidth < minContentWidth) {
		document.getElementById('contentbegin').style.width = businessChannelWidth;
		document.getElementById('contentend').style.width = businessChannelWidth;
		if (document.getElementById('businesschannel')){
			document.getElementById('businesschannel').style.width = businessChannelWidth;
		}
	} 
	document.getElementById('contentbegin').style.backgroundPosition = contentWidth + 'px';
	document.getElementById('contentend').style.backgroundPosition = contentWidth + 'px';
	document.getElementById('content').style.backgroundPosition = contentWidth + 'px';
	if (document.getElementById('businesschannellinks')){
		document.getElementById('businesschannellinks').style.width = contentWidth + 'px';
	}
	if (document.getElementById('businesschannellinksenvelope')){
		document.getElementById('businesschannellinksenvelope').style.width = contentWidth + 'px';
	}
	document.getElementById('maincontent').style.width = mainContentWidth + 'px';
	document.getElementById('brandarea').style.width = mainContentWidth + 'px';
	document.getElementById('stakeholderarea').style.width = stakeholderAreaWidth + 'px';
	document.getElementById('header').style.width = contentWidth + 'px';


// alert(contentWidth +":"+mainContentWidth +":"+businessChannelWidth +":"+stakeholderAreaWidth +":"+outerWidth);

	// Emulate "min-width:" property
	if (getInnerWidth() < minContentWidth) {
		document.getElementById('content').style.width = minContentWidth + 'px';
	} else {
		if (typeof document.body.style.overflowX != 'undefined' && !isOpera) {
			document.body.style.overflowX = 'hidden';
		}
	}
	// Flip images in brand area
	var headlines = ["primaryheadlineimage", "secondaryheadlineimage", "leadtextimage"];
	for (var h = 0; h < headlines.length; h++) {
		if (document.getElementById(headlines[h])) {
			switchImages(document.getElementById(headlines[h]), switchSize);
		}
	}
	if (document.getElementById('brandareaimage')) {
		switchDivs(document.getElementById('brandareaimage'), switchSize);
	}

	// Flip image in stakeholder area
	for (var i = 0; i < stakeholderArea.childNodes.length; i++) {
		stakeholderAreaChildNode = stakeholderArea.childNodes[i];
		if (stakeholderAreaChildNode && stakeholderAreaChildNode["tagName"] && stakeholderAreaChildNode.className.toLowerCase().indexOf("visualstakeholdernavsegment") != -1) {
			divs = stakeholderAreaChildNode.getElementsByTagName("div");
			numNavElts = 0;
			var navElts = new Array();
			for (var j = 0; j < divs.length; j++) {
				div = divs[j];
				if (div && div.className.toLowerCase().indexOf('visualnavigationelement') != -1) {
					navElts[numNavElts++] = div;
				}
			}
			navEltWidth = parseInt(stakeholderAreaWidth / numNavElts) - 1;

			// Set the width of the visual stakeholder navigation elements
			for (var j = 0; j < numNavElts; j++) {
				navElt = navElts[j];
				navElt.style.width = navEltWidth + 'px';					// width of element div
				navElt.parentNode.style.width = navEltWidth + 'px';	// width of td containing element
			}

			// Set the width of the visual stakeholder navigation element images
			images = stakeholderAreaChildNode.getElementsByTagName("img");
			numImg = 0;
			for (var k = 0; k < images.length; k++) {
				if (images[k]) {
					if (images[k].className == "visualnavigationimage") {
						navEltImg = images[k];
						navEltImg.style.width = navEltWidth + 'px';
						switchImages(navEltImg, switchSize);
					}
					if (images[k].className == "stakeholderareabottom") {
						segmentDivider = images[k];
						segmentDivider.style.width = eval((navEltWidth * numNavElts) + (numNavElts - 1)) + 'px';
					}
				}
			}
		}
		// Flip images of stakeholder images
		if (stakeholderAreaChildNode && stakeholderAreaChildNode["tagName"] && stakeholderAreaChildNode.className.toLowerCase().indexOf("stakeholderimage") != -1) {
			switchDivs(stakeholderAreaChildNode, switchSize);
		}
	}
	
	/*
	// Reload page if right content part wrapped
	if (UBS_getY(document.getElementById('rightcontentpart')) > UBS_getY(document.getElementById('brandarea'))) {
		if (!document.all) {
			// Try to prevent NS7.1 from looping by expanding viewing window
			if (outerWidth > 787 && outerWidth < 802) {
				window.outerWidth = 802;
			} else {
				window.outerWidth++;
			}
		} else {
			// document.body.clientWidth++;
		}
		// reloadPage();
	}
	*/
	/*
	// Reload page if business channel background corrected
	if (outerWidth < minContentWidth && prevOuterWidth >= minContentWidth) {
		alert('reload: BC');
		setTimeout("reloadPage()", 0);
	}
	prevOuterWidth = outerWidth;
	*/
	/*
	*/
	// Control horizontal scroll bar to prevent IE6 problem.
	// In IE6 unnecessary horizontal scroll bar appears.  Scrolling
	// right causes IE6 to display blank area.
	/*
	if (outerWidth < 795) {
		document.body.style.overflowX = "auto";
		if (typeof document.body.scrollLeft != 'undefined' && !isOpera) {
			prevScrollLeft, stableScrollLeft = 0;
			iInterval = nIntervals - 1;
			checkHorizontalScroll = setInterval("refreshAfterHorizontalScroll()", pollIntervals[iInterval]);
		}
	} else {
		if (typeof checkHorizontalScroll != 'undefined') {
			checkHorizontalScroll = clearInterval(checkHorizontalScroll);
		}
		document.body.style.overflowX = "visible";
	}
	*/
}

function getContentWidth() {
	var outerWidth = getOuterWidth();
	var contentWidth;
	if (outerWidth >= switchSize) {
		contentWidth = 890;
	} else {
		contentWidth = 766;
	}
	return contentWidth;
}

function getMainContentWidth() {
	var outerWidth = getOuterWidth();
	var mainContentWidth;
	if (outerWidth >= switchSize) {
		mainContentWidth = 693;
	} else {
		mainContentWidth = 569;
	}
	return mainContentWidth;
}

function getStakeholderAreaWidth() {
	var outerWidth = getOuterWidth();
	var stakeholderAreaWidth;
	if (outerWidth >= switchSize) {
		stakeholderAreaWidth = 496;
	} else {
		stakeholderAreaWidth = 372;
	}
	return stakeholderAreaWidth;
}

function getBusinessChannelWidth() {
	var outerWidth = getOuterWidth();
	var businessChannelWidth;
	if (outerWidth < minContentWidth) {
		businessChannelWidth = '780px';
	} else {
		businessChannelWidth = '';
	}
	return businessChannelWidth;
}

function refreshAfterHorizontalScroll() {
	var scrollLeft = (document.body.scrollLeft) ? document.body.scrollLeft : 0;
	// alert('polled');
	if (scrollLeft == stableScrollLeft) {
		if (scrollLeft != prevScrollLeft) {
			document.body.scrollLeft = 0;
			// alert('refreshing');
			document.body.scrollLeft = scrollLeft;
			prevScrollLeft = scrollLeft;
			if (iInterval < nIntervals - 1) {
				// Slow down polling
				iInterval++;
				// alert('slowing down polling: '+pollIntervals[iInterval]);
				checkHorizontalScroll = clearInterval(checkHorizontalScroll);
				checkHorizontalScroll = setInterval("refreshAfterHorizontalScroll()", pollIntervals[iInterval]);
			}
		}
	} else {
		if (iInterval > 0) {
			// Speed up polling
			iInterval--;
			// alert('speeding up polling: '+pollIntervals[iInterval]);
			checkHorizontalScroll = clearInterval(checkHorizontalScroll);
			checkHorizontalScroll = setInterval("refreshAfterHorizontalScroll()", pollIntervals[iInterval]);
		}
		stableScrollLeft = scrollLeft;
	}
	
}

function replace(s, t, u) {
	i = s.indexOf(t);
	r = "";
	if (i == -1) return s;
	r += s.substring(0,i) + u;
	if ( i + t.length < s.length)
		r += replace(s.substring(i + t.length, s.length), t, u);
	return r;
}

/*
function getHeight(ele){ 
	var h=0; 
	if(ele.offsetHeight){ 
		h=ele.offsetHeight; 
	} else if(ele.clip && ele.clip.height){ 
		h=ele.clip.height; 
	}else if(ele.style && ele.style.pixelHeight){ 
		h=ele.style.pixelHeight; 
	}
return parseInt(h); 
}
*/

function switchImages(img, switchSize) {
	if (img.src != '') {
		if (getOuterWidth() >= switchSize) {
			if (img.src.indexOf('&imagesize=small') > 0) {
				imageURI = replace(img.src, '&imagesize=small','&imagesize=large');
				img.src = imageURI;
			}
		} else {
			if (img.src.indexOf('&imagesize=large') > 0) {
				imageURI = replace(img.src, '&imagesize=large','&imagesize=small');
				img.src = imageURI;
			}
		}
		return true;
	} else {
		return false;
	}
}

function switchDivs (div, switchSize) {

	var smallDiv, largeDiv;
	var childNodes = div.childNodes;

	for (var i = 0; i < childNodes.length; i++) {
		if (childNodes[i]["tagName"] && childNodes[i].className.toLowerCase().indexOf("smallimage") != -1) {
			smallDiv = childNodes[i];
		}
		if (childNodes[i]["tagName"] && childNodes[i].className.toLowerCase().indexOf("largeimage") != -1) {
			largeDiv = childNodes[i];
		}
		if (smallDiv && largeDiv) {
			break;
		}
	}
	if (smallDiv && largeDiv) {
		if (getOuterWidth() >= switchSize) {
			smallDiv.style.display = "none";
			largeDiv.style.display = "block";
		} else {
			smallDiv.style.display = "block";
			largeDiv.style.display = "none";
		}
		return true;
	} else {
		return false;
	}
}

function getOuterWidth() {
	return (document.all) ? document.body.clientWidth + 28 : window.outerWidth;
}

function getInnerWidth() {
	return (document.all) ? document.body.clientWidth : window.innerWidth;
}
