var BeyondNav = (function(){
	function constructor(nav,boxes,current){
		this.nav = nav;
		this.boxes = boxes;
		this.current = current;
		this.tween;
		this.scrollingElement;
	}				  
	
	

	
	constructor.prototype.init = function(){
		document.getElementById(this.nav[this.current]).style.backgroundPosition = "219px 50%";
		document.getElementById("next-list").style.top = "-"+this.current*44+"px";
		if(this.current==this.nav.length-1){
			document.getElementById("next").style.backgroundPosition = "16px -51px";
		}
		if(/MSIE/.test(navigator.userAgent)){
			if(/MSIE 6/.test(navigator.userAgent)){
				this.scrollingElement = document.body;
				document.body.controller = this;
				document.body.onscroll = this.scrollController;
			} else {
				this.scrollingElement = document.documentElement;
				window.controller = this;
				window.onscroll = this.scrollController;
			}
		} else if(/webkit/i.test(navigator.userAgent)){
			this.scrollingElement = document.body;
			window.controller = this;
			window.addEventListener("scroll", this.scrollController, false);
		} else {
			this.scrollingElement = document.documentElement;
			window.controller = this;
			window.addEventListener("scroll", this.scrollController, false);
		}
		this.scrollingElement.scrollTop = document.getElementById(this.boxes[this.current]).offsetTop;
		document.getElementById('left-col').style.top = 0;
		
		for(var k=0;k<this.nav.length;k++){
			document.getElementById(this.nav[k]).firstChild.controller = this;
			document.getElementById(this.nav[k]).firstChild.num = k;			
			document.getElementById(this.nav[k]).firstChild.onclick = function(){
				if((!this.controller.tween || !this.controller.tween.playing) && this.num != this.controller.current){
					if(/MSIE 6|webkit/i.test(navigator.userAgent)){
						this.controller.tween = new Tween(document.documentElement.lastChild,"scrollTop",document.documentElement.lastChild.scrollTop,document.getElementById(this.controller.boxes[this.num]).offsetTop,250,'','',"easeSin",false);
					} else {
						this.controller.tween = new Tween(document.documentElement,"scrollTop",document.documentElement.scrollTop,document.getElementById(this.controller.boxes[this.num]).offsetTop,250,'','',"easeSin",false);
					}
					this.controller.tween.play();
					this.controller.setCurrent(this.num);
					this.href = "#"+this.controller.boxes[this.num].replace(/-/g,"+");
					if(/MSIE 6/.test(navigator.userAgent)){return false;}
				} else {
					return false;	
				}
			}
			if(document.getElementById(this.nav[k].replace(/nav/,"next")) && document.getElementById(this.nav[k].replace(/nav/,"next")).firstChild){
				document.getElementById(this.nav[k].replace(/nav/,"next")).firstChild.controller = this;
				document.getElementById(this.nav[k].replace(/nav/,"next")).firstChild.num = k+1;
				document.getElementById(this.nav[k].replace(/nav/,"next")).firstChild.onclick = function(){
					if((!this.controller.tween || !this.controller.tween.playing) && this.num != this.controller.current){					
						if(/MSIE 6|webkit/i.test(navigator.userAgent)){
							this.controller.tween = new Tween(document.documentElement.lastChild,"scrollTop",document.documentElement.lastChild.scrollTop,document.getElementById(this.controller.boxes[this.num]).offsetTop,250,'','',"easeSin",false);
						} else {
							this.controller.tween = new Tween(document.documentElement,"scrollTop",document.documentElement.scrollTop,document.getElementById(this.controller.boxes[this.num]).offsetTop,250,'','',"easeSin",false);
						}
						
						this.controller.tween.play();
						this.controller.setCurrent(this.num);
						this.href = "#"+this.controller.boxes[this.num].replace(/-/g,"+");
						if(/MSIE 6/.test(navigator.userAgent)){return false;}
					} else {
						return false;	
					}
				}
			}

		}

		
	}
	
	constructor.prototype.scrollController = function(){
		if(!this.controller.tween || (this.controller.tween && !this.controller.tween.playing)){
			var h = document.getElementById(this.controller.boxes[0]).offsetHeight;
			var cs = this.controller.scrollingElement.scrollTop+h/2;
			var cb = this.controller.boxes;
			for(var k=0;k<cb.length;k++){
				if(cs>=h*k&&cs<=h*(k+1)&&this.controller.current!=k){
					this.controller.setCurrent(k);	
				}
			}
		}
	}
	
	constructor.prototype.setCurrent = function(num){
		document.getElementById(this.nav[this.current]).tween = new Tween(document.getElementById(this.nav[this.current]),"backgroundPosition",219,240,75,"px 50%","","easeSin",true);
		document.getElementById(this.nav[this.current]).tween.play();
		document.getElementById(this.nav[num]).tween = new Tween(document.getElementById(this.nav[num]),"backgroundPosition",240,219,75,"px 50%","","easeSin",true);
		document.getElementById(this.nav[num]).tween.play();
		
		document.getElementById("next-list").tween = new Tween(document.getElementById("next-list"),"top",Number(document.getElementById("next-list").style.top.match(/\d+/)),document.getElementById(this.nav[num].replace(/nav/,"next")).offsetTop,250,"px","-","easeSin",true);
		document.getElementById("next-list").tween.play();
		
		if(num==this.nav.length-1){
			document.getElementById("next").tween = new Tween(document.getElementById("next"),"backgroundPosition",0,-51,250,"px","16px ","easeSin",true);
			document.getElementById("next").tween.play();	
		} else if(/\s[^0]/.test(document.getElementById("next").style.backgroundPosition)) {
			document.getElementById("next").tween = new Tween(document.getElementById("next"),"backgroundPosition",-51,0,250,"px","16px ","easeSin",true);
			document.getElementById("next").tween.play();
		}
		window.location.hash = this.boxes[num].replace(/-/g,"+");
		this.current = num;
	}
	
	return constructor;
})();


