$(document).ready ->
  parallaxElements = []
  windowHeight = 0

  parallax = (scrollTop) ->
    for element in parallaxElements
      #distance of element from top of viewport
      viewportOffsetTop = element.initialOffsetY - scrollTop;
      #distance of element from bottom of viewport
      viewportOffsetBottom = windowHeight - viewportOffsetTop;
      if ((viewportOffsetBottom >= element.start) && (viewportOffsetBottom <= element.stop)) 
        $(element.elm)
          .css({
            top: (-viewportOffsetBottom * 1.4) + 800
          })
      else if (viewportOffsetBottom > element.stop) 
      #scrolled past the stop value, make position relative again

        $(element.elm)
          .css({
            position: 'relative',
            top: -460,
            left: 'auto',
            marginLeft: 'auto'
          })
      else if (viewportOffsetBottom < element.start) 
      #scrolled up back past the start value, reset position

        $(element.elm)
          .css({
            position: 'relative',
            top: 100,
            left: 'auto',
            marginLeft: 'auto'
          })

  windowHeight = $(window).height()
  $("html,body").scrollTop(1)

  #touch event check stolen from Modernizr
  touchSupported = (('ontouchstart' in window) ||
              window.DocumentTouch && document instanceof DocumentTouch)

  #if touch events are supported, tie our animation to the position to these events as well
  if (touchSupported) 
    $(window)
      .bind('touchmove', (e) ->
        val = e.currentTarget.scrollY;
        parallax(val);
      )

  $(window).bind('scroll', (e) ->
    val = $(this).scrollTop()
    parallax(val)
    $getStarted = $("header.landingPage a.btn.getStarted")
    if val > 400
      if $getStarted.hasClass("notScrolled")
        $("header.landingPage a.btn.getStarted").removeClass("notScrolled").addClass("scrolled")
    else if val < 400
      if $getStarted.hasClass("scrolled")
        $("header.landingPage a.btn.getStarted").removeClass("scrolled").addClass("notScrolled")
  )

  $('.parallax').each ->
    $elm = $(this)
    id = $elm.data("id")

    newElement = {
      id: $elm.data("id"),
      start: $elm.data('start'),
      stop: $elm.data('stop'),
      speed: $elm.data('speed'),
      elm: $elm[0],
      initialOffsetY: $elm.offset().top,
      height: $elm.height(),
      width: $elm.outerWidth()
    }
    parallaxElements.push newElement


  #Howit works change
  $("ul.hiw a").click ->
    if !($(this).hasClass("active"))
      $("ul.hiw a").removeClass("active")
      $(this).addClass("active")
      id = $(this).attr("id")
      $("#threeBlocks .block").hide()
      $("#threeBlocks ##{id}.block").show()

  #VOS List change
  $("ul#VoSIcons a").click ->
    if !($(this).hasClass("active"))
      $("ul#VoSIcons a").removeClass("active")
      $(this).addClass("active")
      id = $(this).attr("id")
      $("#VoSList li").hide()
      $("#VoSList li##{id}").show()
      if $(window).scrollTop() < 300
        $('html, body').animate(
          scrollTop: ($(this).offset().top) - 60
        , 1250)

  #Show Carousel on Hover
  $(".carouselToggle").mouseover ->
    $("div.carouselNav").addClass("active")

  $(".carouselToggle").mouseleave ->
    $("div.carouselNav").removeClass("active")

  $("ul#VoSList div.carouselNav a.left").click ->
    active = $("ul#VoSIcons li a.active")
    activeId = active.attr("id")
    if activeId == "rate"
      active.removeClass("active")
      $("ul#VoSIcons li a#comments").addClass("active")
      $("#VoSList li").hide()
      $("#VoSList li#comments").show()
    else 
      activeItem = active.parent("li")
      prev = activeItem.prev("li")
      active.removeClass("active")
      prev.find("a").addClass("active")
      id = prev.find("a").attr("id")
      $("#VoSList li").hide()
      $("#VoSList li##{id}").show()

  $("ul#VoSList div.carouselNav a.right").click ->
    active = $("ul#VoSIcons li a.active")
    activeId = active.attr("id")
    if activeId == "comments"
      active.removeClass("active")
      $("ul#VoSIcons li a#rate").addClass("active")
      $("#VoSList li").hide()
      $("#VoSList li#rate").show()
    else 
      activeItem = active.parent("li")
      next = activeItem.next("li")
      active.removeClass("active")
      next.find("a").addClass("active")
      id = next.find("a").attr("id")
      $("#VoSList li").hide()
      $("#VoSList li##{id}").show()


  #toggle IWHIW
  toggleHIW = (id) ->
    if id == "analyze"
        $("#HIW h3").text("Analyze")
        $("p.hiw").text("Our VoS platform analyzes email programs to identify teams striving to deliver the best subscriber experience possible")
      else if id == "benchmark"
        $("#HIW h3").text("Benchmark")
        $("p.hiw").text("Subscriber experience metrics are individually benchmarked by specific industry verticals")
      else if id == "rate"
        $("#HIW h3").text("Rate")
        $("p.hiw").text("Brands receiving qualifying scores become eligible to display the InboxWorthy Seal")
      else if id == "monitor"
        $("#HIW h3").text("Monitor")
        $("p.hiw").text("InboxWorthy brands are monitored to ensure their ongoing commitment to the subscriber experience")

  $("#HIW div.carouselNav a.left").click ->
    active = $("ul.hiw li.active")
    activeId = active.attr("id")
    if activeId == "analyze"
      active.removeClass("active")
      $("ul.hiw li#monitor").addClass("active")
      toggleHIW("monitor")
    else 
      prev = active.prev("li")
      active.removeClass("active")
      prev.addClass("active")
      id = prev.attr("id")
      toggleHIW(id)

  $("#HIW div.carouselNav a.right").click ->
    active = $("ul.hiw li.active")
    activeId = active.attr("id")
    if activeId == "monitor"
      active.removeClass("active")
      $("ul.hiw li#analyze").addClass("active")
      toggleHIW("analyze")
    else 
      next = active.next("li")
      active.removeClass("active")
      next.addClass("active")
      id = next.attr("id")
      toggleHIW(id)

  #InboxWorthy How It Works
  $("ul.hiw li").click ->
    if !($(this).hasClass("active"))
      id = $(this).attr("id")
      $("ul.hiw li").removeClass("active")
      $(this).addClass("active")
      toggleHIW(id)


