var bookmarks = new Array();

function bookmark(id, title, url, visited) {
    this.id = id;
    this.title = title;
    this.visited = visited;
}

function BookmarkContainer(divID, containerID, commandID) {
    this.divID = divID;
    this.containerID = containerID;
    this.commandID = commandID
    this.bookmarks = new Array();
    this.cookieName = "kht_bookmarks";
    this.iconIndex = "bmi_";
    this.loaded = false;
}


BookmarkContainer.prototype.showBookmarkIcon = function(id) {
    if (this.getBookmarkIndex(id) == null) {
        document.getElementById(this.iconIndex + id).style.visibility = "visible";
    }
}

BookmarkContainer.prototype.setBookmark = function(id, title, icon) {
    index = this.getBookmarkIndex(id);
    if (index == null) {
        this.bookmarks.push(new bookmark(id, title, false));
        icon.style.display = "none";
    }
    this.refreshContent();
    this.saveCookie();
}

BookmarkContainer.prototype.visitBookmark = function(id) {
    index = this.getBookmarkIndex(id);
    if (index != null) {
        this.bookmarks[index].visited = true;
    }
    this.refreshContent();
    this.saveCookie();
}

BookmarkContainer.prototype.delBookmark = function(id) {
    if (this.bookmarks == null || this.bookmarks.length == 0) {
        return false;
    }
    newBookmarks = new Array();
    for (i = 0; i < this.bookmarks.length; i++) {
        if (this.bookmarks[i] && this.bookmarks.id != id) {
            newBookmarks.push(this.bookmarks[i]);
        }
    }
    this.bookmarks = newBookmarks;
    this.refreshContent();
    this.saveCookie();
}

BookmarkContainer.prototype.delVisitedBookmarks = function() {
    if (this.bookmarks == null || this.bookmarks.length == 0) {
        return false;
    }
    newBookmarks = new Array();
    for (i = 0; i < this.bookmarks.length; i++) {
        if (this.bookmarks[i] && !this.bookmarks[i].visited) {
            newBookmarks.push(this.bookmarks[i]);
        }
    }
    this.bookmarks = newBookmarks;
    this.refreshContent();
    this.saveCookie();
}

BookmarkContainer.prototype.delAllBookmarks = function() {
    this.bookmarks = new Array();
    this.refreshContent();
    this.saveCookie();
}

BookmarkContainer.prototype.refreshContent = function() {
    var c = document.getElementById(this.containerID);
    if (!c) return false;

    if (this.bookmarks.length == 0) {
        c.innerHTML = "<center><br>Nincs megjelölt könyvjelző<br><br></center>";
        document.getElementById(this.commandID).style.display = "none";
    } else {
        c.innerHTML = "";
        document.getElementById(this.commandID).style.display = "block";
        var ul = document.createElement("ul");
        for (i = 0; i < this.bookmarks.length; i++) {
            if (!this.bookmarks[i]) continue;

            var li = document.createElement("li");
            li.className = this.bookmarks[i].visited ? "off" : "on";
            var a = document.createElement("a");
            a.href = "cikk/0/" + this.bookmarks[i].id + ".html";
            a.innerHTML = this.bookmarks[i].title;
            a.setAttribute("onclick", "bookmarks.visitBookmark(" + this.bookmarks[i].id + ");");
            a.onclick = "bookmarks.visitBookmark(" + this.bookmarks[i].id + ");";
            li.appendChild(a);
            ul.appendChild(li);
        }
        c.appendChild(ul);
    }
}

BookmarkContainer.prototype.loadCookie = function() {
    cookie = getCookie(this.cookieName);
    if (cookie) {
        this.bookmarks = cookie.parseJSON();
    }
    this.loaded = true;
}

BookmarkContainer.prototype.saveCookie = function() {
    if (!this.loaded) {
        return;
    }
    var exp = new Date();
    exp.setTime(exp.getTime() + (365 * 24 * 60 * 60 * 1000));
    setCookie(this.cookieName, this.bookmarks.toJSONString(), exp, "/");
}

BookmarkContainer.prototype.getBookmarkIndex = function(id) {
    if (!this.loaded || this.bookmarks.length == 0) {
        return null;
    }
    for (i = 0; i < this.bookmarks.length; i++) {
        if (this.bookmarks[i].id == id) {
            return i;
        }
    }
    return null;
}


BookmarkContainer.prototype.showContent = function() {
    document.getElementById("bookmark").style.display = "block";
}

BookmarkContainer.prototype.hideContent = function() {
    document.getElementById("bookmark").style.display = "none";
}