var Collection = {};

Collection.Entity = Backbone.Collection.extend({
	model: Model.Entity,
	initialize : function(models, options) {
		if(options) {
			if(options.url) {
				this.url = options.url;
			}
			if(options.poll) {
				
			}
		}
	},
	getByAttribute: function(key, value) {
		var result = undefined;
		_.each(this.models, function(model) {
			if(model.get(key) == value) {
				result = model;
			}
		}, this);
		return result;
	},
	next: function(model) {
		return (i = this.indexOf(model)) < (this.size() - 1) ? this.at(i+1) : this.at(0);
	},
	prev: function(model) {
		return (i = this.indexOf(model)) > 0 ? this.at(i-1) : this.at(this.size()-1);
	}
});

Collection.Template = Backbone.Collection.extend({
	model : Model.Template,
	fetch : function(options) {
		var that = this;
		$.get('/html/templates.html', function(data) {
			$(data).filter('script').each(function(index, element) {
				that.add(new Model.Template({id: $(element).attr('id'), html: $(element).html()}));
			});
			if(options.success) options.success(that);
		});
	}
});
