/********************************************************************************

	build html from corporateNavArray.js

********************************************************************************/

var theRealTier1 = location.pathname.split("/")[1].toLowerCase();
var theRealTier2 = ''
if ( location.pathname.split("/").length > 2 ){theRealTier2 = location.pathname.split("/")[2].toLowerCase();}
var currentTier1Nav = false

function Nav( href, title, tier, parent ){
	this.href = href.replace( /nav/gi, "" )		//cut off 'nav' from path
	this.thingName = href.replace( /\//gi, "XX" ).replace( /\.asp/g , "")		// replace slashes with XX and remove file extensions
	this.title = title;
	this.tier = tier;
	this.imgName = href.replace( /\//g, "") //remove slashes
	this.kids = new Array();
	this.hasKids = false
	this.parent = parent

	this.isGood = ( this.tier < 2 || this.href.split("/")[1].toLowerCase() == theRealTier1 )
}
Nav.prototype.add = function( path, title, tier, parent ){
	this.kids[ path ] = new Nav( this.href + '/' + path , title, tier, this );
	this.hasKids = true
}


Nav.prototype.htmlNav = function(){
	var where = '_off'
	var color = ''
	var html = ''
	
	switch( this.tier ){

		case 0: html += '<div class=tier' + this.tier + 'Nav id=' + this.thingName + 'Div>&nbsp;</div>';  break;		
		
		case 1:
			if ( theRealTier1 == this.href.split('/')[1] ) {where = '_at';  currentTier1Nav = this;}
			html += '<div class=tier' + this.tier + 'Nav id=' + this.thingName + 'Div><a href=' + this.href + ' onmouseover=mouseover(' +  '\'' + this.thingName + 'Img' + '\'' + ') onmouseout=mouseout(' +  '\'' + this.thingName + 'Img' + '\'' + ')><img src=/images/nav/nav'+ this.imgName + where + '.gif alt=' + this.title + ' border=0 name=' + this.thingName + 'Img></a></div>\n'; break;		
//needed if dynamically placing tier1

		//case 2: 
		//	if ( theRealTier2 == this.href.split('/')[2] ) {color = 'At';} 
		//	if ( ! this.isGood ) return ''
		//	html += '<div class=tier' + this.tier + 'Nav id=' + this.thingName + 'Div  onClick=document.location=' + '\'' + this.href + '\'' + ';><table cellpadding=0 cellspacing=0 border=0 bgcolor=#a2be78><tr><td nowrap>&nbsp;<img src=/bp.gif width=1 height=5 border=0>&nbsp;<a href=' + this.href + ' class=navlinkT2' + color + '>' + this.title.toUpperCase() + '</a></td></tr></table></div>'; break;

		case 3: html += '<div class=tier' + this.tier + 'Nav id=' + this.thingName + 'Div onClick=document.location=' + '\'' + this.href + '\'' + ';><table cellpadding=0 cellspacing=0 width=131 border=0><tr><td width=1><img src=/bp.gif border=0></td><td width=101><img src=/bp.gif width=101 height=1 border=0></td><td width=13><img src=/bp.gif width=13 height=1 border=0></td><td width=16><img src=/bp.gif width=16 height=1 border=0></td></tr><tr><td bgcolor=000000><img src=/p.gif border=0></td><td align=right><a href=' + this.href + ' class=navlink>' + this.title + '</a></td><td></td><td bgcolor=B05E0C></td></tr></table></div>'; break;
			
		case 4: html += '<div class=tier' + this.tier + 'Nav id=' + this.thingName + 'Div onClick=document.location=' + '\'' + this.href + '\'' + ';><table cellpadding=0 cellspacing=0 width=130 border=0><tr><td width=8><img src=/p.gif width=8 height=1 border=0></td><td width=122><a href=' + this.href + ' class=navlink>' + this.title + '</a></td></tr></table></div>'; break;
	
	}
	
	for( var i in this.kids ){html += this.kids[i].htmlNav()} //recurse downwards through tree
	
	return html
}

Nav.prototype.setPos = function(x,y){
	
	if( ! this.isGood ) return new Point(0,0); //if not in the current tier1 path stop
	this.thing = Thing.all[this.thingName]
	
	this.thing.moveTo(x,y);
	switch( this.tier ){
		case 0: x=0; break;
		case 1: x=0; y += 18; break; //fixed height of tier1 images is 18px
		case 2: y += this.thing.size.y + 20; break; // 20 sets the offset of tier 3 from tier 2
		case 3: x += 145; break; //131 is the width of tier 3 ( this.thing.size.x ) + 14 is the offset of tier 4 = 145
		case 4: x += 0; break; //at this point, nothing is positioned after t4, this line is not used
	}
	//go through kids
		for( var i in this.kids ){
			if( this.kids[i].isGood ){
				var size = this.kids[i].setPos(x,y);
				switch( this.tier ){
					//case 0: x += size.x; break; //not needed when tier1 is statically placed
					case 1: x += size.x; break;
					case 2: y += size.y; break;
					case 3: y += size.y; break;
					case 4: y += 0; break; // at this point, t4 never has kids, this line is not used
				}
			}
		}
	
	return this.thing.size; //recursive call
}
Nav.prototype.toString = function(){
	return this.thingName
}

function getNavBg(){
	var isIE4 = false;
	if( document.all ){if (!document.getElementsByTagName) isIE4 = true;}
	// early versions of ie don't support clipping, so don't show bg screen image
	
	if (isIE4){ html = '<div id=tier3NavBgDiv><table cellpadding=0 cellspacing=0 width=139 border=0><tr><td width=8><img src=/images/pixel.gif width=8 height=1></td><td width=1><img src=/images/blackpixel.gif width=1 height=10 border=0></td><td width=122><img src=/images/pixel.gif width=122 height=1 border=0></td><td width=8><img src=/images/pixel.gif width=8 height=1></td></tr><tr><td><img src=/images/pixel.gif width=8 height=1></td><td><img src=/images/blackpixel.gif width=1 height=10 border=0></td><td bgcolor=#CC9933><img src=/images/pixel.gif width=122 height=1 border=0></td><td bgcolor=#CC9933><img src=/images/pixel.gif width=8 height=1 border=0></td></tr></table></div><div id=tier4NavBgDiv>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</div>'
	}
	else { html = '<div id=tier3NavBgDiv><table cellpadding=0 cellspacing=0 width=139 border=0><tr><td width=8><img src=/images/pixel.gif width=8 height=1></td><td width=1><img src=/images/blackpixel.gif width=1 height=10 border=0></td><td width=122><img src=/images/pixel.gif width=122 height=1 border=0></td><td width=8><img src=/images/pixel.gif width=8 height=1></td></tr><tr><td><img src=/images/pixel.gif width=8 height=1></td><td><img src=/images/blackpixel.gif width=1 height=10 border=0></td><td bgcolor=#CC9933><img src=/images/pixel.gif width=122 height=1 border=0></td><td bgcolor=#CC9933><img src=/images/pixel.gif width=8 height=1 border=0></td></tr><tr><td colspan=3 background=/images/nav/navPattern.gif><img src=/images/pixel.gif width=131 height=300></td><td width=8><img src=/images/pixel.gif width=8 height=1></td></tr></table></div><div id=tier4NavBgDiv><table cellpadding=0 cellspacing=0 width=135 border=0><tr><td width=5><img src=/images/pixel.gif width=5 height=10></td><td width=130><img src=/images/pixel.gif width=130 height=10></td></tr><tr><td width=5><img src=/images/pixel.gif width=5 height=200></td><td width=130 background=/images/nav/navPattern.gif><img src=/images/pixel.gif width=130 height=300></td></tr></table></div>'
	}
	return html
}

/********************************************************************************

	mouse events

********************************************************************************/

var nav = new Nav( "nav", "", 0, "" )
var currentNav = nav;

Nav.prototype.getCurrNav = function(p){
	// if hit, return self
	if( this.thing.isHit(p) ){
		return this;
	}
	for( var i in this.kids){
		if( this.kids[i].thing.isHit(p) ){
			return this.kids[i];
		}
	}
	if ( this.parent ){
		return this.parent.getCurrNav(p); // recurse upwards
	}
	// at this point, we know no nav is hit.
	if ( tier3NavBg.isHit(p) || tier4NavBg.isHit(p) ){
		return currentNav;
	}
	if ( this.tier == 0 ){
		return currentTier1Nav;
	}
	
	return currentNav; //this is nav if none of the above tests are true
}

Thing.mouseMove = function(p){ //called whenever mouse is in motion
	if(currentTier1Nav){ // don't do anything if on home page
		var newNav = currentNav.getCurrNav(p);
		if( newNav == null ) newNav = currentTier1Nav
		if(newNav){ if( newNav.tier == 1 ){ newNav = currentTier1Nav;} }
		if( newNav == currentNav || newNav == null ){ //if you hit one of the static divs or nothing new  
			newNav = currentNav //reset it to the current
			return PASS_EVENT;
		}
		if( newNav.tier <= currentNav.tier ){ //going up a tier or staying on the same tier
			currentNav.hideKids();
			if( newNav.parent != currentNav.parent ){ //going into a parent of a sibling
				currentNav.parent.hideKids(); //hide self
				if( newNav.tier <= 2 ){ //when coming out of tier 3 into tier 1 or 2
					tier3NavBg.hide();
					tier4NavBg.hide();
				} 
			}
		}
		
		if( newNav.tier == 1 ){ //when out of bounds
			newNav.hideAll()
			currentNav = currentTier1Nav 
			return KILL_EVENT;
		}
		currentNav = newNav;
		currentNav.showKids();
		return (currentNav) ? KILL_EVENT : PASS_EVENT;
	}
}

Nav.prototype.showKids = function(){

	if( ! this.isGood ) return null;
	var flipName = this.thing.name + 'Img'
	var clipHeight = 8 // 8 is the lower offest of tier3NavBg and tier 4NavBg
	
	for ( var i in this.kids ){
		if( this.kids[i].isGood ){
			this.kids[i].thing.show();
			clipHeight += this.kids[i].thing.size.y 
		}
	}
	
	switch( this.tier ){
		case 1:
			if( flip[flipName] ) flip[flipName].on();
			break;
			
		case 2:
			if( this.hasKids ){
				t3x = this.thing.position.x - 8  // 8 is the left offest of tier3NavBg and tier 4NavBg
				t3y = this.thing.size.y + this.thing.position.y
				tier3NavBg.moveTo( t3x , t3y  );
				if (!isIE4) tier3NavBg.clipTo( 0, 139, clipHeight + 20, 0 );
				tier3NavBg.show();
			}
			else{
				tier3NavBg.hide();
			}
			break;
			
		case 3:
			this.thing.setBackground("#B05E0C");
			if(this.hasKids){
				greenArrow.moveTo( (this.thing.position.x + 120) , (this.thing.position.y + 5))
				greenArrow.show();
				t4x = (this.thing.position.x) + 131
				t4y = this.thing.position.y
				tier4NavBg.moveTo( t4x , t4y  );
				if (!isIE4) tier4NavBg.clipTo( 0, 135, clipHeight, 0 );
				tier4NavBg.show();
			}
			else{
				tier4NavBg.hide();
			}
			break;

		case 4:
			this.thing.setBackground("#B05E0C"); 
			tier4NavBg.show(); 
			break;
	}
}

Nav.prototype.hideKids = function(){
	if( this.tier > 1 ){
		this.thing.setBackground("#CC9933")
		for( var i in this.kids ){
			this.kids[i].thing.hide();
		if ( greenArrow ){ greenArrow.hide(); }
		}
	}
}

Nav.prototype.hideParents = function(){
	if (this.parent){
		greenArrow.hide();
		if ( this.parent.tier >= 1 ){ this.parent.hideKids(); this.parent.hideParents(); } // hide kids, then recurse upwards
	}
}

Nav.prototype.hideAll = function(){
	//tier2NavBg.hide();
	tier3NavBg.hide();
	tier4NavBg.hide();
	currentNav.hideParents();
	currentNav.hideKids();
	
}
/********************************************************************************

	clickstream and page heads

********************************************************************************/
//var tier1Flip
var titleStr = 'Weyerhaeuser'

if(!tier2Title){ var tier2Title = ""}
if(!tier3Title){var tier3Title = ""}

var path = location.pathname  //.replace("default.asp", '').replace(".asp","")
function clickstream( pos ){
	var suffix = ""
	if (pos == 'footer')suffix = pos
	var html = '<a target="_parent" href="http://www.weyer.com/roots/onlinemap.htm" class="linkstream' + suffix + '">Home</a> &gt;'
	var n = nav;
	var folders = path.split("/")
	for( i=1; i< folders.length; i++ ){
		if( n.kids[ folders[i].toLowerCase() ] ){
			n = n.kids[ folders[i].toLowerCase() ]
			html += '  <a href="' + n.href + '" class="linkstream'+ suffix +'">' + n.title +'</a> &gt;'
			if (i == 2 && n.title != null && tier2Title == "" )tier2Title = n.title //set global title variables according to current tier in loop
			if (i == 3 && n.title != null && tier3Title == "" )tier3Title = n.title
			titleStr += ' > ' + n.title //makes the last folder/filename the title
		}
		else{
			html = html.slice(0 , -4)	//remove last '&gt;' from string
			return html
		}
	}
	html = html.slice(0 , -4)	//remove last '&gt;' from string
	return html;
}