define(['jquery', 'underscore', 'backbone', 'text!modules/note/noteViewTemplate.html', 'text!modules/note/noteItemTemplate.html', 'modules/base/baseView', 'classes/profile'],
        function ($, _, Backbone, noteViewTemplate, noteItemTemplate, BaseView, Profile) {

            var NoteView = BaseView.extend({
                collection: null,
                //initialize template
                template: _.template(noteViewTemplate),
                itemtemplate: _.template(noteItemTemplate),
                current_ricetta_id: null,
                initialize: function (options) {
                    this.id = "NoteView";
                    var self = this;
                    NoteView.__super__.initialize.call(this);
                    this.collection = options.collection;
                    this.collection.fetch({success: function () {
                            self.renderItem();
                        }});
                },
                events: {
                    "click #deleteItem": "deleteItemClick",
                    "popupafterclose #popupMenu": "popupMenuPopupafterclose"
                },
                updateList: function () {
                    var $ul = this.$el.find('#allNote');
                    $ul.html('');
                    var items = this.itemtemplate({data: this.collection.toJSON()});

                    $ul.html(items);
                    $ul.listview("refresh");
                    $ul.trigger("updatelayout");
                },
                renderItem: function () {
                    this.updateList();

                    $.mobile.loading('hide');
                    var self = this;
                    this.$el.find('.optionLink').on("click", function (e) {
                        e.preventDefault();
                        self.current_ricetta_id = $(e.target.parentElement).find('#itemID').text();
                        self.$el.find('#popupMenu').popup("open");
                    });
                },
                deleteItemClick: function (e) {
                    e.preventDefault();
                    var self = this;
                    Profile.removeRicettaBloccoNote(this.current_ricetta_id, function (msg) {
                        var item = self.collection.get(self.current_ricetta_id);

                        self.$el.find('#popupMenu').popup("close");

                        self.collection.remove(item);
                        self.updateList();
                    });
                },
                popupMenuPopupafterclose: function (event, ui) {
                    this.current_ricetta_id = null;
                },
                //render the content into div of view
                render: function () {
                    if (this.created === true)
                    {
                        //this.header.title = "pippo";
                        NoteView.__super__.render.call(this);

                        var self = 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("#noteBtn").addClass("ui-btn-active");
                    return this;
                }
            });
            return NoteView;
        });