var lang = '';
function addBreadCrumb(){
	if($('#breadcrumb').length){
		var bc = document.getElementById('breadcrumb');
		var settings = document.documentElement.lastChild.className;
		var crumb = settings.split(" ");
		if(/-story$|site-map|tertiary|^home$/.test(crumb[crumb.length-1])){crumb.pop();}
		for(var k=0;k<crumb.length;k++){
			var li = document.createElement('li');
			var a = document.createElement('a');
			if($('#nav .'+crumb[k]).length){
				var node = $('#nav .'+crumb[k])[0];
				a.href = node.firstChild.href;
				a.appendChild(document.createTextNode(node.firstChild.firstChild.nodeValue));
				li.appendChild(a);
				if(k==crumb.length-1){li.className="last"};
				bc.appendChild(li);
			}
		}
	}
}


function continueAnimation(){
	if(document.getElementById('flash-home') && document.getElementById('flash-home').continueAnimation){
		document.getElementById('flash-home').continueAnimation();
	}	
}
getlang = function() {
	// find what lang the site is current in
	// for IE6 compat. we need to loop through <meta>
	$("meta").each(function() {
		if($(this).attr("name") == "lang") {
			if($(this).attr("content") == "es") {
				lang = "es";
			} else {
				lang = "en"
			}
		}
	});	
}
function changeHomePageHeadline(_current){
	if(lang == "en") {
		if($('#tfy-headlines').length){
			var current = ~~_current;
			if($.browser.msie){
				if(current == 0){//joe
					$('#tfy-major-family').show(0,function(){continueAnimation();});
					$('#tfy-andy').hide();
					$('#tfy-brett').hide();
				} else if(current == 1){
					$('#tfy-major-family').hide();
					$('#tfy-andy').show(0,function(){continueAnimation();});
					$('#tfy-brett').hide();
				} else if(current == 2){
					$('#tfy-major-family').hide();
					$('#tfy-andy').hide();
					$('#tfy-brett').show(0,function(){continueAnimation();});					
				}
			} else {
				if(current == 0){//joe
					$('#tfy-major-family').fadeIn(600,function(){continueAnimation();});
					$('#tfy-andy').fadeOut(100);
					$('#tfy-brett').fadeOut(100);
				} else if(current == 1){
					$('#tfy-major-family').fadeOut(100);
					$('#tfy-andy').fadeIn(600,function(){continueAnimation();});
					$('#tfy-brett').fadeOut(100);
				} else if(current == 2){
					$('#tfy-major-family').fadeOut(100);
					$('#tfy-andy').fadeOut(100);
					$('#tfy-brett').fadeIn(600,function(){continueAnimation();});
				}
			}
		}
	} else {
		continueAnimation();
	}
	// heres an updated version..
	
}

function homeFlash(){
	
	$("#tfy-major-family, #tfy-andy, #tfy-ellis, #tfy-brett").live("click",function() {
		window.location = $("a", this).attr("href");
	});
	getlang();
	if('swfobject' in window){
		if(lang == "es") {
			swfobject.embedSWF("/swf/tfy-home_es.swf", "home-flash","100%", "535", "9.0.0", "expressInstall.swf",null,{wmode:"transparent"},{id:"flash-home",name:"flash-home"},function(e){if(!e.success){$('#home-flash').append('<div id="home-flash-background"></div>')}});
		} else {
			swfobject.embedSWF("/swf/tfy-home.swf", "home-flash","100%", "535", "9.0.0", "expressInstall.swf",null,{wmode:"transparent"},{id:"flash-home",name:"flash-home"},function(e){if(!e.success){$('#home-flash').append('<div id="home-flash-background"></div>')}});
		}
	}
}

function homeIcon(){
	$('#logo').mouseover(function(){$('.tfy-text').css({background:'transparent url(/images/ui/home-icon.png) 100% 7px no-repeat'})});
	$('#logo').mouseout(function(){$('.tfy-text').css({background:'none'})});
}
function shareThisRollOver(){
	$('#share-this-label').mouseover(function(){$(this).css({'background-position':'100% 100%','color':'#fff'})});
	$('#share-this-label').mouseout(function(){$(this).css({'background-position':'0% 0%','color':'#cc6d19'})});
}


