function Support(next_question, resolved, exhausted, num_questions) {
  this.next_question_ = next_question;
  this.resolved_ = resolved;
  this.exhausted_ = exhausted;
  this.num_questions_ = num_questions;
  this.curr_question_ = 1;
  this.initialized_ = false;
};

Support.getEl = function(id) {
  return document.getElementById(id);
};

Support.insertAfter = function(to_insert, ref) {
  ref.parentNode.insertBefore(to_insert, ref.nextSibling);
};

Support.log = function(o) {
  if ('console' in window && 'log' in window.console) {
    console.log(o);
  }
};

Support.prototype.initialize = function() {
  this.next_obj_ = Support.getEl(this.next_question_);
  this.yes_obj_ = null;
  this.no_obj_ = null;
  this.exhausted_obj_ = null;
  this.resolved_obj_ = null;

  if (this.next_obj_) {
    var checks = this.next_obj_.getElementsByTagName('input');
    this.yes_obj_ = checks[0];
    this.no_obj_ = checks[1];
  
    var self = this;
    if (this.yes_obj_) {
      this.yes_obj_.onclick = function() {
        self.handleYes();
      };
    }
    if (this.no_obj_) {
      this.no_obj_.onclick = function() {
        self.handleNo();
      };
    }

    if (this.resolved_) {
      this.resolved_obj_ = Support.getEl(this.resolved_);
    }

    if (this.exhausted_) {
      this.exhausted_obj_ = Support.getEl(this.exhausted_);
    }
  }
  this.initialized_ = true;
};

Support.prototype.showNext = function() {
  Support.log('showNext');

  if (this.curr_question_ + 1 > this.num_questions_) {
    this.handleExhausted();
    return;
  }

  Support.log(this.curr_question_);
  this.curr_question_ = this.curr_question_ + 1;
  Support.log(this.curr_question_);
  var quest_el = Support.getEl('question' + this.curr_question_); 
  Support.log(quest_el);

  quest_el.style.display = 'block';

  Support.insertAfter(this.next_obj_, quest_el);
  if (this.yes_obj_) {
    this.yes_obj_.checked = false;
  }

  if (this.no_obj_) {
    this.no_obj_.checked = false;
  }

  Support.log('here');
};

Support.prototype.hideEndpoints = function() {
  if (this.exhausted_obj_) {
    this.exhausted_obj_.style.display = 'none';
  }

  if (this.resolved_obj_) {
    this.resolved_obj_.style.display = 'none';
  }
};

Support.prototype.handleYes = function() {
  Support.log('yes');
  this.hideEndpoints();
  this.handleSuccess();
};

Support.prototype.handleNo = function() {
  Support.log('no');
  this.hideEndpoints();
  this.showNext();
};

Support.prototype.handleExhausted = function() {
  Support.log('exhausted');
  Support.insertAfter(this.exhausted_obj_, this.next_obj_);
  this.exhausted_obj_.style.display = 'block';
};

Support.prototype.handleSuccess = function() {
  Support.log('success');
  Support.insertAfter(this.resolved_obj_, this.next_obj_);
  this.resolved_obj_.style.display = 'block';
};

Support.load = function(next_question, resolved, exhausted, num_questions) {
  var SUPPORT = new Support(next_question, resolved, exhausted, num_questions);
  SUPPORT.initialize();
  window.SUPPORT = SUPPORT;
};

