{# ---------------------- Pager -------------------------- #}
{% macro pager(page, pages, generator) -%}
{% if pages > 1 %}
<div class="pagination">
    <ul>
    {% set min = page - 3 %}
    {% set max = page + 3 + 1 %}

    {% if min < 0 %}
	{% set max = max - min %}
    {% endif %}
    {% if max >= pages %}
	{% set min = min - max + pages %}
    {% endif %}

    {% if min < 0 %}
	{% set min = 0 %}
    {% endif %}
    {% if max >= pages %}
	{% set max = pages %}
    {% endif %}

    {% if min > 0 %}
    <li>
	<a href="{{ generator(0) }}">&laquo;</a>
    </li>
    {% else %}
    <li class="disabled">
	<a href="javascript:void(0)">&laquo;</a>
    </li>
    {% endif %}
    {% if page > 0 %}
    <li>
	<a href="{{ generator(page-1) }}">&lt;</a>
    </li>
    {% else %}
    <li class="disabled">
	<a href="javascript:void(0)">&lt;</a>
    </li>
    {% endif %}

    {% for p in range(min, max) %}
    {% if page == p %}
    <li class="active">
	<a href="javascript:void(0)">{{ p + 1 }}</a>
    </li>
    {% else %}
    <li>
	<a href="{{ generator(p) }}">{{ p + 1 }}</a>
    </li>
    {% endif %}
    {% endfor %}

    {% if page + 1 < pages %}
    <li>
	<a href="{{ generator(page + 1) }}">&gt;</a>
    </li>
    {% else %}
    <li class="disabled">
	<a href="javascript:void(0)">&gt;</a>
    </li>
    {% endif %}
    {% if max < pages %}
    <li>
	<a href="{{ generator(pages - 1) }}">&raquo;</a>
    </li>
    {% else %}
    <li class="disabled">
	<a href="javascript:void(0)">&raquo;</a>
    </li>
    {% endif %}
    </ul>
</div>
{% endif %}
{%- endmacro %}

{# ---------------------- Forms -------------------------- #}
{% macro render_field(form, field, kwargs={}) %}
  {% set direct_error = h.is_field_error(field.errors) %}
  <div class="control-group{{ ' error' if direct_error else '' }}">
    <div class="control-label">
      {{ field.label.text }}
      {% if h.is_required_form_field(field) %}
	<strong style="color: red">&#42;</strong>
      {% else %}
	&nbsp;
      {% endif %}
    </div>
    <div class="controls">
    <div>
      {{ field(**kwargs)|safe }}
    </div>
    {% if field.description %}
    <p class="help-block">{{ field.description }}</p>
    {% endif %}
    {% if direct_error %}
      <ul{% if direct_error %} class="input-errors"{% endif %}>
      {% for e in field.errors if e is string %}
	<li>{{ e }}</li>
      {% endfor %}
      </ul>
    {% endif %}
    </div>
  </div>
{% endmacro %}

{% macro render_header(form, text) %}
    <h3>{{ text }}</h3>
{% endmacro %}

{% macro render_form_fields(form, form_opts={}, form_rules=None) %}
    {% if form.hidden_tag is defined %}
	{{ form.hidden_tag() }}
    {% else %}

    {% if csrf_token %}
    <input type="hidden" name="csrf_token" value="{{ csrf_token() }}"/>
    {% endif %}

	{% for f in form if f.type == 'HiddenField' %}
	    {{ f }}
	{% endfor %}
    {% endif %}

    {% if form_rules %}
	{% for r in form_rules %}
	    {{ r(form, form_opts=form_opts) }}
	{% endfor %}
    {% else %}
	{% for f in form if f.type != 'HiddenField' and f.type != 'CSRFTokenField' %}
	  {% if form_opts %}
	    {% set kwargs = form_opts.widget_args.get(f.name, {}) %}
	  {% else %}
	    {% set kwargs = {} %}
	  {% endif %}
	  {{ render_field(form, f, kwargs) }}
	{% endfor %}
    {% endif %}
{% endmacro %}

{% macro form_tag(form=None) %}
    <form action="" method="POST" class=" well form-horizontal" enctype="multipart/form-data">
      <fieldset>
	  <legend>{{admin_view.name}} </legend>
	{{ caller() }}
      </fieldset>
    </form>
{% endmacro %}

{% macro render_form_buttons(cancel_url, extra=None) %}
    <div class="control-group">
      <div class="controls">
	<input type="submit" class="btn btn-primary" value="{{ _gettext('Save') }}" />
	{% if extra %}
	{{ extra }}
	{% endif %}
	{% if cancel_url %}
	<a href="{{ cancel_url }}" class="btn">{{ _gettext('Cancel') }}</a>
	{% endif %}
      </div>
    </div>
{% endmacro %}

{% macro render_form(form, cancel_url, extra=None, form_opts={}, form_rules=None) -%}
    {% call form_tag() %}
	{{ render_form_fields(form, form_opts=form_opts, form_rules=form_rules) }}
	{{ render_form_buttons(cancel_url, extra) }}
    {% endcall %}
{% endmacro %}

{% macro format_value(value) %}

    {% if value|isinstance('dict') %}

       {{ render_table(headers=('Key', 'Value'), values=value.items()) }}

    {% elif value|isinstance('list') or value|isinstance('tuple') %}

	<ul>
	    {%for item in value %}<li>{{format_value(item)}}</li> {% endfor %}
	</ul>

    {% elif value|isinstance('Blueprint') or value|isinstance('QuokkaModule') %}

	  {{render_table(headers=('Attribute', 'Value'), values=(("name", value.name), ("module", value.import_name), ("static", value.static_folder), ("templates", value.template_folder)))}}


    {% elif value|isinstance('Theme') %}

	{{render_table(headers=('Attribute', 'Value'), values=(("name", value.name), ("author", value.author),("description", value.description),("version", value.version),("site", value.website)))}}

    {% elif value|isinstance('Rule') %}

	{{render_table(headers=('Attribute', 'Value'), values=(("arguments", value.arguments),("defaults", value.defaults),("methods", value.methods), ("endpoint", value.endpoint)))}}


    {% else %}

       {{value}}

    {% endif %}

{% endmacro %}

{% macro render_table(headers, values) %}
<table class="table table-striped table-hover table-condensed">
    <thead>
       <tr>
	   {% for item in headers %}
	       <th>{{item}}</th>
	   {% endfor %}
       </tr>
    </thead>
    <tbody>

    {% for item in values %}
       <tr>
	   {%for subitem in item %}
	   <td>{{format_value(subitem)}}</td>
	   {% endfor %}
       </tr>
    {% endfor %}

    </tbody>
</table>
{% endmacro %}

{#
	{{render_table(headers=('Attribute', 'Value'), values=(("rule", value.rule), ("arguments", value.arguments),("defaults", value.defaults),("methods", value.methods)))}}



    {% elif value|isinstance('Blueprint') or value|isinstance('QuokkaModule') %}

{{render_table(headers=('Attribute', 'Value'), values=(("name", value.name), ("module", value.import_name), ("static", value.static_folder), ("templates", value.template_folder)))}}

    {% elif value|isinstance('Theme') %}

	{{render_table(headers=('Attribute', 'Value'), values=(("name", value.name), ("author", value.author),("description", value.description),("version", value.version),("site", value.website)))}}

    {% elif value|isinstance('Ruledsdsd') %}


#}