jQuery(document).ready(function($){
  
  Cufon.replace('#dropDown, #options li, #contentHome h3', { fontFamily: 'Avenir' });
  
  $("#dropDownGlow, #goButtonGlow").mouseenter(function(){
    $(this).stop().animate({"opacity": 1}, 600);
  }).mouseleave(function(){
    $(this).stop().animate({"opacity": 0}, 400);
  });
  
  var dropDownPos = "closed";
  var dropSpeed = 400;
  $("#dropDownGlow, #dropDown").mouseenter(function(){
    if(dropDownPos == "closed") {
      toggleDropDown();
    }
  });
  $("#options").mouseleave(toggleDropDown);
  function toggleDropDown() {
    if(dropDownPos == "closed"){
      dropDownPos = "open";
      $("#dropDown").addClass("active");
      $("#options").stop().animate({"height":"186"},dropSpeed,"easeOutSine",function(){
        $('body').bind('click', function(){
          dropDownPos = "closed";
          $("#dropDown").removeClass("active");
          $("#options").stop().animate({"height":"0"},dropSpeed,"easeInSine");
          $('body').unbind();
        });
      });
    } else {
      dropDownPos = "closed";
      $("#dropDown").removeClass("active");
      $("#options").stop().animate({"height":"0"},dropSpeed,"easeInSine");
      $('body').unbind();
    }
  }
  
    $("#dropDownGlow").stop().animate({"opacity": 1}, 1000);
    setTimeout(function(){ $("#dropDownGlow").stop().animate({"opacity": 0}, 1000); },1000);
    setTimeout(function(){ $("#dropDownGlow").stop().animate({"opacity": 1}, 1000); },2000);
    setTimeout(function(){ $("#dropDownGlow").stop().animate({"opacity": 0}, 1000); },3000);
    setTimeout(function(){ $("#dropDownGlow").stop().animate({"opacity": 1}, 1000); },4000);
    setTimeout(function(){ $("#dropDownGlow").stop().animate({"opacity": 0}, 1000); },5000);
  
  // if($.cookie('selectedDropDown') != null) {
  //   $("#options .active").removeClass("active");
  //   $("#options #"+$.cookie('selectedDropDown')).addClass("active");
  //   $("#dropDown").text($("#options .active").text());
  //   Cufon.replace('#dropDown, #options li', { fontFamily: 'Avenir' });
  // }
  
  $("#options li").click(function(){
    if($(this).hasClass("active")){}else{
      $("#options .active").removeClass("active");
      $(this).stop().animate({"paddingLeft": "0px"}, 300);
      $(this).addClass("active");
      $("#dropDown").text($(this).text());
      Cufon.replace('#dropDown, #options li', { fontFamily: 'Avenir' });
      toggleDropDown();
      // $.cookie('selectedDropDown', $(this).text().replace("For ","").toLowerCase());
    }
  });
  
  function reposition() {
    if($("html").height() > $(window).height()) {
      $("#lightBoxes").css("height",$("html").height());
    } else {
      $("#lightBoxes").css("height",$(window).height());
    }
    $("#lightBoxes > div").each(function(){
      $(this).css("marginTop",($(window).height()-$(this).outerHeight())/2);
      $(this).css("top",$(window).scrollTop());
    });
    
  }
  function viewLogin(event) {
    $("#lightBoxes").show();
    $("#login").show();
    reposition();
  }
  
  $("#goButtonGlow, #goButton").click(function(){
    var forwhom = $("#dropDown").text().replace("For ","").toLowerCase();
    if(forwhom == "employers") {
      window.location="/employers";
    } else if (forwhom == "employees") {
      viewLogin();
    } else if (forwhom == "growth") {
      window.location="/growth";
    } else {
      window.location="/insurance-"+forwhom;
    }
  });
  
});
