{% load thumbnail %}
{% load i18n %}
{% load staticfiles %}

{% with all_images=product.images.all %}
    {# use length rather then count as the images get queried anyways #}

    {% if all_images|length > 1 %}

        <div id="product_gallery" class="carousel slide">
            <div class="thumbnail">
                <div class="carousel-inner">
                {% for image in all_images %}
                    <div class="item {% if forloop.first %}active{% endif %}">
                        {% thumbnail image.original "440x400" upscale=False as thumb %}
                        <img src="{{ thumb.url }}" alt="{{ product.get_title }}" />
                        {% endthumbnail %}
                    </div>
                {% endfor %}
                </div>
            </div>

            <ol class="carousel-indicators thumbnail">
                {% for image in all_images %}
                    <li data-target="#product_gallery" data-slide-to="{{ forloop.counter0 }}" class="{% if forloop.first %}active{% endif %}">
                        {% thumbnail image.original "65x55" crop="center" as thumb %}
                        <img src="{{ thumb.url }}" alt="{{ product.get_title }}" />
                        {% endthumbnail %}
                    </li>
                {% endfor %}
            </ol>

            <a class="carousel-control left" href="#product_gallery" data-slide="prev">&lsaquo;</a>
            <a class="carousel-control right" href="#product_gallery" data-slide="next">&rsaquo;</a>
        </div>

    {% else %}

        {# Only one image to show #}
        <div id="product_gallery" class="carousel">
            <div class="thumbnail">
                <div class="carousel-inner">
                    <div class="item active">
                    {% with image=product.primary_image %}
                        {% thumbnail image.original "440x400" upscale=False as thumb %}
                            <img src="{{ thumb.url }}" alt="{{ product.get_title }}" />
                        {% endthumbnail %}
                    {% endwith %}
                    </div>
                </div>
            </div>
        </div>

    {% endif %}
{% endwith %}
