
	function ajaxError(link, data) {
		//$(link).parents('li').addClass();
		alert(data);
	} // ajaxError
	
	function urlencode(str) {
		return escape(str).replace(/\+/g,'%2B').replace(/%20/g, '+').replace(/\*/g, '%2A').replace(/\//g, '%2F').replace(/@/g, '%40');
	} // urlencode
	
	function is_html(data) {
		return (data.replace(/^\s*/, "").replace(/\s*$/, "").substr(0, 1) == '<');
	} // is_html
	
	function deleteInboxMessages(link) {
		var arr = [];		
		jQuery.each($('.message-delete:checked'), function() { arr.push($(this).val()); });
		
		if (arr.length < 1)
			return ajaxError(link, "No messages selected to delete.");
		
		$.post(root + 'inbox/delete/', { threads : arr.join(",") }, function(data) {
			if (data == 'true') {
				window.location = window.location;
			} else {
				ajaxError(link, data);
			}
		});
		
	} // deleteInboxMessages
	
	function deleteInboxMessage(link, message_id) {
		
		$.post(root + 'inbox/delete/', { threads : message_id }, function(data) {
			if (data == 'true') {
				window.location = root + 'inbox/';
			} else {
				ajaxError(link, data);
			}
		});
		
	} // deleteInboxMessage
	
	function checkInboxMessage(form) {
		
		//var url = $(form).attr('action');
		
		if ($('#receiver').val() == '') {
			$('#receiver').get(0).focus();
			return false;
		}
		
		if ($('#message').val() == '') {
			$('#message').get(0).focus();
			return false;
		}
		
		/*var message = {
			message : $('#message').val(),
			receiver : $('#receiver').val(),
			thread : $('#thread').val()
		};*/
		$(form).find('input[type=submit]').attr({'value' : 'Sending...'/*, 'disabled' : 'disabled'*/});
		
		//$.post(url + '?ajax', message, function(data) {
		//	if (data.replace(/^\s*/, "").replace(/\s*$/, "").substr(0, 1) == '<') {
		//		$('.form-container').before(data);
		//		$(form).find('input[type=submit]').attr({'value' : 'Send'}).removeAttr('disabled');
		//		$('#message').val('');
		//	} else {
		//		ajaxError(form, data);
		//	}
		//});
		
		//return false;
	} // checkInboxMessage

	function sendInboxMessage(form) {
		
		var url = $(form).attr('action');
		
		if ($('#receiver').val() == '') {
			$('#receiver').get(0).focus();
			return false;
		}
		
		if ($('#message').val() == '') {
			$('#message').get(0).focus();
			return false;
		}
		
		var message = {
			message : $('#message').val(),
			receiver : $('#receiver').val(),
			thread : $('#thread').val()
		};
		$(form).find('input[type=submit]').attr({'value' : 'Sending...'/*, 'disabled' : 'disabled'*/});
		
		$.post(url + '?ajax', message, function(data) {
			if (is_html(data)) {
				$('#form-container-left').before(data);
				$(form).find('input[type=submit]').attr({'value' : 'Send'}).removeAttr('disabled');
				$('#message').val('');
			} else {
				ajaxError(form, data);
			}
		});
		
		return false;
	} // sendInboxMessage
	
	function deleteSupportTicket(link, message_id) {
		
		$.post(root + 'help/delete/', { threads : message_id }, function(data) {
			if (data == 'true') {
				window.location = root + 'help/';
			} else {
				ajaxError(link, data);
			}
		});
		
	} // deleteSupportTicket
	
	function checkSupportTicket(form) {
		
		if ($('#message').val() == '') {
			$('#message').get(0).focus();
			return false;
		}
		
		$(form).find('input[type=submit]').attr({'value' : 'Sending...'/*, 'disabled' : 'disabled'*/});
		
	} // checkSupportTicket

	function resolveSupportTicket(link, message_id) {
		
		$.post(root + 'help/resolve/', { threads : message_id }, function(data) {
			if (data == 'true') {
				window.location = root + 'help/';
			} else {
				ajaxError(link, data);
			}
		});
		
	} // resolveSupportTicket

	function sendSupportTicket(form) {
		
		var url = $(form).attr('action');
		
		if ($('#message').val() == '') {
			$('#message').get(0).focus();
			return false;
		}
		
		var message = {
			message : $('#message').val(),
			thread : $('#thread').val()
		};
		$(form).find('input[type=submit]').attr({'value' : 'Sending...'/*, 'disabled' : 'disabled'*/});
		
		$.post(url + '?ajax', message, function(data) {
			if (data.replace(/^\s*/, "").replace(/\s*$/, "").substr(0, 1) == '<') {
				$('#form-container-left').before(data);
				$(form).find('input[type=submit]').attr({'value' : 'Send'}).removeAttr('disabled');
				$('#message').val('');
			} else {
				ajaxError(form, data);
			}
		});
		
		return false;
	} // sendSupportTicket
	
	function changeActivityStatus(link, activity_id, new_status) {
		$.post(root + 'activity/status/', { id : activity_id , status : new_status }, function(data) {
			if (is_html(data)) {
				$(link).parent().parent().replaceWith(data);
			} else {
				ajaxError(link, data);
			}
		});
	} // changeActivityStatus
	
	var r = null;
	function removeUndo() {
		$('li.posts.undo').fadeOut(500);
		clearTimeout(r);		
	} // removeUndo
	
	function deletePost(link, id) {
		$(link).parents('li:first').find('.busy').show();
		removeUndo();
		if (confirm("Are you sure you want to delete this post?")) {
			$.post(root + 'posts/delete/', { id : id }, function(data) {
				if (is_html(data)) {
					$(link).parents('li:first').fadeOut(500, function() { 
						updateCount();
						$(this).after(data); 
						r = setTimeout(removeUndo, 15000);
					});
				} else {
					ajaxError(link, data);
				}
			});
		}
		return false;
	} // deletePost
	
	function savePost(link, id) {
		//$.Watermark.HideAll();
		var m = $('#post-' + id + '-edit .message textarea').val();
		var t = $('#post-' + id + '-edit .tags input').val();
		
		//alert(m + ' - ' + t);
		$(link).parents('li:first').find('.busy').show();
		$('li.posts.undo').fadeOut(500);

		$.post(root + 'posts/save/', { id : id , message : m , tags : t }, function(data) {
			if (is_html(data)) {
				$('#post-' + id + '-edit').remove();
				$('#post-' + id).show().replaceWith(data);
				prepLinks();
			} else {
				ajaxError(link, data);
			}			
		});
	} // savePost
	
	function editPost(link, id) {
		$(link).parents('li:first').find('.busy').show();
		$('li.posts.undo').fadeOut(500);
		$.post(root + 'posts/edit/', { id : id }, function(data) {
			if (is_html(data)) {
				$(link).parents('li:first').find('.busy').hide();
				$(link).parents('li:first').hide().after(data);
			} else {
				ajaxError(link, data);
			}
		});
		return false;
	} // editPost
	
	function cancelEditPost(link, id) {
		$('#post-' + id + '-edit').remove();
		$('#post-' + id).show();
	} // cancelEditPost
	
	function archivePost(link, id) {
		$(link).parents('li:first').find('.busy').show();
		$('li.posts.undo').fadeOut(500);
		$.post(root + 'posts/archive/', { id : id }, function(data) {
			if (is_html(data)) {
				$(link).parents('li:first').fadeOut(500, function() { 
					updateCount();
					$(this).after(data); 
					r = setTimeout(removeUndo, 15000);
				});
			} else {
				ajaxError(link, data);
			}
		});
		return false;
	} // archivePost
	
	function unarchivePost(link, id) {
		$(link).parents('li:first').find('.busy').show();
		$('li.posts.undo').fadeOut(500);
		$.post(root + 'posts/unarchive/', { id : id }, function(data) {
			if (is_html(data)) {
				$(link).parents('li:first').fadeOut(500, function() { 
					updateCount();
					$(this).after(data); 
					r = setTimeout(removeUndo, 15000);
				});
			} else {
				ajaxError(link, data);
			}
		});
		return false;
	} // unarchivePost
	
	function undoPost(link, action, id) {
		$.post(root + 'posts/undo/', { action : action , id : id }, function(data) {
			if (data == 'true') {
				$(link).parents('li:first').fadeOut(500, function() { 
					$(this).remove(); 
					$('#post-' + id + ' .busy').hide();
					$('#post-' + id).fadeIn(500); 
					updateCount();
				});
			} else {
				ajaxError(link, data);
			}
		});
	} // undoPost
	
	function favoritePost(link, id) {
		$(link).parents('li:first').find('.busy').show();
		$('li.posts.undo').fadeOut(500);
		$.post(root + 'posts/favorite/', { id : id }, function(data) {
			if (is_html(data)) {
				$(link).parents('li:first').replaceWith(data);
				prepLinks();
			} else {
				ajaxError(link, data);
			}
		});
	} // favoritePost
	
	function unfavoritePost(link, id) {
		$(link).parents('li:first').find('.busy').show();
		$('li.posts.undo').fadeOut(500);
		$.post(root + 'posts/unfavorite/', { id : id }, function(data) {
			if (is_html(data)) {
				$(link).parents('li:first').replaceWith(data);
				prepLinks();
			} else {
				ajaxError(link, data);
			}
		});
	} // unfavoritePost
	
	function readPost(link, id) {
		$(link).parents('li:first').find('.busy').show();
		$('li.posts.undo').fadeOut(500);
		$.post(root + 'posts/read/', { id : id }, function(data) {
			if (is_html(data)) {
				$(link).parents('li:first').replaceWith(data);
				updateCount();
				prepLinks();
			} else {
				ajaxError(link, data);
			}
		});
	} // readPost
	
	function unreadPost(link, id) {
		$(link).parents('li:first').find('.busy').show();
		$('li.posts.undo').fadeOut(500);
		$.post(root + 'posts/unread/', { id : id }, function(data) {
			if (is_html(data)) {
				$(link).parents('li:first').replaceWith(data);
				updateCount();
				prepLinks();
			} else {
				ajaxError(link, data);
			}
		});
	} // unreadPost
	
	function quickread(link, id) {
		$.post(root + 'posts/read/', { id : id }, function(data) {
			if (is_html(data)) {
				$(link).parents('li:first').replaceWith(data);
				updateCount();
				prepLinks();
			} else {
				ajaxError(link, data);
			}
			if ($(link).attr('class') != 'external')
				setTimeout("window.location.href = '" + link + "';", 10);
		});
	} // quickread
	
	function updateCount() {
		$.post(root + 'posts/new/', {}, function(data) {
			if (document.title.indexOf('Dashboard') > -1) {
				var title = document.title.split('-');
				title[0] = (data > '0') ? 'Dashboard (' + data + ') ' : 'Dashboard ';
				document.title = title.join('-');
			}
			if (data > '0') {
				$('#nav-dashboard-count').html(' (' + data + ')');				
			} else {
				$('#nav-dashboard-count').html('');
			}
		});
	} // updateCount
	
	function checkForNew() {
		//setTimeout(function() { updateCount(); checkForNew(); }, 15000);
	} // checkForNew
	
	function prepLinks() {
		$('li.posts.unread h3 a:not(.favorite)').unbind('click').bind('click', function() {
			var link = this;
			$(link).parents('li:first').find('.busy').show();
			quickread(link, $(link).parents('li:first').attr('id').replace('post-', ''));
			return ($(link).attr('class') == 'external');
		});
		$('.posts, .contacts').unbind('mouseover').unbind('mouseout').bind('mouseover', function() { $(this).find('.utilities').show(); }).bind('mouseout', function() { $(this).find('.utilities').hide(); });
	} // prepLinks
	
	$(function() {
		$('input.submit-btn').parents('form:first').bind('submit', function(e) {
			$(this).find('input.submit-btn')/*.attr('disabled', 'disabled')*/.attr('value', 'Sending...');
		});
		prepLinks();
		checkForNew();
	});
	
	/**** SEARCH WIDGET ****/
	function search_focus(field) {
		search_scope_hide();
		$(field).addClass('focused'); 
		if (field.value == default_search_text)
			field.value = '';
	} // search_focus
	
	function search_blur(field) {
		$(field).removeClass('focused'); 
		if (field.value == '') 
			field.value = default_search_text; 
	} // search_blur
	
	function search_scope_change(which) {
		$('#search-scope').val($(which).attr('id'));
		$(which).parent().parent().find('li').removeClass('selected'); 
		$(which).parent().addClass('selected');
		var old_default_search_text = default_search_text;
		default_search_text = "Search " + $(which).html();
		if ($('#search-query').val() == old_default_search_text)
			$('#search-query').val(default_search_text);
		if ($(which).attr('id') == 'contact')
			$('#search-query').addClass('autosuggest');
		else
			$('#search-query').removeClass('autosuggest');
		search_scope_hide();
	} // search_scope_change
	
	function search_scope_hide() {
		$('#search-controls').hide();
		$(document.window).unbind('click', search_scope_hide);
	} // search_scope_hide
	
	function search_scope_show() {
		$('#search-controls').show();
		setTimeout(function() { $(document.window).bind('click', search_scope_hide); }, 10);
	} // search_scope_show
	
	function search_scope_toggle() {
		if ($('#search-controls').css('display') == 'none')
			search_scope_show();
		else
			search_scope_hide();
	} // search_scope_toggle

