function initNav() {
	var menuItems = document.getElementById("navItems").getElementsByTagName("a");
	for (x = 0; x < menuItems.length; x++) {
		var dd = new Dropdown(menuItems[x]);
		Dropdown.drops[menuItems[x].id] = dd;
	}
	YAHOO.util.Event.on("print", "click", printPage);
	YAHOO.util.Event.on("email", "click", emailPage);
	
	initTextboxes();
	new LoginDrop();
	
	new LoginForm("drop");
	if (document.getElementById("homeLogin")) {
		// on home page
		new LoginForm("home");
	}
	if (document.getElementById("rightLogin")) {
		// has right login 
		new LoginForm("right");
	}
	
	// setup checking of width of browser
	YAHOO.util.Event.on(window, "resize", checkWidth);
}

function Dropdown(link) {
	this.init(link);
}
Dropdown.drops = {};
Dropdown.current = null;

Dropdown.prototype = {
	init: function(link) {
		this.id = link.id;
		this.menuItem = link;
		this.overCount = 0;
		this.isShowing = false;
		this.down = null;
		this.up = null;
		this.selected = YAHOO.util.Dom.hasClass(link, "selected");
	
		var droplist = document.getElementById(this.id.replace(/_nav/,"_drop"));
		if (droplist) {
			this.droplist = droplist;
			this.innerlist = droplist.getElementsByTagName("ul")[0];
			this.dropMax = this.innerlist.offsetHeight;
			this.dropframe = document.getElementById(this.droplist.id + "Frame");
		}
		YAHOO.util.Event.addListener([this.id, this.droplist, this.innerlist], "mouseover", this.incrOver, this, true);
		YAHOO.util.Event.addListener([this.id, this.droplist, this.innerlist], "mouseout", this.decrOver, this, true);
	},
	incrOver: function(e) {
		this.overCount++;
		if (this.isShowing) {
			// already showing
			return;
		}
		if (this.down == null) {
			this.down = setTimeout("showDropdown('" + this.id + "')", 75);
		}
	},
	decrOver: function(e) {
		this.overCount--;
		if (this.overCount <= 0) {
			this.overCount = 0;
			if (this.up != null) {
				clearTimeout(this.up);
			}
			this.up = setTimeout("hideDropdown('" + this.id + "')", 300);
		}
	},
	hide: function() {
		if (!this.isShowing) {
			return;
		}
		// only hide the droplist if there is one
		if (this.droplist) {
			if (this.dropframe) {
				var frameDrop = new YAHOO.util.Anim(this.dropframe, {height: {from: this.dropMax - 12, to: 0} }, .2, YAHOO.util.Easing.easeOut);
				frameDrop.onComplete.subscribe(this.hideDropFrame, this, true);
				frameDrop.animate();
			}
			var drop = new YAHOO.util.Anim(this.droplist, {height: {from: this.dropMax, to: 0} }, .2, YAHOO.util.Easing.easeOut);
			drop.animate();
		}
		// only fade out orange if not the current selected section
		if (!this.selected) {
			var fade = new YAHOO.util.Anim(this.menuItem, {opacity: {from: 1, to: 0} }, .2);
			fade.animate();
		}
		this.isShowing = false;
	},
	show: function() {
		// only fade in orange if not the current selected section
		if (!this.selected) {
			var fade = new YAHOO.util.Anim(this.menuItem, {opacity: {from: 0, to: 1} }, .25, YAHOO.util.Easing.easeIn);
			fade.animate();
		}
		// only drop the droplist if there is one
		if (this.droplist) {
			var drop = new YAHOO.util.Anim(this.droplist, {height: {from: 0, to: this.dropMax} }, .2, YAHOO.util.Easing.easeOut);
			drop.animate();
			if (this.dropframe) {
				this.dropframe.style.display = "block";
				var frameDrop = new YAHOO.util.Anim(this.dropframe, {height: {from: 0, to: this.dropMax - 12} }, .2, YAHOO.util.Easing.easeOut);
				frameDrop.animate();
			}
		}
		this.isShowing = true;
	},
	hideDropFrame: function() {
		this.dropframe.style.display = "none";
	}
};

function showDropdown(ddId) {
	var dd = Dropdown.drops[ddId];
	dd.down = null;
	if (dd.overCount <= 0 || ddId == Dropdown.current) {
		// already showing, not over the item anymore
		return;
	}
	for (id in Dropdown.drops) {
		if (id != ddId) {
			Dropdown.drops[id].hide();
		}
	}
	dd.show();
	Dropdown.current = dd.id;
}
function hideDropdown(ddId) {
	var dd = Dropdown.drops[ddId];
	dd.up = null;
	if (dd.isShowing && dd.overCount <= 0) {
		dd.hide();
	}
	if (Dropdown.current == ddId) {
		Dropdown.current = null;
	}
}

/**
 * sets up the text boxes to be friendly (clear out text and replace as needed).  Doesn't
 * work perfectly in IE 6 (any surprise there?) but works good enough for IE 6.
 **/
