// Get window dims before everything loads
//FastInit.addOnLoad(alertSize, initTimelineRollovers);
FastInit.addOnLoad(alertSize);

// When browser is resized center layout
window.onresize=function(){
	alertSize();
}
window.onload=function(){
	if (document.getElementById("jumpToContent")) {
		prepJumpToContentLinks();
	}
	
	// activate the retailNearYou onclick to expose the state UL
	/*if (document.getElementById("retailNearYou")) {
		document.getElementById("retailNearYou").onclick = function(){
			return toggleStates(this);
		};
	}*/
	 
	/* when user hovers over the "Find a retailer neer you" button on the homedesign page, need to toggle the sub menu */
	var toggleSpeed = 350;	
	 $("#retailNearYou").parent().hover(
      function () {
       $("#retailNearYou").siblings().slideDown(toggleSpeed);
      }, 
      function () {
       $("#retailNearYou").siblings().slideUp(toggleSpeed);
      }
    );
	
	
}

function toggleStates(pEl) {
 	var states = document.getElementById("states");
 	if (states.style.display == "none") {
 		states.style.display = "block";
 	} else {
 		states.style.display = "none";
 	}
 	// don't follow the retailNearYou url
	return false;
}

function alertSize() {
	var myWidth = 0, myHeight = 0;
	var isHome = false;
	if (document.getElementById("homePage")) {
		isHome = true;
	}
	
	
	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	}
	
	// on all pages, the container needs to be centered vertically
	if(myHeight > 630) {
		document.getElementById('container').style.top = ((myHeight/2)-315)+'px'; 
	} else {
		document.getElementById('container').style.top = '0px';
	}
	
	// on the home page. the container is 960 and needs to be centered horizontally
	if (isHome) {
		if(myWidth > 960) {
			document.getElementById('container').style.left = ((myWidth/2)-480)+'px';
		} else {
			document.getElementById('container').style.left = '0px';
		}
	} 
		
	// on subpages the container is 100% the browser width, the header alone needs to be centered horizontally (it is 960px wide)
	else {	
		if(myWidth > 960) {
			document.getElementById('header').style.left = ((myWidth/2)-480)+'px';
			
		} else {
			document.getElementById('header').style.left = '0px';
		}	
	}
}

function prepJumpToContentLinks() {
	if( document.getElementById && document.getElementsByTagName ){	
		var aLinks = document.getElementById("jumpToContent").getElementsByTagName("A");		
		for( var i=0; i < aLinks.length; i++ ){							
			aLinks[i].onclick = function(){
			return toggleJumpToContentLinks(this);
			};				
		}
	}	
}

function toggleJumpToContentLinks(pEl) {
	if( document.getElementById && document.getElementsByTagName ){	
		var aLinks = document.getElementById("jumpToContent").getElementsByTagName("A");		
		for( var i=0; i < aLinks.length; i++ ){							
			if (aLinks[i] == pEl) {
				aLinks[i].className+=" on";
			} else {
				aLinks[i].className=aLinks[i].className.replace(new RegExp(" on\\b"), "");
			}						
		}
	}	
}

function getChildByTag(pNode, theTagName) {
    var theChildren = pNode.childNodes;    
    for (i=0; i<theChildren.length; i++) {        
        if (theChildren[i].tagName == theTagName) {    
            return theChildren[i];
        }
    }
    return false;
}

function initTimelineRollovers() {
	var timeline = document.getElementById("timeline");
	if (timeline) {
		var td_array = timeline.getElementsByTagName("TD");
		for (var i = 0; i < td_array.length; i++) {
			td_array[i].onmouseover = function () {
				var theCaption = getChildByTag(this, "DIV");
				if (theCaption && theCaption.className == "caption") theCaption.style.display = "block";
			}
			td_array[i].onmouseout = function () {
				var theCaption = getChildByTag(this, "DIV");
				if (theCaption && theCaption.className == "caption") theCaption.style.display = "none";
			}
		}
	}
}

