$(document).ready ->
  

  #Set Intervals

  weekInterval = 120

  monthInterval = 76

  yearInterval = 70

  weekTicks = ["Dec 11", "Dec 12", "Dec 13", "Dec 14", "Dec 15", "Dec 16", "Dec 17"]

  monthTicks = ["Nov 17", "Nov 20", "Nov 23", "Nov 26", "Nov 29", "Dec 2", "Dec 5", "Dec 8", "Dec 11", "Dec 14", "Dec 17"]

  threeMonthTicks = ["Sep 18", "Sep 27", "Oct 6", "Oct 15", "Oct 24", "Nov 2", "Nov 11", "Nov 20", "Nov 29", "Dec 8", "Dec 17"]

  sixMonthTicks = ["Jun 21", "Jul 9", "Jul 27", "Aug 14", "Sep 1", "Sep 19", "Oct 7", "Oct 25", "Nov 12", "Nov 30", "Dec 17"]

  yearTicks = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]

  barTicks = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sun"]

  messageTicks = ["1", "2", "3", "4", "5"]

  ratingsTicks = ["1", "2", "3", "4", "5"]

  typeTicks = ["Type 1", "Type 2", "Type 3", "Type 4"]

  timeTicks = ["0h", "6h", "12h", "18h", "24h"]

  npsTicks = ["-100", "-50", "0", "50", "100"]

  reviewsTicks = ["veryunhappy", "unhappy", "neutral", "happy", "veryhappy"]
  
  unsubTicks = ["0", "15%", "30%", "45%", "60%"]

  frequencyData = [{ "x": 0,   "y": 1}, { "x": 1,  "y": 2},
                { "x": 2,  "y": 5}, { "x": 3,  "y": 4},
                { "x": 4,  "y": 3}, { "x": 5, "y": 1},
                { "x": 6,  "y": 5}];

  typeData = [{ "x": 0,   "y": 2}, { "x": 1,  "y": 3},
                { "x": 2,  "y": 3}, { "x": 3,  "y": 3},
                { "x": 4,  "y": 2}, { "x": 5, "y": 2},
                { "x": 6,  "y": 3}];

  type2Data = [{ "x": 0,   "y": 2}, { "x": 1,  "y": 4},
                { "x": 2,  "y": 3}, { "x": 3,  "y": 3},
                { "x": 4,  "y": 2}, { "x": 5, "y": 2},
                { "x": 6,  "y": 3}]; 

  dateBarData = [2, 3, 2.3, 4.2, 2.1, 1, 3.7]

  timeData = [{ "x": 0,   "y": 1}, { "x": 1,  "y": 5},
                { "x": 2,  "y": 2}, { "x": 3,  "y": 5},
                { "x": 4,  "y": 3}, { "x": 5, "y": 5},
                { "x": 6,  "y": 2}];

  satisfactionData = [{ "x": 0,   "y": 3.7}, { "x": 1,  "y": 3.76},
                { "x": 2,  "y": 3.6}, { "x": 3,  "y": 3.68},
                { "x": 4,  "y": 3.9}, { "x": 5, "y": 4},
                { "x": 6,  "y": 4.1}];

  ratingsData = [{ "x": 0,   "y": 3.4}, { "x": 1,  "y": 2.9},
                { "x": 2,  "y": 3.6}, { "x": 3,  "y": 2.7},
                { "x": 4,  "y": 3.1}, { "x": 5, "y": 3.3},
                { "x": 6,  "y": 3.2}];

  valueData = [{ "x": 0,   "y": 2}, { "x": 1,  "y": 1},
                { "x": 2,  "y": 3}, { "x": 3,  "y": 2},
                { "x": 4,  "y": 1}, { "x": 5, "y": 2},
                { "x": 6,  "y": 3}];

  frequencyRData = [{ "x": 0,   "y": 4}, { "x": 1,  "y": 1},
                { "x": 2,  "y": 2}, { "x": 3,  "y": 2},
                { "x": 4,  "y": 3}, { "x": 5, "y": 5},
                { "x": 6,  "y": 5}];

  relevancyData = [{ "x": 0,   "y": 1}, { "x": 1,  "y": 2},
                { "x": 2,  "y": 3}, { "x": 3,  "y": 2},
                { "x": 4,  "y": 1}, { "x": 5, "y": 1},
                { "x": 6,  "y": 5}];

  designData = [{ "x": 0,   "y": 1}, { "x": 1,  "y": 3},
                { "x": 2,  "y": 2}, { "x": 3,  "y": 3},
                { "x": 4,  "y": 2}, { "x": 5, "y": 5},
                { "x": 6,  "y": 2}];

  reviewsData = [{ "x": 0,   "y": 3.1}, { "x": 1,  "y": 4.5},
                { "x": 2,  "y": 4.7}, { "x": 3,  "y": 4.85},
                { "x": 4,  "y": 5}, { "x": 5, "y": 4.8},
                { "x": 6,  "y": 5}];

  survey1Data = [{ "x": 0,   "y": 3}, { "x": 1,  "y": 2},
                { "x": 2,  "y": 1}, { "x": 3,  "y": 3},
                { "x": 4,  "y": 4}, { "x": 5, "y": 1},
                { "x": 6,  "y": 1}];

  survey2Data = [{ "x": 0,   "y": 5}, { "x": 1,  "y": 2},
                { "x": 2,  "y": 1}, { "x": 3,  "y": 3},
                { "x": 4,  "y": 2}, { "x": 5, "y": 1},
                { "x": 6,  "y": 1}];

  survey3Data = [{ "x": 0,   "y": 2}, { "x": 1,  "y": 1},
                { "x": 2,  "y": 2}, { "x": 3,  "y": 3},
                { "x": 4,  "y": 2}, { "x": 5, "y": 3},
                { "x": 6,  "y": 2}];

  survey4Data = [{ "x": 0,   "y": 3}, { "x": 1,  "y": 5},
                { "x": 2,  "y": 1}, { "x": 3,  "y": 5},
                { "x": 4,  "y": 4}, { "x": 5, "y": 3},
                { "x": 6,  "y": 3}];

  survey5Data = [{ "x": 0,   "y": 4}, { "x": 1,  "y": 1},
                { "x": 2,  "y": 4}, { "x": 3,  "y": 1},
                { "x": 4,  "y": 3}, { "x": 5, "y": 3},
                { "x": 6,  "y": 3}];

  survey6Data = [{ "x": 0,   "y": 1}, { "x": 1,  "y": 1},
                { "x": 2,  "y": 2}, { "x": 3,  "y": 5},
                { "x": 4,  "y": 4}, { "x": 5, "y": 2},
                { "x": 6,  "y": 5}];

  monthData = [{ "x": 0,   "y": 1}, { "x": 1,  "y": 1},
                { "x": 2,  "y": 2.2}, { "x": 3,  "y": 5},
                { "x": 4,  "y": 4}, { "x": 5, "y": 2},
                { "x": 6,  "y": 5}, { "x": 7, "y": 2.1},
                { "x": 8,  "y": 5}, { "x": 9, "y": 2.4},
                { "x": 10,  "y": 5}];

  threeMonthData = [{ "x": 0,   "y": 3}, { "x": 1,  "y": 2},
                { "x": 2,  "y": 3.2}, { "x": 3,  "y": 4.2},
                { "x": 4,  "y": 4.1}, { "x": 5, "y": 2},
                { "x": 6,  "y": 3}, { "x": 7, "y": 2.1},
                { "x": 8,  "y": 3}, { "x": 9, "y": 2.4},
                { "x": 10,  "y": 3}];

  sixMonthData = [{ "x": 0,   "y": 1.2}, { "x": 1,  "y": 3},
                { "x": 2,  "y": 2.2}, { "x": 3,  "y": 1,2},
                { "x": 4,  "y": 4.1}, { "x": 5, "y": 2.6},
                { "x": 6,  "y": 5}, { "x": 7, "y": 2.1},
                { "x": 8,  "y": 3.2}, { "x": 9, "y": 2.4},
                { "x": 10,  "y": 4.1}];

  yearData = [{ "x": 0,   "y": 1}, { "x": 1,  "y": 1},
                { "x": 2,  "y": 2.2}, { "x": 3,  "y": 5},
                { "x": 4,  "y": 4}, { "x": 5, "y": 2},
                { "x": 6,  "y": 5}, { "x": 7, "y": 2.1},
                { "x": 8,  "y": 5}, { "x": 9, "y": 2.4},
                { "x": 10,  "y": 5}, { "x": 11,  "y": 3}];

  graphHolders = d3.selectAll(".graphHolder")

  #svg Holder divs
  svgDivs = graphHolders.append("svg")
    .attr("width", 920)
    .attr("height", 320)

  #Plot Background
  svgDivs.append("rect")
    .attr("class", "plot")
    .attr("width", 840)
    .attr("height", 300)
    .attr("x", 55)
    .attr("y", 1)
    .attr("rx", 5)
    .attr("ry", 5)
    .attr("stroke", "#dbe0e2")
    .attr("stroke-width", 1)
    .style("fill", "white")

  #Add GridLines Groups
  gridGroup = svgDivs.append("g")
    .attr("class", "gridLines")

  #Add yAxisGridLines group
  yLineGroup = gridGroup.append("g")
    .attr("class", "yLines")
    .attr("transform", "translate(55,280)")

  #Add Line Groups
  yLines = yLineGroup.selectAll("g")
    .data(messageTicks)
    .enter()
    .append("g")
    .attr("transform", (d,i) -> return "translate(0," + -((i*56)+28) + ")")

  #Add Lines to the groups
  yLines.each((tickData) ->
    d3.select(this)
      .append("line")
      .data(tickData)
      .attr("y2", 0)
      .attr("x2", 840)
      .attr("style", "stroke: #eaeff4; fill: none; stroke-width: 1px;")
  )

  #Group for the xAxisLabels
  xAxisLabels = svgDivs.append("g").attr("class", "xAxisLabels")
    .attr("transform", "translate(54,280)")
    
  #xAxisLabels Background
  xAxisLabels.append("rect")
    .attr("class", "xAxisLabelsBG")
    .attr("width", 842)
    .attr("height", 30)
    .style("fill", "#eaeff4")

  setXLines = (interval, ticks) ->
    #Add xAxisGridLines group
    xLineGroup = gridGroup.append("g")
      .attr("class", "xLines")
      .attr("transform", "translate(55,300)")

    #Add Line Groups
    xLines = xLineGroup.selectAll("g")
      .data(ticks)
      .enter()
      .append("g")
      .attr("transform", (d,i) -> return "translate(" + ((i*interval)) + ",0)")

    #Add Lines to the groups
    xLines.each((tickData) ->
      d3.select(this)
        .append("line")
        .data(tickData)
        .attr("y2", -298)
        .attr("x2", 0)
        .attr("style", "stroke: #eaeff4; fill: none; stroke-width: 1px;")
    )

    #Add groups for the daily text 
    tickGroups = xAxisLabels.selectAll("g")
      .data(ticks)
      .enter()
      .append("g")
      .attr("class", "tickGroups")
      .attr("transform", (d,i) -> return "translate(" + ((i*interval)) + ",0)")

    #Add the date data to text fields
    tickGroups.each((tickData) ->
      d3.select(this)
        .append("text")
        .text(tickData)
        .attr("y", 10)
        .attr("dy", ".71em")
        .attr("style", "text-anchor: middle; fill: #586874;")
    )

  setBarTicks = ->
    #Add groups for the daily text 
    tickGroups = xAxisLabels.selectAll("g")
      .data(barTicks)
      .enter()
      .append("g")
      .attr("class", "tickGroups")
      .attr("transform", (d,i) -> return "translate(" + ((i*120)+60) + ",0)")

    #Add the date data to text fields
    tickGroups.each((tickData) ->
      d3.select(this)
        .append("text")
        .text(tickData)
        .attr("y", 10)
        .attr("dy", ".71em")
        .attr("style", "text-anchor: middle; fill: #586874;")
    )

  resetXLines = ->
    d3.selectAll("g.xLines").remove()
    d3.selectAll("g.tickGroups").remove()

  #yAxis label functions
  setYLabels = (graph, data) ->
    #Group for the yAxisLabels
    yAxisLabels = graph.append("g").attr("class", "yAxisLabels")
      .attr("transform", "translate(40,280)")
    #Add groups for the text 
    yAxisTickGroups = yAxisLabels.selectAll("g")
      .data(data)
      .enter()
      .append("g")
      .attr("transform", (d,i) -> return "translate(0," + -((i*56)+28) + ")")
    #Add the yAxis number data with text fields
    yAxisTickGroups.each((tickData) ->
      d3.select(this)
        .append("text")
        .text(tickData)
        .attr("x", 0)
        .attr("y", 0)
        .attr("dy", ".32em")
        .attr("style", "text-anchor: end; fill: #586874;")
    )

  setRatingsLabels = (graph) ->
    #Group for the yAxisLabels
    yAxisLabels = graph.append("g").attr("class", "yAxisLabels")
      .attr("transform", "translate(25,280)")
    #Add groups for the text 
    yAxisTickGroups = yAxisLabels.selectAll("g")
      .data(ratingsTicks)
      .enter()
      .append("g")
      .attr("transform", (d,i) -> return "translate(0," + -((i*56)+28) + ")")
    #Add the yAxis number data with text fields
    yAxisTickGroups.each((tickData) ->
      d3.select(this)
        .append("image")
        .attr("width", 28)
        .attr("height", 28)
        .attr("x", -14)
        .attr("y", -14)
        .attr("xlink:href", "/static/images/star.svg")
    )

  setReviewsLabels = (graph) ->
    #Group for the yAxisLabels
    yAxisLabels = graph.append("g").attr("class", "yAxisLabels")
      .attr("transform", "translate(25,280)")
    #Add groups for the text 
    yAxisTickGroups = yAxisLabels.selectAll("g")
      .data(reviewsTicks)
      .enter()
      .append("g")
      .attr("transform", (d,i) -> return "translate(0," + -((i*56)+28) + ")")
    #Add the yAxis number data with text fields
    yAxisTickGroups.each((tickData) ->
      d3.select(this)
        .append("image")
        .attr("width", 36)
        .attr("height", 36)
        .attr("x", -22)
        .attr("y", -18)
        .attr("xlink:href", "/static/images/#{tickData}.svg")
    )

  #Line/Circle Drawing Functions


  #Initialize Current Interval
  currentInterval = weekInterval

  currentTicks = weekTicks

  setLineFunction =  ->
    return lineFunction = d3.svg.line()
      .x((d) -> return ((d.x*currentInterval)))
      .y((d) -> return -((d.y*56)+28))
      .interpolate("linear")

  #Intial LineDrawer
  lineFunction = setLineFunction()

  #on CircleMouseOver
  circleMouseOver = ->
    circle = d3.select(this)
    circle.transition().duration(500).attr("r", 11)
    circleData = circle.data()[0]
    left = ((circleData.x)*currentInterval)+28
    top = ((6 - (circleData.y))*56)+53
    $(".toolTip .dataValue p").text(circleData.y)
    $(".toolTip").css("left", "#{left}px").css("top", "#{top}px").addClass("visible").removeClass("fade")

  #on CircleMouseOut
  circleMouseOut = ->
    circle = d3.select(this)
    circle.transition().duration(500).attr("r", 8)
    $(".toolTip").addClass("fade").removeClass("visible")

  #set circle function
  setCircles = (graph, data, interval)  ->
    circleGroup = graph.append("g")
      .attr("class", "circleGroup")
      .attr("transform", "translate(55,336)")
    circles = circleGroup.selectAll("circle")
      .data(data)
      .enter()
      .append("circle")
      .attr("r", 8)
      .attr("style", "fill: #ffffff; stroke: #6f90ab; stroke-width: 3;")
      .attr("cx", (d) -> return ((d.x*interval)))
      .attr("cy", (d) -> return -((d.y*56)+28))
      .on("mouseover", circleMouseOver)
      .on("mouseout", circleMouseOut)

  #Set Range
  y = d3.scale.linear().domain([1,d3.max(dateBarData)]).range([224, 0])

  #Set Bars
  setBars = (graph, data)  ->
    barWidth = 420 / data.length
    barGroup = graph.append("g")
      .attr("class", "barGroup")
      .attr("transform", "translate(35,55)")
    bars = barGroup.selectAll("bar")
      .data(data)
      .enter()
      .append("rect")
      .attr("x", (d, i) -> return (i * barWidth * 2) + 52)
      .attr("y", (d) -> return y(d) - 27)
      .attr("height", (d) -> return 251 - y(d))
      .attr("width", barWidth - 1)
      .attr("style", "fill: #6f90ab;")

  #set line function
  setLine = (graph, data) ->
    lineGroup = graph.append("g")
      .attr("class", "lineGroup")
      .attr("transform", "translate(55,336)")
    paths = lineGroup.append("path")
      .attr("d", lineFunction(data))
      .attr("stroke", "#6f90ab")
      .attr("stroke-width", 5)
      .attr("fill", "none")

  #initialize graph for week
  setXLines(120, weekTicks)

  initializeGraph = (graph, data, ticks) ->
    setLine(graph, data)
    setCircles(graph, data, currentInterval)
    setYLabels(graph, ticks)

  initializeRatingsGraph = (graph, data) ->
    setLine(graph, data)
    setCircles(graph, data, currentInterval)
    setRatingsLabels(graph)

  initializeReviewsGraph = (graph, data) ->
    setLine(graph, data)
    setCircles(graph, data, currentInterval)
    setReviewsLabels(graph)

  resetGraph = (graph) ->
    graph.selectAll(".lineGroup").remove()
    graph.selectAll(".circleGroup").remove()
    graph.select(".yAxisLabels").remove()
    graph.select(".barGroup").remove()
    resetXLines()
    setXLines(currentInterval, currentTicks)


  #reset Time Toggle
  resetTime = ->
    $("#timeToggle button").removeClass("active")
    $("#timeToggle button#week").addClass("active")
    currentTicks = weekTicks
    currentInterval = weekInterval
    lineFunction = setLineFunction()
  



  #Set Engagement Graph line and circles for Satisfaction data
  engagementGraph = d3.select("#VOSGraph svg")
  initializeGraph(engagementGraph, satisfactionData, npsTicks)
  
  #Set Unsub Graph line and circles for Satisfaction data
  unsubGraph = d3.select("#unsubGraph svg")
  initializeGraph(unsubGraph, survey1Data, unsubTicks)

  #Set Message Graph line and circles for frequency data
  messageGraph = d3.select("#messageGraph svg")
  initializeGraph(messageGraph, frequencyData, messageTicks)

