$.fn.titleBg = function() {
    return $(this).each(function() {
      var h1scrolltop = $(this).offset().top;

      $(this).lettering('words').children('span').each(function(i){
        console.log(h1scrolltop);
        if($(this).offset().top > h1scrolltop) {
          h1scrolltop = $(this).offset().top;
          $(this).parent().children('span').eq(i-1).addClass('last-of-line');

          // double check offset()
          if( $(this).parent().children('span').eq(i-1).offset().top  == h1scrolltop ) {
            $(this).parent().children('span').eq(i-1).removeClass('last-of-line');
            $(this).parent().children('span').eq(i-2).addClass('last-of-line');
          }
        }
      });
    });
  }

$(function() {

  var pathArray = window.location.href.split( '/' );
  var baseURL = 'http://' + pathArray[2];

  $.fn.ToggleInputValue = function(){
    return $(this).each(function(){
      var Input = $(this);
      var default_value = Input.val();
      
      Input.focus(function() {
        if(Input.val() == default_value) {
          Input.val("");
          $(this).css({ color: "#000000" });
          $(this).next().css({
            color: "#686868"
          });
        } else {
          $(this).css({ color: "#000000" });
          $(this).next().css({
            color: "#686868"
          });
        }
      }).blur(function() {
        if(Input.val().length == 0) {
          Input.val(default_value);
          $(this).css({ 
            color: "#6e7178",
            background: "#ffffff url('" + baseURL + "/assets/images/star-form.png') 0 -90px no-repeat"
          });
          $(this).next().css({
            color: "#b6b7bb"
          });
        } else {
          $(this).css({ 
            color: "#000000",
            background: "#ffffff url('" + baseURL + "/assets/images/star-form.png') 0 -45px no-repeat" 
          });
          $(this).next().css({
            color: "#b6b7bb"
          });
        }
      });
    });
  }

  $.fn.vAlign = function() {
    return this.each(function() {
      var h = $(this).height();
      var ph = 533;
      var mt = (ph - h) / 2;
      $(this).css("margin-top", mt + "px");
    });
  }

  $.fn.vAlign2 = function() {
    return this.each(function() {
      var h = $(this).height();
      var ph = 396;
      var mt = (ph - h) / 2;
      $(this).css("margin-top", mt + "px");
    });
  }

  $.fn.magicArrow = function() {
    var el, elWidth, newWidth, leftPos;
    var mainNav = $(".sub-nav .nav");
    var magicArrow = $(".sub-nav-active");

    elWidth = $(".sub-nav .nav li.active").width();
    w = mainNav.width();
    a = $(".sub-nav-active").width();
    leftPos = (elWidth - a) / 2;

    magicArrow.animate({
      left: leftPos
    });

  }

  $.fn.magicDots = function() {
    return this.each(function() {
      var lWidth = $(this).prev().outerWidth();
      var rWidth = $(this).next().outerWidth();
      var nWidth = 227 - (lWidth + rWidth + 20);

      $(this).css("width", nWidth + "px")
    });
  }

  $(".careers-box ul li span.dots").magicDots();

  $(".tour-list .tour-left .col1, .tour-list .tour-left .col2, .tour-list .tour-right .col2, .tour-list .tour-right .col3").vAlign();

  $(".tour-list #analytics .col2, .tour-list #analytics .col3").vAlign2();

  $('.signup-form form input').ToggleInputValue();
  
  $('.form6 form input').ToggleInputValue();

  jQuery.validator.addMethod("defaultInvalid", function(value, element) {
    if (element.value == element.defaultValue) {
      return false;
    }
      return true;
  });

  jQuery.validator.addMethod("border", function(value, element) {
    if (element.value == element.defaultValue) {
      $(this).css({ border: "1px solid #fb431e" })
      return false;
    }
      return true;
  });

  jQuery.validator.addMethod("isChecked", function(value, element) {
    return element.checked;
  });

  var skipErrors,
    $form = $('#signup-form'),
    validator = $form.validate({
    rules: {
      last_name: "required defaultInvalid border",
      company: "required defaultInvalid",
      '00NE0000000SpvM': "required defaultInvalid",
      '00NE0000000SpvR': "required defaultInvalid",
      email: {
        required: true,
        email: true
      },
      email_confirm: {
        required: true,
        equalTo: "#email"
      },
      terms: "isChecked"
    },
    messages: {
      last_name: "This field is required",
      company: "This field is required.",
      '00NE0000000SpvM': "This field is required.",
      '00NE0000000SpvR': "This field is required.",
      email: "Must be a valid email address.",
      email_confirm: "Does not match the above Email Address. Give it another try.",
      terms: "You must agree to the Trial Terms & Conditions."
    },
    focusInvalid: false,
    errorPlacement: function( $err, $elem ) {
        if ( skipErrors ) {
            return;
        }
        // if ( $elem[0].name === 'terms' ) {
        //     $err.insertAfter( $elem.next('label') );
        // } else {
            $err.insertAfter( $elem );
        // }
    }
  });
/*

  var skipErrors,
    $form = $('#form6'),
    validator = $form.validate({
    rules: {
      Field9: "required defaultInvalid border",
      Field10: "required defaultInvalid",
      Field20: "required defaultInvalid",
      Field12: "required defaultInvalid",
      Field11: {
        required: true,
        email: true
      },
     messages: {
      Field9: "This field is required",
      Field10: "This field is required.",
      Field20: "This field is required.",
     Field12: "This field is required.",
     Field11: "Must be a valid email address.",
      },
    focusInvalid: false,
    errorPlacement: function( $err, $elem ) {
        if ( skipErrors ) {
            return;
        }
        // if ( $elem[0].name === 'terms' ) {
        //     $err.insertAfter( $elem.next('label') );
        // } else {
            $err.insertAfter( $elem );
        // }
    }
  });

*/

  $(".signup .star").css("visibility", "hidden");

  var checkForm = function() {
    // Temporarily override showing the errors
    skipErrors = true;
    validator.settings.errorClass = 'skipped';
    // Check the whole form
    if ( validator.form() ) {
        // Add valid class if valid
        $form.addClass('valid');
        $(".signup .star").css("visibility", "visible");
    } else if ( $form.hasClass('valid') ) {
        $form.removeClass('valid');
        $(".signup .star").css("visibility", "hidden");
    }
    skipErrors = false;
    validator.settings.errorClass = 'error';
  };

  // Check the whole form as inputs change
  $('input[type="text"]').on('focusin focusout keyup', checkForm);
  $('input[type="checkbox"]').on('click', checkForm);

  // $("#signup-form input").blur(function() {
  //   alert("Valid: " + $("#signup-form").valid());
  //   return false;
  // });

  //$(".sub-nav .nav").append("<div class='sub-nav-active'></div>");

  $('.nav-tour, .features .sub-nav-wrap, .api-information .api-security .terciary-nav, .careers-detail .sidebar, .blog.single .arrow-prev, .blog.single .arrow-next').scrollToFixed({
    limit: $('.top'),
    zIndex: 999,
  });

  $('.blog.single .arrow-prev-wrap, .blog.single .arrow-next-wrap').scrollToFixed({
    zIndex: 999,
  });

  $('.nav-tour a, .top a, .row-tour .cta-box a.btn-blue').smoothScroll({
    offset: -82
  });

  if($(".features .sub-nav .nav")[0]) {
    $('.sub-nav .nav').onePageNav({
      currentClass: 'active',
      scrollOffset: '118'
    });
  }

  if($(".api-security .terciary-nav")[0]) {
    $(".api-security .terciary-nav").onePageNav({
      currentClass: 'active',
      scrollOffset: '30'
    });
  }

 if($(".api-information .terciary-nav")[0]) {
    $(".api-information .terciary-nav").onePageNav({
      currentClass: 'active',
      scrollOffset: '30'
    });
  }

  if($(".careers-detail") [0]) {
    $(".careers-detail .sidebar").onePageNav({
      currentClass: 'active',
      scrollOffset: '0'
    });
  }

  if($(".pricing") [0]) {
    $(".hero .col2, .hero .col4").onePageNav();
  }

  // $(".post-wrapper:nth-child(3n+3)").after("<div class='clearfix'></div>");

  $(".h1-wrapper h1 a").titleBg();
  
  $(".blog .main-nav .nav li").hover(function() {
    $(this).children(".dropdown").show();
  },
    function() {
      $(this).children(".dropdown").hide();
    }
  );
  


  

    // $('.sub-nav-active').bind('activeClass', function() {
    //   //$("sub-nav .nav").magicArrow();
    //   var e = $(this).val();
    //   alert(e);
    // });

  

  // if ($(".sub-nav .nav li").hasClass("active")) {
  //   //$(this).append("<div class='sub-nav-active'></div>");
  //   var v = $(this).html();
  //   alert(v);
  // }

  // $(".sub-nav .nav li").click(function() {
  //   //$(this).append("<div class='sub-nav-active'></div>");
  //   var v = $(this).html();
  //   alert(v);
  // });

  // $.fn.arrowClass = function() {
  //   return $(this).each(function() {
  //     //var elem = $(this);
  //     var w = $(this).width();

  //     if($(this).hasClass("active")) {
  //       $(this).append("<div class='sub-nav-active'></div>");
  //     }

  //     var elemW = $(".sub-nav-active").width();
  //     var newW = (w - elemW) / 2;

  //     $(".sub-nav-active").css("left", newW + "px")
  //     alert(newW);
  //   });

  // }

  // $(".sub-nav .nav li").arrowClass();


  // function initSubnav() {
  //   var $el, leftPos, newWidth;
  //     $mainNav = $(".sub-nav .nav");
    
  //   $mainNav.append("<div class='sub-nav-active'></div>");
    
  //   var $magicArrow = $(".sub-nav-active");
    
  //   $magicArrow
  //     .width($(".active").width())
  //     //.height($mainNav2.height())
  //     .css("left", $(".active a").position().left)
  //     .data("origLeft", $(".active a").position().left)
  //     .data("origWidth", $margicArrow.width())
                
  //   $(".sub-nav .nav a").click(function() {
  //     $el = $(this);
  //     leftPos = $el.position().left;
  //     newWidth = $el.parent().width();
  //     $magicArrow.stop().animate({
  //         left: leftPos,
  //         width: newWidth,
  //     })
  //   }, function() {
  //     $magicArrow.stop().animate({
  //       left: $magicArrow.data("origLeft"),
  //       width: $magicArrow.data("origWidth"),
  //     });    
  //   });
    
  //   /* Kick IE into gear */
  //   $(".active a").mouseenter();
  // }

  

  




});

