/*
 * Copyright 2006, Jeffrey Palm.
 */


Delicious.Format = Class.create();
Delicious.Format.prototype = {
	initialize: function() {},

	/**
	 * List(Delicious.Post) -> DOM|String
	 */
	formatPosts: function(title,posts) {
			var top = $n('div');
			var h1 = $n('h1',top);
			h1.innerHTML = title;
			for (var i=0; i<posts.length; i++) {
				var post = posts[i];
				var a = $n('a',top);
				a.href = post.getLink();
				a.innerHTML = post.getTitle();
				top.appendChild($n('br'));
			}
			return top;
	},

	_dummy: function() {}
};

var deliciousFormat = new Delicious.Format();

Delicious.UserNode = Class.create();
Delicious.UserNode.prototype = {

	initialize: function(user,isRoot) {
		this.user = user;
		this.isRoot = isRoot;
	},

	toString: function() {
		var s = '';
		if (this.isRoot) s += this.getName() + " (";
		s += this.user.toString();
		if (this.isRoot) s += ")";
		return s;
	},

	getChildren: function(api,callback) {
		callback([ 
							new Delicious.NetworkNode(this.user),
							new Delicious.TagsNode(this.user)
							 ]);
	},	

	getName: function() {
		return 'Del.icio.us';
	},


	/**
	 * Jeff.API (String[html] -> _ ) -> _
	 * @interface
	 */
	getMainHTML: function(api, callback) {
		var thiz=this;
		this.user.getPosts('_none_', function (posts) {
												 callback(deliciousFormat.formatPosts(thiz.user.getUsername(),posts));
											 });
	},

	_dummy: function() {}

};


Delicious.NetworkNode = Class.create();
Delicious.NetworkNode.prototype = {
	
	initialize: function(user) {
		this.user = user;
	},

	toString: function() {
		return "Network";
	},
	
	getChildren: function(api,callback) {
		this.user.getNetwork
		(function(ps) {
			var kids = [];
			for (var i=0; i<ps.length; i++) {
				kids.push(new Delicious.UserNode(ps[i]));
			}
			callback(kids);
		});
	},

	_dummy: function() {}
	
};

Delicious.TagsNode = Class.create();
Delicious.TagsNode.prototype = {
	
	initialize: function(user) {
		this.user = user;
	},

	toString: function() {
		return "Tags";
	},

	getChildren: function(api,callback) {
		this.user.getTags
		(function(ps) {
			var kids = [];
			for (var i=0; i<ps.length; i++) {
				kids.push(new Delicious.TagNode(ps[i]));
			}
			callback(kids);
		});
	},
	
	_dummy: function() {}
	
};

Delicious.TagNode = Class.create();
Delicious.TagNode.prototype = {
	
	/*
	 * Can't call this tag, because of stupid arrays
	 */

	initialize: function(tag) {
		this.tag = tag;
	},

	toString: function() {
		return ""+this.tag;
	},

	getChildren: function(api,callback) {
 		callback([  
 							new Delicious.RelatedTagsNode(this.tag) 
 							]); 
	},

	/**
	 * Jeff.API (String[html] -> _ ) -> _
	 * @interface
	 */
	getMainHTML: function(api, callback) {
		var thiz=this;
		this.tag.getUser().getPosts(this.tag.getName(), 
																function (posts) {
																	callback(deliciousFormat.formatPosts(thiz.tag.getUser().getUsername() + 
																																			 " / " + thiz.tag.getName(),
																																			 posts));	
																});
	},
	
	_dummy: function() {}
	
};

Delicious.RelatedTagsNode = Class.create();
Delicious.RelatedTagsNode.prototype = {
	
	initialize: function(tag) {
		this.tag = tag;
	},

	toString: function() {
		return "Related";
	},

	getChildren: function(api,callback) {
		this.tag.getRelated
		(function(ps) {
			var kids = [];
			for (var i=0; i<ps.length; i++) {
				kids.push(new Delicious.TagNode(ps[i]));
			}
			callback(kids);
		});
	},
	
	_dummy: function() {}
	
};

