/*
 * Copyright 2008-2009 Palm, Inc. All rights reserved.
 */

/**
 * The data source for the bookmark list (BigList).
 */
BookmarkDataSource = Class.create({

	bookmarkModel: undefined,

	/**
	 * Constructor.
	 */
	initialize : function(bookmarkModel) {
		this.items = $A();
		this.limit = this.offset = 0;
		this.bookmarkModel = bookmarkModel;

		for (var i = 0; i < bookmarkModel.contents.length; i++) {
			var item = bookmarkModel.contents[i];
			if (item instanceof UrlReference) {
				this.items.push({title:item.title, url:item.url});
			}
		}
	},

	updateOffsetAndLimit: function(offset, limit) {
	    this.offset = offset;
	    this.limit = limit;
	    this.sendNewData();
	},

	dataAsResultSet: function() {
		var lastIndex = this.offset + this.limit;
		if (lastIndex > this.items.length) {
			lastIndex = this.items.length;
		}
		var offset = this.offset;
		if (offset > this.items.length) {
			offset = this.items.length;
		}
		var resultSet = {
			offset: offset,
			limit: lastIndex,
			list: this.items.slice(offset, lastIndex),
			total: this.items.length};
		var itemIndex = offset + 1;
		resultSet.list.each(function(item) {item.index = itemIndex;++itemIndex;});
		return resultSet;
	},

	sendNewData: function() {
		if (this.controller && this.controller.handleNewData) {
			var resultSet = this.dataAsResultSet();
			this.controller.handleNewData.bind(this.controller).delay(0.2, resultSet);
		}
	}
});