function findLinks(){
	//WT tracking 20100902
	/*$('a.third-party').each(function(){$(this).click(function(){
		if('dcsMultiTrack' in window){
			dcsMultiTrack('DCS.dcsuri',$(this).attr('href'));	
		}
	})});*/
	$('#rep-finder-form').bind('submit',function(){
		if('dcsMultiTrack' in window){
			dcsMultiTrack('DCS.dcssip',document.location.hostname,'DCS.dcsuri',document.location.pathname,'DCS.dcsqry','?zip='+$('#zip-code-answers').val(),'WT.dl','27','WT.z_repfinder','1','WT.si_n','Representative_Finder','WT.si_x','1');
			setTimeout((function(form){return function(){form.submit();};})(this),500);			
			return false;
		}
	});
	$('#rep-form').bind('submit',function(){
		if('dcsMultiTrack' in window){
			dcsMultiTrack('DCS.dcssip',document.location.hostname,'DCS.dcsuri',document.location.pathname,'DCS.dcsqry','?zip='+$('#rep-zip').val(),'WT.dl','27','WT.z_repfinder','1','WT.si_n','Representative_Finder','WT.si_x','1');
			setTimeout((function(form){return function(){form.submit();};})(this),500);			
			return false;
		}
	});
	$('#get-answers-callout form').bind('submit',function(){
		if('dcsMultiTrack' in window){
			dcsMultiTrack('DCS.dcssip',document.location.hostname,'DCS.dcsuri',document.location.pathname,'DCS.dcsqry','?zip='+$('#zip-code-answers').val(),'WT.dl','27','WT.z_repfinder','1','WT.si_n','Representative_Finder','WT.si_x','1');
			setTimeout((function(form){return function(){form.submit();};})(this),500);			
			return false;
		}
	});	
	$('a.form').each(function(){$(this).bind('click',function(){													   
		if('dcsMultiTrack' in window){
			dcsMultiTrack('DCS.dcssip',document.location.hostname,'DCS.dcsuri',document.location.pathname,'DCS.dcsqry',this.search,'WT.ti',$(this).text(),'WT.dl','24','WT.z_emailregistration','1');
			setTimeout((function(href){return function(){window.open(href+"?WT.z_emailregistration=1",'_self');};})(this.href),500);			
			return false;
		}
	})});
	$('a.careform').each(function(){$(this).bind('click',function(){
		if('dcsMultiTrack' in window){
			dcsMultiTrack('DCS.dcssip',document.location.hostname,'DCS.dcsuri',document.location.pathname,'DCS.dcsqry',this.search,'WT.ti',$(this).text(),'WT.dl','24','WT.z_careprogram','1');
			setTimeout((function(href){return function(){window.open(href+"&WT.z_careprogram=1",'_self');};})(this.href),500);			
			return false;
		}	
	})});
	$('a.pdf').each(function(){$(this).click(function(){									  
		if('dcsMultiTrack' in window){
			dcsMultiTrack('DCS.dcssip',document.location.hostname,'DCS.dcsuri',$(this).attr('href'),'WT.dl','20','WT.ti',"download: "+$(this).text(),'WT.z_pdfdownload',this.pathname,'WT.z_pdfdownloadcount','1');
			window.open(this.href+"?WT.z_pdfdownload=1&WT.z_pdfdownloadcount=1","_blank");
			return false;
		}
	})});
	
	$('a.webinar-btn,a.webinar-btn-callout').each(function(){$(this).click(function(){									  
		if('dcsMultiTrack' in window){
			dcsMultiTrack('DCS.dcssip',document.location.hostname,'DCS.dcsuri',document.location.pathname,'WT.dl','24','WT.z_webinar','1');
		}
	})});
	
	
	$('a.baxject-ii-video').click(function(){window.open(this.href,'baxject-ii-demo','height=640,width=800,scrollbars=no');return false;});
	

	
	$('#share-this p').click(function(){
		var location = encodeURIComponent(document.location.protocol+"//"+document.location.hostname+document.location.pathname);
		var title = encodeURIComponent(document.title);
		var desc = encodeURIComponent($('meta[name=description]').attr('content'));
		if($('#social-twitter').length){
			
				$('#social-twitter').click(function(){
					window.open(this.href);
					if('dcsMultiTrack' in window){
						dcsMultiTrack('DCS.dcssip',document.location.hostname,'DCS.dcsuri',document.location.pathname,'DCS.dcsqry',this.search,'WT.ti',"Share This: Twitter",'WT.dl','24');
					}
					return false;
				});
				var API = 'http://api.bit.ly/v3/shorten?login=sireninteractive&apiKey=R_5deb9e1155305ef9d05f7fa0c4ee113b&longUrl=';
				URL = API + location + '&callback=?';
				if(!/bit\.ly/.test($('#social-twitter').attr('href'))){
					$.getJSON(URL, function(bitly){
						if(bitly.data.url){
								$('#social-twitter').attr('href',$('#social-twitter').attr('href')+bitly.data.url);
						} else {
							$('#social-twitter').attr('href',$('#social-twitter').attr('href')+location);	
						}
					});
				}
		}
		if($('#social-email').length){
			
			$('#social-email').attr('href',$('#social-email').attr('href')+document.title+"&body="+location);
			$('#social-email').click(function(){
				window.location(this.href);
				dcsMultiTrack('DCS.dcssip',document.location.hostname,'DCS.dcsuri',document.location.pathname,'DCS.dcsqry',this.search,'WT.ti',"Share This: Email",'WT.dl','24');
				return false;
			});
		}
		if($('#social-facebook').length){
			$('#social-facebook').attr('href',$('#social-facebook').attr('href')+location+"&t="+title);
			$('#social-facebook').click(function(){
				window.open(this.href);
				dcsMultiTrack('DCS.dcssip',document.location.hostname,'DCS.dcsuri',document.location.pathname,'DCS.dcsqry',this.search,'WT.ti',"Share This: Facebook",'WT.dl','24');
				return false;
			});
		}
		if($('#social-myspace').length){
			$('#social-myspace').attr('href',$('#social-myspace').attr('href')+location);
			$('#social-myspace').click(function(){
				window.open(this.href);
				dcsMultiTrack('DCS.dcssip',document.location.hostname,'DCS.dcsuri',document.location.pathname,'DCS.dcsqry',this.search,'WT.ti',"Share This: MySpace",'WT.dl','24');
				return false;
			});
		}
									  
		$('#social-sites').animate({"marginLeft":-353,"width":355},400);
		$('#share-this p').unbind('click');
  });
}

