/*
 * Copyright 2006, Jeffrey Palm.
 */


//
// here we add the node attributes to the Myspace elements
//

Myspace.UserNode = Class.create();
Myspace.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 Myspace.ContactsNode(this.user),
							//new Myspace.SetsNode(this.user),
							//new Myspace.PhotosNode(this.user),
							 ]);
	},	

	getIcon: function() {
		return 'upload/icon.jpg';
	},

	getName: function() {
		return 'Myspace';
	},

	/**
	 * Jeff.API (Jeff.Point -> Void) -> Void
	 */
	getPoint: function(api, callback) {
		this.user.getLocation(function(loc) {
														geocodeLocation(loc, function(p) {
																							callback(p);
																						});
													});
	},

	/**
	 * Jeff.API (String[html] -> _ ) -> _
	 * @interface
	 */
	getMainHTML: function(api, callback) {
		this.getMapHTML(api, callback);
	},

	/**
	 * Jeff.API (DOMElement[html] -> Void) -> Void
	 * @interface
	 */
	getMapHTML: function(api, callback) {
		var thiz = this;
		this.user.resolve(function(u) {
												
												var top = $n('div');
												var tab = $n('table');
												top.appendChild(tab);
												tab.border = "0";
												tab.cellpadding = "0";
												tab.cellspacing = "0";

												var tr = $n('tr');
												tab.appendChild(tr);
												var td = $n('td');
												tr.appendChild(td);
												var img = $n('img');
												td.appendChild(img);
												img.src= u.img;
												td = $n('td');
												tr.appendChild(td);
												td.style.verticalAlign = "top";
												var b = $n('b');
												td.appendChild(b);
												b.appendChild($t(u.toString()));
												td.appendChild($n('br'));
												td.appendChild($t(u.location()));
												
												td.appendChild($n('br'));
												td.appendChild($n('br'));

												// zoom link
												var a = $n('a');
												td.appendChild(a);
												a.href = "javascript:void(0)";
												a.onclick = thiz._zoomTo(api);
												a.appendChild($t('zoom'));

												// myspace link
												td.appendChild($n('br'));
												a = $n('a');
												td.appendChild(a);
												a.href = u.getURL();
												a.target = "_";
												a.appendChild($t('myspace info'));
												

												callback(top);
											});
	},

	_zoomTo: function(api) {
		return function(event) {
			api.getMap().zoomTo(6);
		};
	},

	_dummy: function() {}

};


Myspace.ContactsNode = Class.create();
Myspace.ContactsNode.prototype = {
	
	initialize: function(user) {
		this.user = user;
		this.kids = 0; //we actually want to save these for putting them on the map
	},

	toString: function() {
		return "Contacts";
	},
	
	getChildren: function(api,callback) {
		if (this.kids) {
			callback(this.kids);
			return;
		}
		var thiz = this;

		this.user.getContacts
		(function(ps) {
			thiz.kids = [];
			for (var i=0; i<ps.length; i++) {
				thiz.kids.push(new Myspace.UserNode(ps[i]));
				
			}
			callback(thiz.kids);
		});
	},

	onClick: function(api) {
		var thiz = this;
		this.getChildren(api, function(kids) {
											 var msg = "showing all of <em>" + thiz.user + "'s</em> contacts";
											 api.showMessage(msg);
											 $A(kids).each(function(k) {																			 
																			 api.showOnMap(k,false);
																		 });
										 });
	},

	_dummy: function() {}
	
};

