var ADDARROWSFLAG = true; // set to false to turn arrow off
// FUNCTION: getIEVersion - grab IE version number
var getIEVersion = function () {
	var ua = navigator.userAgent;
	var MSIEOffset = ua.indexOf("MSIE ");
	if (MSIEOffset == -1) {
		return 0;	
	} else {
		return parseFloat(ua.substring(MSIEOffset + 5, ua.indexOf(";", MSIEOffset)));
	}
};
// setup global IE Test Vars
var isIE = getIEVersion() >= 4;
var isIE6Min = getIEVersion() <= 6;
// FUNCTION: ieManualAlign - controls alignment of navigation in IE
var ieManualAlign = function () {
	if (isIE) {
		var setLoc = document.getElementById('UniNav_Container');
		// test for doctype or IE6
		if ((null === document.body.parentNode.parentNode.firstChild.nodeValue) || (isIE6Min)) {
			// set IE6 specific css
			if (isIE6Min) {
				var ieSixCSS = document.createElement('link');
				setLoc.appendChild(ieSixCSS);
				ieSixCSS.setAttribute('rel','stylesheet');
				ieSixCSS.setAttribute('href','http://www.colgate.com/UnifiedNav/Common/v4/styles/UNv4_master_IE6.css');
			}
			viewportWidth = document.body.clientWidth;
			navItemSet = document.getElementById('UniNav_Brand');
			navFullWidth = navItemSet.offsetWidth;
			navItemWidth = navItemSet.offsetWidth / 2;
			leftPos = (viewportWidth / 2) / 2;
			navHalved = navItemWidth / 2;
			// check viewport width | adjust to ensure first parent item is always viewable
			if (navFullWidth > viewportWidth) {
				setLeftPosition = 0;
			} else if (navItemWidth >= leftPos) {
				screenOrientationDiff = navItemWidth - leftPos;
				setLeftPosition = leftPos - screenOrientationDiff + 'px';
			}
			// set background width
			var navBackground = document.getElementById('UniNav_Background');
			navBackground.style.width = viewportWidth + 'px';
			setLoc.style.left = setLeftPosition;
			setLoc.style.width = 'auto';
		} else {
			setLoc.style.width = '100%';	
		}
	} // END isIE
};
// FUNCTION: UNB_Handle - controls Mouse Over / Out Events
var UNB_Handle = function (sectionParent, sectionChild, childWidth, parentWidth) {
	// set initial arrow states
	if (ADDARROWSFLAG) {
		if (isIE && isIE6Min) {
			arrowIcon = '<div class="UniNav_arrowIcon" style="visibility: hidden;"><!-- image --></div>';
		} else {
			arrowIcon = '<div class="UniNav_arrowIcon" id="OFF"><!-- image --></div>';
		}
		var theNode = '';
		// if IE
		if (navigator.appName == "Microsoft Internet Explorer") {
			h1Node = 0;
			anchorNode = 0;
		} else {
			h1Node = 1;
			anchorNode = 0;
		}
		sectionH1 = sectionParent.childNodes[h1Node].childNodes[anchorNode];
		h1Content = sectionH1.innerHTML;
		sectionH1.innerHTML = arrowIcon + h1Content;
	}
	// set hover/click states
	if (screen.width <= 480) {
		// ONCLICK
		sectionParent.onclick = function() {
			if (sectionChild.style.display != 'block') {
				sectionChild.style.display = 'block';
			} else {
				sectionChild.style.display = 'none';	
			}
		}
	} else {
		// OVER
		sectionParent.onmouseover = function() {
			sectionChild.style.display = 'block';
			if (ADDARROWSFLAG) {
				if (isIE && isIE6Min) {
					// do not change states
				} else {
					this.childNodes[h1Node].childNodes[0].childNodes[0].setAttribute('id','ON');
					sectionChild.onmouseover = function() {
						this.parentNode.childNodes[h1Node].childNodes[0].childNodes[0].setAttribute('id','ON');				
					}
				}
			}
		}
		// OUT
		sectionParent.onmouseout = function() {
			sectionChild.style.display = 'none';
			if (ADDARROWSFLAG) {
				if (isIE && isIE6Min) {
					// do not change states
				} else {
					this.childNodes[h1Node].childNodes[0].childNodes[0].setAttribute('id','OFF');
					sectionChild.onmouseout = function() {
						this.parentNode.childNodes[h1Node].childNodes[0].childNodes[0].setAttribute('id','OFF');
					}
				}
			}
		}
	}
}
// FUNCTION: UNB_Controller
var UNB_Controller = function () {
	// test for UniNav_Container before continuing | step through if no exist
	if (!document.getElementById('UniNav_Container')) {
		return;
	}
	var setLoc = document.getElementById('UniNav_Container');
	var setBG = document.getElementById('UniNav_Background');
	var setEndCap = document.getElementById('uninavendcap');
	var UNB_Div = document.getElementById('UniNav_Brand');
	var initHeight = UNB_Div.offsetHeight;
	var masterLink = UNB_Div.getElementsByTagName('h1');
	// set UNB_FLAG | step through if exist
	if (!setLoc.getAttribute('name')) {
		setLoc.setAttribute('name','UNB_FLAG');
	} else {
		return;
	}
	// preload arrow images | test if they exist to avoid running twice
	var onArrow, offArrow;
	if ((!onArrow) && (!offArrow)) {
		onArrow = new Image();
		onArrow.src = 'http://www.colgate.com/UnifiedNav/Common/v5/UniNav_Brand_OnArrow.gif';
		offArrow = new Image();
		offArrow.src = 'http://www.colgate.com/UnifiedNav/Common/v5/UniNav_Brand_OffArrow.gif';
	}
	// Background/Nav Height and nodeSet
	nodeSet = 3;
	setBG.style.height = initHeight + 'px';
	setLoc.style.height = initHeight + 'px';
	// IE specific
	if (isIE) { 
		ieManualAlign();
		// Background/Nav Height and node set
		nodeSet = 1;
		if (null === document.body.parentNode.parentNode.firstChild.nodeValue) {
			setBG.style.height = initHeight + 6 + 'px';
			setLoc.style.height = initHeight + 6 + 'px';
		} else {
			setBG.style.height = initHeight + 'px';
			setLoc.style.height = initHeight + 'px';
		}
		setEndCap.style.paddingBottom = initHeight + 8 + 'px';
		window.onresize = ieManualAlign;
	}
	// loop through all h1(top level) links
	for (i = 0; i < masterLink.length; i++) {
		sectionParent = masterLink[i].parentNode;
		// set Height of Links
		masterLink[i].style.height = initHeight + 'px';
		var parentCount = masterLink.length - 1;
		// turn off first parent left border
		var firstItem = masterLink[0].parentNode;
		firstItem.style.borderLeft = 'none';
		firstItem.setAttribute('id','leftRemove');
		// turn off last parent right border
		var lastItem = masterLink[parentCount].parentNode;
		lastItem.style.borderRight = 'none';
		lastItem.setAttribute('id','rightRemove');
		// setup children item elements
		if (sectionParent.getElementsByTagName('div').length > 0) {
			sectionChild = sectionParent.getElementsByTagName('div');
			sectionChild = sectionChild[0];
			// turn on first child item left border if it exists
			var leftRemove = document.getElementById('leftRemove');
			if (leftRemove.childNodes[nodeSet]) {
				var leftControl = leftRemove.childNodes[nodeSet];
				leftControl.style.borderLeft = '1px solid #CCC';
				leftControl.style.marginRight = '-1px';
				if (isIE) {
					leftControl.style.position = 'relative';
					leftControl.style.left = '1px';
				}
			}
			// turn on last child item right border if it exists
			var rightRemove = document.getElementById('rightRemove');
			if (rightRemove.childNodes[nodeSet]) {
				var rightControl = rightRemove.childNodes[nodeSet];
				rightControl.style.borderRight= '1px solid #CCC';
				rightControl.style.marginLeft = '0';
				if (isIE) {
					rightControl.style.position = 'relative';
					rightControl.style.left = '1px';
				}
			}
			// set children width to parent width
			var parentWidth = sectionParent.clientWidth;
			var childWidth = sectionChild.style.width;
			if (ADDARROWSFLAG) {
				sectionChild.style.width = parentWidth + 6 + 'px';
				var h2Set = sectionChild.getElementsByTagName('a');
				for (h = 0; h < h2Set.length; h++) {
					h2Set[h].style.paddingLeft = '14px';
					if (isIE) {
						h2Set[h].style.paddingLeft = '17px';
						sectionChild.style.width = parentWidth + 7 + 'px';
					}
				}
			} else {
				sectionChild.style.width = parentWidth + 'px';
			}
			// run UNB_Handle
			UNB_Handle(sectionParent, sectionChild, childWidth, parentWidth);
		} // END children element setup
	} // END h1 loop
};
// onload control
var winReady = function() {
	// Initialize UNB_Controller - if Opera wait 1 second
	if (/Opera[\/\s](\d+\.\d+)/.test(navigator.userAgent)){
		setTimeout ( "UNB_Controller()", 1000 );
	} else {
		UNB_Controller();
	}
};
window.onload = winReady;
