$(document).ready(function() {
  
  $('a.tab-link').click(function() { 
    var divToShow = $(this).attr("href");
    var anchor = divToShow.split('#')[1];
    var selectedSection = $(this).parent().parent();
    selectedSection.slideUp();
    selectedSection.removeClass('active-section');
    $(divToShow).slideDown();
    $(divToShow).addClass('active-section');
    selectNavBar(anchor);
    $.scrollTo($('#stepHeading'))
    document.location.hash = anchor;
    return(false);
  });
  
  var q = $.parseQuery();
  if(q.aid != null){
    set_bm_cookies(q);
  }

});

function selectNavBar(anchorString){
  $('div.selected').removeClass('selected');
  $('div.sidebar_btn').each(function(i){
    linkHref = $(this).children().children().attr("href")
    if(linkHref != undefined && linkHref.split('#')[1] == anchorString ){
      $(this).parent().addClass('selected');
    };
  });
};

function chooseSectionToShow(){
  $('div.section').each(function(i){ $(this).hide();});

  var url = document.location.toString();
  if (url.match('#')) { // the URL contains an anchor
    var anchor = url.split('#')[1];
    var toShow = $('#'+ anchor);
  } else {
    var toShow = $('#cover'); // default section
  };
  toShow.show();
  toShow.addClass('active-section');
  if(anchor != undefined){selectNavBar(anchor)}; 
};

function quoteNavBarLinks(){
  $('div.sidebar_btn p a').click(function(){
    // select the right nav bar
    var selectedOrNot = $(this).parent().parent().parent();
    if(selectedOrNot.hasClass('selected')){
      //nothing really since we are on the selected section already
    }else{
      // show the correct section
      anchor = $(this).attr("href").split('#')[1];
      selectedSection = $('#'+ anchor);
      selectedSection.slideDown();
      $('div.active-section').hide();
      $('div.active-section').removeClass('active-section');
      selectedSection.addClass('active-section');
      $('div.selected').removeClass('selected');
      selectedOrNot.addClass('selected')
      document.location.hash = anchor;
    }
    return(false);
  });
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

jQuery.parseQuery = function(qs,options) {
	var q = (typeof qs === 'string'?qs:window.location.search), o = {'f':function(v){return unescape(v).replace(/\+/g,' ');}}, options = (typeof qs === 'object' && typeof options === 'undefined')?qs:options, o = jQuery.extend({}, o, options), params = {};
	jQuery.each(q.match(/^\??(.*)$/)[1].split('&'),function(i,p){
		p = p.split('=');
		p[1] = o.f(p[1]);
		params[p[0]] = params[p[0]]?((params[p[0]] instanceof Array)?(params[p[0]].push(p[1]),params[p[0]]):[params[p[0]],p[1]]):p[1];
	});
	return params;
}

function set_bm_cookies(q){
 // collect params passed by google ads
 // aid (Account ID, ex: SFB), c (campaign name), ag (ad group), and kw (keyword)
 createCookie('a_id', q.aid, 1);
 createCookie('c_name', q.c, 1);
 createCookie('a_group', q.ag, 1);
 createCookie('keyword', q.kw, 1);
}