function initTextboxes() {
	var inputBoxes = document.getElementsByTagName("input");
	for (x=0; x < inputBoxes.length; x++) {
		var type = inputBoxes[x].getAttribute("type");
		if (type == "text" || type == "password") {
			if (inputBoxes[x].getAttribute("defaultType") == "password" && inputBoxes[x].getAttribute("defaultVal")) {
				// there is a default value for this password box, change it to be of type text so the default value
				// is readable
				try {
					inputBoxes[x].setAttribute("type", "text");
				} catch (ex) {
					// do nothing, this is just an IE 6/7 problem
				}
				inputBoxes[x].value = inputBoxes[x].getAttribute("defaultVal");
			}
			YAHOO.util.Event.on(inputBoxes[x], "focus", clearTextBox);
			YAHOO.util.Event.on(inputBoxes[x], "blur", resetTextBox);
		}
	}
}
/**
 * clears out the text box with nothing in it if it has a defaultVal attribute and it is the
 * same as the value in the text box currently.
 **/
function clearTextBox(e) {
	if (this.getAttribute("defaultVal") == this.value) {
		this.value = "";
	}
	try {
		if (this.getAttribute("defaultType") == "password" && this.type != "password") {
			this.type = "password";
		}
	} catch (ex) {
		// do nothing this is an IE 6/7 problem
	}
}
/**
 * resets the text box to the default value if the current value is empty
 **/
function resetTextBox(e) {
	if (this.value.replace(/^ */, "").replace(/ *$/, "") == "" && this.getAttribute("defaultVal")) {
		this.value = this.getAttribute("defaultVal");
		try {
			if (this.type == "password" && this.getAttribute("defaultType") == "password") {
				// let's reset it to a plain text box with the default value in it
				this.type = "text";
			}
		} catch (ex) {
			// do nothing this is an IE 6/7 problem
		}
	}
}

function LoginDrop() {
	this.init();
}

LoginDrop.login = null;
LoginDrop.prototype = {
	init: function() {
		this.container = document.getElementById("loginDrop");
		this.background = document.getElementById("loginDropBg");
		this.isShowing = false;
		this.isAnimating = false;
		this.dropMax = 110;
		this.form = document.getElementById("dropLoginForm");
		this.user = document.getElementById("dropUser");
		this.password = document.getElementById("dropPassword");
		this.button = document.getElementById("dropSubmit").getElementsByTagName("a")[0];

		YAHOO.util.Event.on(document.getElementById("custLoginLink"), "click", this.toggle, this, true);
		
		YAHOO.util.Event.on(this.container, "mouseover", this.incrOver, this, true);
		YAHOO.util.Event.on(this.container, "mouseout", this.decrOver, this, true);
		
		var formElements = ["dropCustType", "dropUser", "dropPassword"];
		YAHOO.util.Event.on(formElements, "focus", this.incrOver, this, true);
		YAHOO.util.Event.on(formElements, "blur", this.decrOver, this, true);
		
		LoginDrop.login = this;
	},
	incrOver: function(e) {
		this.overCount++;
		if (this.delayedHide != null) {
			clearTimeout(this.delayedHide);
		}
		this.delayedHide = setTimeout("LoginDrop.login.longtermHide()", 15000);
	},
	decrOver: function(e) {
		this.overCount--;
		if (this.overCount <= 0) {
			this.overCount = 0;
			if (this.up != null) {
				clearTimeout(this.up);
			}
			this.up = setTimeout("LoginDrop.login.attemptHide()", 3000);
		}
		if (this.delayedHide != null) {
			clearTimeout(this.delayedHide);
		}
		this.delayedHide = setTimeout("LoginDrop.login.longtermHide()", 15000);
	},
	/**
	 * this shows the login box if it isn't showing.  Only checks whether the
	 * isShowing flag is false first.
	 **/
	show: function() {
		if (!this.isShowing) {
			this.isAnimating = true;
			this.container.style.display = "block";
			this.background.style.display = "block";
			var fade = new YAHOO.util.Anim(this.container, {opacity: {from: 0, to: 1} }, .30, YAHOO.util.Easing.easeIn);
			var drop = new YAHOO.util.Anim(this.background, {height: {from: 0, to: this.dropMax} }, .2, YAHOO.util.Easing.easeOut);
			fade.animate();
			drop.animate();
			fade.onComplete.subscribe(this.animateComplete, this, true);
			document.getElementById("dropCustType").focus();
			this.up = setTimeout("LoginDrop.login.attemptHide()", 5000);
			this.isShowing = true;
		}
	},
	/**
	 * this hides the login box if it is showing.  No safety checks, other
	 * than whether the isShowing flag is true.
	 **/
	hide: function() {
		if (this.isShowing) {
			this.isAnimating = true;
			this.overCount = 0;
			var fade = new YAHOO.util.Anim(this.container, {opacity: {from: 1, to: 0} }, .2);
			var drop = new YAHOO.util.Anim(this.background, {height: {from: this.dropMax, to: 0} }, .2, YAHOO.util.Easing.easeOut);
			fade.onComplete.subscribe(this.hideComplete, this, true);
			fade.onComplete.subscribe(this.animateComplete, this, true);
			fade.animate();
			drop.animate();
			this.isShowing = false;
		}
	},
	hideComplete: function() {
		this.container.style.display = "none";
		this.background.style.display = "none";
	},
	animateComplete: function() {
		this.isAnimating = false;
	},
	/**
	 * shows/hides the login box depending on its current state.  This also forces
	 * it to hide regardless of whether there is an overCount > 0.
	 **/
	toggle: function(e) {
		YAHOO.util.Event.stopEvent(e);
		if (!this.isAnimating) {
			if (this.isShowing) {
				this.hide();
			} else {
				this.show();
			}
		}
	},
	/**
	 * will try to hide the login if it is showing and the overCount <= 0.
	 **/
	attemptHide: function() {
		if (this.isShowing && this.overCount <= 0) {
			this.up = null;
			this.hide();
		}
	},
	/**
	 * special case hiding that will hide the form if nothing has been interacted with on the form
	 * regardless of whether it has focus or not.
	 **/
	longtermHide: function() {
		if (this.up != null) {
			clearTimeout(this.up);
			this.up = null;
		}
		this.hide();
	}
}

