/* Make the nav position fixed, but only if it fits in the browser window.
Tested in IE7 and Firefox, exclludes IE5.5. To work in IE6 or higher, 
the browser should be forced to standards mode by adding a doctype declaration to the html page, eg:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
*/




neoarchaic.FixNav = function(){
	if (!document.getElementById){
		return;
	}
	//Check for ie version
	var ver=navigator.appVersion.split("MSIE")
	ver=parseFloat(ver[1])
	this.ie7 = (ver > 6);
		
	this.addLoadEvents();
	this.addEvent(window, "resize", "fixNav");
	this.t = null;
}

neoarchaic.protoObj = {
	className: "FixNav",
	
	init:function(){
		if (this.isInit) return;
		this.isInit = true;
		this.nav = document.getElementById("nav");
		this.navpadding = document.getElementById("navpadding");
		this.fixNav();
	},
	
	fixNav: function(){
		if (!document.getElementById){
			return;
		}
		
		if (window.innerHeight){
		//Firefox  etc.
			if (window.innerHeight >= this.nav.clientHeight){
				this.fixElements();
			}else{
				this.unfixElements();
			}
		} else if (document.body.clientHeight && this.ie7){
			//Windows
			if (document.body.clientHeight >= this.nav.clientHeight){
				this.fixElements();
			}else{
				this.unfixElements();
			}
		} else if (document.body.clientHeight && document.documentElement){
			//Windows
			if (document.body.clientHeight >= this.nav.clientHeight){
				clearInterval(this.t);
				var me = this;
				this.t = setInterval(function () { me.slideNav(); }, 1);
				
			}else{
				clearInterval(this.t);
				this.nav.style.pixelTop = 0;
			}			
		}
	},
	
	fixElements:  function(){
			this.nav.style.position = "fixed";
			document.body.style.backgroundAttachment = "fixed";
			this.navpadding.style.position = "fixed";
	},
	
	unfixElements: function(){
		this.nav.style.position = "absolute";
		document.body.style.backgroundAttachment = "scroll"
		this.navpadding.style.position = "absolute";
	},
	
	slideNav: function(){
		if (!(document.body.clientHeight && document.documentElement)){
			  return;
	   }
		if (document.body.clientHeight < this.nav.clientHeight){
			this.nav.style.pixelTop = 0;
			return;
		}
		var scrollTop = Math.max(document.body.scrollTop, document.documentElement.scrollTop);		   		
		if (this.nav.style.pixelTop == scrollTop){
			return;
		}
		var dif = this.nav.style.pixelTop - scrollTop
		if (Math.abs(dif) > 9){
			this.nav.style.pixelTop -= dif/10
		} else if (this.nav.style.pixelTop < scrollTop){
			this.nav.style.pixelTop ++;
		}else{
			this.nav.style.pixelTop --;
		}
	}
}

neoarchaic.Core.extend(neoarchaic.FixNav, null, neoarchaic.protoObj);
delete neoarchaic.protoObj;

neoarchaic.Core.newObject("nav", "FixNav");


