BREV.WordMorph = {
  
  container_node: null,
  
  delay_list:   5000, // ms
  delay_phrase: 50,   // ms
  
  interval_list:    null,
  interval_phrase:  null,
  
  phrase_current: null,
  phrase_next:    null,
  phrase_list: [
    "12:45, restate my assumptions, press return",
    "One, mathematics is the language of nature",
    "Everything can be represented through numbers",
    "If you graph the numbers of any system, patterns emerge",
    "Therefore, there are patterns everywhere in nature",
    "Evidence: the cycling of disease epidemics",
    "Sunspot patterns, the rise and fall of the Nile",
    "So what about the stock market",
    "I'm better off without you tearing my will down",
    "Right in front of me, hiding behind the numbers",
    "It's just that this is not the way I'm wired",
    "Does this mean I'm afraid",
    "To convince you it's all right",
    "What am I to do with all this silence",
    "We've been over this before",
    "Just give me what I came for",
    "While I formulate denials of your effect on me"
  ],


  /**
   * Write current phrase to browser screen
   */
  drawCurrentPhrase: function() {
    this.container_node.innerHTML = this.phrase_current;
  },

  /**
   * Single step of merging two phrases together
   * @param string p1 First phrase
   * @param string p2 Second phrase
   * @return string|boolean Merged string on success or False on failure
   */
  getHybridPhrase: function(p1, p2) {
    if(p1 === p2) {
      return false; // same phrase, done
    }
    
    // find next unmatching letter index
    var p1list = p1.split('');
    var p2list = p2.split('');
    do {  
      var i = Math.floor(Math.random() * p2list.length);
    } while(p1list[i] === p2list[i]);
    
    // if p1 is bigger than p2 then remove a letter, or merge
    if(p1list.length > p2list.length) {
      p1list.splice(i, 1);  
    } else {
      p1list[i] = p2list[i];
    }
    
    return p1list.join('');
  },

  /**
   * Get a random phrase from the list
   * @return string Random phrase from list
   */
  getRandomPhrase: function() {
    var randex = Math.floor(Math.random() * this.phrase_list.length);
    return(this.phrase_list[randex]); 
  },

  /**
   * Start iterating through whole list of phrases, 1 at a time
   */
  iterateListStart: function() {
    var that = this;
    this.interval_list = setInterval(function() { that.iteratePhraseStart() }, this.delay_list);
  },

  /**
   * Modify current phrase a single step at a time until done
   */
  iteratePhraseChange: function() {
    var mid_phrase = this.getHybridPhrase(this.phrase_current, this.phrase_next);
    if(mid_phrase) {
      this.phrase_current = mid_phrase;
      this.drawCurrentPhrase();
    } else {
      this.iteratePhraseStop();
    }
  },
  
  /**
   * Start munging a single phrase because it's done
   */
  iteratePhraseStart: function() {
    this.phrase_next = this.getRandomPhrase();
    var that = this;
    this.interval_phrase = setInterval(function() { that.iteratePhraseChange() }, this.delay_phrase);
  },

  /**
   * Stop munging a single phrase because it's done
   */
  iteratePhraseStop: function() {
    clearInterval(this.interval_phrase);
  },


  /**
   * Init this object singleton
   * @param object containerEl Container node element for the phrases
   */
  init: function(containerEl) {
    this.container_node = containerEl;
    this.phrase_current = this.container_node.innerHTML;
    this.drawCurrentPhrase(); 
    this.iterateListStart();
  }

};