#Stop Copy
  
  options = $(".accountOptions")
  nav = $("#accountNav")

  updatePos = ->
    mainHeight = $("#rightContent").height()
    $("#accountNav").height(mainHeight)
    $("#mainContent").height(mainHeight)
    if options.hasClass("fixed")
      if $(window).scrollTop() + $(window).height() > mainHeight + 39
        options.removeClass("fixed").addClass("relative")
        nav.removeClass("fixed").addClass("relative")
    if options.hasClass("relative")
      if $(window).scrollTop() + $(window).height() < mainHeight + 39
        options.removeClass("relative").addClass("fixed")
        nav.removeClass("relative").addClass("fixed")

  updatePos()

  #Category Click
  $(".col h2").click ->
    if !($(this).hasClass("active"))
      $('.col h2').removeClass('active')
      $("i.fa-chevron-up").addClass("hidden")
      $("i.fa-chevron-down").removeClass("hidden")
      $("ul.subs li").removeClass("active")
      $("ul.subBreakdown").removeClass("active")
      $(this).find("i.fa-chevron-left").addClass("hidden")
      $(this).find("i.fa-chevron-down").removeClass("hidden")
  
  $('h2#engagement').click ->
    if !($(this).hasClass("active"))
      $(this).addClass("active")
      $('.dash').hide()
      $('#VOS.dash').show() 
      $("ul.subs li#satisfaction").addClass("active")
      $("div.graphWrapper h3").text("Satisfaction Trend")
      #resetGraph Data
      resetTime()
      resetGraph(engagementGraph)
      initializeGraph(engagementGraph, satisfactionData, npsTicks)
      updatePos()

  $('h2#unsub').click ->
    if !($(this).hasClass("active"))
      $(this).addClass("active")
      $('.dash').hide()
      $('#unsub.dash').show()
      $("ul.subs li#surveyFrequency").addClass("active")
      $("div.graphWrapper h3").text("Survey Trend: Message Frequency")
      #resetGraph Data
      resetTime()
      resetGraph(unsubGraph)
      initializeGraph(unsubGraph, survey1Data, unsubTicks)
      updatePos()

  $('h2#message').click ->
    if !($(this).hasClass("active"))
      $(this).addClass("active")
      $('.dash').hide()
      $('#message.dash').show()
      $("ul.subs li#frequency").addClass("active")
      $("div.graphWrapper h3").text("Message Frequency Trend")
      #resetGraph Data
      resetTime()
      resetGraph(messageGraph)
      initializeGraph(messageGraph, frequencyData, messageTicks)
      updatePos()


  resetSubs = (parent) ->
    $("ul.subs li").removeClass("active")
    $("i.fa-chevron-down").removeClass("hidden")
    $("i.fa-chevron-up").addClass("hidden")
    $("ul.subBreakdown").removeClass("active")
    parent.find("ul.subBreakdown").addClass("active")
    parent.find("i.fa-chevron-down").addClass("hidden")
    parent.find("i.fa-chevron-up").removeClass("hidden")
    updatePos()

  #Individual subcategories

  $("ul.subs li#satisfaction button.subSelector").click ->
    if !($("ul.subs li#satisfaction").hasClass("active"))
      resetSubs($("ul.subs li#satisfaction"))
      $("ul.subs li#satisfaction").addClass("active")
      $("div.graphWrapper h3").text("Satisfaction Trend")
      #Set satisfactionGraph Data
      resetTime()
      resetGraph(engagementGraph)
      initializeGraph(engagementGraph, satisfactionData, npsTicks)

  $("ul.subs li#ratings button.subSelector").click ->
    if !($("ul.subs li#ratings").hasClass("active"))
      resetSubs($("ul.subs li#ratings"))
      $("ul.subs li#ratings").addClass("active")
      $("div.graphWrapper h3").text("Ratings Trend")
      #Set ratingsGraph Data
      resetTime()
      resetGraph(engagementGraph)
      initializeRatingsGraph(engagementGraph, ratingsData)
      #Reset subBreakdown
      $("ul.subBreakdown li").removeClass("active")
    else
      sub = $(this).parent("li").find("ul.subBreakdown")
      if sub.hasClass("active")
        $(this).parent("li").find("ul.subBreakdown").removeClass("active")
        $(this).find("i.fa-chevron-down").removeClass("hidden")
        $(this).find("i.fa-chevron-up").addClass("hidden")
      else
        $(this).parent("li").find("ul.subBreakdown").addClass("active")
        $(this).find("i.fa-chevron-down").addClass("hidden")
        $(this).find("i.fa-chevron-up").removeClass("hidden")
      updatePos()

  $("ul.subs li#reviews button.subSelector").click ->
    if !($("ul.subs li#reviews").hasClass("active"))
      resetSubs($("ul.subs li#reviews"))
      $("ul.subs li#reviews").addClass("active")
      $("div.graphWrapper h3").text("Reviews Trend")
      #Set reviewsGraph Data
      resetTime()
      resetGraph(engagementGraph)
      initializeReviewsGraph(engagementGraph, reviewsData)

  #CHURN
  #Frequency
  $("ul.subs li#surveyFrequency").click ->
    if !($(this).hasClass("active"))
      resetSubs($(this))
      $(this).addClass("active")
      $("div.graphWrapper h3").text("Survey Trend: Message Frequency")
      #Set survey1Graph Data
      resetTime()
      resetGraph(unsubGraph)
      initializeGraph(unsubGraph, survey1Data, unsubTicks)

  #Overload
  $("ul.subs li#surveyOverload").click ->
    if !($(this).hasClass("active"))
      resetSubs($(this))
      $(this).addClass("active")
      $("div.graphWrapper h3").text("Survey Trend: Inbox Overload")
      #Set survey2Graph Data
      resetTime()
      resetGraph(unsubGraph)
      initializeGraph(unsubGraph, survey2Data, unsubTicks)

  #Expectation
  $("ul.subs li#surveyExpectation").click ->
    if !($(this).hasClass("active"))
      resetSubs($(this))
      $(this).addClass("active")
      $("div.graphWrapper h3").text("Survey Trend: Didn't Meet Expectation")
      #Set survey3Graph Data
      resetTime()
      resetGraph(unsubGraph)
      initializeGraph(unsubGraph, survey3Data, unsubTicks)

  #Relevancy
  $("ul.subs li#surveyRelevancy").click ->
    if !($(this).hasClass("active"))
      resetSubs($(this))
      $(this).addClass("active")
      $("div.graphWrapper h3").text("Survey Trend: Messages not Relevant")
      #Set survey4Graph Data
      resetTime()
      resetGraph(unsubGraph)
      initializeGraph(unsubGraph, survey4Data, unsubTicks)

  #Repetitive
  $("ul.subs li#surveyRepetitive").click ->
    if !($(this).hasClass("active"))
      resetSubs($(this))
      $(this).addClass("active")
      $("div.graphWrapper h3").text("Survey Trend: Messages Repetitive")
      #Set survey5Graph Data
      resetTime()
      resetGraph(unsubGraph)
      initializeGraph(unsubGraph, survey5Data, unsubTicks)

  #Other
  $("ul.subs li#surveyOther").click ->
    if !($(this).hasClass("active"))
      resetSubs($(this))
      $(this).addClass("active")
      $("div.graphWrapper h3").text("Survey Trend: Other")
      #Set survey6Graph Data
      resetTime()
      resetGraph(unsubGraph)
      initializeGraph(unsubGraph, survey6Data, unsubTicks)

  #Ratings Value
  $("ul.subBreakdown li#value").click ->
    if !($(this).hasClass("active"))
      #reset subBreakdown
      $("ul.subBreakdown li").removeClass("active")
      $(this).addClass("active")
      $("div.graphWrapper h3").text("Ratings Trend: Value")
      #Set value Data
      resetTime()
      resetGraph(engagementGraph)
      initializeRatingsGraph(engagementGraph, valueData)

  #Ratings Frequency
  $("ul.subBreakdown li#frequencyR").click ->
    if !($(this).hasClass("active"))
      #reset subBreakdown
      $("ul.subBreakdown li").removeClass("active")
      $(this).addClass("active")
      $("div.graphWrapper h3").text("Ratings Trend: Frequency")
      #Set value Data
      resetTime()
      resetGraph(engagementGraph)
      initializeRatingsGraph(engagementGraph, frequencyRData)

  #Ratings Relevancy
  $("ul.subBreakdown li#relevancy").click ->
    if !($(this).hasClass("active"))
      #reset subBreakdown
      $("ul.subBreakdown li").removeClass("active")
      $(this).addClass("active")
      $("div.graphWrapper h3").text("Ratings Trend: Relevancy")
      #Set value Data
      resetTime()
      resetGraph(engagementGraph)
      initializeRatingsGraph(engagementGraph, relevancyData)

  #Ratings Design
  $("ul.subBreakdown li#design").click ->
    if !($(this).hasClass("active"))
      #reset subBreakdown
      $("ul.subBreakdown li").removeClass("active")
      $(this).addClass("active")
      $("div.graphWrapper h3").text("Ratings Trend: Design")
      #Set value Data
      resetTime()
      resetGraph(engagementGraph)
      initializeRatingsGraph(engagementGraph, designData)

  #Messages
  $("ul.subs li#frequency button.subSelector").click ->
    if !($("ul.subs li#frequency").hasClass("active"))
      resetSubs($("ul.subs li#frequency"))
      $("ul.subs li#frequency").addClass("active")
      $("div.graphWrapper h3").text("Message Frequency Trend")
      #Set frequencyGraph Data
      resetGraph(messageGraph)
      resetTime()
      initializeGraph(messageGraph, frequencyData, messageTicks)

  #Dates
  $("ul.subs li#date button.subSelector").click ->
    if !($("ul.subs li#date").hasClass("active"))
      resetSubs($("ul.subs li#date"))
      $("ul.subs li#date").addClass("active")
      $("div.graphWrapper h3").text("Average Frequency by Day of Week")
      #Set typeGraph Data
      resetGraph(messageGraph)
      resetTime()
      resetXLines()
      setBarTicks()
      setYLabels(messageGraph, messageTicks)
      setBars(messageGraph, dateBarData)
      updatePos()

  #Time
  $("ul.subs li#time button.subSelector").click ->
    if !($("ul.subs li#time").hasClass("active"))
      resetSubs($("ul.subs li#time"))
      $("ul.subs li#time").addClass("active")
      $("div.graphWrapper h3").text("Time Sent Trend")
      #Set timeGraph Data
      resetTime()
      resetGraph(messageGraph)
      initializeGraph(messageGraph, timeData, timeTicks)
      #move ticks over and set font smaller

  #Toggle Time View
  $("#timeToggle button").click ->
    if !($(this).hasClass("active"))
      $("#timeToggle button").removeClass("active")
      $(this).addClass("active")
      currentId = $("li.sub.active").attr("id")
      time = $(this).attr("id")
      #Not Changing for date right now
      if currentId != "date"
        #Set Interval
        if time == "week"
          currentInterval = weekInterval
          currentTicks = weekTicks
        else if time == "month"
          currentInterval = monthInterval
          currentTicks = monthTicks
        else if time == "3month"
          currentInterval = monthInterval
          currentTicks = threeMonthTicks
        else if time == "6month"
          currentInterval = monthInterval
          currentTicks = sixMonthTicks
        else
          currentInterval = yearInterval
          currentTicks = yearTicks

        #Set Line Drawer
        lineFunction = setLineFunction()
        #Set Graph based on currentId
        if currentId == "satisfaction"
          resetGraph(engagementGraph)
          if time == "week" then initializeGraph(engagementGraph, satisfactionData, npsTicks)
          else if time == "month" then initializeGraph(engagementGraph, monthData, npsTicks)
          else if time == "3month" then initializeGraph(engagementGraph, threeMonthData, npsTicks)
          else if time == "6month" then initializeGraph(engagementGraph, sixMonthData, npsTicks)
          else if time == "year" then initializeGraph(engagementGraph, yearData, npsTicks)

        else if currentId == "ratings"
          resetGraph(engagementGraph)
          if time == "week" then initializeRatingsGraph(engagementGraph, ratingsData)
          else if time == "month" then initializeRatingsGraph(engagementGraph, monthData)
          else if time == "3month" then initializeRatingsGraph(engagementGraph, threeMonthData)
          else if time == "6month" then initializeRatingsGraph(engagementGraph, sixMonthData)
          else if time == "year" then initializeRatingsGraph(engagementGraph, yearData)

        else if currentId == "reviews"
          resetGraph(engagementGraph)
          if time == "week" then initializeReviewsGraph(engagementGraph, reviewsData)
          else if time == "month" then initializeReviewsGraph(engagementGraph, monthData)
          else if time == "3month" then initializeReviewsGraph(engagementGraph, threeMonthData)
          else if time == "6month" then initializeReviewsGraph(engagementGraph, sixMonthData)
          else if time == "year" then initializeReviewsGraph(engagementGraph, yearData)

        else if currentId == "surveyFrequency"
          resetGraph(unsubGraph)
          if time == "week" then initializeGraph(unsubGraph, survey1Data, unsubTicks)
          else if time == "month" then initializeGraph(unsubGraph, monthData, unsubTicks)
          else if time == "3month" then initializeGraph(unsubGraph, threeMonthData, unsubTicks)
          else if time == "6month" then initializeGraph(unsubGraph, sixMonthData, unsubTicks)
          else if time == "year" then initializeGraph(unsubGraph, yearData, unsubTicks)

        else if currentId == "surveyOverload"
          resetGraph(unsubGraph)
          if time == "week" then initializeGraph(unsubGraph, survey2Data, unsubTicks)
          else if time == "month" then initializeGraph(unsubGraph, monthData, unsubTicks)
          else if time == "3month" then initializeGraph(unsubGraph, threeMonthData, unsubTicks)
          else if time == "6month" then initializeGraph(unsubGraph, sixMonthData, unsubTicks)
          else if time == "year" then initializeGraph(unsubGraph, yearData, unsubTicks)
          
        else if currentId == "surveyExpectation"
          resetGraph(unsubGraph)
          if time == "week" then initializeGraph(unsubGraph, survey3Data, unsubTicks)
          else if time == "month" then initializeGraph(unsubGraph, monthData, unsubTicks)
          else if time == "3month" then initializeGraph(unsubGraph, threeMonthData, unsubTicks)
          else if time == "6month" then initializeGraph(unsubGraph, sixMonthData, unsubTicks)
          else if time == "year" then initializeGraph(unsubGraph, yearData, unsubTicks)
          
        else if currentId == "surveyRelevancy"
          resetGraph(unsubGraph)
          if time == "week" then initializeGraph(unsubGraph, survey4Data, unsubTicks)
          else if time == "month" then initializeGraph(unsubGraph, monthData, unsubTicks)
          else if time == "3month" then initializeGraph(unsubGraph, threeMonthData, unsubTicks)
          else if time == "6month" then initializeGraph(unsubGraph, sixMonthData, unsubTicks)
          else if time == "year" then initializeGraph(unsubGraph, yearData, unsubTicks)
          
        else if currentId == "surveyRepetitive"
          resetGraph(unsubGraph)
          if time == "week" then initializeGraph(unsubGraph, survey5Data, unsubTicks)
          else if time == "month" then initializeGraph(unsubGraph, monthData, unsubTicks)
          else if time == "3month" then initializeGraph(unsubGraph, threeMonthData, unsubTicks)
          else if time == "6month" then initializeGraph(unsubGraph, sixMonthData, unsubTicks)
          else if time == "year" then initializeGraph(unsubGraph, yearData, unsubTicks)
          
        else if currentId == "surveyOther"
          resetGraph(unsubGraph)
          if time == "week" then initializeGraph(unsubGraph, survey6Data, unsubTicks)
          else if time == "month" then initializeGraph(unsubGraph, monthData, unsubTicks)
          else if time == "3month" then initializeGraph(unsubGraph, threeMonthData, unsubTicks)
          else if time == "6month" then initializeGraph(unsubGraph, sixMonthData, unsubTicks)
          else if time == "year" then initializeGraph(unsubGraph, yearData, unsubTicks)

        else if currentId == "frequency"
          resetGraph(messageGraph)
          if time == "week" then initializeGraph(messageGraph, frequencyData, messageTicks)
          else if time == "month" then initializeGraph(messageGraph, monthData, messageTicks)
          else if time == "3month" then initializeGraph(messageGraph, threeMonthData, messageTicks)
          else if time == "6month" then initializeGraph(messageGraph, sixMonthData, messageTicks)
          else if time == "year" then initializeGraph(messageGraph, yearData, messageTicks)
          
        else if currentId == "time"
          resetGraph(messageGraph)
          if time == "week" then initializeGraph(messageGraph, survey6Data, timeTicks)
          else if time == "month" then initializeGraph(messageGraph, monthData, timeTicks)
          else if time == "3month" then initializeGraph(messageGraph, threeMonthData, timeTicks)
          else if time == "6month" then initializeGraph(messageGraph, sixMonthData, timeTicks)
          else if time == "year" then initializeGraph(messageGraph, yearData, timeTicks)
