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

/**
 * A folder of bookmarks and other folders
 */
BookmarkFolder = Class.create({

	/**
	 * Constructor.
	 */
	initialize: function(name) {
		this.contents = $A();
		this.name = name;
	},

	/**
	 * Add a UrlReference or BookmarkFolder to this folder. The caller
	 * is responsible for checking for duplicates.
	 * 
	 * @param {UrlReference} item	The item to add. Caller is responsible for ensuring no dups.
	 */
	addItem: function(item) {
		this.contents.push(item);
	},

	/**
	 * Remove the item (UrlReference or BookmarkFolder) from this folder.
	 * 
	 * * @param {UrlReference} item The item to remove.
	 */
	removeItem: function(item) {
		for (var i = 0; i < this.contents.length; i++) {
			if (this.contents[i] == item) {
				this.contents.splice(i, 1);
				return;
			}
		}
	},

	/**
	 * Looks for a UrlReference recursively in this folder and all subfolders
	 *  and returns it if found.
	 *
	 * @return The item, or undefined if not found.
	 */
	findUrlRecursive: function(url) {
		// TODO - Need to implement recursive function.
		return this.findUrl(url);
	},

	/**
	 * Looks for a UrlReference and returns it if found.
	 *
	 * @return The item, or undefined if not found.
	 */
	findUrl: function(url) {
		for (var i = 0; i < this.contents.length; i++) {
			var item = this.contents[i];
			if (item instanceof UrlReference) {
				if (item.url == url) {
					return item;
				}
			}
		}
	},

	/**
	 * Looks for a BookmarkFolder and returns it if found.
	 *
	 * @return The folder, or undefined if not found.
	 */
	findFolder: function(folderName) {
		for (var i = 0; i < this.contents.length; i++) {
			var item = this.contents[i];
			if (item instanceof BookmarkFolder) {
				if (item.name == folderName) {
					return item;
				}
			}
		}
	}
});


