define(['jquery', 'underscore', 'backbone', 'text!modules/search/foundViewTemplate.html', 'text!modules/search/foundItemTemplate.html', 'modules/base/baseView'],
        function ($, _, Backbone, foundViewTemplate, foundItemTemplate, BaseView) {

            var FoundView = BaseView.extend({
                collection: null,
                //initialize template
                template: _.template(foundViewTemplate),
                itemTemplate: _.template(foundItemTemplate),
                initialize: function (options) {
                    this.id = "FoundView";
                    var self = this;
                    FoundView.__super__.initialize.call(this);
                    this.collection = options.collection;

                    this.collection.fetch({
                        success: function () {
                            self.renderItem();
                        }});
                },
                isStayInPage: function () {
                    return false;
                },
                events: {
                    "click #pagSucc": "nextPageSearch",
                    "click #pagPrec": "prevPageSearch"
                },
                goTop: function (element) {
                    $('html, body').animate({
                        scrollTop: '0px'
                    }, 'fast');
                    return element; // for chaining...
                },
                renderItem: function () {
                    var $ul = $('#foundRicette');
                    $ul.html('');
                    var items = this.itemTemplate({data: this.collection.toJSON()});

                    $ul.html(items);
                    $ul.listview("refresh");
                    $ul.trigger("updatelayout");

                    if (this.collection.indexItems === 0)
                        $('#pagPrec').hide();
                    else
                        $('#pagPrec').show();

                    if (this.collection.getTotalRecords() < (this.collection.indexItems + 1) * this.collection.numItems)
                        $('#pagSucc').hide();
                    else
                        $('#pagSucc').show();

                    $.mobile.loading('hide');
                    //$( '[data-alpha="true"]' ).alphascroll();
                },
                nextPageSearch: function (e) {
                    e.preventDefault();
                    var self = this;
                    this.collection.indexItems++;
                    this.collection.fetch({reset: true,
                        success: function () {
                            self.renderItem();
                        }});
                    this.goTop();
                },
                prevPageSearch: function (e) {
                    e.preventDefault();
                    var self = this;
                    this.collection.indexItems--;
                    this.collection.fetch({reset: true,
                        success: function () {
                            self.renderItem();
                        }});
                },
                //render the content into div of view
                render: function () {
                    //this.header.title = "pippo";
                    FoundView.__super__.render.call(this);
                    //this.el is the root element of Backbone.View. By default, it is a div.
                    //$el is cached jQuery object for the view's element.
                    //append the compiled template into view div container
                    this.$el.append(this.header.$el);
                    this.$el.append(this.template());
                    this.$el.append(this.footer.$el);
                    //return to enable chained calls
                    this.$el.find("#searchBtn").addClass("ui-btn-active");
                    return this;
                }
            });
            return FoundView;
        });