Template.tabs.helpers({
  class_for: function(tab_name) {
    var templateName = Session.get('templateName');

    if (templateName === tab_name) {
      return tab_name + ' active';
    } else if (templateName === 'display' && tab_name === 'news') {
      return tab_name + ' active'
    } else {
      return tab_name;
    }
  },

  class_for_changer: function() {
    var hide = Session.get('hide_controller_selection');
    return (hide) ? "controller_selection hidden" : "controller_selection";
  },

  selected_controller: function() {
    return
  },

  accessible: Meteor.zoblak.client.accessible
});

function saveParamsAndGo(where) {
  var hideControllerSelection = Session.get('hide_controller_selection');
  if (hideControllerSelection) {
    Router.go(where + "?controller_id=" + Session.get('controller_id'));
  } else {
    Router.go(where);
  }
}

Template.tabs.events({
  'click .start': function() {
    saveParamsAndGo('/');
  },
  'click .weather': function() {
    saveParamsAndGo('/weather');
  },
  'click .log': function() {
    saveParamsAndGo('/log');
  },
  'click .surveillance': function() {
    saveParamsAndGo('/surveillance');
  },

  'click .alarm': function() {
    saveParamsAndGo('/alarm');
  },
  'click .settings': function() {
    Session.set('templateName', 'settings');
  },

  'click #switch': function() {
    var instance = Template.instance();
    controller_id = instance.$('#controller').val();
    Session.setPersistent('controller_id', controller_id);
  }
});