/**
 * The prefix is what is part of the id of each of the control items to
 * make it unique from the same control when multiple login forms are on
 * the same page.
 **/
function LoginForm(prefix) {
	this.init(prefix);
}

LoginForm.prototype = {
	init: function(prefix) {
		this.prefix = prefix;
		this.form = document.getElementById(prefix + "LoginForm");
		this.dropdown = document.getElementById(prefix + "CustType");
		this.user = document.getElementById(prefix + "User");
		this.password = document.getElementById(prefix + "Password");
		this.button = document.getElementById(prefix + "Submit");
		
		YAHOO.util.Event.on(this.form, "submit", this.submit, this, true);
		YAHOO.util.Event.on(this.button, "click", this.submit, this, true);
		
		var keyListener = new YAHOO.util.KeyListener(prefix + "User", {keys:13},
			{fn:this.submit, scope:this, correctScope:true});
		keyListener.enable();
		keyListener = new YAHOO.util.KeyListener(prefix + "Password", {keys:13},
			{fn:this.submit, scope:this, correctScope:true});
		keyListener.enable();
		keyListener = new YAHOO.util.KeyListener(prefix + "CustType", {keys:13},
			{fn:this.submit, scope:this, correctScope:true});
		keyListener.enable();
		
		this.forgot = document.getElementById(prefix + "Forgot");
		if (this.forgot) {
			// has forgot password link
			YAHOO.util.Event.on(prefix + "Forgot", "click", this.forgotClick, this, true);
		}
		this.register = document.getElementById(prefix + "Register");
		if (this.register) {
			// has register link
			YAHOO.util.Event.on(prefix + "Register", "click", this.registerClick, this, true);
		}
	},
	submit: function(e) {
		YAHOO.util.Event.stopEvent(e);
		if (this.dropdown.options[this.dropdown.selectedIndex].value == "Packaging") {
			this.form.action = "http://packaging.boiseinc.com/login.asp";
			this.user.name = "userid";
			this.password.name = "password";
			this.form.submit();
			return false;
		} else if (this.dropdown.options[this.dropdown.selectedIndex].value == "Paper") {
			this.form.action = "http://www.boiseinc.com/paper/j_security_check";
			this.user.name = "j_username";
			this.password.name = "j_password";
			this.form.submit();
			return false;
		} else {
			alert("Please select a customer type.");
			this.dropdown.focus();
			return false;
		}
		return false;
	},
	forgotClick: function(e) {
		if (this.dropdown.options[this.dropdown.selectedIndex].value == "Packaging") {
			this.forgot.href = "http://www.boiseinc.com/products/ForgotPasswordPKG.html";
			return true;
		} else if (this.dropdown.options[this.dropdown.selectedIndex].value == "Paper") {
			var date = new Date();
			if (date.getTime() < 1203870600000) {
				// haven't cutover to the new server yet
				this.forgot.href = "https://www.boisepaper.com/paper/forgotMyPassword.do";
			} else {
				this.forgot.href = "https://www.boiseinc.com/paper/forgotMyPassword.do";
			}
			return true;
		} else {
			YAHOO.util.Event.stopEvent(e);
			alert("Please select a customer type.");
			this.dropdown.focus();
			return false;
		}
	},
	registerClick: function(e) {
		this.register.href = "http://www.boiseinc.com/products/BecomeCustomer.html";
		return true;
	}
}

function printPage(e) {
	YAHOO.util.Event.stopEvent(e);
	window.print();
}
function emailPage(e) {
	YAHOO.util.Event.stopEvent(e);
	document.location = "mailto:?subject=" + document.title + "&body=" + document.URL;
}
function checkWidth(e) {
	var width = YAHOO.util.Dom.getViewportWidth();
	if (width < 920) {
		document.getElementById("under900css").disabled = false;
		document.getElementById("over900css").disabled = true;
	} else {
		document.getElementById("under900css").disabled = true;
		document.getElementById("over900css").disabled = false;
	}
	YAHOO.util.Event.stopEvent(e);
}
YAHOO.util.Event.onDOMReady(initNav);
YAHOO.util.Event.on(window, "load", checkWidth);