
function nextPressed(link, hideothers) {
  
  if(!link.attr('rel')) return false;
  
  var step_to_show = link.attr('rel');
       
  // hide others
  if (hideothers) {
    $('#' + step_to_show).parent('.oneatatime').find('.step').hide();
    
    // notSelected on others
    link.parents('.choices').find('.choice').addClass('notSelected');
    link.parents('.choices').find('.button').removeClass('active').addClass('inactive');
    
    link.parents('.choice').removeClass('notSelected');
  }
  
  
  link.parent('.buttons').find('.button').addClass('inactive');
  link.removeClass('inactive').addClass('active');
  
  // hide absolutely everything after one we want to show
  var found = false;

  $('#flowchart .step').each(
    function(idx, elem) {
      if(found) {
        $(elem).hide();
      } else if(elem.id == step_to_show) {
        found = true;      
      }
    }
  );
  
  found = false;
  
   $('#flowchart .choices').each(
    function(idx, elem) {
      if(found) {
        $(elem).hide();
      } else if(elem.id == step_to_show) {
        found = true;
      }
    }
  );
  
    
  // remove the existing open classes
  $('#flowchart .open').removeClass('open');
  
  // show the required step
  $('#' + step_to_show + ' .choice').removeClass('notSelected');
  $('#' + step_to_show + ' .button').removeClass('inactive').removeClass('active');
 
  
  $('#' + step_to_show + ' .step').show();  
  $('#' + step_to_show + ' .choices').show();
 
  $('#' + step_to_show).slideDown('fast');
  
  // add the open class
  $('#' + step_to_show).addClass('open');
  $('#' + step_to_show + ' .step').addClass('open');
    
  return false;
  
}

function applyBehaviour() {
  // apply behaviour to the next buttons
  $('.reveal').click(function(){ return nextPressed($(this), false) });
  $('.revealone').click(function(){ return nextPressed($(this), true) });
}


$(function() {
  applyBehaviour();  
});
