(function(window, $, undefined) {
  
  var dataName = 'selectionUrl',
  inactiveClass = 'inactive',
  invalidClass = 'invalid',
  selectActiveClass = 'activated',
  containerSelector = '#journey',
  tumorTypeSelector = '#tumorType',
  storyPointSelector = '#storyPoint',
  selectionLinkSelector = 'ul.drop li a',
  selectButtonSelector = 'a.select-container',
  selectTextSelector = 'span.select-btn span.text',
  beginLinkSelector = 'a.begin-btn',
  cookieName = 'splashPageSelections',
  initialCookie = biooncIAC.helpers.getCookie(cookieName),
  initialValues = {tumorType : '', storyPoint : ''},
  // Extend blank properties with any stored in the cookie
  beginPath = null,
  queryObj = biooncIAC.helpers.parseUri(window.location.href).queryKey;
  
  if (queryObj.hasOwnProperty('cancer')) {
    initialValues.tumorType = queryObj.cancer+"-cancer";
    beginPath = initialValues;
    biooncIAC.helpers.setCookie(cookieName, JSON.stringify(beginPath), 365, '/');
  } else {
    beginPath = (biooncIAC.helpers.isJSON(initialCookie)) ? $.extend(initialValues, JSON.parse(initialCookie)) : initialValues;
  }
  
  function updateSelections(keyValue, $begin) {
    // If we have no key or value passed in, use what was retrieved from the cookie
    if (!keyValue) {
      for (var key in beginPath) {
        if (beginPath[key] !== "") {
          $('#'+key).find(selectTextSelector).text($('#'+beginPath[key]).attr('rel'));
        }
      }
    } else {
      // Update variable and cookie with most recent selection
      beginPath[keyValue.k] = keyValue.v;
      biooncIAC.helpers.setCookie(cookieName, JSON.stringify(beginPath), 365, '/');
    }
    
    // If we have selected both keys, make begin button active with appropriate href
    if (beginPath.tumorType !== '' && beginPath.storyPoint !== '') {
      $begin.attr('href', sitePath + beginPath.tumorType + '/' + beginPath.storyPoint + '/index.html').removeClass(inactiveClass);
    }
  }

  $(document).ready(function() {
    
    // Init DOM vars
    var $journey = $(containerSelector),
        $selections = $journey.find(selectionLinkSelector).each(function() {
          // Remove hrefs from select links and store them in a data attr
          var $this = $(this), href = $this.attr('href');
          $this.data(dataName, href).attr('href', '#').removeAttr('title');  
        }),
        $selectionHolders = $journey.find('div.path > ul > li'),
        $begin = $journey.find(beginLinkSelector),
        // Init functions
        showDropdown = function(e) {
          var $this = $(this);
          
          if (e.type === 'mouseleave') {
            $this.removeClass(selectActiveClass);
          } else {
            $this.addClass(selectActiveClass);
          }
          
          e.preventDefault();
        };
    
    // Update selectors with any values stored in cookies first
    updateSelections(null, $begin);
    
    // Remove inactive states since we gots the JS
    $journey.find('#'+storyPointSelector+' .'+inactiveClass).removeClass(inactiveClass);
    
    // Add click handlers and hover handlers as fallbacks
    $selectionHolders.click(showDropdown).mouseenter(showDropdown).mouseleave(showDropdown);
    
    // When a selection is clicked
    $selections.click(function(e) {
      var $this = $(this),
          $container = $this.parents('div.path'),
          $select = $container.find(selectButtonSelector).removeClass(invalidClass),
          selectedText = $this.attr('rel'),
          selectedType = $container.attr('id'),
          selectedId = $this.attr('id'),
          $selectText = $container.find(selectTextSelector).text(selectedText);
      
      // Update cookie and begin button href
      updateSelections({k: selectedType, v: selectedId}, $begin);

      e.preventDefault();
    });
    
    
    $begin.click(function(e) {
      var $this = $(this),
          href = $this.attr('href');
      // We don't want the begin button to do anything...yet
      if (href === '#' || $this.hasClass(inactiveClass)) {
        for (var key in beginPath) {
          if (beginPath[key] === '') {
            // Add invalid class to anything that hasn't been selected yet
            $journey.find('#'+key+' '+selectButtonSelector).addClass(invalidClass);
          }
        }
        e.preventDefault();
      }
    });
    
  });

})(window, jQuery);
