// variables
var currentUrl = window.location.pathname;
var currentPage = currentUrl.substr(currentUrl.lastIndexOf("/") + 1);
var tooltipContent;
var searchVal;
// functions
function setEqualHeight(columns) {
	
	 var tallestcolumn = 0;
	 
	 columns.each(function() {
						   
		 currentHeight = $(this).height();
		 
		 if(currentHeight > tallestcolumn) {
			 
			tallestcolumn  = currentHeight;
		 }
	 });
	 
	 columns.height(tallestcolumn);
 
}
// Tabs function
	
function createTabs(tabContainer, subnavContainer, hasAllTab) {
	
	$(tabContainer+'>div').hide();
	if (location.hash) {
		
		if (location.hash.substr(1) == 'tab-all') {
			
			$(tabContainer+'>div').show();
		} else if(location.hash.substr(1) == 'medical-visit')
		{
			$(tabContainer+'>div').show();
			$('#medical-visit-q').addClass("open");
			$('#medical-visit-q >p').show();
			$('html, body').animate({
				scrollTop: $("#medical-visit").offset().top
			}, 500);


			
		}  else if(location.hash.substr(1) == 'vaccination-schedule')
		{
			$(tabContainer+'>div').show();
			$('#vaccination-schedule-q').addClass("open");
			$('#vaccination-schedule-q >p').show();
			document.getElementById("vaccination-schedule").scrollIntoView();
			
		}  else if(location.hash.substr(1) == 'vaccine-disease')
		{
			$(tabContainer+'>div').show();
			$('#vaccine-disease-q').addClass("open");
			$('#vaccine-disease-q >p').show();
			document.getElementById("vaccine-disease").scrollIntoView();
			
		}  else if(location.hash.substr(1) == 'flu-cold')
		{
			$(tabContainer+'>div').show();
			$('#flu-cold-q').addClass("open");
			$('#flu-cold-q >p').show();
			document.getElementById("flu-cold").scrollIntoView();
			
		}  else if(location.hash.substr(1) == 'mmr-autism')
		{
			$(tabContainer+'>div').show();
			$('#mmr-autism-q').addClass("open");
			$('#mmr-autism-q >p').show();
			document.getElementById("mmr-autism'").scrollIntoView();
		} else {
		
			$(tabContainer+'>div[id$="' + location.hash.substr(1) + '"]').show();
		}
		
		$(subnavContainer+' a[href*="' + location.hash.substr(1) + '"]').addClass("active");
	}
	
	else {
		
		if (hasAllTab) {
			
			$(tabContainer+'>div').show();
		}
		
		else {
			
			$(tabContainer+'>div').first().show();
		}
		
		$(subnavContainer+' a').first().addClass("active");
	}
	$(subnavContainer+' a').click(function(event){
										
		$(tabContainer+'>div').hide();
		$(subnavContainer+' a').removeClass();
		
		 if (this.href.indexOf("view=") !== -1) { 
		  
			  var clickedHREF = this.href;  
			  var clickedView = clickedHREF.split("view=");
			  
			  if (clickedView[1] == 'tab-all') {
				  
					$(tabContainer+'>div').show();
			  }
			  
			  else {
				  
					$(tabContainer+'>div[id$="' + clickedView[1] + '"]').show();
			  }
			  
			  $(this).addClass('active');
			  location.hash = clickedView[1];  
			  return false;  
		  }  
		
	 });
}