function init(){
	if($.browser.opera){
		$("head").append('<link rel="stylesheet" href="/css/opera.css" type="text/css" media="all" />');	
	}
	if(arguments.callee.doOnce){return;}
	arguments.callee.doOnce=true;
	addBreadCrumb();
	homeIcon();
	if('Cufon' in window){Cufon.now();}
	if('LikeTool' in window && $('#social').length){var lt = new LikeTool();}
	if('Poll' in window && $('#survey-form').length){var poll = new Poll();}
	findLinks();
	if('swfobject' in window && $('#home-animation').length){homeFlash();}
	if('VideoPopUp' in window){var vpu = new VideoPopUp();}

	if($.isFunction($.fn.familynav)) {
		// English only
		// if this is required, update es header.html and es css+scripts
		/*
		if(lang == "en" || !lang) {
			$("#familynav2").familynav(config);
		}
		*/
		
		
		if(lang == "es") {

			config = {
				header:"See other Baxter Healthcare Corporation hemophilia sites",
				links:[
					{name:"Education Advantage",url:"http://www.myeducationadvantage.com"}/*,
					{title:"ADVATE [factor antihemof&iacute;lico (recombinante) de m&eacute;todo sin plasma/alb&uacute;mina]",url:"http://advate.com/",
					links:[
						{name:"Informaci&oacute;n de prescripci&oacute;n",url:"/es/pdf/advate_pi.pdf",rel:"external"},
						{name:"Informaci&oacute;n importante sobre riesgos",url:"/es/pdf/advate-dri.pdf?WT.z_pdfdownload=1&WT.z_pdfdownloadcount=1",rel:"external"}
					]}*/
				]
			};				
		} else { // default english 
		
			
			config = {
				header:"See other Baxter Healthcare Corporation hemophilia sites",
				links:[
					{name:"Education Advantage",url:"http://www.myeducationadvantage.com"}/*,
					{title:"ADVATE [Antihemophilic Factor (Recombinant), Plasma/Albumin-Free Method]",url:"http://advate.com/",
					links:[
						{name:"Prescribing Information",url:"/pdf/advate_pi.pdf",rel:"external"},
						{name:"Important Risk Information",url:"/pdf/advate-dri.pdf?WT.z_pdfdownload=1&WT.z_pdfdownloadcount=1",rel:"external"}
					]},
					{title:"RECOMBINATE [Antihemophilic Factor (Recombinant)]",url:"http://recombinate.com/",
					links:[
						{name:"Prescribing Information &ndash; 5mL diluent",url:"http://recombinate.com/pdf/recombinate-5ml-pi.pdf",rel:"external"},
						{name:"Important Risk Information",url:"http://recombinate.com/pdf/recombinate-dri.pdf",rel:"external"},
						{name:"Prescribing Information &ndash; 10mL diluent",url:"http://recombinate.com/pdf/recombinate-pi-baxject-ii.pdf",rel:"external"}
					]}*/
				]
			};
		}
		$("#familynav2").familynav(config);
	}
	$('#nav > ul > li').each(function(){$(this).hover(
		function(){
			Cufon.replace('#nav > ul > .'+$(this).attr('class') +' > a',{fontFamily:'tfy2',color:'#fff',textShadow:'#006bb7 0 1px'},false);
			if(/MSIE 6/.test(navigator.userAgent)){
				$(this).toggleClass("show");
			}
		}
		,
		function(){
			if(!$('body').hasClass($(this).attr('class'))){
				if(/MSIE 6/.test(navigator.userAgent)){
					$(this).toggleClass("show");
				}
				Cufon.replace('#nav > ul > .'+$(this).attr('class') +' > a',{fontFamily:'tfy2',textShadow:'#fff 0 1px'},false);
			}
			
		}
	)});
	
	if($('#qa-list').length){
		$('#qa-list ol .question p').each(function(){$(this).parent().find('div').css("padding-top",$(this).height()/2-10+"px");});
	}
	
	if($('body').attr('class')){
		$("#left-nav ."+$('body').attr('class').split(' ').slice(-1)).prev('li').addClass('prev');
	}
	if('$' in window && /MSIE 6/.test(navigator.userAgent)){
		$('#main #text #right-nav > div:first-child').css("border","none");
		try{
			document.execCommand("BackgroundImageCache", false, true)
		}
		catch(e){}
		$('#logo span').click(function(){document.location = "http://www.thereforyou.com"});
	}
	
	if(/MSIE\s[67]/.test(navigator.userAgent)){
		
		$('a.external').each(function(){
			$(this).attr('class',"external no-bg");
			$(this).append("<span class=\"external\">&nbsp;</span>");
		});
		
		$('a.pdf').each(function(){
			$(this).attr('class',"pdf no-bg");
			$(this).append("<span class=\"pdf\">&nbsp;</span>");
		});
	}
	
	$("#rep-zip, #zip-code-answers").focus(function(){
		$(this).attr("value","");
		$(this).unbind("focus");
		$(this).css("color","#333");
	});
	
	if($('.bookmark-link').length){
		if($.browser.msie){
			if(window.external){
				$('.bookmark-link').click(function(){
					window.external.AddFavorite("http://www.thereforyou.com/community-connections/nhf-annual-meeting.html","National Hemophilia Foundation (NHF) Annual Meeting");
					return false;
				});
			}
		} else if($.browser.mozilla){
			if(window.sidebar && window.sidebar.addPanel){
				$('.bookmark-link').click(function(){
					window.sidebar.addPanel("National Hemophilia Foundation (NHF) Annual Meeting","http://www.thereforyou.com/community-connections/nhf-annual-meeting.html","");
					return false;
				});
			}
		}
	}
	
	
	if(document.location.search && $('#source-field').length){
		var source = document.location.search.toString().match(/SOURCE=([^&]+)(?:&|$)/);
		if(source.length && source[1]){
			$('#source-field').val(source[1]);
		}
	}
}



