/*
 * Copyright 2006, Jeffrey Palm.
 */


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

//---------------------------------------------------
// Constants
// --------------------------------------------------

//---------------------------------------------------
// Misc
// --------------------------------------------------



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

Myspace.Format = Class.create();

Myspace.Format.prototype = {

	initialize: function() {

	},

	_dummy: function() {}
};

// singleton
var myspaceFormat = new Myspace.Format();


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

	initialize: function() {},

	/** 
	 * Myspace.User ((String->String) -> _)
	 */
	setUserId: function(that, username,callback) {
		var thiz = this;
		var url = 'getUserId.pl';
		var prs = 'username=' + username;
		var cab = function (res) {
			/*
				<person username="itswhisler" id="3655464"/>
			*/
			var doc = getDoc(res);
			if (!doc) return;
			var person = getElementByTagName(doc,'person');
			var id = person.getAttribute('id');
			callback(id);
		};
		thiz.request(url,prs,cab);
	},

	/** 
	 * Myspace.User ((String->String) -> _)
	 */
	getUserInfo: function(that, callback) {
		var thiz = this;
		that.getId(function(id) {
								 var url = 'getUserInfo.pl';
								 var prs = 'id=' + id;
								 var cab = function (res) {
									 if (!res) return;
									 /*
										 <person name="Will" id="3655464"/>
									 */
									 var doc = getDoc(res);
									 if (!doc) return;
									 var person = getElementByTagName(doc,'person');
									 callback(person);
								 };
								 thiz.request(url,prs,cab);
							 });
	},

	/**
	 * Myspace.User (List(Myspace.User) -> _)
	 */
	getContacts: function(that, callback) {
	var thiz = this;
	that.getId(function(id) {
							 var url = 'getContacts.pl';
							 var prs = 'id=' + id;
							 var cab = function (res) {
									 var doc = getDoc(res);
									 if (!doc) return;
								 /*
									 <person id="3655464">
									 <contact name="THE Iron .." id="14516899"/>
									 <contact name="Chris" id="44194218"/>
								 */
								 var matches = [];
								 var contacts = doc.getElementsByTagName('contact');
								 if (contacts) {
									 for (var i=0; i<contacts.length; i++) {
										 var c = contacts[i];
										 var cName = c.getAttribute('name');
										 var cID = c.getAttribute('id');
										 var u = new Myspace.User(cName,false);
										 u.setId(cID);
										 matches.push(u);
									 }
								 }
								 callback(matches);
							 };
							 thiz.request(url,prs,cab);
						 });
	},
	

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

	_dummy: function() {}

};

var myspace = new Myspace.Service();


/**
 * These are created from usernames, names, and ids.  In order to
 * resolve these you need to call the getters with callbacks.  If you
 * want to just access the values simply access the fields.
 *
 * i.e. 'u.getName(function(name) {...})' means go find the name if
 * you don't have it, where as 'u.name' means just give me the name,
 * even if undefined.
 */
Myspace.User = Class.create();
Myspace.User.prototype = {

	initialize: function(username,resolveNow) {
		this.username = username;
		this.name = '';
		this.id = 0;
		this.age = 0;
		this.sex = 0;
		this.city = 0;
		this.state = 0;
		this.country = 0;
		this.isResolved = false;
		var thiz = this;
		if (resolveNow) {
			myspace.setUserId(this,this.username, function(id) {thiz.setId(id);});
		}
	},

	getUsername: function(callback) {
		if (this.username) callback(this.username);
		else {

		}
	},

	setId: function(id) {this.id = id;},
	setName: function(name) {this.name = name;},

	/**
	 * DOMElement -> DomElement
	 */
	save: function(n) {
		this.name = n.getAttribute('name');
		this.id = n.getAttribute('id');
		this.age = n.getAttribute('age');
		this.sex = n.getAttribute('sex');
		this.city = n.getAttribute('city');
		this.state = n.getAttribute('state');
		this.country = n.getAttribute('country');
		this.img = n.getAttribute('img');
		return n;
	},

	/**
	 *  -> String
	 * This will only be called after we've resolved
	 * TODO: THis is nasty, start using real continuations!!!
	 */
	getURL: function() {
		return "http://profile.myspace.com/index.cfm?fuseaction=user.viewprofile&friendid=" + this.id;
	},

	/**
	 * (String[id] -> _) -> _
	 */
	getId: function(callback) {
		if (this.id != 0) {
			callback(this.id);
		} else {
			var thiz = this;
			myspace.setUserId(this,this.username, callback);
			//myspace.getUserInfo(this,function(attrs) {callback(thiz.save(attrs).getAttribute('id'))});
		}
	},

	/**
	 * (String[name] -> _) -> _
	 */
	getName: function(callback) {
		if (this.name) {
			callback(this.name);
		} else {
			var thiz = this;
			myspace.getUserInfo(this,function(attrs) {callback(thiz.save(attrs).getAttribute('name'))});
		}
	},

	/**
	 * (String[name] -> _) -> _
	 */
	getImage: function(callback) {
		if (this.img) {
			callback(this.img);
		} else {
			var thiz = this;
			myspace.getUserInfo(this,function(attrs) {callback(thiz.save(attrs).getAttribute('img'))});
		}
	},

	/**
	 * (Myspace.User -> _) -> _
	 */
	resolve: function(callback) {
		if (this.isResolved) {
			callback(this);
			return;
		}
		var thiz = this;
		myspace.getUserInfo(this,function(attrs) {
													thiz.save(attrs);
													thiz.isResolved = true;
													callback(thiz);
												});
	},
	
	/**
	 * (List(Myspace.User) -> _) -> _
	 */
	getContacts: function(callback) {
		myspace.getContacts(this,callback);
	},

	/**
	 * (String[location] -> _) -> _
	 */
	getLocation: function(callback) {
		if (this.city || this.state || this.country) {
			callback(this.location());
		} else {
			var thiz = this;
			myspace.getUserInfo(this,function(attrs) {
														thiz.save(attrs); 
														callback(thiz.location());
													});
		}		
	},

	location: function() {
		return this.city + ", " + this.state + ", " + this.country;
	},

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

	_dummy: function() {}

};


