'use strict';


/*global window, _, $, ko, N*/


var knownKeywords = _.chain(require('../../../lib/embedded_fonts/configs'))
  // get list of keywords of all glyphs in the font,
  // flatten array, make sure all elementes are Strings,
  // and return an array with unique elements only
  .map(function (font) {
    return _.map(font.glyphs, function (glyph) {
      return glyph.search;
    });
  }).flatten().map(String).uniq().value();


////////////////////////////////////////////////////////////////////////////////


function ToolbarModel(fontsList, N) {

  //
  // Essential properties
  //

  this.fontname = ko.observable('');
  this.fontSize = ko.observable(N.config.app.glyph_size.val).extend({ throttle: 100 });

  //
  // Proxy to fontsList properties
  //

  this.selectedGlyphs = fontsList.selectedGlyphs;
  this.selectedCount  = fontsList.selectedCount;

  //
  // Handlers
  //

  this.resetAll       = function () {
    if (window.confirm(N.runtime.t('confirm.app_reset'))) {
      this.fontname('');
      N.emit('reset_all');
    }
  }.bind(this);

  this.resetSelected  = function () {
    N.emit('reset_selected');
  };

  this.importFile     = require('./_import_file').bind(this);
  this.startDownload  = require('./_start_download').bind(this);

  //
  // Notify application about font size changes
  //

  this.fontSize.subscribe(function (value) {
    N.emit('font_size_change', value);
  });

  //
  // emit/subscribe session save/load
  //

  this.fontname.subscribe(function (value) {
    N.emit('session_save', { fontname: value });
  });

  N.on('session_load', function (session) {
    this.fontname(session.fontname || '');
  }.bind(this));
}


////////////////////////////////////////////////////////////////////////////////


module.exports = function (window, N) {
  N.once('fonts_ready', function (fontsList) {
    var toolbar = new ToolbarModel(fontsList, N);

    $(function () {
      var $view = $('#toolbar');

      //
      // Trigger change of hidden file input
      //

      toolbar.chooseFile = function (model, event) {
        event.preventDefault();

        if (!window.FileReader) {
          N.emit('notification', 'error', N.runtime.t('errors.no_file_reader'));
          return false;
        }

        $view.find('#import-file').click();
        return false;
      };

      //
      // Initialize jquery fontSize slider
      //

      $view.find('#glyph-size-slider').slider({
        orientation:  'horizontal',
        range:        'min',
        value:        N.config.app.glyph_size.val,
        min:          N.config.app.glyph_size.min,
        max:          N.config.app.glyph_size.max,
        slide:        function (event, ui) {
          /*jshint bitwise:false*/
          toolbar.fontSize(~~ui.value);
        }
      });

      //
      // Initialize Twitter Bootstrap typeahead plugin
      //

      $view.find('#search')
        .on('keyup', function (event) {
          N.emit('filter_keyword', $.trim($(this).val()));
        })
        .on('focus keyup', _.debounce(function () {
          $(this).typeahead('hide');
        }, 5000))
        .typeahead({
          source: knownKeywords
        });

      //
      // Apply KO bindings
      //

      ko.applyBindings(toolbar, $view.get(0));
    });
  });
};

@@ -548,16 +548,26 @@
 ntsList,
+ fontname,
  N) %7B%0A%0A 
@@ -624,25 +624,16 @@
 e = 
-ko.observable('')
+fontname
 ;%0A  
@@ -1418,65 +1418,174 @@
 %7D);%0A
-%0A  //%0A  // emit/subscribe session save/load%0A  //%0A%0A  this.
+%7D%0A%0A%0A////////////////////////////////////////////////////////////////////////////////%0A%0A%0Amodule.exports = function (window, N) %7B%0A  var fontname = ko.observable('');%0A%0A  
 font
@@ -1718,29 +1718,24 @@
 sion) %7B%0A    
-this.
 fontname(ses
@@ -1763,297 +1763,175 @@
 %0A  %7D
-.bind(this));%0A%7D%0A%0A%0A////////////////////////////////////////////////////////////////////////////////%0A%0A%0Amodule.exports = function (window, N) %7B%0A  N.once('fonts_ready', function (fontsList) %7B%0A    var toolbar = new ToolbarModel(fontsList, N);%0A%0A    $(function () %7B%0A      var $view = $('#toolbar'
+);%0A%0A  N.once('fonts_ready', function (fontsList) %7B%0A    $(function () %7B%0A      var%0A      $view   = $('#toolbar'),%0A      toolbar = new ToolbarModel(fontsList, fontname, N
 );%0A%0A

Fix fontname subscriptions