if('Cufon' in window){

	Cufon.replace('#nav > ul > li > a',{fontFamily:'tfy2',textShadow:'#fff 0 1px',hover:false});
	Cufon.replace('.about-hemophilia #header #nav .about-hemophilia>a,.managing-hemophilia #header #nav .managing-hemophilia>a,.hemophilia-therapies #header #nav .hemophilia-therapies>a,.patient-resources #header #nav .patient-resources>a,.community-programs #header #nav .community-programs>a,.community-connections #header #nav .community-connections>a,.insurance-and-advocacy #header #nav .insurance-and-advocacy>a,.clinical-initiatives #header #nav .clinical-initiatives>a',{fontFamily:'tfy2',textShadow:'#006bb7 0 1px',hover:false});
	
	
	Cufon.replace('#home #banner h1',{fontFamily:'tfy',textShadow:"0 1px rgba(0, 0, 0, 1)"});
	Cufon.replace('#home #banner p',{fontFamily:'tfy2'});
	Cufon.replace('#home #banner h2',{fontFamily:'tfy',hover:true,textShadow:"0 1px rgba(0, 0, 0, 1)"});
	Cufon.replace('#home #banner #read-all-stories,.read-all-stories',{fontFamily:'tfy',hover:true});
	Cufon.replace('#home-callouts h1',{fontFamily:'tfy2'});
	Cufon.replace('#survey-results #survey-buttons li a',{fontFamily:'tfy2'});
	Cufon.replace('#home #content #text h1',{fontFamily:'tfy2'});
	Cufon.replace('#home #content #text h3',{fontFamily:'tfy2'});
	Cufon.replace('#social #share-this p',{fontFamily:'tfy2',hover:true});
	Cufon.replace('#main #text h1',{fontFamily:'tfy'});
	Cufon.replace('#main #text h2',{fontFamily:'tfy2'});
	Cufon.replace('#sub-head-content strong',{fontFamily:'tfy2',textShadow:"0 1px rgba(0, 0, 0, 1)"});
	Cufon.replace('#sub-head-content em',{fontFamily:'tfy',textShadow:"0 1px rgba(0, 0, 0, 1)"});
	Cufon.replace('.about-hemophilia #sub-head-content strong',{fontFamily:'tfy2'});
	Cufon.replace('.about-hemophilia #sub-head-content em',{fontFamily:'tfy'});
	Cufon.replace('.clinical-initiatives #sub-head-content strong',{fontFamily:'tfy2'});
	Cufon.replace('.clinical-initiatives #sub-head-content em',{fontFamily:'tfy'});
	Cufon.replace('.nhf-walk-story #sub-head-content strong',{fontFamily:'tfy2'});
	Cufon.replace('.nhf-walk-story #sub-head-content em',{fontFamily:'tfy'});
	Cufon.replace('.joe2-story #sub-head-content strong',{fontFamily:'tfy2',color:"#000000",textShadow:"0 1px rgba(255, 255, 255, 1)"});
	Cufon.replace('.joe2-story #sub-head-content em',{fontFamily:'tfy2',color:"#000000",textShadow:"0 1px rgba(255, 255, 255, 1)",fontWeight:"normal"});
	Cufon.replace('#main #sub-head .read-story',{fontFamily:'tfy'});
	Cufon.replace('#right-nav h3',{fontFamily:'tfy2'});
	Cufon.replace('#newsletter-callout #register-now',{fontFamily:'tfy2',hover:true});
	Cufon.replace('#main #text #newsletter-callout h3',{fontFamily:'tfy2'});
	Cufon.replace('#main #text #events #month-list li a',{fontFamily:'tfy2'});
	Cufon.replace('#main #text #nhf-events #type-list li a',{fontFamily:'tfy2'});
	Cufon.replace('#main #text #events .event-header',{fontFamily:'tfy'});
	Cufon.replace('#main #text #nhf-events .event-header',{fontFamily:'tfy'});
	Cufon.replace('#prompt-inner h1',{fontFamily:'tfy2'});
	Cufon.replace('#main #text #qa-list ol .question div',{fontFamily:'tfy2'});
	//Cufon.replace('.box-button a',{fontFamily:'tfy2',hover:true});
	Cufon.replace('#main #text .perspectives-enroll',{fontFamily:'frutigercn'});
	Cufon.replace('#main #text .circle-enroll',{fontFamily:'frutigercn'});
	Cufon.replace('#main #text .circle-enroll p',{fontFamily:'frutigercn'});
	Cufon.replace('#main #text .circle-enroll a',{fontFamily:'frutigercn'});
	Cufon.replace('.quote',{fontFamily:'Gill Sans MT'});
	
	Cufon.replace('#nhf-footer-content h3',{fontFamily:'tfy2'});
	Cufon.replace('#sponsored-callout p',{fontFamily:'tfy'});
	Cufon.replace('#sponsored-callout p strong',{fontFamily:'tfy2'});
	Cufon.replace('#sponsored-callout p em',{fontFamily:'tfy2'});
	Cufon.replace('#sponsored-callout h3',{fontFamily:'tfy2'});
	Cufon.replace('#sponsored-callout h4',{fontFamily:'tfy2'});
	Cufon.replace('#main #text .nhf-orange',{fontFamily:'tfy2'});
	Cufon.replace('#gallery-inner h2',{fontFamily:'tfy'});
	Cufon.replace('#main #text .subhead-p',{fontFamily:'tfy2'});
	Cufon.replace('.perspectives-enroll-now',{fontFamily:'frutigercn'});
	Cufon.replace('#hfa-footer span',{fontFamily:'tfy2'});
}