function findLinks(){
	var a = document.getElementsByTagName('a');
	for(var k=0;k<a.length;k++){
		if(/order-form/i.test(a[k].className)){
			a[k].onclick = function(){
				window.open(this.href,"beyond_form","width=790,height=600,top=10,left=10,resizable=yes,scrollbars=yes,menubar=no,toolbar=no,status=yes,location=no");
				return false;
			}
		} else if(/alpha-1-ref|difference-ref|risk-ref/i.test(a[k].className)){
			a[k].onclick = function(){
				this.ref = document.getElementById(this.className);
				this.pos = this.ref.offsetTop-document.documentElement.offsetHeight/2;
				this.tween = new Tween(document.documentElement,"scrollTop",document.documentElement.scrollTop,this.pos,250,'','',"easeSin",false);	
				this.tween.play();
				return false;
			}
		} else if(/alpha-1-aka|physician-aka/i.test(a[k].className)){
			a[k].onclick = function(){
				this.aka = document.getElementById(this.className);
				this.aka.style.display = this.aka.style.display=="block"?"none":"block";
				return false;
			}
		}
	}
}


function setCookie(){
	document.cookie = "resourceFlyer=findoutforsure; path=/;"
}

function hasCookie(){
	var cookies = document.cookie.split(';');
	for(var i=0;i < cookies.length;i++){
		var c = cookies[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf("resourceflyer") == 0) return true;//cookie is set
	}
	return false;//cookie is not set
}

function initContentArea(){
	var a = document.getElementById('content').getElementsByTagName('div');
	for(var k=0;k<a.length;k++){
		if(/content-area/i.test(a[k].className) && document.documentElement.offsetHeight>445){
			var h = document.documentElement.offsetHeight>445?document.documentElement.offsetHeight-98:445;
			var p = (h-445)/2+30;
			if(p<0){p=0;}
		//	a[k].style.paddingTop = p+"px";
		//	a[k].style.height = (h-p)+"px";
		}
	}
}


function init(){
	if(arguments.callee.doOnce){return;}
	arguments.callee.doOnce=true;
	document.getElementById('content').style.backgroundPosition = "0 "+(document.documentElement.offsetHeight-99)+"px";

	if(document.location.search == "?resourceflyer=findoutforsure" && !hasCookie()){
		setCookie();	
	}
	if(hasCookie()){
		var a = document.getElementsByTagName('a');
		for(var k=0;k<a.length;k++){
			if(a[k].href == "https://webforms.baxterbioscience.com/beyondcopd/paOrder.jspa"){
				a[k].href = a[k].href + "?resourceflyer=findoutforsure";
			}
		}
	}
	
	
	findLinks();
	if(document.getElementById('index')){
		initContentArea();
		window.onresize = initContentArea;
		var current;
		if(window.location.hash){
			switch(window.location.hash.replace(/\+/g,'-')){
				case '#alpha-1':current=0;break;
				case '#whats-the-difference':current=1;break;
				case '#am-i-at-risk':current=2;break;
				case '#find-out':current=3;break;
			}
		} else {
			current = 0;	
		}
		bn = new BeyondNav(['alpha-1-nav','difference-nav','risk-nav','find-out-nav'],['alpha-1','whats-the-difference','am-i-at-risk','find-out'],current);
		bn.init();
		

	}
}

if(document.addEventListener){document.addEventListener("DOMContentLoaded",init,null);}
window.onload=init;