//Accordian for talks
var $accordianControls = $('.js-accordian-control');
$('.js-toggle-cont').slideToggle('fast'); //all details closed to start
$accordianControls.click(function(event) {
  event.preventDefault();
  //TODO Find correct selector for this line
  $(this).next().toggleClass('expansion-arrow--open');
  $(this).next().next().slideToggle('fast');
});
$('.js-arrow').click(function(event) {
  $(this).toggleClass('expansion-arrow--open');
  $(this).next().slideToggle('fast');
});

w.optly.mrkt.utils.smoothScroll = function(event) {
	var targetElmPos = $(this.getAttribute('href')).offset().top - 180;
	event.preventDefault();
	$('html,body').animate({
		scrollTop: targetElmPos
	}, 1000);
};

//Smoothscroll for Days
$('#day-one-link').on('click', w.optly.mrkt.utils.smoothScroll);
$('#day-two-link').on('click', w.optly.mrkt.utils.smoothScroll);

//Make filter row fixed after scroll
var $filterRow = $('#filter-row'),
    $dayOne = $('#day-one-header'),
    $dayTwo = $('#day-two-header'),
    $window = $(window);
$window.on('scroll', function(e) {
  var scrollTop = $window.scrollTop();
  if (scrollTop > 200) {
    $filterRow.addClass('fixed-filter');
  } else {
    $filterRow.removeClass('fixed-filter');
  }
  if (scrollTop > 200 && $window.scrollTop < 1360 ) {
    $dayOne.addClass('agenda-day--active up-arrow');
    $dayTwo.removeClass('agenda-day--active up-arrow');
  }
  if (scrollTop > 1360) {
    $dayTwo.addClass('agenda-day--active up-arrow');
    $dayOne.removeClass('agenda-day--active up-arrow');
  }
});

/*
  If item is in array, remove it
  else, add it
*/
function toggleArrayItem(array, item) {
  var itemIndex = $.inArray(item, array);
  if(itemIndex !== -1) {
    array.splice(itemIndex,1);
  } else {
    array.push(item);
  }
}

/*
  Check that every item in arr1
  is in arr2 i.e. arr1 is subset
  of arr2
*/
function isSubset(arr1, arr2) {
  return arr1.every(function(val) {
    return arr2.indexOf(val) >= 0;
  });
}

var $dropdownElems = $('.js-dropdown'),
    $events = $('.js-event'),
    filterList = [],
    $filterListItem = $('.js-filter-item');

//Top filter dropdown
$dropdownElems.click(function(event) {
  var $this = $(this);
  $this.toggleClass('active');
  $dropdownElems.not( $this ).removeClass( 'active' );
  $this.mouseleave(function(){
    $this.removeClass( 'active' );
  });
});

//Filter logic
$filterListItem.on('click', function(event) {
  event.stopPropagation();
  var $this = $(this),
      selectedData = ($.trim($this.attr('data-filter')));

  toggleArrayItem(filterList, selectedData);
  $this.toggleClass('selected');

  if(filterList) {
    $events.each(function(eventIndex, eventItem) {
      var eventFilterList = ($(eventItem).data('track') + ' ' + $(eventItem).data('roles')).split(' ');
      if(! isSubset(filterList, eventFilterList)) {
        $(eventItem).hide();
      } else {
        $(eventItem).show();
      }
    });
  }
});

// Reset -- clear all filters
$('.js-reset').on('click', function(event) {
  filterList = [];
  $events.each(function(eIndex, eventItem) {
    $(eventItem).show();
  });
  $filterListItem.each(function(eIndex, eventItem) {
    $(this).removeClass('selected');
  });
});

@@ -862,42 +862,38 @@
 %0A%0A//
-Make filter row fixed after scroll
+Scroll logic for day selection
 %0Avar
@@ -1095,16 +1095,42 @@
 lTop();%0A
+  console.log(scrollTop);%0A
   if (sc
@@ -1133,25 +1133,25 @@
  (scrollTop 
-%3E
+%3C
  200) %7B%0A    
@@ -1161,19 +1161,22 @@
 lterRow.
-add
+remove
 Class('f
@@ -1196,66 +1196,95 @@
 ;%0A  
-%7D else %7B%0A    $filterRow.removeClass('fixed-filter
+  $dayOne.removeClass('up-arrow');%0A    $dayOne.addClass('agenda-day--active
 ');%0A  %7D
-%0A
  
+else
  if 
@@ -1299,49 +1299,112 @@
 op %3E
+=
  200
- && $window.scrollTop %3C 1360 ) %7B%0A
+) %7B%0A    $filterRow.addClass('fixed-filter');%0A    if (scrollTop %3C 2890) %7B // activate day one%0A 
     
+ 
 $day
@@ -1440,32 +1440,34 @@
 ive up-arrow');%0A
+  
     $dayTwo.remo
@@ -1506,21 +1506,26 @@
 rrow');%0A
+  
   %7D
-%0A
  
+else
  if (scr
@@ -1536,17 +1536,40 @@
 op %3E
- 1360) %7B%0A
+= 2890) %7B // activate day two%0A  
     
@@ -1600,37 +1600,30 @@
 -day--active
- up-arrow
 ');%0A
+  
     $dayOne.
@@ -1666,16 +1666,99 @@
 rrow');%0A
+      if (scrollTop %3E= 2980) %7B%0A        $dayTwo.addClass('up-arrow');%0A      %7D%0A    %7D%0A
   %7D%0A%7D);%0A

make scrolling more precise