/**
 *    Copyright 2006 - 2011 Opera Software ASA
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 *
 **/


window.templates || (window.templates = {});

templates.repl_main = function()
{
  return [
    "div", [[
      ["div", [
         "ol", "class", "repl-lines js-source"
         ], "class", "repl-output"],
      ["div", [[
        ["span", ">>>\xA0", "class", "repl-prefix"],
        ["div", ["textarea",
                 "focus-handler", "repl-textarea",
                 "blur-handler", "blur-textarea",
                 "rows", "1"]]
      ]], "class", "repl-input"]
    ]], "class", "padding"
  ];
};

templates.repl_output_native = function(s)
{
  return ["span", s, "class", "repl-native"];
};

templates.repl_output_native_or_pobj = function(thing)
{
  if (thing.type == "native") {
    return templates.repl_output_native(thing.value);
  }
  else
  {
    return templates.repl_output_pobj(thing);
  };
};

templates.repl_output_pobj = function(data)
{
  var is_element_type = settings.command_line.get("is-element-type-sensitive") && 
                        /(?:Element)$/.test(data.name)
  return [
    'code',
    data.friendly_printed ? this.friendly_print(data.friendly_printed) : data.name,
    'handler', is_element_type ? 'inspect-node-link' : 'inspect-object-link',
    'rt-id', data.rt_id.toString(),
    'obj-id', data.obj_id.toString(),
    'class', 'repl-pobj'
  ];
};

templates.repl_output_traceentry = function(frame, index)
{
    var tpl = ['li',
      ui_strings.S_TEXT_CALL_STACK_FRAME_LINE.
        replace("%(FUNCTION_NAME)s", ( frame.objectValue ? frame.objectValue.functionName : ui_strings.ANONYMOUS_FUNCTION_NAME ) ).
        replace("%(LINE_NUMBER)s", ( frame.lineNumber || '-' ) ).
        replace("%(SCRIPT_ID)s", ( frame.scriptID || '-' ) ),
      'ref-id', index.toString(),
      'script-id', String(frame.scriptID), //.toString(),
      'line-number', String(frame.lineNumber),
      'scope-variable-object-id', String(frame.variableObject),
      'this-object-id', String(frame.thisObject),
      'arguments-object-id', String(frame.argumentObject)
    ];
  return tpl;
};

templates.repl_output_trace = function(trace)
{
  var lis = trace.frameList.map(templates.repl_output_traceentry);
  var tpl = ["div", ["ol", lis, "class", "console-trace",
                     'handler', 'select-trace-frame',
                     'runtime-id', trace.runtimeID.toString()
                    ],
                    "class", "console-trace-container"];
  return tpl;
};

templates.repl_group_line = function(group)
{
  return [["button", "class", "folder-key"+(group.collapsed ? "" : " open" ),
                     "handler", "repl-toggle-group", "group-id", group.id
          ], group.name];
};

templates.repl_output_location_link = function(id, line)
{
  return ["span", "(" + line + ")",
                          "class", "repl-output-go-to-source",
                          "handler", "show-log-entry-source",
                          "data-scriptid", String(id),
                          "data-scriptline", String(line)
         ];
}