//FeedLounge Object Definitions

var fl = {
    NAME : "FeedLounge"
    ,VERSION : "1.0a5"
}

fl.Base = function() {
    this.className = "fl.Base";
    this.toString = function() {
        return "Base: " + this.repr();
    };
    this.repr = function() {
        return this.className;
    };
    this.test = function() {
        alert(this.toString());
    };
}

fl.DictEntry = function(k,v){
    this.key = k;
    this.value = v;
    this.className = "DictEntry";
    this.repr = function() {
        return k + ":" + v;
    };
}
fl.DictEntry.prototype = new fl.Base();
fl.DictEntry.prototype.constructor = fl.DictEntry;

fl.DictIterator = function(obj){
    var o = [] ;    //      Create an indexing array
    for (var p in obj) o[o.length] = obj[p] ;       //      fill it up
    var position = 0 ;
    this.current = null ;
    this.entry = null ;
    this.key = null ;
    this.value = null ;
    this.atEnd = false ;
    this.moveNext = function() {
        if (this.atEnd) return !this.atEnd ;
        this.entry = this.current = o[position] ;
        if (this.entry) {
            this.key = this.entry.key ;
            this.value = this.entry.value ;
        }
        if (position == o.length) this.atEnd = true ;
        position++ ;
        return !this.atEnd ;
    } ;
    this.reset = function() {
        position = 0 ;
        this.atEnd = false ;
    } ;
};

fl.Dict = function(){
    this.items = new Array();
    this.count = 0;
    this.className = "fl.Dict";
    this.name = "Dict";
    this.repr = function() {
        var result = this.name + ":[";
        var iter = this.getIterator();
        while (iter.moveNext()) {
            result += iter.value.repr() + ",";
        }
        result += "]";
        return result;
    };
    this.add = function(k,v){
        //What about when you are replacing the key, the count should not increase.
        this.items[k] = new fl.DictEntry(k,v);
        this.count++;
    };
    this.clear = function(){
        this.items = {};
        this.count = 0;
    };
    this.clone = function(){
        var d = new fl.Dict();
        for (var p in this.items) d.add(this.items[p].key, this.items[p].value);
        return d;
    };
    this.contains = this.containsKey = function(k){
        return (this.items[k] != null);
    };
    this.containsValue = function(v){
        var e = this.getIterator();
        while (e.moveNext()) {
            if (e.value == v) return true;
        }
        return false;
    };
    this.item = function(f){
        return this.items[k];
    };
    this.getIterator = function(){
        return new fl.DictIterator(this.items);
    };
    this.remove = function(k){
        delete this.items[k];
        this.count--;
    };
};
fl.Dict.prototype = new fl.Base();
fl.Dict.prototype.constructor = fl.Dict;

fl.FeedList = function() {
    this.className = "fl.FeedList";
    this.tags = new fl.Dict();
    this.tags.name = "tags";
    this.feeds = new fl.Dict();
    this.feeds.name = "feeds";
    this.addFeed = function(feed) {
        if (!this.feeds.contains(feed.id)) {
            this.feeds.add(feed.id, feed);
        }
    };
    this.addTag = function(tag) {
        if (!this.tags.contains(tag.id)) {
            this.tags.add(tag.id, tag);
        }
    };
}

fl.Feed = function(id) {
    this.className = "fl.Feed";
    this.id = id;
    this.title = "<Untitled>";
    this.rssLink = "";
    this.unreadCount = 0;
    this.tags = new fl.Dict();
    this.tags.name = "tags";
    this.repr = function() {
        return this.className + "(" + this.id + "){" + this.tags.repr() + "}";
    };
    this.addTag = function(tag) {
        if (!this.tags.contains(tag.id)) {
            this.tags.add(tag.id, tag);
        }
        if (!tag.feeds.contains(this.id)) {
            tag.addFeed(this);
        }
    };
}
fl.Feed.prototype = new fl.Base();
fl.Feed.prototype.constructor = fl.Feed;


fl.Tag = function(id) {
    this.className = "fl.Tag";
    this.id = id;
    this.title = "<Untitled>";
    this.feeds = new fl.Dict();
    this.feeds.name = "feeds";
    this.repr = function() {
        return this.name + ":{" + this.className + "}";
    };
    this.unreadCount = function() {
        var it = this.feeds.getIterator();
        var result = 0;
        while (it.moveNext()) {
            result += it.value.unreadCount;
        }
        return result;
    };
    this.addFeed = function(feed) {
        if (!this.feeds.contains(feed.id)) {
            this.feeds.add(feed.id, feed);
        }
        if (!feed.tags.contains(this.id)) {
            feed.addTag(this);
        }
    };
}
fl.Tag.prototype = new fl.Base();
fl.Tag.prototype.constructor = fl.Tag;


function testEverything() {
    var feedlist = new fl.FeedList();
    var feed = null;
    var tag = null;

    tag = new fl.Tag("6BC6877F-4349-BC5F-C0A8-6F055BB7DC62");
    tag.title = "a-list";
    feedlist.addTag(tag);

    for (i=0; i< 2500; i++) {
        feed = new fl.Feed(i);
        feed.title = "dotnot.org";
        feed.rssLink = "http://dotnot.org/blog/feed";
        feed.unreadCount = i;
        feed.addTag(tag);
        feedlist.addFeed(feed);
    }

    var feed = new fl.Feed("F3F76D7D-4349-C202-C0A8-6F05-1ADD72A");
    feed.title = "alexking.org";
    feed.rssLink = "http://alexking.org/blog/feed";
    feed.unreadCount = 15;
    feed.addTag(tag);
    feedlist.addFeed(feed);

    alert(tag.unreadCount());

    return true;
}

