/*
 * Copyright 2006, Jeffrey Palm.
 */


var Delicious = {
  Version: '1.1.2',
  prototypeVersion: parseFloat(Prototype.Version.split(".")[0] + "." + Prototype.Version.split(".")[1])
};
if((typeof Prototype=='undefined') || Delicious.prototypeVersion < 1.3)
	throw("Delicious requires the Prototype JavaScript framework >= 1.3");

// --------------------------------------------------
// Classes
// --------------------------------------------------

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

	initialize: function() {},

	/**
	 * Delicious.User (List(Delicious.User) -> _)
	 */
	getNetwork: function(that, callback) {
		var url = 'getNetwork.pl';
		var prs = 'username=' + that.getUsername();
		var cab = function (doc) {
			/*
				<person query="http://del.icio.us/network/bengebre" username="bengebre">
				 <contact username="atomicham"/>
				 <contact username="fi5e"/>
				 <contact username="fruminator"/>
				</person>
			*/
			var matches = [];
			var vs = doc.getElementsByTagName('contact');
			if (vs) {
				for (var i=0; i<vs.length; i++) {
					var v = vs[i];
					var username = v.getAttribute('username');
					matches.push(new Delicious.User(username));
				}
			}
			callback(matches);
		}
		this.request(url,prs,cab);
	},

	/**
	 * Delicious.User (List(Delicious.Tag) -> _)
	 */
	getTags: function(that, callback) {
		var url = 'getTags.pl';
		var prs = 'username=' + that.getUsername();
		var cab = function (doc) {
			/*
				<person query="http://del.icio.us/bengebre" username="bengebre">
				 <tag num="1" name="20something"/>
				 <tag num="2" name="3d"/>
				 <tag num="2" name="_blank"/>
				 <tag num="2" name="adsense"/>
				 <tag num="2" name="advertising"/>
				 <tag num="2" name="ai"/>
			 </person>
			*/
			var matches = [];
			var vs = doc.getElementsByTagName('tag');
			if (vs) {
				for (var i=0; i<vs.length; i++) {
					var v = vs[i];
					var name = v.getAttribute('name');
					var num = v.getAttribute('num');
					matches.push(new Delicious.Tag(that,name,num));
				}
			}
			callback(matches);
		}
		this.request(url,prs,cab);
	},

	/**
	 * Delicious.Tag (List(Delicious.Tag) -> _)
	 */
	getRelatedTags: function(that, callback) {
		var url = 'getRelatedTags.pl';
		var prs = 'username=' + that.getUser().getUsername() + '&tag=' + that.getName();
		var cab = function (doc) {
			/*
				<person query="http://del.icio.us/bengebre" username="bengebre">
				 <tag num="1" name="20something"/>
				 <tag num="2" name="3d"/>
				 <tag num="2" name="_blank"/>
				 <tag num="2" name="adsense"/>
				 <tag num="2" name="advertising"/>
				 <tag num="2" name="ai"/>
			 </person>
			*/
			var matches = [];
			var vs = doc.getElementsByTagName('tag');
			if (vs) {
				for (var i=0; i<vs.length; i++) {
					var v = vs[i];
					var name = v.getAttribute('name');
					var num = v.getAttribute('num');
					matches.push(new Delicious.Tag(that.getUser(),name,num));
				}
			}
			callback(matches);
		}
		this.request(url,prs,cab);
	},

	/**
	 * Delicious.User String (List(Delicious.Post) -> _)
	 */
	getPosts: function(that,tag,callback) {
		var url = 'getPosts.pl';
		var prs = 'username=' + that.getUsername() + '&tag=' + tag;
		var cab = function (doc) {
			/*
				<person query="http://del.icio.us/bengebre/ajax" username="bengebre">
				 <post link="http://weblog.rubyonrails.com/..." 
				       notes="" 
							 title="Rails 1.1: RJS, ..."/>
				</person>
			*/
			var matches = [];
			var vs = doc.getElementsByTagName('post');
			if (vs) {
				for (var i=0; i<vs.length; i++) {
					var v = vs[i];
					var link = v.getAttribute('link');
					var notes = v.getAttribute('notes');
					var title = v.getAttribute('title');
					matches.push(new Delicious.Post(link,notes,title));
				}
			}
			callback(matches);
		}
		this.request(url,prs,cab);
	},


	request: function(method, params, callback) {
		var url = '/cgi-bin/delicious/' + method;
		new Ajax.Request (url,
											{  method: 'get', 
													parameters: params, 
													onSuccess: function(res) {
													var doc = getDoc(res);
													if (doc) callback(doc);
												}
													});
	},

	_dummy: function() {}

};

var delicious = new Delicious.Service();

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

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

	getUsername: function() {
		return this.username;
	},

	getNetwork: function(callback) {
		delicious.getNetwork(this,callback);
	},

	getTags: function(callback) {
		delicious.getTags(this,callback);
	},

	toString: function() {
		return this.username;
	},

	/**
	 * String (List[Delicious.Post] -> _) -> _
	 */
	getPosts: function(tag,callback) {
		delicious.getPosts(this,tag,callback);
	},

	_dummy: function() {}

};

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

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

	getUser: function() {
		return this.user;
	},
	
	getName: function() {
		return this.name;
	},

	getNum: function() {
		return this.num;
	},

	getRelated: function(callback) {
		delicious.getRelatedTags(this,callback);
	},

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

	_dummy: function() {}

};

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

	initialize: function(link,notes,title) {
		this.link = link;
		this.notes = notes;
		this.title = title;
	},

	getLink:  function() {return this.link;},
	getNotes: function() {return this.notes;},
	getTitle: function() {return this.title;},

	_dummy: function() {}
};


