Type.registerNamespace('MySpace.ProfileDisplay');

// todo: namespace? MySpace.UI.blah or just MySpace.blah
MySpace.ProfileDisplay._InterestModule = function() {
	MySpace.ProfileDisplay._InterestModule.initializeBase(this);
};

MySpace.ProfileDisplay._InterestModule.prototype = {
	/// region  CONSTANTS
	_srchType: {
		general: "G",
		music: "Mu",
		movies: "M",
		books: "B",
		site: ""

 },
	
	/* Hex 3c & 3e are < and >, respectively */
	_htmlTagRegEx: /([\x3C\x3E])/gi,
	
	/*
		Delimiters in hex:
			,	\x2C
			(	\x28
			)	\x29
			-	\x2D
			:	\x3A
			;	\x3B
			*	\x2A
			/	\x2F
			\	\x5C
			.	\x2E
			!	\x21
			?	\x3F
			{	\x7B
			}	\x7D
			[	\x5B
			]	\x5D
			+	\x2B
			Control chars in hex:
			<	\x3C
			>	\x3E
	*/
	_itemsRegEx: /([^\s\x2C\x28\x29\x2D\x3A\x3B\x2A\x2F\x5C\x2E\x21\x3F\x7B\x7D\x5B\x5D\x2B]+([\s]+[^\s\x2C\x28\x29\x2D\x3A\x3B\x2A\x2F\x5C\x2E\x21\x3F\x7B\x7D\x5B\x5D\x2B]+)*)/g,

	// classes on each LI (this feature only applies to those ones listed below):
	_classRegEx: /interest(General|Music|Movies|Television|Books|Heroes)/,

	/// endregion  (CONSTANTS)


	initialize: function() {
	},

	addLinks: function(id) {
		var h = $get("module"+id);
		if (!h) return;
		var ls =  _proxy_jslib_handle(h, 'getElementsByTagName', '', 1, 0)('li');
		for (var i = 0; i < ls.length; i++){
			var m =  _proxy_jslib_handle(ls, (i), 0, 0).className.match(this._classRegEx);
			if (m && m.length > 0){
				var s =  _proxy_jslib_handle( _proxy_jslib_handle(ls, (i), 0, 0), 'getElementsByTagName', '', 1, 0)('span');
				if (s.length > 0)
					this._makeLinks( s[0], this._getSrchType(m[1]) );
			}
		}
	},
	
	_getSrchType: function(s) {
		switch (s){
			case "General": return this._srchType.general;
			case "Music": return this._srchType.music;
			case "Movies": return this._srchType.movies;
			case "Books": return this._srchType.books;
			default: return this._srchType.site;
		}
	},

	_makeLinks: function(e,srchType) {
		var s =  _proxy_jslib_handle(e, 'innerHTML', '', 0, 0);
		// SKIP doing this if either:
		//   1. User has included "<z>" in the field,
		//   2. User's content contains HTML tags (known by presence of "<" or ">")
		if (!this._htmlTagRegEx.test(s) && (s.indexOf('<z>') == -1))
			 _proxy_jslib_assign('', e, 'innerHTML', '=', (  _proxy_jslib_handle(s, 'replace', '', 1, 0)(this._itemsRegEx, "<a href=\"#\" title=\""+ 
				((srchType == this._srchType.site)?"search all MySpace":"find Friends with this Interest")+ // todo: i18n
				"\" onclick=\"MySpace.ProfileDisplay.InterestModule.search('" +srchType+ "',this.innerHTML);return false;\">$1</a>")));
	},
	
	search: function(srchType, srchTerm) {
		// Performs either a "site search" (all MySpace) or Find Friends with the given Interest
		srchTerm = encodeURI(srchTerm);
		if (srchType == this._srchType.site)
			 _proxy_jslib_assign('',  _proxy_jslib_handle(document, 'location', '', 0, 0), 'href', '=', ( "http://sads.myspace.com/index.cfm?fuseaction=advancedFind.results&searchtarget=tms&searchtype=myspace&t=tms&get=1&websearch=1&searchBoxID=HeaderWebResults&searchString=" +srchTerm+ "&q=" +srchTerm));
		else
			 _proxy_jslib_assign('',  _proxy_jslib_handle(document, 'location', '', 0, 0), 'href', '=', ( "http://searchservice.myspace.com/index.cfm?fuseaction=search&searchBy=First&interestType=" +srchType+ "&f_search_criteria=" +srchTerm));
	},
	
	dispose: function() {
	}
}

MySpace.ProfileDisplay.InterestModule = new (MySpace.ProfileDisplay._InterestModule)();
 ;
_proxy_jslib_flush_write_buffers() ;