$(document).ready(init);

$(document).ready(function() {
	strpos = function (haystack, needle, offset) {
		var i = (haystack + '').indexOf(needle, (offset || 0));
		return i === -1 ? false : i;
	}
	// preserve uri segments, prevents uri changes after page load
	var fullurl = window.location.href;
	var hostname = window.location.hostname;
	var pathname = window.location.pathname;
	$("#language-nav a").live("click",function(e) {
		e.preventDefault();
		$.fn.defaultLangURLs = function() {
			if($(this).attr("href") == "/") {
				// spanish to english
				var redirect = fullurl.replace("/es","");
				var error = "http://"+hostname;
			} else {
				// english to spanish
				var redirect = "http://"+hostname+"/es"+pathname;
				var error = "http://"+hostname+"/es/404_es.html";
			}
			// check page exists
			$.ajax({url:redirect,
			error:function() {
				window.location = error;
			},success:function() {
				window.location = redirect;
			}});
			return $(this);
		}
		no_caseLangURLs = function(url) {
			/*
			// this was used before it was translated
			if(strpos(url,"/facts-first.html")) { // if on spanish going to english
				return "http://"+hostname+"/community-programs/facts-first/";
			} else if(strpos(url,"/facts-first/")) { // if on english going to spanish
				if(strpos(url,".html")) {
					if(strpos(url,"index.html")) { // landing page
						return "http://"+hostname+"/es/community-programs/facts-first.html";
					} else { // 404 page
						return "http://"+hostname+"/es/404_es.html";
					}
				} else { // landing page
					return "http://"+hostname+"/es/community-programs/facts-first.html";
				}
			} else {
				return false;
			}
			*/
			return false;
		}
		if($(this).parent().attr("class") != "current") {
			var specialcase = no_caseLangURLs(pathname);
			if(specialcase !== false){
				window.location = specialcase;
			} else {
				$(this).defaultLangURLs();
			}
		}
	});
	$(".healthcare-reform-state-tool #launch-legend").live("click",function(e) {
		e.preventDefault();
		$("#ext-link-bg").fadeTo(200,.4,function(){
			$(".healthcare-reform-state-tool .legend,.thisx").fadeTo(300,1);
		});
	});
	$(".healthcare-reform-state-tool .xBtn,#ext-link-bg").live("click",function(e) {
		e.preventDefault();
		$(".healthcare-reform-state-tool .legend,#ext-link-bg,.thisx").fadeOut(200);
	});
	

});
