/**
 * profiles.js
 * @author Bram Van Damme <bramus@netlash.com>
 */
	// jQuery Wrapper
		/**
		 * JS_AB Object
		 */
			if (!JS_AB) { var JS_AB = new Object(); }

		/**
		 * JS_AB - Profiles object
		 */
			JS_AB.profiles = {

				/**
				 * Datamembers
				 */
					debug				: false,

				/**
				 * init
				 * @return void
				 */
					init				: function() {

						// init registerHandler
						JS_AB.profiles.registerHandler.init();

						// init urlHandler
						JS_AB.profiles.urlHandler.init();

						// init commentsHandler
						JS_AB.profiles.commentsHandler.init();

						// init friendsHandler
						JS_AB.profiles.friendsHandler.init();

						// init quickSearchHandler
						JS_AB.profiles.quickFriendSearchHandler.init();
					}
			}
		/*
		 * JS_AB - Profiles object - registerHandler (handles the registration part)
		 */
			JS_AB.profiles.registerHandler = {
				/**
				 * Datamembers (config)
				 */
					spinner			: '<img src="/modules/core/layout/images/spinner.gif" id="spinner_register" />',
					timeToWait		: 700,
					timer			: null,
					evt				: null,
					existsusername	: 0,
					existsurl		: 0,
					defaultUrl		: null,


				/**
				 * init - hook ourselves to the search field!
				 * @return void
				 * -------------------------------------------------------------
				 */

					init			: function() {

						if ($('#registerform_step2, #registerform_step3').length > 0) {

							// store the default URL
							JS_AB.profiles.registerHandler.defaultUrl = $('#yoururl').html();

							// inject span if needed
							if ($('#nickname_wrapper span').length == 0) {
								$('#nickname').after('<span style="display: none;"></span>');
							}

							// hook the keypress event of the username
							$('#nickname').attr('autocomplete','off').bind('keypress', function(evt) {
								// don't let 'm type if we're already checking!
								if ($('#spinner_register').length > 0) {
									evt.preventDefault();

								// aah, we're not checking yet ... go bramus go!
								} else {

									// clear previous timer
									clearTimeout(JS_AB.profiles.registerHandler.timer);

									// Store the event
									JS_AB.profiles.registerHandler.evt = evt;

									// Che-che-che ... check.it.out!
									JS_AB.profiles.registerHandler.timer = setTimeout(function() { JS_AB.profiles.registerHandler._initCheckUsername(); }, JS_AB.profiles.registerHandler.timeToWait);
								}
							});

							// hook the submitting of the form
							$('#registerform_step2, #registerform_step2').bind('submit', function(evt) {

								// don't submit if we're still checking ;)
								if ($('#spinner_register').length > 0) {
									evt.preventDefault();

								// not checking the username ... go ahead (make.my.day!)
								} else {

									// Check the form
									formOK = JS_AB.profiles.registerHandler.checkFormStep2();

									// Form not ok, don't submit!
									if (formOK !== true) {
										evt.preventDefault();
										alert(formOK);
									}

								}

							});
						}
					},


				/**
				 * checkFormStep2 - Checks the form
				 * @return mixed
				 * -------------------------------------------------------------
				 */


					checkFormStep2			: function() {

						// Some fields not filled
						if (!JS_NETLASH.utils.form.isFilled($('#nickname')) || !JS_NETLASH.utils.form.isFilled($('#name')) || !JS_NETLASH.utils.form.isFilled($('#surname')) || !JS_NETLASH.utils.form.isChecked($('#sex_Male')))
							return $('#formchecker_warning').html();

						// Invalid username
						if (JS_NETLASH.utils.string.urlise($('#nickname').val()) == '')
							return $('#formchecker_username_invalid').html();

						// Username already taken
						if (JS_AB.profiles.registerHandler.existsusername == 1)
							return $('#formchecker_username_exists').html();

						// All clear now :-)
							return true;

					},


				/**
				 * _initCheckUsername - Handle the flag comment link click event
				 * @return void
				 * -------------------------------------------------------------
				 */

					_initCheckUsername		: function(evt) {

						// get the event
							evt = evt || JS_AB.profiles.registerHandler.evt;

						// debug
							if (JS_AB.profiles.debug)	console.log(evt);

						// hide previous message
							$('#nickname_wrapper span').hide();

						// inject spinner
							$(evt.target).after(JS_AB.profiles.registerHandler.spinner);

						// get username
							username	= $(evt.target).val();

						// don't call if empty!
							if ($.trim(username) == '') {
								JS_AB.profiles.registerHandler.existsusername = 1;
								JS_AB.profiles.registerHandler.existsurl = 1;
								$('#yoururl').html(JS_AB.profiles.registerHandler.defaultUrl);
								$('#spinner_register').remove();
								return;
							}

						// define postData
							postData	= 'username=' + encodeURIComponent(username);

						// 	Now make the ajax call
							JS_AB.profiles.registerHandler._doCheckUsername(postData);

					},


				/**
				 * _doCheckUsername - do the Ajax Dance
				 * @return void
				 * -------------------------------------------------------------
				 */

					_doCheckUsername		: function(postData) {

						// post it
							$.ajax({
								url:		'/ajax.php?module=profiles&action=checkusername',
								type:		'post',
								dataType:	'json',
								cache:		false,
								data:		postData,

								// success making call
								success:	function(json) {

									// process the ajax response
									JS_AB.profiles.registerHandler._doneCheckUsername(json);

								},

								// error making call - something went horribly wrong!
								error:		function(xhr,err,e) {

									// give notice
									alert(err + ' ' + e, 'Critical Error');

									// reload the page
									window.location.reload();

								}

							});
					},



				/**
				 * _doneCheckUsername - process the Ajax response
				 * @return void
				 * -------------------------------------------------------------
				 */

					_doneCheckUsername		: function(json) {

						// debug
						if (JS_AB.profiles.debug)	console.log(json);

						// what what (in the butt)?
							switch (parseInt(json.status.code)) {

								// OK - We haz got response
									case 200:

										// update existschecks
											JS_AB.profiles.registerHandler.existsusername 	= parseInt(json.content.existsusername);
											JS_AB.profiles.registerHandler.existsurl 		= parseInt(json.content.existsurl);

										// give notices
											if (JS_AB.profiles.registerHandler.existsusername == 1) {
												$('#nickname_wrapper span').removeClass().addClass('form-error').html($('#formchecker_username_exists').html()).show();
											} else if (JS_AB.profiles.registerHandler.existsurl == 1) {
												$('#nickname_wrapper span').removeClass().addClass('form-warning').html($('#formchecker_url').html()).show();
											} else {
												$('#nickname_wrapper span').hide().html('');
											}

										// update url
											$('#yoururl').html(json.content.suggestedurl);

									break;

								// ERROR - Something went wrong (most likely insufficient params)
									case 500:
									default:

										// Give notice
										alert(json.status.text);

									break;

							}

						// remove spinner
							$('#spinner_register').remove();

					},


				/**
				 * end of object
				 * -------------------------------------------------------------
				 */

					_eoo				: true


			}







		/**
		 * JS_AB - Profiles object - urlHandler
		 * -------------------------------------------------------------
		 */

			JS_AB.profiles.urlHandler = {


				/**
				 * Datamembers (config)
				 * -------------------------------------------------------------
				 */

					linkBlob		: '<dd><input type="text" class="input-text" value="{link}" name="websites[]" id="websites{id}"/> <a href="#" title="{$lblDelete}" class="deleteWebsiteLink" rel="websites{id}">{$lblDelete}</a></dd>',


				/**
				 * init - hook ourselves to the search field!
				 * @return void
				 * -------------------------------------------------------------
				 */

					init				: function() {

						// all.systems.are.go!
						if ($('#addWebsiteLink').length > 0) {

							// Add friend to list (addEmail link clicked)
								$('#addWebsiteLink').bind('click', JS_AB.profiles.urlHandler._addUrl);

							// Add site to list (enter pressed in website field) + remove autocomplete!
								$('#website').attr("autocomplete", "off").bind('keypress', function(evt) {
									// if enter pressed, then add it
									if (evt.which == 13) {
										evt.preventDefault();
										evt.stopPropagation();
										JS_AB.profiles.urlHandler._addUrl(evt);
									}
								}).bind('focus', function(evt) {
									if ($(this).val() == '') {
										$(this).val('http://');
									}
								}).bind('blur', function(evt) {
									if ($(this).val() == 'http://') {
										$(this).val('');
									}
								});

							// delete website links
								$('.deleteWebsiteLink').bind('click', JS_AB.profiles.urlHandler._deleteUrl);

						}

					},


				/**
				 * _addEmail - Adds the emailaddress (if valid) to the togglelist
				 * @return void
				 * -------------------------------------------------------------
				 */
					_addUrl			: function(evt) {

						// don't jump/submit
						evt.preventDefault();

						// valid email!
						if (JS_NETLASH.utils.form.isUrl($('#website'))) {

							// Hide notice
								$('#invalidWebsite').hide();

							// define nextId
								var lastField = $('#addWebsiteWrapper').prev().find('input');
								if(lastField.length > 0)
								{
									nextId = parseInt(lastField.attr('id').substr(8)) + 1;
								}
								else
								{
									nextId = 1;
								}

							// add it (if not exists yet!)
								$('#addWebsiteWrapper').before(
									JS_NETLASH.utils.string.replaceAll(JS_NETLASH.utils.string.replaceAll(JS_AB.profiles.urlHandler.linkBlob, '{link}', $('#website').val()), '{id}', nextId)
								);

							// rebind delete links
								$('.deleteWebsiteLink').unbind('click').bind('click', JS_AB.profiles.urlHandler._deleteUrl);

							// clear val & focus
								$('#website').val('').focus();
						}

						// no valid email
						else
						{
							// give notice
							$('#invalidWebsite').show();

							// focus the field
							$('#website').focus();
						}

					},


				/**
				 * _deleteUrl - handles the click events on the itemz
				 * @return void
				 * -------------------------------------------------------------
				 */

					_deleteUrl		: function(evt) {

						// don't jump!
						evt.preventDefault();

						// remove if sure
						if (confirm('{$msgAreYouSure}')) {
							$('#' + $(this).attr('rel')).parent().remove();
						}

					},



				/**
				 * end of object
				 * -------------------------------------------------------------
				 */

					_eoo				: true


			}








		/**
		 * JS_AB - profiles object - commentsHandler
		 * -------------------------------------------------------------
		 */

			JS_AB.profiles.commentsHandler = {


				/**
				 * Datamembers (config)
				 * -------------------------------------------------------------
				 */

					blob			: '<div class="shoutbox-comment secondarycontent clearfix"><div class="avatar"><a title="{nick}" class="noborder" href="{url}"><img title="{nick}" alt="{nick}" src="/modulefiles/profiles/avatars/48x48/{avatar}"/></a></div><div class="message"><p class="user"><a title="{nick}" href="{url}">{nick}</a></p><p>{text}</p><div class="flag"><p id="flagLink_{comment_id}" class="flagLink"><a id="flag_comment_{comment_id}" href="#">{lblFlagContent}</a></p></div></div></div>',
					spinner			: '<img src="/modules/core/layout/images/spinner.gif" id="spinner_{id}" alt="" title="" />',


				/**
				 * init - hook ourselves to the form!
				 * @return void
				 * -------------------------------------------------------------
				 */

					init			: function() {

						// hook submit button
						if ($('#btnAddComment').length > 0) {
							$('#btnAddComment').removeAttr('disabled').bind('click', JS_AB.profiles.commentsHandler._initAddComment);
						}

						// hook flagLinks
						$('p.flagLink a').bind('click', JS_AB.profiles.commentsHandler._initFlagComment);
					},


				/**
				 * _initAddComment - Handle the add comment button click event
				 * @return void
				 * -------------------------------------------------------------
				 */

					_initAddComment		: function(evt) {

						// don't submit!
							evt.preventDefault();

						// disable the button & show the spinner
							$('#btnAddComment').attr('disabled','disabled');
							$('#spinner').css('visibility','visible');

						// define postData
							postData	= $('#addCommentForm').serialize();

						// 	Now make the ajax call
							JS_AB.profiles.commentsHandler._doAddComment(postData);

					},


				/**
				 * _doAddComment - do the Ajax Dance
				 * @return void
				 * -------------------------------------------------------------
				 */

					_doAddComment		: function(postData) {

						// post it
							$.ajax({
								url:		'/ajax.php?module=profiles&action=add_comment',
								type:		'post',
								dataType:	'json',
								cache:		false,
								data:		postData,

								// success making call
								success:	function(json) {

									// process the ajax response
									JS_AB.profiles.commentsHandler._doneAddComment(json);

								},

								// error making call - something went horribly wrong!
								error:		function(xhr,err,e) {

									// give notice
									alert(err + ' ' + e, 'Critical Error');

									// reload the page
									//if(!JS_AB.profiles.debug) window.location.reload();

								}

							});
					},



				/**
				 * _doneAddComment - process the Ajax response
				 * @return void
				 * -------------------------------------------------------------
				 */

					_doneAddComment		: function(json) {

						// debug
						if (JS_AB.profiles.debug)	console.log(json);

						// what what (in the butt)?
							switch (parseInt(json.status.code)) {

								// OK and NOK (added // spammed)
									case 200:
									case 400:

										// define what to insert
										toInsert	= JS_NETLASH.utils.string.assignFromObject(JS_NETLASH.utils.string.assignFromObject(JS_AB.profiles.commentsHandler.blob, json.content.user), json.content.comment);
										toInsert	= JS_NETLASH.utils.string.replaceAll(toInsert, '{lblFlagContent}', json.content.lblFlagContent);

										// debug
										if (JS_AB.profiles.debug)	console.log(toInsert);

										// insert comment into dom
										$('#shoutbox-comments').prepend(toInsert);

										// reset the form
										$('#addCommentForm').get(0).reset();

										// don't show "no messages"-message
										$('#shoutbox-no-comments').remove();

										// hook flagLinks
										$('p.flagLink a').bind('click', JS_AB.profiles.commentsHandler._initFlagComment);

									break;

								// ERROR - Something went wrong (most likely insufficient params)
									case 500:
									default:

										// Give notice
										alert(json.status.text);

									break;

							}

						// reactivate form
							$('#btnAddComment').removeAttr('disabled');
							$('#spinner').css('visibility','hidden');
					},


				/**
				 * _initFlagComment - Handle the flag comment link click event
				 * @return void
				 * -------------------------------------------------------------
				 */

					_initFlagComment		: function(evt) {

						// don't jump to the top
							evt.preventDefault();

						// debug
							if (JS_AB.profiles.debug)	console.log(evt);

						// define the id
							var id = evt.target.id.toString().substring(13);

						// inject spinner
							$(evt.target).parent().html(JS_NETLASH.utils.string.replaceAll(JS_AB.profiles.commentsHandler.spinner, '{id}', id));

						// define postData
							postData	= 'comment_id=' + id;

						// 	Now make the ajax call
							JS_AB.profiles.commentsHandler._doFlagComment(postData, id);

					},


				/**
				 * _doFlagComment - do the Ajax Dance
				 * @return void
				 * -------------------------------------------------------------
				 */

					_doFlagComment		: function(postData, id) {

						// post it
							$.ajax({
								url:		'/ajax.php?module=profiles&action=flag_comment',
								type:		'post',
								dataType:	'json',
								cache:		false,
								data:		postData,

								// success making call
								success:	function(json) {

									// process the ajax response
									JS_AB.profiles.commentsHandler._doneFlagComment(json, id);

								},

								// error making call - something went horribly wrong!
								error:		function(xhr,err,e) {

									// give notice
									alert(err + ' ' + e, 'Critical Error');

									// reload the page
									if(!JS_AB.profiles.debug) window.location.reload();

								}

							});
					},



				/**
				 * _doneFlagComment - process the Ajax response
				 * @return void
				 * -------------------------------------------------------------
				 */

					_doneFlagComment		: function(json, id) {

						// debug
						if (JS_AB.profiles.debug)	console.log(json);

						// what what (in the butt)?
							switch (parseInt(json.status.code)) {

								// OK - Flagged & NOK - Already flagged
									case 200:
									case 400:
										$('#flagLink_'+id).parent().parent().parent().slideUp(null, function() {$(this).remove(); } );


									break;

								// ERROR - Something went wrong (most likely insufficient params)
									case 500:
									default:

										// Give notice
										alert(json.status.text);

										// reload the page
										if(!JS_AB.profiles.debug) window.location.reload();


									break;

							}
					},


				/**
				 * end of object
				 * -------------------------------------------------------------
				 */

					_eoo				: true


			}





		/**
		 * JS_AB - profiles object - friendsHandler
		 * -------------------------------------------------------------
		 */

			JS_AB.profiles.friendsHandler = {


				/**
				 * Datamembers (config)
				 * -------------------------------------------------------------
				 */

					spinner				: '<img src="/modules/core/layout/images/spinner.gif" id="friend_spinner" style="visibility: visible;" />',
					btnNotFriend		: '<div><a href="#" class="button button-fixed" id="btnAddFriendRequest"><span>{msg}</span></a></div>',
					btnFriendRequested	: '<div class="awaitingrequest secondarycontent clearfix">{msg}<a href="#" class="cancelButton modal-box-toggle" rel="removeFriendRequest">{cancel}<span>&nbsp;</span></a></div>',
					lblIsFriend			: '<div class="friend-status secondarycontent clearfix">{msg}</div></li>',


				/**
				 * init - hook ourselves to the form!
				 * @return void
				 * -------------------------------------------------------------
				 */

					init			: function() {

						// hook buttons in detail view
						$('#btnAddFriendRequest').bind('click', JS_AB.profiles.friendsHandler._initAddFriendRequest);
						$('#btnRemoveFriendRequest').bind('click', JS_AB.profiles.friendsHandler._initRemoveFriendRequest);
						$('#btnRemoveFriend').bind('click', JS_AB.profiles.friendsHandler._initRemoveFriend);

						// hook selectAll
						$('#addFriendAll').bind('change', JS_AB.profiles.friendsHandler._selectAll);

						// hook addSelected
						$('#addSelected').bind('click', JS_AB.profiles.friendsHandler._initAddFriends);

						// hook denySelected
						$('#denySelected').bind('click', JS_AB.profiles.friendsHandler._initDenyFriendRequests);

					},


				/**
				 * _initAddFriendRequest - Handle the add as friend button click event
				 * @return void
				 * -------------------------------------------------------------
				 */

					_initAddFriendRequest		: function(evt) {

						// don't submit!
						evt.preventDefault();

						// disable the button & show the spinner
						$('#btnAddFriendRequest').addClass('button-disabled');
						$('#friend_spinner').css('visibility','visible');

						// define postData
						postData	= $('#friendRequestForm').serialize();

						// 	Now make the ajax call
						JS_AB.profiles.friendsHandler._doAddFriendRequest(postData);

					},

				/**
				 * _initRemoveFriendRequest - Handle the cancel friend request button click event
				 * @return void
				 * -------------------------------------------------------------
				 */

					_initRemoveFriendRequest		: function(evt) {

						// don't submit!
						evt.preventDefault();

						// hide the modal dialog
						$('.modal-box-close', this.parentNode).click();

						// show the spinner
						$('#friend_spinner').css('visibility','visible');

						// define postData
						postData	= 'profile_id=' + profileId;

						// 	Now make the ajax call
						JS_AB.profiles.friendsHandler._doRemoveFriendRequest(postData);

					},


				/**
				 * _doAddFriendRequest - do the Ajax Dance
				 * @return void
				 * -------------------------------------------------------------
				 */

					_doAddFriendRequest		: function(postData) {

						// post it
							$.ajax({
								url:		'/ajax.php?module=profiles&action=add_friend_request',
								type:		'post',
								dataType:	'json',
								cache:		false,
								data:		postData,

								// success making call
								success:	function(json) {

									// process the ajax response
									JS_AB.profiles.friendsHandler._doneAddFriendRequest(json);

								},

								// error making call - something went horribly wrong!
								error:		function(xhr,err,e) {

									// give notice
									alert(err + ' ' + e, 'Critical Error');

								}

							});
					},



				/**
				 * _doRemoveFriendRequest - do the Ajax Dance
				 * @return void
				 * -------------------------------------------------------------
				 */

					_doRemoveFriendRequest		: function(postData) {

						// post it
							$.ajax({
								url:		'/ajax.php?module=profiles&action=remove_friend_request',
								type:		'post',
								dataType:	'json',
								cache:		false,
								data:		postData,

								// success making call
								success:	function(json) {

									// process the ajax response
									JS_AB.profiles.friendsHandler._doneRemoveFriendRequest(json);

								},

								// error making call - something went horribly wrong!
								error:		function(xhr,err,e) {

									// give notice
									alert(err + ' ' + e, 'Critical Error');

									// reload the page
									//if(!JS_AB.profiles.debug) window.location.reload();

								}

							});
					},



				/**
				 * _doneAddFriendRequest - process the Ajax response
				 * @return void
				 * -------------------------------------------------------------
				 */

					_doneAddFriendRequest		: function(json) {

						// debug
						if (JS_AB.profiles.debug)	console.log(json);

						// what what (in the butt)?
							switch (parseInt(json.status.code)) {

								// OK and NOK (added // exists)
									case 200:
									case 400:

										// reset the form
										$('#friendRequestForm').get(0).reset();

										if(json.content.status == 'friends')
										{
											// set the new button
											$('#btnAddFriendRequest').parent().parent().html(
												JS_NETLASH.utils.string.assignFromObject(
													JS_AB.profiles.friendsHandler.lblIsFriend, json.content
												)
											);

										}
										else
										{
											// set the new button
											$('#btnAddFriendRequest').parent().parent().html(
												JS_NETLASH.utils.string.assignFromObject(
													JS_AB.profiles.friendsHandler.btnFriendRequested, json.content
												)
										);

										}

										JS_AB.general._hookModalBoxes();

										this.init();

									break;

								// ERROR - Something went wrong (most likely insufficient params)
									case 500:
									default:

										// Give notice
										alert(json.status.text);

									break;

							}

						// reactivate form
							$('#friend_spinner').css('visibility','hidden');
					},


				/**
				 * _doneRemoveFriendRequest - process the Ajax response
				 * @return void
				 * -------------------------------------------------------------
				 */

					_doneRemoveFriendRequest		: function(json) {

						// debug
						if (JS_AB.profiles.debug)	console.log(json);

						// what what (in the butt)?
							switch (parseInt(json.status.code)) {

								// OK and NOK (added // exists)
									case 200:
									case 400:

										// reset the form
										//$('#friendRequestForm').get(0).reset();

										// set the new button
										$('.cancelButton').parent().parent().html(
											JS_NETLASH.utils.string.assignFromObject(
												JS_AB.profiles.friendsHandler.btnNotFriend,
												{'msg' : json.content.msg}
											)
										);

										this.init();

									break;

								// ERROR - Something went wrong (most likely insufficient params)
									case 500:
									default:

										// Give notice
										alert(json.status.text);

									break;

							}

						// reactivate form
							$('#friend_spinner').css('visibility','hidden');
					},


				/**
				 * _initAddFriends - Handle adding the selected friends
				 * @return void
				 * -------------------------------------------------------------
				 */

					_initAddFriends		: function(evt) {

						// don't jump to the top
						evt.preventDefault();

						// debug
						if (JS_AB.profiles.debug)	console.log(evt);

						// get the friend_id's that have to be sent
						var friendId = JS_AB.profiles.friendsHandler._getChecked().join(',');

						if(friendId == "") { return false; }

						// inject spinner
						$(this).parent().prepend(JS_AB.profiles.friendsHandler.spinner);
						// disable buttons
						$('#addSelected, #denySelected').addClass('button-disabled');

						// get the friend_id's that have to be sent
						var friendId = JS_AB.profiles.friendsHandler._getChecked().join(',');

						// define postData
						postData	= 'friend_id=' + friendId;

						// 	Now make the ajax call
						JS_AB.profiles.friendsHandler._doAddFriend(postData, friendId);
					},


				/**
				 * _doAddFriend - do the Ajax Dance
				 * @return void
				 * -------------------------------------------------------------
				 */

					_doAddFriend		: function(postData, id) {

						// post it
							$.ajax({
								url:		'/ajax.php?module=profiles&action=add_friend',
								type:		'post',
								dataType:	'json',
								cache:		false,
								data:		postData,

								// success making call
								success:	function(json) {

									// process the ajax response
									JS_AB.profiles.friendsHandler._doneAddFriend(json, id);
								},

								// error making call - something went horribly wrong!
								error:		function(xhr,err,e) {

									// give notice
									alert(err + ' ' + e, 'Critical Error');
								}

							});
					},



				/**
				 * _doneAddFriend - process the Ajax response
				 * @return void
				 * -------------------------------------------------------------
				 */

					_doneAddFriend		: function(json, id) {

						// debug
						if (JS_AB.profiles.debug)	console.log(json);


						// what what (in the butt)?
							switch (parseInt(json.status.code)) {

								// OK - Flagged & NOK - Already flagged
									case 200:
									case 400:
										// remove the selected friends from the list and add them to the friend list
										jQuery.each(JS_AB.profiles.friendsHandler._getChecked(), function() {

											// remove some classes and some stuff that is unique for the requests list
											var li = $('#addFriend_'+this).parent().parent();
											li.removeClass('request');
											$('.check', li).remove();

											if($('#my-friends').length == 0) $('#friend_holder').html('<ul id="my-friends" class="friends-list clearfix"></ul>');

											// add it to the friends list
											li.remove().prependTo('#my-friends');
										});

										// if there are no more requests, remove it
										// "== 1" because we will always have the select-all checkbox
										if($('#friend-requests input[type=checkbox]').length == 1)
										{
											$('#requests').slideUp(500, function() { $(this).remove(); });
										}
										$('#friend-status').html(json.content.msg);

										// reactivate form and hide spinner
										$('#addSelected, #denySelected').removeClass('button-disabled');
										$('#friend_spinner').css('visibility','hidden');
									break;

								// ERROR - Something went wrong (most likely insufficient params)
									case 500:
									default:

										// Give notice
										alert(json.status.text);

										// reload the page
										if(!JS_AB.profiles.debug) window.location.reload();


									break;

							}
					},


				/**
				 * _initDenyFriendRequests - Deny a friend request
				 * @return void
				 * -------------------------------------------------------------
				 */

					_initDenyFriendRequests		: function(evt) {
						// don't jump to the top
						evt.preventDefault();

						// debug
						if (JS_AB.profiles.debug)	console.log(evt);

						// get the friend_id's that have to be sent
						var friendId = JS_AB.profiles.friendsHandler._getChecked().join(',');

						if(friendId == "") { return false; }

						// inject spinner
						$(this).parent().prepend(JS_AB.profiles.friendsHandler.spinner);
						// disable buttons
						$('#addSelected, #denySelected').addClass('button-disabled');

						// get the friend_id's that have to be sent
						var friendId = JS_AB.profiles.friendsHandler._getChecked().join(',');

						// define postData
						postData	= 'friend_id=' + friendId;

						// 	Now make the ajax call
						JS_AB.profiles.friendsHandler._doDenyFriendRequests(postData, friendId);
					},


				/**
				 * _doDenyFriendRequests - do the Ajax Dance
				 * @return void
				 * -------------------------------------------------------------
				 */

					_doDenyFriendRequests		: function(postData, id) {

						// post it
							$.ajax({
								url:		'/ajax.php?module=profiles&action=deny_friend_request',
								type:		'post',
								dataType:	'json',
								cache:		false,
								data:		postData,

								// success making call
								success:	function(json) {

									// process the ajax response
									JS_AB.profiles.friendsHandler._doneDenyFriendRequests(json, id);
								},

								// error making call - something went horribly wrong!
								error:		function(xhr,err,e) {

									// give notice
									alert(err + ' ' + e, 'Critical Error');

									// reload the page
									if(!JS_AB.profiles.debug) window.location.reload();

								}

							});
					},



				/**
				 * _doneDenyFriendRequests - process the Ajax response
				 * @return void
				 * -------------------------------------------------------------
				 */

					_doneDenyFriendRequests		: function(json, id) {

						// debug
						if (JS_AB.profiles.debug)	console.log(json);


						// what what (in the butt)?
							switch (parseInt(json.status.code)) {

								// OK - Flagged & NOK - Already flagged
									case 200:
									case 400:
										// remove the selected friends from the list
										jQuery.each(JS_AB.profiles.friendsHandler._getChecked(), function() {

											// remove the request
											var li = $('#addFriend_'+this).parent().parent().remove();
										});

										// if there are no more requests, remove it
										// "== 1" because we will always have the select-all checkbox
										if($('#friend-requests input[type=checkbox]').length == 1)
										{
											$('#requests').slideUp(500, function() { $(this).remove(); });
										}
										$('#friend-status').html(json.content.msg);

										// reactivate form and hide spinner
										$('#addSelected, #denySelected').removeClass('button-disabled');
										$('#friend_spinner').css('visibility','hidden');
									break;

								// ERROR - Something went wrong (most likely insufficient params)
									case 500:
									default:

										// Give notice
										alert(json.status.text);

									break;

							}
					},


				/**
				 * _initRemoveFriend - Handle the flag comment link click event
				 * @return void
				 * -------------------------------------------------------------
				 */

					_initRemoveFriend		: function(evt) {

						// don't jump to the top
						evt.preventDefault();

						// debug
						if (JS_AB.profiles.debug)	console.log(evt);

						// hide the modal dialog
						$('.modal-box-close', this.parentNode).click();

						// show the spinner
						$('#friend_spinner').css('visibility','visible');

						// define postData
						postData	= $('#friendRequestForm').serialize();

						// 	Now make the ajax call
						JS_AB.profiles.friendsHandler._doRemoveFriend(postData);

					},

				/**
				 * _doRemoveFriend - do the Ajax Dance
				 * @return void
				 * -------------------------------------------------------------
				 */

					_doRemoveFriend		: function(postData) {

						// post it
							$.ajax({
								url:		'/ajax.php?module=profiles&action=remove_friend',
								type:		'post',
								dataType:	'json',
								cache:		false,
								data:		postData,

								// success making call
								success:	function(json) {

									// process the ajax response
									JS_AB.profiles.friendsHandler._doneRemoveFriend(json);

								},

								// error making call - something went horribly wrong!
								error:		function(xhr,err,e) {

									// give notice
									alert(err + ' ' + e, 'Critical Error');

								}

							});
					},



				/**
				 * _doneRemoveFriend - process the Ajax response
				 * @return void
				 * -------------------------------------------------------------
				 */

					_doneRemoveFriend		: function(json, id) {

						// debug
						if (JS_AB.profiles.debug)	console.log(json);


						// what what (in the butt)?
							switch (parseInt(json.status.code)) {

								// OK - Flagged & NOK - Already flagged
									case 200:
									case 400:
										// reset the form
										//$('#friendRequestForm').get(0).reset();

										// set the new button
										$('.friend-status').parent().html(
											JS_NETLASH.utils.string.assignFromObject(
												JS_AB.profiles.friendsHandler.btnNotFriend,
												{'msg' : json.content.msg}
											)
										);

										this.init();
									break;

								// ERROR - Something went wrong (most likely insufficient params)
									case 500:
									default:

										// Give notice
										alert(json.status.text);

										// reload the page
//										if(!JS_AB.profiles.debug) window.location.reload();


									break;

							}
							// reactivate form
							$('#friend_spinner').css('visibility','hidden');
					},


				/**
				 * _selectAll - select all friend requests
				 * @return void
				 * -------------------------------------------------------------
				 */

					_selectAll			: function(evt) {

						// get the new checkbox value
						var value = $(this).attr('checked');

						// get all checkboxes in this form
						$('#friend-requests input[type=checkbox]').each(function() { $(this).attr('checked', value); });
					},

				/**
				 * _getChecked - get the id's of the checked profiles
				 * @return array
				 * -------------------------------------------------------------
				 */

					_getChecked			: function() {

						var options = new Array();

						// get all checkboxes in this form
						$('#friend-requests input[type=checkbox]').each(function() {
							if($(this).attr('checked') && $(this).attr('name')=="add_friend[]") options.push(parseInt($(this).val()));
						});

						return options;
					},


				/**
				 * end of object
				 * -------------------------------------------------------------
				 */

					_eoo				: true


			}



		/*
		 * JS_AB - Profiles object - quickSearchHandler (basically a copy of the one from artists ;))
		 * -------------------------------------------------------------
		 */

			JS_AB.profiles.quickFriendSearchHandler = {


				/**
				 * init - hook ourselves to the search field!
				 * @return void
				 * -------------------------------------------------------------
				 */

					init			: function() {
						// bind events
						JS_AB.profiles.quickFriendSearchHandler._initDeleteFriend();

						$('#add_friend_button').bind('click', function(evt) {
							JS_AB.profiles.quickFriendSearchHandler._addFriend(evt);
						});
					},

					_addFriend		: function(evt) {
							// prevent default
							evt.preventDefault();
							evt.stopPropagation();

							// cleanup
							if($('#friends_list li').length == 0) $('#buddies').val('');

							var id = $('#friends').val().split(':::')[0];
							var avatar = $('#friends').val().split(':::')[1];
							var nick = $('#friends').val().split(':::')[2];

							if(id == '-1') return false;

							if($('#buddy_' + id).length == 0)
							{
								$('#buddies').val($('#buddies').val() + ':::' + id);

								// add to list
								JS_AB.profiles.quickFriendSearchHandler._doneAddFriend(id, avatar, nick);
							}
							else $('#buddy_' + id).fadeTo('normal', 0.33).fadeTo('normal', 1);

					},

					_doneAddFriend	: function(id, avatar, nick) {
						// build html
						html = '<li id="buddy_'+ id +'"><div class="avatar avatar-48"><img src="/modulefiles/profiles/avatars/64x64/'+ avatar +'" width="48" height="48" /></div><p>'+ nick +' <a class="deleteTag noborder" href="#" rel="'+ id +'" title="{$lblDelete}">{$lblDelete}</a></p></li>';

						// append html
						$('#friends_list').append(html);

						// show
						$('#recipients').fadeIn('fast');

						// bind events
						JS_AB.profiles.quickFriendSearchHandler._initDeleteFriend();
					},

					_initDeleteFriend	: function(evt) {
						$('.deleteTag').bind('click', function(evt) {
							// prevent default
							evt.preventDefault();
							evt.stopPropagation();

							// get id
							id = $(this).attr('rel');

							// remove id
							$('#buddies').val($('#buddies').val().replace(':::' + id, ''));

							// cleanup
							if($('#friends_list li').length == 0) $('#buddies').val('');

							// remove visual
							$('#buddy_'+ id).remove();
						});
					},

				/**
				 * end of object
				 * -------------------------------------------------------------
				 */

					_eoo				: true


			}



	/**
	 * Main Run : Init objects when document is loaded
	 * -------------------------------------------------------------
	 */

		$(document).ready(function() {
			JS_AB.profiles.init();
		});