$(document).ready(function() {

	// set all title tags to alt tag values
	$('img').each(function(){
		$(this).attr('title', $(this).attr('alt'))
	})
				   
	// startup actions
	
	$('a[href^="http://"], a[href^="https://"]').attr({
								 
		target: "_blank", 
		title: "Opens in a new window"
	});

	if(!($.browser.msie && $.browser.version=="6.0")) {
		$("header #search").corner("5px");
	}
	
	//$("header #search").show();

	$('#side_2-1-x div[id$="01"]').show();
	
    $('#subnav a[href*="'+currentPage+'"]').addClass("active");

	
	var hover_config = {    
		over: makeTall, 
		timeout: 500,  
		out: makeShort     
	};

	function makeTall() {
		$(this).children('[class="dropdown"]').slideDown("slow");
		var dropdownLeftPos = ($(this).children('a').outerWidth() / 2) - ($(this).children('[class="dropdown"]').width() / 2);
		$(this).children('[class="dropdown"]').css("left", dropdownLeftPos);
	}
	function makeShort(menuObj)	{
		$(this).children('[class="dropdown"]').slideUp("fast");
	}
	
	$(".dropdown-container").hoverIntent( hover_config );
		
	$("#infographic_container_lg").iviewer( {
		
		   src: $("#infographic_container_lg").next("img").attr("src"), 
		   update_on_resize: false,
		   initCallback: function () {
		   }
	  });
	  
	$("#body_1-4_inner h4").nextAll().hide();
	$("#body_2-x_inner>a, #body_2-5_inner>div>a").each(function() {
		   
		  tooltipContent = $(this).next().html();
		  
		  $(this).qtip(
		  {
			 content: tooltipContent,
			 position: {
				  corner: {
					 target: 'leftMiddle',
					 tooltip: 'rightMiddle'
				  }
			 },
			 show: { 
			 	delay: 0,
				effect: { length: 1 }
			},
			 hide: {
				delay: 0,
				fixed: true,
				effect: { length: 0 }
			 }
		  });
		  
	   });
	
	setEqualHeight($(".home_col_inner, .subnav_1-0_inner, .subnav_2-0_inner"));
	
	// call this one after so it will calculate based on new inner height
	setEqualHeight($(".home_col_outer, .subnav_1-0_outer, .subnav_2-0_outer"));
	   
	createTabs('#body_1-4_inner', '#subnav_1-4', true);
	
	createTabs('#body_2-5_inner', '#subnav_2-5', false);
	
	createTabs('#body_2-1-x_inner', '#subnav_2-1-x', false);
	
	$('#side_2-1-x>div').hide();
	
	$('#side_2-1-x_set-01').show();
	
	// event handlers
	
	$('header #search input[type="text"], #search_2 input[type="text"]').focus(function() {
		
	  searchVal = $(this).val();
	  $(this).val("");
	  
	}).blur(function() {
		
	  if ($(this).val() == "") {
		  
	  		$(this).val(searchVal);
	  }
	  
	});
	
	$('#share_print').click(function(event){
										
		event.preventDefault();
		window.print();
		
	 });
	 
	 $(".subnav_1-0_outer, .subnav_2-0_outer").click(function(){
										
		window.location = $(this).children("a").attr("href");
		
	 });
	 
	 $("#body_1-4_inner h4").toggle(function() {
		 
		  $(this).nextAll().show();
		  $(this).parent().addClass("open");
		  
		}, function() {
			
		  $(this).nextAll().hide();
		  $(this).parent().removeClass();
		  
		});
	 $('#subnav_2-1-x a').click(function(event){
										
		// switch out different sets of modules
		$('#side_2-1-x>div').hide();
		
		var thisTab = $(this).attr('href').substr(-2);
		
		$('#side_2-1-x div[id$="' + thisTab + '"]').show();
		
	 });

	// The following was to solve a weird problem with link going within the same page
 	 $('a.innerlink').click(function(event){
		var thisTab = $(this).attr('href').substr(-2);
		var parts =  document.location.href.split("?")
		document.location = parts[0] + "#tab-" + thisTab;
	 });

	 // send to a friend email overlay
	$('#open-staf, .ftr-staf').click(function(event){
		event.preventDefault();
		if ( location.href.match('why-vaccinate-fact') )
		{
			$('div#staf-overlay').css({padding:'20px 0 0 0'});
			$('#staf-overlay').css({top:'0px'});
		} else {
			$('#staf-overlay').css({top:(parseInt($(window).scrollTop()))+'px'});
			$('#staf-overlay').css({padding:($(window).height()/6)+'px 0 0 0'});
		}
		$('#staf-overlay').css({width:(parseInt($(window).width()))+'px'});
		$('#staf-bg').css({display:'block',height:($(document).height())+'px'});
	});	
	$('#close-staf, #staf-bg').click(function(){
		$('#staf-overlay').css({width:'980px'});
		$('#staf-overlay').css({top:'-1200px'});
		$('#staf-bg').css({display:'none'});
	});	
	// send to a friend email switch version per page
	var section11=/why-vaccinate/gi;
	if (window.location.pathname.match(section11))
	{
		/* see below for details on which staf form is being rendered */
	} else {
		$('a.link_popup').attr('id', 'staf-email');
	}
	/* staf - form render generic, why vaccinate, and factsheet specific  */
	$('a#staf-email').click(function(){
		$('#email-form').attr('src', 'staf.html');
		$('#email-form').attr('class', 'staf-site');
	});	
	if (location.href.match ('why-vaccinate.cfm'))
	{
		$('div#share a.link_popup').click(function(){
			$('#email-form').attr('src', 'staf-1-1.html');
			$('#email-form').attr('class', 'staf-index');
		});
	}
	$('a#fact01-email').click(function(){
		$('#email-form').attr('src', 'email-fact01.html');
		$('#email-form').attr('class', 'staf-infographic');
	});
	$('a#fact02-email').click(function(){
		$('#email-form').attr('src', 'email-fact02.html');
		$('#email-form').attr('class', 'staf-infographic');
	});
	$('a#fact03-email').click(function(){
		$('#email-form').attr('src', 'email-fact03.html');
		$('#email-form').attr('class', 'staf-infographic');
	});
	$('a#fact04-email').click(function(){
		$('#email-form').attr('src', 'email-fact04.html');
		$('#email-form').attr('class', 'staf-infographic');
	});
	$('a#fact05-email').click(function(){
		$('#email-form').attr('src', 'email-fact05.html');
		$('#email-form').attr('class', 'staf-infographic');
	});
	$('a#fact06-email').click(function(){
		$('#email-form').attr('src', 'email-fact06.html');
		$('#email-form').attr('class', 'staf-infographic');
	});
	$('a#fact07-email').click(function(){
		$('#email-form').attr('src', 'email-fact07.html');
		$('#email-form').attr('class', 'staf-infographic');
	});
	$('a#fact08-email').click(function(){
		$('#email-form').attr('src', 'email-fact08.html');
		$('#email-form').attr('class', 'staf-infographic');
	});
	$('a#fact09-email').click(function(){
		$('#email-form').attr('src', 'email-fact09.html');
		$('#email-form').attr('class', 'staf-infographic');
	});
	$('a#fact10-email').click(function(){
		$('#email-form').attr('src', 'email-fact10.html');
		$('#email-form').attr('class', 'staf-infographic');
	});
	$('a#fact11-email').click(function(){
		$('#email-form').attr('src', 'email-fact11.html');
		$('#email-form').attr('class', 'staf-infographic');
	});
	$('a#fact12-email').click(function(){
		$('#email-form').attr('src', 'email-fact12.html');
		$('#email-form').attr('class', 'staf-infographic');
	});

	/*  for ie 6 removes twitter */
	if($.browser.msie && $.browser.version=="6.0") {
		$('div.dropdown-container a.active, div.dropdown-container a#nav-eo').css({color:'#fdc871'});
		$('span#section-twitter').empty(); 
		$('a[href$="twitter.com/share"]').remove();
	}

	/*  for ie 6 removes twitter */	
	/*  set fb likes */
	$('span#fact01').append('<iframe class="like-1-1" src="http://www.facebook.com/plugins/like.php?href=http://immyounity.com/why-vaccinate-fact-01.cfm&amp;layout=button_count" scrolling="no" frameborder="0" allowtransparency="true"></iframe>');		
	$('span#fact02').append('<iframe class="like-1-1" src="http://www.facebook.com/plugins/like.php?href=http://immyounity.com/why-vaccinate-fact-02.cfm&amp;layout=button_count" scrolling="no" frameborder="0" allowtransparency="true"></iframe>');
	$('span#fact03').append('<iframe class="like-1-1" src="http://www.facebook.com/plugins/like.php?href=http://immyounity.com/why-vaccinate-fact-03.cfm&amp;layout=button_count" scrolling="no" frameborder="0" allowtransparency="true"></iframe>');
	$('span#fact04').append('<iframe class="like-1-1" src="http://www.facebook.com/plugins/like.php?href=http://immyounity.com/why-vaccinate-fact-04.cfm&amp;layout=button_count" scrolling="no" frameborder="0" allowtransparency="true"></iframe>');
	$('span#fact05').append('<iframe class="like-1-1" src="http://www.facebook.com/plugins/like.php?href=http://immyounity.com/why-vaccinate-fact-05.cfm&amp;layout=button_count" scrolling="no" frameborder="0" allowtransparency="true"></iframe>');
	$('span#fact06').append('<iframe class="like-1-1" src="http://www.facebook.com/plugins/like.php?href=http://immyounity.com/why-vaccinate-fact-06.cfm&amp;layout=button_count" scrolling="no" frameborder="0" allowtransparency="true"></iframe>');
	$('span#fact07').append('<iframe class="like-1-1" src="http://www.facebook.com/plugins/like.php?href=http://immyounity.com/why-vaccinate-fact-07.cfm&amp;layout=button_count" scrolling="no" frameborder="0" allowtransparency="true"></iframe>');
	$('span#fact08').append('<iframe class="like-1-1" src="http://www.facebook.com/plugins/like.php?href=http://immyounity.com/why-vaccinate-fact-08.cfm&amp;layout=button_count" scrolling="no" frameborder="0" allowtransparency="true"></iframe>');
	$('span#fact09').append('<iframe class="like-1-1" src="http://www.facebook.com/plugins/like.php?href=http://immyounity.com/why-vaccinate-fact-09.cfm&amp;layout=button_count" scrolling="no" frameborder="0" allowtransparency="true"></iframe>');
	$('span#fact10').append('<iframe class="like-1-1" src="http://www.facebook.com/plugins/like.php?href=http://immyounity.com/why-vaccinate-fact-10.cfm&amp;layout=button_count" scrolling="no" frameborder="0" allowtransparency="true"></iframe>');
	$('span#fact11').append('<iframe class="like-1-1" src="http://www.facebook.com/plugins/like.php?href=http://immyounity.com/why-vaccinate-fact-11.cfm&amp;layout=button_count" scrolling="no" frameborder="0" allowtransparency="true"></iframe>');
	$('span#fact12').append('<iframe class="like-1-1" src="http://www.facebook.com/plugins/like.php?href=http://immyounity.com/why-vaccinate-fact-12.cfm&amp;layout=button_count" scrolling="no" frameborder="0" allowtransparency="true"></iframe>');
	/*  set fb likes */
	// cross link boxes - on tab click
	$('a[href*="?view=tab-01"]').click(function(){
		$('#side_2-1-x_set-01').show();
		$('#side_2-1-x_set-02, #side_2-1-x_set-03, #side_2-1-x_set-04').hide();
	});
	$('a[href*="?view=tab-02"]').click(function(){
		$('#side_2-1-x_set-02').show();
		$('#side_2-1-x_set-01, #side_2-1-x_set-03, #side_2-1-x_set-04').hide();
	});
	$('a[href*="?view=tab-03"]').click(function(){
		$('#side_2-1-x_set-03').show();
		$('#side_2-1-x_set-01, #side_2-1-x_set-02, #side_2-1-x_set-04').hide();
	});
	$('a[href*="?view=tab-04"]').click(function(){
		$('#side_2-1-x_set-04').show();
		$('#side_2-1-x_set-01, #side_2-1-x_set-02, #side_2-1-x_set-03').hide();
	});
	// cross link boxes - on url
	if ($('a[href*="?view=tab-01"]').hasClass('active'))
	{
		$('#side_2-1-x_set-01').show();
		$('#side_2-1-x_set-02, #side_2-1-x_set-03, #side_2-1-x_set-04').hide();
	}
	if ($('a[href*="?view=tab-02"]').hasClass('active'))
	{
		$('#side_2-1-x_set-02').show();
		$('#side_2-1-x_set-01, #side_2-1-x_set-03, #side_2-1-x_set-04').hide();
	}
	if ($('a[href*="?view=tab-03"]').hasClass('active'))
	{
		$('#side_2-1-x_set-03').show();
		$('#side_2-1-x_set-01, #side_2-1-x_set-02, #side_2-1-x_set-04').hide();
	}
	if ($('a[href*="?view=tab-04"]').hasClass('active'))
	{
		$('#side_2-1-x_set-04').show();
		$('#side_2-1-x_set-01, #side_2-1-x_set-02, #side_2-1-x_set-03').hide();
	}
	// cross link boxes 
	/*  for ie 7 css fixes */
	if (location.href.match ('vaccinations.cfm'))
	{
		$('#subnav_2-5 a').css ({height:'38px'});
		if($.browser.msie && $.browser.version=="7.0") {
			$('#body_content_2-5').css ({border:'0px'});
		}
	}
	if (location.href.match ('vaccine-questions-answers.cfm'))
	{
		if($.browser.msie && $.browser.version=="7.0") {
			$('.body_content').css ({border:'0px'});
		}
	}
});
