var $ = require('jquery')
var _ = require('lodash')
var mapView = require('./mapView')
var noUiSlider = require('nouislider');
var toggles = {}

module.exports = {
    init: initNavigation
}

var mapMissing = true
function initNavigation() {
    $('.filters button, #single').click(function (e) {
        var $button = $(this)
        $button.toggleClass('inactive')
        var id = $button.prop('id')
        toggleObj(id, toggles)
        setTimeout(function () {
            $('#schedule').toggleClass(id)
        }, 1)
    })

    $('.toggleInformation').click(function () {
        $('.information').show()
    })
    $('.toggleMapInformation').click(function () {
        $('#map_wrapper').show()
        if (mapMissing) {
            mapView.renderMap()
            mapMissing = false
        }
    })
    $('.toggleFilters, .filters .close').click(function () {
        $('.filters').toggleClass('collapsed')
    })
    initTimeFilter()
}

function toggleObj(key, obj) {
    if (key in obj) delete obj[key]
    else obj[key] = true
}
var start = 60
var end = 235
var all = _.range(60, 235, 5)

function initTimeFilter() {
    var slider = document.getElementById('slider')
    noUiSlider.create(slider, {
        start:    [60, 230],
        step:     5,
        //margin:   20,
        connect:  true,
        range:    {
            'min': 60,
            'max': 230
        },
        format:   {
            to:   formatTime,
            from: x => x
        }
    })
    slider.noUiSlider.on('update', function (values, endTime) {
        var isStart = !endTime
        setStartAndEndLabels(isStart, parseTime(values[endTime]))
    })
    slider.noUiSlider.on('change', function (values, endTime) {
        setTimeFilterClasses()
    })
}
var $rangeLabel = $('.rangeLabel')

function setStartAndEndLabels(isStart, val) {
    if (isStart) start = Number(val)
    else end = Number(val)
    $rangeLabel.html(formatTime(start) + '-' + formatTime(end))
}

function parseTime(isoTime) {
    console.log('parse',isoTime)
    var hm = isoTime.split(':')
    return String(Number(hm[0]) * 10 + Number(hm[1]) / 6)
}

function formatTime(val) {
    console.log('formt',arguments)
    var hour = Math.floor(val / 10)
    var min = Math.round(val % 10 * .6)
    return hour + ':' + min + '0'
}
var $schedule = $('#schedule')

function setTimeFilterClasses() {
    var hiddenTimes = all.filter(function (time) {
        return time < start || time > end
    })
    $schedule.prop('class', _.map(toggles, function (v, k) { return k }).concat(hiddenTimes.map(function (time) { return 'h' + time })).join(' '))
}

function saveFilters(obj) {
    localStorage.setItem('filters', JSON.stringify(obj))
}

function loadFilters() {
    return JSON.parse(localStorage.getItem('filters')) || {}
}

@@ -124,23 +124,71 @@
 ');%0A
+%0A
 var 
-toggles = %7B
+settings = %7B%0A    toggles: %7B%7D,%0A    start: 60,%0A    end: 235%0A
 %7D%0A%0Am
@@ -255,16 +255,17 @@
  = true%0A
+%0A
 function
@@ -467,16 +467,25 @@
 Obj(id, 
+settings.
 toggles)
@@ -1098,36 +1098,8 @@
 e%0A%7D%0A
-var start = 60%0Avar end = 235
 %0Avar
@@ -1875,16 +1875,25 @@
 sStart) 
+settings.
 start = 
@@ -1913,16 +1913,25 @@
    else 
+settings.
 end = Nu
@@ -1972,16 +1972,25 @@
 matTime(
+settings.
 start) +
@@ -2007,16 +2007,25 @@
 matTime(
+settings.
 end))%0A%7D%0A
@@ -2059,41 +2059,8 @@
 ) %7B%0A
-    console.log('parse',isoTime)%0A
     
@@ -2179,43 +2179,8 @@
 ) %7B%0A
-    console.log('formt',arguments)%0A
     
@@ -2426,16 +2426,25 @@
  time %3C 
+settings.
 start %7C%7C
@@ -2451,16 +2451,25 @@
  time %3E 
+settings.
 end%0A    
@@ -2505,16 +2505,25 @@
 , _.map(
+settings.
 toggles,

Remove console.log's and put settings to same object