$(document).ready(function() {

  // Superfish top-nav rollover menus
  // hoverClass:    'sfHover',          // the class applied to hovered list items
  // pathClass:     'overideThisToUse', // the class you have applied to list items that lead to the current page
  // pathLevels:    1,                  // the number of levels of submenus that remain open or are restored using pathClass
  // animation:     {opacity:'show'},   // an object equivalent to first parameter of jQuery’s .animate() method
  // disableHI:     false,              // set to true to disable hoverIntent detection
  // onInit:        function(){},       // callback function fires once Superfish is initialised – 'this' is the containing ul
  // onBeforeShow:  function(){},       // callback function fires just before reveal animation begins – 'this' is the ul about to open
  // onShow:        function(){},       // callback function fires once reveal animation completed – 'this' is the opened ul
  // onHide:        function(){}        // callback function fires after a sub-menu has closed – 'this' is the ul that just closed

  $("ul.sf-menu").superfish({
    delay:         100,                // the delay in milliseconds that the mouse can remain outside a submenu without it closing
    speed:         'fast',           // speed of the animation. Equivalent to second parameter of jQuery’s .animate() method
    autoArrows:    false,               // if true, arrow mark-up generated automatically = cleaner source code at expense of initialisation performance
    dropShadows:   false               // completely disable drop shadows by setting this to false
  });

  // Hover over for results page Doctor's login-hover
  if ($("#hover-login").length > 0) {
    $("#hover-login").hover(
      function() { // when hovering
        $("#login").fadeIn(150);
        $("#login").removeClass("hidden");
        $("#hover-login-link").fadeOut(150);
      },
      function() { // when lost hover
        $("#login").fadeOut(150);
        $("#hover-login-link").fadeIn(150);
      }
    );

    // Kill the default action for the results WLS link since we're just showing the bubble
    $("#hover-login").click(function(e) {
      e.preventDefault();
      return false;
    });
  }

  // If the overlay exists (ie, we're on the home page) and gets clicked on, show an alert
  if ($("#submit-overlay").length > 0) {
    $("#submit-overlay").click(function(e) {
      alert("Email address is required");
      $("#UserEmail").css("border-color", "#FF6600");
      $("#UserEmail").css("border-width", "2px");
      $("#UserEmail").css("background-color", "#FFF6D2");
      e.preventDefault();
    });
  }

  // If there's some characters in the email box, remove the overlay so the button can be clicked
  if ($("#UserEmail").length > 0) {
    $("#UserEmail").keyup(function(e) {
      var email = $("#UserEmail").val();
      if (email) {
        $("#submit-overlay").css("width", "1px");
      } else {
        $("#submit-overlay").css("width", "370px");
      }
    });
  }

  // create a cookie for submitted values
  function create_cookie() {
    var email = $("#UserEmail").val();
    if (email) {
      // Set the cookie with results (using the jquery plugin jquery.cookie.js) so we can retrieve it later
      var COOKIE_NAME = 'bonfire_answers';
      var cookie_options = { path: '/', domain: 'bonfirehealth.com', expires: 2 }; // expires in 2 days

      // Get the form values
      var age = $("#UserAge").val();
      var heightFt = $("#UserHeight-feet").val();
      var heightIn = $("#UserHeight-inches").val();
      var height = '';
      if (heightFt) {
        height = heightFt+' ft';
        if (heightIn) {
          height = height+' '+heightIn+' in';
        }
      }
      var weight = $("#UserWeight").val();
      var gender = $("input[@id='UserGender']:checked").val();

      // Replace any previous values already saved from the cookie (if any)
      var values = '"email":"'+email+'"';
      var option_values = '"age":"'+age+'", "height":"'+height+'", "weight":"'+weight+'", "gender":"'+gender+'"'
      // Add the concatonated values to the cookie (replacing current ones)
      $.cookie(COOKIE_NAME, values, cookie_options);
      $.cookie('bonfire_options', option_values, cookie_options);
    }
  }

  // home page promo form button clicked or form submitted
  if ($("#pre-survey").length > 0) {
    $("#submit-button").click(function(e) {
      create_cookie();
      e.preventDefault();
    });
    $("#pre-survey").submit(function(e) {
      create_cookie();
      e.preventDefault();
    });
  }

  $(".resizable").each(function() {
    var t = this.tagName.toLowerCase();
    switch(t) {
      case "h1":
        $(this).attr("style", "font-size: 1.6em;");
        break;
      case "h2":
        $(this).attr("style", "font-size: 1.5em;");
        break;
      case "h3":
        $(this).attr("style", "font-size: 1.4em;");
        break;
      case "h4":
        $(this).attr("style", "font-size: 1.3em;");
        break;
      case "h5":
        $(this).attr("style", "font-size: 1.2em;");
        break;
      case "h6":
        $(this).attr("style", "font-size: 1.2em;");
        break;
      case "div":
        if ($(this).attr("id") == "breadcrumbs") {
          $(this).attr("style", "font-size: 12px;");
        } else {
          $(this).attr("style", "font-size: 1.2em;");
        }
        break;
      default:
        $(this).attr("style", "font-size: 1.2em;");
        break;
    }
  });

  // createCookie and readCookie are thanks to AListApart.com
  // http://www.alistapart.com/articles/alternate/
  // Create Cookie
  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 expires = "";
    document.cookie = name+"="+value+expires+"; path=/";
  }

  // Read Cookie
  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;
  }

  // Looks for the variable for cancelling an account in the url and just displays a message, that's all.
  function cancelMsg() {
    // Code used from: http://ajaxcssblog.com/jquery/url-read-request-variables/
    paramsRaw = (document.location.href.split("?", 2)[1] || "").split("#")[0].split("&") || [];
    for(var i = 0; i< paramsRaw.length; i++) {
      var single = paramsRaw[i].split("=");
      if (single[0] == 'account_cancelled' && single[1] == 'true') {
        alert('Thank you for being a Bonfire member for as long as you were.  Your account has been cancelled successfully.');
      }
    }
  }
  cancelMsg();

});