var GM = {
	version: "0.1"
};

GM.Consts = {};

GM.Utils = {};

GM.Utils.FavoriteManager = new Class({
	options: {
		cookie: "favorites",
		duration: 30,
		maxEntries: 15
	},
	
	initialize: function(options) {
		this.setOptions(options);
	},
	
	get: function() {
		//var favs = Cookie.get(this.options.cookie); //Mootools 1.1
		var favs = Cookie.read(this.options.cookie); //Mootools 1.2
		if (favs) {
			favs = favs.split(",");
			if (favs.length > this.options.maxEntries)
				favs.splice(0, favs.length - this.options.maxEntries);
			
			return favs.map(function(fav, idx) {
				return decodeURIComponent(fav);
			});
		}
		
		return [];
	},
	
	set: function(favs) {
		if (favs.length > 0) {
			if (favs.length > this.options.maxEntries)
				favs.splice(0, favs.length - this.options.maxEntries);
			
			Cookie.set(
				this.options.cookie,
				favs.map(function(fav, idx) {
					return encodeURIComponent(fav);
				}).join(","),
				{duration: this.options.duration}
			);
		}
		else
			this.removeAll();
		
		return this;
	},
	
	add: function(fav) {
		var favs = this.get();
		if (!favs.contains(fav)) {
			favs.push(fav);
			
			this.set(favs);
		}
		
		return this;
	},
	
	remove: function(fav) {
		var favs = this.get();
		if (favs.contains(fav))
			this.set(favs.remove(fav));
		
		return this;
	},
	
	removeAll: function() {
		Cookie.remove(this.options.cookie);
		
		return this;
	},
	
	contains: function(fav) {
		return this.get().contains(fav);
	},
	
	count: function() {
		return this.get().length;
	}
});
GM.Utils.FavoriteManager.implement(new Options);