function Bookmarks()
{
    this.cookieID = 'HtmlHelp-bookmarks';
    this.bookmarks = [];

    this.init = function()
    {
        if (!this.bookmarks.length) {
            $('bookmarks').innerHTML = 'No bookmarks found';
            return;
        }
        
        var html = '';
        html += '<div>Your bookmarks: <b>?</b></div>'.format(this.bookmarks.length);
        html += '<table cellspacing="0" cellpadding="0" class="bookmarks">';
        for (var i = 0; i < this.bookmarks.length; i++) {
            html += '<tr><td><a class="delete" title="Delete bookmark" href="javascript:Bookmarks.deleteBookmark(\''+this.bookmarks[i]+'\');"><img width="9" height="9" alt="Delete bookmark" title="Delete bookmark" border="0" src="'+Image_deleteBookmark+'"></a>&nbsp;</td>';
            html += '<td><img width="18" height="18" src="?"></td><td><a href="javascript:Bookmarks.loadBookmark(\'?\');">?</a></td></tr>'.format(Image_document, this.bookmarks[i], tree.getNodeIdName(this.bookmarks[i]));
        }
        html += '</table>';
        $('bookmarks').innerHTML = html;
    }

    this.bookmarkPage = function()
    {
        this.addBookmark(tree.getActiveNodeId());
        Options.init();
    }

    this.loadBookmark = function(bookmarkId)
    {
        Topics.loadTopic(bookmarkId);
    }

    this.addBookmark = function(bookmarkId)
    {
        if (!this.bookmarkExists(bookmarkId)) {
            this.bookmarks.push(bookmarkId);
            this.init();
        }
    }

    this.deleteBookmark = function(bookmarkId)
    {
        this.bookmarks.removeByValue(bookmarkId);
        this.init();
    }

    this.bookmarkExists = function(bookmarkId) {
        return this.bookmarks.contains(bookmarkId);
    }

    this.loadState = function()
    {
        this.bookmarks = [];
        if (getCookie(this.cookieID)) {
            this.bookmarks = getCookie(this.cookieID).split("|");
        }
        this.bookmarks.filter(function(bookmarkId) { return tree.documentIdExists(bookmarkId); });
    }

    this.saveState = function()
    {
        if (this.bookmarks.length) {
            setCookie(this.cookieID, this.bookmarks.join("|"));
        } else {
            delCookie(this.cookieID);
        }
    }
}
var Bookmarks = new Bookmarks();
