﻿(function(global) {
  'use strict';

  function Scope() {}

  var bindPattern = /([\w\.\$]*)[\s]*as[\s]*([\w]*)/;
  var repeatPattern = /([\w]*)[\s]*in[\s]*([\w\.\$]*)/;

  function createBindRepeatBinding(model, path, name, node) {
    var binding = new CompoundBinding(function(values) {
      return values['value'];
    });

    var scopeName, scopePath;
    var match = path.match(repeatPattern);
    if (match) {
      scopeName = match[1];
      scopePath = match[2];
    } else {
      match = path.match(bindPattern);
      if (match) {
        scopeName = match[2];
        scopePath = match[1];
      } else {
        return;
      }
    }

    binding.bind('value', model, scopePath);
    templateScopeTable.set(node, { model: model, scope: scopeName });
    return binding;
  }

  function createStringIfTruthyBinding(model, className, path) {
    var binding = new CompoundBinding(function(values) {
      return values['value'] ? className : '';
    });

    binding.bind('value', model, path);
    return binding;
  }

  var templateScopeTable = new SideTable;

  HTMLTemplateElement.syntax['Polymer'] = {
    getBinding: function(model, path, name, node) {
      if (node.nodeType === Node.ELEMENT_NODE &&
          (name === 'bind' || name === 'repeat') &&
          node.tagName === 'TEMPLATE') {
        return createBindRepeatBinding(model, path, name, node);
      }

      // {{ string: path }}
      var match = path.match(/([\w]+):[\W]*([\w\.\$]*)/);
      if (match)
        return createStringIfTruthyBinding(model, match[1], match[2]);
    },

    getInstanceModel: function(template, model) {
      var scopeInfo = templateScopeTable.get(template);
      if (!scopeInfo)
        return model;

      var scope;
      if (scopeInfo.model) { // instanceof Scope) {
        scope = Object.create(scopeInfo.model);
      } else {
        scope = new Scope();
      }

      scope[scopeInfo.scope] = model;
      return scope;
    }
  };
})(this);
@@ -99,16 +99,16 @@
 %5B%5Cs%5D
-*
++
 as%5B%5Cs%5D
-*
++
 (%5B%5Cw
@@ -152,16 +152,16 @@
 %5B%5Cs%5D
-*
++
 in%5B%5Cs%5D
-*
++
 (%5B%5Cw
@@ -240,108 +240,8 @@
  %7B%0D%0A
-    var binding = new CompoundBinding(function(values) %7B%0D%0A      return values%5B'value'%5D;%0D%0A    %7D);%0D%0A%0D%0A
     
@@ -568,24 +568,120 @@
   %7D%0D%0A    %7D%0D%0A
+    var binding = new CompoundBinding(function(values) %7B%0D%0A      return values%5B'value'%5D;%0D%0A    %7D);
 %0D%0A    bindin

correct scoped name regex: match "foo in bar" not "fooinbar"