/**
 * @author sundar
 */
(function () {
	var util = ZOOMIN.util.Util;
	var DEFAULT_OPTIONS = {
		total : 0,
		pageSize : 0,
		totalPages : 0,
		idField : "ImageId"
	};
	ZOOMIN.util.Datastore = function (options) {
		options = util.initializeOptions(options, DEFAULT_OPTIONS);
		this.pageSize = options.pageSize;
		this.total = options.total;
		this.totalPages = Math.ceil(this.total / this.pageSize);
		this.idField = options.idField;
		this.clear();
	};
	ZOOMIN.util.Datastore.prototype = {
		data : null,
		index : null,
		pageSize : 0,
		total : 0,
		totalPages : 0,
		idField : null,
		changePageSize : function (size) {
			this.pageSize = size;
			this.totalPages = Math.ceil(this.total / this.pageSize);
			this.clear();
		},
		isPageLoaded : function (page) {
			var res = false, start, end;
			if (typeof page !== "undefined" && page < this.totalPages) {
				start = page * this.pageSize;
				end = (page + 1) * this.pageSize;
				if (end > this.total) {
					end = this.total;
				}
				res = true;
				for (var i = start; i < end; i++) {
					if (!this.data[i]) {
						res = false;
						break;
					}
				}
			}
			return res;
		},
		getPageData : function(page) {
			var res = [], start, end;
			if (typeof page !== "undefined" && page < this.totalPages) {
				start = page * this.pageSize;
				end = (page + 1) * this.pageSize;
				if (end > this.total) {
					end = this.total;
				}
				for (var i = start; i < end; i++) {
					if (this.data[i]) {
						res.push(this.data[i]);
					} else {
						res = [];
						break;
					}
				}
			}
			return res;
		},
		addPageData : function(arr, page) {
			if ($.isArray(arr)) {
				if (!this.isPageLoaded(page)) {
					for (var i = page * this.pageSize, j = 0; j < arr.length; j++, i++) {
						this.data[i] = arr[j];
						this.index[this.data[i][this.idField]] = i;
					}
				}
			}
		},
		updateData: function(id, ob) {
			var item = this.getItem(id);
			if (typeof item !== "undefined" && typeof ob !== "undefined") {
				for (var prop in ob) {
					if (util.hasOwnProperty(ob, prop) && prop !== this.idField) {
						item[prop] = ob[prop];
					}
				}
			}
		},
		clear : function (page) {
			var i, item, total;
			if (typeof page === "undefined") {
				this.data = new Array(this.total);
				this.index = {};
			} else {
				if (page < this.totalPages) {
					total = (page + 1) * this.pageSize;
					for (i = page * this.pageSize; i < total; i++) {
						item = this.data[i];
						this.data[i] = undefined;
						if (typeof item !== "undefined") {
							this.index[item[this.idField]] = undefined;
						}
						item = null;
					}
				}
			}
		},
		getItem: function(id) {
			var pos, item;
			
			var pos = this.getPosition(id);
			item = this.data[pos.position];
			return item;
		},
		getPage: function(id) {
			var pos = this.getPosition(id);
			return pos.page;
		},
		getPosition: function(id) {
			var pos, page;
			
			if (util.hasOwnProperty(this.index, id)) {
				pos = this.index[id];
				page = Math.floor(pos / this.pageSize);
			}
			
			return {
				position: pos,
				page: page
			};
		},
		getNextItem: function(id) {
			var pos = this.getPosition(id);
			var item;
			if (typeof pos.position !== "undefined" && typeof pos.page !== "undefined") {
				if (pos.position < (this.total - 1)) {
					pos.position += 1;
					item = this.data[pos.position];
				}
			}
			return item;
		},
		getPrevItem: function(id) {
			var pos = this.getPosition(id);
			var item;
			if (typeof pos.position !== "undefined" && typeof pos.page !== "undefined") {
				if (pos.position !== 0) {
					pos.position -= 1;
					item = this.data[pos.position];
				}
			}
			return item;
		},
		deleteItem: function(id) {
			var pos = this.getPosition(id);
			var item;
			if (typeof pos.position !== "undefined") {
				item = this.data[pos.position];
				this.data.splice(pos.position, 1);
				this.total--;
				this.reIndex();
			}
			return item;
		},
		reIndex : function() {
			this.totalPages = Math.ceil(this.total / this.pageSize);
			this.index = {};
			for (var i = 0; i < this.total; i++) {
				if (this.data[i]) {
					this.index[this.data[i][this.idField]] = i;
				}
			}
		}
		
	};
})();
