App.Collections.ImapFolders = Backbone.Collection.extend({
	model: App.Models.ImapFolder,
	comparator:'globalName',
	currentAcct:0,
	deferred:null,
	
	initialize: function(){
		this.deferred = false;
	},

	url: function() {
		return App.baseUrl + '/inbox/folder?acct='+this.currentAcct;
	}
});

App.Collections.ImapFolders.prototype.setCurrentAcct = function(acct){
	this.currentAcct = acct;
}

App.Collections.ImapFolders.prototype.getCurrentAcct = function(){
	return this.currentAcct;
}

App.Collections.ImapFolders.prototype.getByGlobalName = function(name){

	if(typeof name === "undefined"){
		return null;
	}

	for(var i=0;i<this.length;i++){
		var m,n;

		m = this.at(i);

		if(m && m.get('globalName') === name ){
			return m;
		}
	}
	return null;
};

App.Collections.ImapFolders.prototype.getFolderUnreadCount = function(folderName){
	var folder = this.getByGlobalName(folderName);
	if(folder){
		return folder.get('unread');
	}
};

App.Collections.ImapFolders.prototype.addToFolderUnread = function(folderName, count){
	var unread, folder 

	folder = this.getByGlobalName(folderName);
	if(folder){
		unread = folder.get('unread');
		count += parseInt(unread, 10);
		folder.set('unread',count);
	}
		
};

App.Collections.ImapFolders.prototype.subFromFolderUnread = function(folderName, count){
	var unread, folder;
	
	folder = this.getByGlobalName(folderName);
	if(folder){
		unread = folder.get('unread');
		unread = parseInt(unread, 10) - count;
		folder.set('unread',unread);
	}
};
