// JavaScript Linklex: helper_fns.js 

// Close Linklog
$(document).ready(function() {
	$(col_name).click(function(event) {
		if ($(event.target).is('#close_this')) {
			var this_text = '<p class="msg_right">Close Linklog? (<span class="answer">Yes</span>) '; 
			this_text = this_text + '(<span class="answer">No</span>)</p>';
			MsgRight(this_text);
		}
	});
});
// Close Message right using icon - msg_right or ajax_form
$(document).ready(function() {
	$(col_name).click(function(event) {
		if ($(event.target).is('#close_msg')) {
			// Remove any open window
			var restore = 'Y';
			RemoveMsg(restore);
		}
	});
});
// Monitor answers from Right Message
$(document).ready(function() {
	$(col_name).click(function(event) {
		if ($(event.target).is('.answer')) {
			var this_text = $(event.target).text();
			this_text = jQuery.trim(this_text);
			switch (this_text) {
				case 'Yes' :
					$.post('destroy_llog_ajax.php', {
					'open_llog_id': $(llog_id_input).val() 
					});
					$(llog_id_input).val('');
					$('.linklog').remove();
					$('.msg_right').remove();
					if ($('.msg_left').text() != '') {
						$('.msg_left').remove();
					}
					break;
				case 'OK': 
					$('.ajax_form').remove();
					break;
				case 'Private':
					DisplayLoading();
					$.post('publish_linklog_ajax.php', {
						'publish_llog_id': $(llog_id_input).val(),
						'audience': 'private'
						}, function(response){
  							response = jQuery.trim(response);					
							if (response > 0) {
								$(col_name + ' .loading').hide();
								var publish_msg = '<div class="ajax_form"><h2>The Linklog has been Published.</h2>';
								publish_msg = publish_msg + '<h2 class="answer">OK</h2></div>';
								DisplayLinklog(response, publish_msg);
							} else {
								$(col_name + ' .loading').hide();
								var publish_error = '<div class="msg_right"><img src="../img/close.png" alt="Close" border="0" id="close_msg" />';
								publish_error = publish_error + '<p>Could not publish. Please try again.</p></div>';
								MsgRight(publish_error);
							}		
					}, "text");
					break;
				case 'Public':
					DisplayLoading();
					$.post('publish_linklog_ajax.php', {
						'publish_llog_id': $(llog_id_input).val(),
						'audience': 'public'
						}, function(response){
  							response = jQuery.trim(response);					
							if (response > 0) {
								$(col_name + ' .loading').hide();
								var publish_msg = '<div class="ajax_form"><h2>The Linklog has been Published.</h2>';
								publish_msg = publish_msg + '<h2 class="answer">OK</h2></div>';
								DisplayLinklog(response, publish_msg);
							} else {
								$(col_name + ' .loading').hide();
								var publish_error = '<div class="msg_right"><img src="../img/close.png" alt="Close" border="0" id="close_msg" />';
								publish_error = publish_error + '<p>Could not publish. Please try again.</p></div>';
								MsgRight(publish_error);
							}		
					}, "text");
					break;
				case 'No':
					$('.msg_right').remove();
					$('.linklog').fadeTo("fast", 1);
					break;
				case 'Log in':
					var loading_text = '<div class="ajax_form"><h1>Loading...</h1><div class="loading">'; 
					loading_text = loading_text + '<img src="../img/throbber.gif" alt="Loading" border="0" id="loading" /></div></div>';
					MsgRight(loading_text);
					$(col_name + ' .ajax_form .loading').show();
					if (this_page == 'results.php') {
						var search_term = $('input[name="search_term"]').val();
						var redirect = 'results.php?q=' + search_term;
					} else {
						var redirect = this_page;
					}
					$('.ajax_form').load('login_ajax.php', {'redirect': redirect});
					break;
			 	case 'Yes, delete it':
					DisplayLoading();
					$.post('delete_linklog_ajax.php', {
						'open_llog_id': $(llog_id_input).val()
						}, function(response){
  							response = jQuery.trim(response);					
							if (response > 0) {
								$(col_name + ' .loading').hide();
								$(llog_id_input).val('');
								$('.linklog').remove();	
							} else {
								$(col_name + ' .loading').hide();
								var delete_error = '<div class="msg_right"><img src="../img/close.png" alt="Close" border="0" id="close_msg" />';
								delete_error = delete_error + '<p>Could not delete. Please try again.</p></div>';
								MsgRight(delete_error);
							}		
					}, "text");
					break;
				case '(Yes, delete link)':
					var l_id = $(event.target).closest('.xfolkentry').attr('id');
					DisplayLoading('link');
					$.post('delete_link_ajax.php', {
						'open_llog_id': $(llog_id_input).val(),
						'l_id': l_id
						}, function(response){
  							response = jQuery.trim(response);					
							if (response > 0) {
								RemoveMsg();
								$('#' + response).slideUp('slow', function () {
									$('#' + response).remove();	
								});
							} else {
								$(col_name + ' .l_action_box .loading').hide();
								var delete_error = '<div class="l_action_msg">Could not delete. Please try again.';
								delete_error = delete_error + '<span id="close_msg">(hide)</span></div>'
								MsgLink(delete_error, 'N');
							}		
					}, "text");
					break;
				case '(Move up)': 
					var l_id = $(event.target).closest('.xfolkentry').attr('id');
					var curr = $(event.target).closest('.xfolkentry');
					var prev = curr.prev();
					var this_clone = curr.clone();
					DisplayLoading('link');
					$.post('move_link_ajax.php', {
						'open_llog_id': $(llog_id_input).val(),
						'l_id': l_id,
						'where': 'up'
						}, function(response){
  							response = jQuery.trim(response);					
							if (response > 0) {
								MoveUp(curr, prev, this_clone);								
							} else {
								$(col_name + ' .l_action_box .loading').hide();
								var move_error = '<div class="l_action_msg">Could not move up.';
								move_error = move_error + '<span id="close_msg">(hide)</span></div>'
								MsgLink(move_error, 'N');
							}		
					}, "text");					
					break;
				case  '(Move down)': 
					var l_id = $(event.target).closest('.xfolkentry').attr('id');
					var curr = $(event.target).closest('.xfolkentry');
					var next = curr.next();
					var this_clone = curr.clone();
					DisplayLoading('link');
					$.post('move_link_ajax.php', {
						'open_llog_id': $(llog_id_input).val(),
						'l_id': l_id,
						'where': 'down'
						}, function(response){
  							response = jQuery.trim(response);					
							if (response > 0) {
								MoveDown(curr, next, this_clone);
							} else {
								$(col_name + ' .l_action_box .loading').hide();
								var move_error = '<div class="l_action_msg">Could not move down.';
								move_error = move_error + '<span id="close_msg">(hide)</span></div>'
								MsgLink(move_error, 'N');
							}		
					}, "text");					
					break;
				case  '(Vote up)': 
					var l_id = $(event.target).closest('.xfolkentry').attr('id');
					DisplayLoading('link');
					$.post('vote_link_ajax.php', {
						'open_llog_id': $(llog_id_input).val(),
						'l_id': l_id,
						'where': 'up',
						'search_term': $('input[name="search_term"]').val()
						}, function(response){
  							response = jQuery.trim(response);					
							if (response != '') {
								$('.l_action_box').css('height', '9em');
								MsgLink(response, 'N');
								$('#select_search_term').bind('change', function(event) {
  									var value = $(event.target).val(); 
									$('.l_action_box').css('height', '14em');
									$('#selected_item').html(value);
								});
							} else {
								var edit_error = '<div class="msg_right"><img src="../img/close.png" alt="Close" border="0" id="close_msg" />';
								edit_error = edit_error + '<p>Could not get your vote. Please try again.</p></div>';
								MsgLink(edit_error, 'N');
							}		
					}, "html");					
					break;
				case  '(Vote down)': 
					var l_id = $(event.target).closest('.xfolkentry').attr('id');
					DisplayLoading('link');
					$.post('vote_link_ajax.php', {
						'open_llog_id': $(llog_id_input).val(),
						'l_id': l_id,
						'where': 'down',
						'search_term': $('input[name="search_term"]').val()
						}, function(response){
  							response = jQuery.trim(response);					
							if (response != '') {
								$('.l_action_box').css('height', '9em');
								MsgLink(response, 'N');
								$('#select_search_term').bind('change', function(event) {
  									var value = $(event.target).val(); 
									$('.l_action_box').css('height', '14em');
									$('#selected_item').html(value);
								});
							} else {
								var edit_error = '<div class="msg_right"><img src="../img/close.png" alt="Close" border="0" id="close_msg" />';
								edit_error = edit_error + '<p>Could not get your vote. Please try again.</p></div>';
								MsgLink(edit_error, 'N');
							}		
					}, "html");					
					break;
				case  'Permalink (more descriptive)': 
					var restore = 'Y';
					RemoveMsg(restore);
					var url = $('input[name="permalink"]').val();
					var title = $('a[name="linklog"]').attr('id');
					return addthis_open(this, '', url, title);					
					break;
				case  'Short URL (better for Twitter)': 
					var restore = 'Y';
					RemoveMsg(restore);
					var url = $('input[name="short_url"]').val();
					var title = $('a[name="linklog"]').attr('id');
					return addthis_open(this, '', url, title);					
					break;
				case  'Spam (advertising, misleading, off topic)': 
					var l_id = $(event.target).closest('.xfolkentry').attr('id');
					DisplayLoading('link');
					$.post('flag_link_ajax.php', {
						'open_llog_id': $(llog_id_input).val(),
						'l_id': l_id,
						'type': 'spam'
						}, function(response){
  							response = jQuery.trim(response);					
							if (response != '') {
								$('.l_action_box').css('height', '8em');
								var flag_msg = '<div class="l_action_msg">Thank you! The link has been marked as Spam.';
								var flag_msg = flag_msg + '<span id="close_msg">(hide)</span></div>';
								MsgLink(flag_msg, 'N');
							} else {
								var edit_error = '<div class="msg_right"><img src="../img/close.png" alt="Close" border="0" id="close_msg" />';
								edit_error = edit_error + '<p>Could not flag. Please try again.</p></div>';
								MsgLink(edit_error, 'N');
							}		
					}, "html");					
					break;
				case  'Offensive (nudity, profanity, violence)': 
					var l_id = $(event.target).closest('.xfolkentry').attr('id');
					DisplayLoading('link');
					$.post('flag_link_ajax.php', {
						'open_llog_id': $(llog_id_input).val(),
						'l_id': l_id,
						'type': 'offensive'
						}, function(response){
  							response = jQuery.trim(response);					
							if (response != '') {
								$('.l_action_box').css('height', '8em');
								var flag_msg = '<div class="l_action_msg">Thank you! The link has been marked as Offensive.';
								var flag_msg = flag_msg + '<span id="close_msg">(hide)</span></div>';
								MsgLink(flag_msg, 'N');
							} else {
								var edit_error = '<div class="msg_right"><img src="../img/close.png" alt="Close" border="0" id="close_msg" />';
								edit_error = edit_error + '<p>Could not flag. Please try again.</p></div>';
								MsgLink(edit_error, 'N');
							}		
					}, "html");					
					break;
				case  'Not working (incorrect URL)': 
					var l_id = $(event.target).closest('.xfolkentry').attr('id');
					DisplayLoading('link');
					$.post('flag_link_ajax.php', {
						'open_llog_id': $(llog_id_input).val(),
						'l_id': l_id,
						'type': 'notworking'
						}, function(response){
  							response = jQuery.trim(response);					
							if (response != '') {
								$('.l_action_box').css('height', '8em');
								var flag_msg = '<div class="l_action_msg">Thank you! The link has been marked as Not working.';
								var flag_msg = flag_msg + '<span id="close_msg">(hide)</span></div>';
								MsgLink(flag_msg, 'N');
							} else {
								var edit_error = '<div class="msg_right"><img src="../img/close.png" alt="Close" border="0" id="close_msg" />';
								edit_error = edit_error + '<p>Could not flag. Please try again.</p></div>';
								MsgLink(edit_error, 'N');
							}		
					}, "html");					
					break;	
			}
		}
	});
});
// Control Linklog actions
$(document).ready(function() {
	$(col_name).click(function(event) {
		if ($(event.target).is('#permalink')) {
			$('#permalink_field').select();
		}
		if ($(event.target).is('#permalink_field')) {
			$('#permalink_field').select();
		}
		if ($(event.target).is('#short_url')) {
			$('#short_url_field').select();
		}
		if ($(event.target).is('#short_url_field')) {
			$('#short_url_field').select();
		}
		if ($(event.target).is('#open_in_search')) {
			$('#open_in_search').css('cursor', 'default');
			$('#open_in_search').text('please wait...');
			$.post('create_llog_id_session.php', {
				'open_llog_id': $(llog_id_input).val()
				}, function(response){
					response = jQuery.trim(response);
					if (response != '') {
						top.location.href = 'results.php?q=' + response;
					} else {
						$('#open_in_search').css('cursor', 'pointer');
						$('#open_in_search').text('(Open in Search)');
					}
			}, "html");
		}
		if ($(event.target).is('#linklog_wiki')) {
			var this_text = '<div class="msg_right"><img src="../img/close.png" alt="Close" border="0" id="close_msg" />'; 
			this_text = this_text + '<p>Please <span class="answer">Log in</span> to open the linklog wiki.</p></div>';	
			var event_text = $(event.target).text();
			$.get("check_user_ajax.php", function(response){					
				response = jQuery.trim(response);
				if (response == 'Y') {
					$('#linklog_wiki').css('cursor', 'default');
					$('#linklog_wiki').text('please wait...');
					if (event_text == 'linklog wiki') {
						$.post('get_linklog_wiki_ajax.php', {
							'llog_id': $(llog_id_input).val()
							}, function(response){
								response = jQuery.trim(response);
								if (response != '') {
									$(event.target).after(response);
									$('#linklog_wiki').css('cursor', 'pointer');
									$('#linklog_wiki').text('close wiki');
								} else {
									$('#linklog_wiki').css('cursor', 'pointer');
									$('#linklog_wiki').text('linklog wiki');
								}
						}, "html");
					} else {
						if (event_text == 'close wiki') {
							$('.wiki').remove();
							$('#linklog_wiki').css('cursor', 'pointer');
							$('#linklog_wiki').text('linklog wiki');
						}
					}
				} else {
					MsgRight(this_text);
				}
			});
		}
		if ($(event.target).is('#offensive')) {
			var this_text = '<div class="msg_right"><img src="../img/close.png" alt="Close" border="0" id="close_msg" />'; 
			this_text = this_text + '<p>Please <span class="answer">Log in</span> to mark as offensive.</p></div>';	
			$.get("check_user_ajax.php", function(response){					
				response = jQuery.trim(response);
				if (response == 'Y') {	
					$('#offensive').css('cursor', 'default');
					$('#offensive').text('please wait...');
					$.post('mark_offensive.php', {
						'open_llog_id': $(llog_id_input).val()
						}, function(response){
							response = jQuery.trim(response);
							if (response != '') {
								$('#offensive').css('text-decoration', 'underline');
								$('#offensive').css('color', '#dd3c10');
								$('#offensive').text('marked as offensive');
							} else {
								$('#offensive').css('cursor', 'pointer');
								$('#offensive').text('offensive?');
							}
					}, "html");
				} else {
					MsgRight(this_text);
				}
			});
		}
		if ($(event.target).is('#actions *')) {
			var this_action = $(event.target).text();
			var this_text = '<div class="msg_right"><img src="../img/close.png" alt="Close" border="0" id="close_msg" />'; 
			this_text = this_text + '<p>Please <span class="answer">Log in</span> to ' + this_action + '.</p></div>';	
			this_action = jQuery.trim(this_action);
			switch (this_action) {
   				case "Edit":
					$.get("check_user_ajax.php", function(response){					
						response = jQuery.trim(response);
						if (response == 'Y') {
							var loading_text = '<div class="ajax_form"><h1>Loading...</h1><div class="loading">'; 
							loading_text = loading_text + '<img src="../img/throbber.gif" alt="Loading" border="0" id="loading" /></div></div>';
							MsgRight(loading_text);
							$(col_name + ' .ajax_form .loading').show();
							$.post('edit_linklog_ajax.php', {
								'open_llog_id': $(llog_id_input).val()
								}, function(response){
									response = jQuery.trim(response);
									if (response != '') {
										$('.ajax_form').html(response);
									} else {
										var edit_error = '<div class="msg_right"><img src="../img/close.png" alt="Close" border="0" id="close_msg" />';
										edit_error = edit_error + '<p>Could not Edit. Please try again.</p></div>';
										MsgRight(edit_error);
									}
							}, "html");
						} else {
							MsgRight(this_text);
						}
					});
					break;
   				case "Add Link":
					$.get("check_user_ajax.php", function(response){					
						response = jQuery.trim(response);
						if (response == 'Y') {
							var loading_text = '<div class="ajax_form"><h1>Loading...</h1><div class="loading">'; 
							loading_text = loading_text + '<img src="../img/throbber.gif" alt="Loading" border="0" id="loading" /></div></div>';
							MsgRight(loading_text);
							$(col_name + ' .ajax_form .loading').show();
							$.post('add_link_ajax.php', {
								'open_llog_id': $(llog_id_input).val()
								}, function(response){
									response = jQuery.trim(response);
									if (response != '') {
										$('.ajax_form').html(response);
									} else {
										var edit_error = '<div class="msg_right"><img src="../img/close.png" alt="Close" border="0" id="close_msg" />';
										edit_error = edit_error + '<p>Could not Add Link. Please try again.</p></div>';
										MsgRight(edit_error);
									}
							}, "html");
						} else {
							MsgRight(this_text);
						}
					});
					break;
   				case "Delete":
      				var delete_msg = '<div class="msg_right"><img src="../img/close.png" alt="Close" border="0" id="close_msg" />';
      				delete_msg = delete_msg + '<p>Delete? (<span class="answer">Yes, delete it</span>) (<span class="answer">No</span>)</p>';
	  				MsgRight(delete_msg);				
					break;
			   	case "Save":
      				$.get("check_user_ajax.php", function(response){
  						response = jQuery.trim(response);
						if (response == 'Y') {
							DisplayLoading();
							$.post('save_linklog_ajax.php', {
								'open_llog_id': $(llog_id_input).val()
								}, function(response){
  									response = jQuery.trim(response);					
									if (response > 0) {
										var existing_id = response;
										var saved_msg = '<div class="ajax_form"><h2>The Linklog has been Saved.</h2>';
										saved_msg = saved_msg + '<h2 class="answer">OK</h2></div>';
										DisplayLinklog(existing_id, saved_msg);
									} else {
										$(col_name + ' .loading').hide();
										var saved_error = '<div class="msg_right"><img src="../img/close.png" alt="Close" border="0" id="close_msg" />';
										saved_error = saved_error + '<p>Could not Save. Please try again.</p></div>';
										MsgRight(saved_error);
									}										
							}, "text");						
						} else {
							MsgRight(this_text);
						}
					});
      				break;
				case "Share":
	  				var share_msg = '<div class="ajax_form"><img src="../img/close.png" alt="Close" border="0" id="close_msg" />';
					share_msg = share_msg + '<form name="share_linklog"><fieldset><h1>Share</h1><ol><li class="answer">Permalink (more descriptive)</li>';
					share_msg = share_msg + '<li class="answer">Short URL (better for Twitter)</li></ol></fieldset></form></div>';
					MsgRight(share_msg);
					break;		
				case "Publish":
					$.get("check_user_ajax.php", function(response){					
						response = jQuery.trim(response);
						if (response == 'Y') {
							var loading_text = '<div class="ajax_form"><h1>Loading...</h1><div class="loading">'; 
							loading_text = loading_text + '<img src="../img/throbber.gif" alt="Loading" border="0" id="loading" /></div></div>';
							MsgRight(loading_text);
							$(col_name + ' .ajax_form .loading').show();
							$.post('publish_linklog_ajax.php', {
								'open_llog_id': $(llog_id_input).val()
								}, function(response){
									response = jQuery.trim(response);
									if (response != '') {
										$('.ajax_form').html(response);
									} else {
										var edit_error = '<div class="msg_right"><img src="../img/close.png" alt="Close" border="0" id="close_msg" />';
										edit_error = edit_error + '<p>Could not Publish. Please try again.</p></div>';
										MsgRight(edit_error);
									}
							}, "html");
						} else {
							MsgRight(this_text);
						}
					});
					break;
				case "Follow":
      				$.get("check_user_ajax.php", function(response){
  						response = jQuery.trim(response);
						if (response == 'Y') {
							DisplayLoading();
							$.post('follow_linklog_ajax.php', {
								'open_llog_id': $(llog_id_input).val(),
								'action': 'follow'
								}, function(response){
  									response = jQuery.trim(response);					
									if (response > 0) {
										var existing_id = response;
										var follow_msg = '<div class="ajax_form"><h2>You are now following this linklog.</h2>';
										follow_msg = follow_msg + '<h2 class="answer">OK</h2></div>';
										DisplayLinklog(existing_id, follow_msg);
										if (remove_linklog == 'Y') {
											ShowLinklog(existing_id);
										}
									} else {
										$(col_name + ' .loading').hide();
										var saved_error = '<div class="msg_right"><img src="../img/close.png" alt="Close" border="0" id="close_msg" />';
										saved_error = saved_error + '<p>Could not Follow linklog. Please try again.</p></div>';
										MsgRight(saved_error);
									}										
							}, "text");						
						} else {
							MsgRight(this_text);
						}
					});
      				break;
				case "Remove":
      				$.get("check_user_ajax.php", function(response){
  						response = jQuery.trim(response);
						if (response == 'Y') {
							DisplayLoading();
							$.post('follow_linklog_ajax.php', {
								'open_llog_id': $(llog_id_input).val(),
								'action': 'remove'
								}, function(response){
  									response = jQuery.trim(response);					
									if (response > 0) {
										var existing_id = response;
										var remove_msg = '<div class="ajax_form"><h2>You are no longer following this linklog</h2>';
										remove_msg = remove_msg + '<h2 class="answer">OK</h2></div>';
										DisplayLinklog(existing_id, remove_msg);
										if (remove_linklog == 'Y') {
											HideLinklog(existing_id);
										}
									} else {
										$(col_name + ' .loading').hide();
										var saved_error = '<div class="msg_right"><img src="../img/close.png" alt="Close" border="0" id="close_msg" />';
										saved_error = saved_error + '<p>Could not Remove linklog. Please try again.</p></div>';
										MsgRight(saved_error);
									}										
							}, "text");						
						} else {
							MsgRight(this_text);
						}
					});
      				break;
			}
		}
	});
});
// Control Link actions (l_actions)
$(document).ready(function() {
	$(col_name).click(function(event) {
		if ($(event.target).is('.l_actions *, .vote *')) {
			var this_action = $(event.target).text();
			var this_text = '<div class="msg_right"><img src="../img/close.png" alt="Close" border="0" id="close_msg" />'; 
			this_text = this_text + '<p>Please <span class="answer">Log in</span> to ' + this_action + '.</p></div>';
			if ($(event.target).is('.vote *')) {
				var this_text = '<div class="msg_right"><img src="../img/close.png" alt="Close" border="0" id="close_msg" />'; 
				this_text = this_text + '<p>Please <span class="answer">Log in</span> to Vote.</p></div>';
			}	
			this_action = jQuery.trim(this_action);
			switch (this_action) {
   				case "Edit":
					$.get("check_user_ajax.php", function(response){					
						response = jQuery.trim(response);
						if (response == 'Y') {
							var loading_text = 'Loading...<div class="loading">'; 
							loading_text = loading_text + '<img src="../img/throbber.gif" alt="Loading" border="0" id="loading" /></div>';
							var this_action = $(event.target);
							MsgLink(loading_text, this_action);
							$(col_name + ' .l_action_box .loading').show();
							$.post('edit_link_ajax.php', {
								'l_id' : $(event.target).closest('.xfolkentry').attr('id')
								}, function(response){
									response = jQuery.trim(response);
									if (response != '') {
										$('.l_action_box').css('height', '20em');
										MsgLink(response, 'N');
									} else {
										var edit_error = '<div class="msg_right"><img src="../img/close.png" alt="Close" border="0" id="close_msg" />';
										edit_error = edit_error + '<p>Could not edit. Please try again.</p></div>';
										MsgLink(edit_error, 'N');
									}
							}, "html");
						} else {
							MsgRight(this_text);
							var destination = $('a[name="top"]').offset().top;
							$("html").animate({ scrollTop: destination}, 1000);
						}
					});
					break;
   				case "Delete":
					$.get("check_user_ajax.php", function(response){					
						response = jQuery.trim(response);
						if (response == 'Y') {
							var this_msg = 'Delete?<span class="answer">(Yes, delete link)</span>';
							var this_event = $(event.target);
							MsgLink(this_msg, this_event);
						} else {
							MsgRight(this_text);
							var destination = $('a[name="top"]').offset().top;
							$("html").animate({ scrollTop: destination}, 1000);
						}
					});
					break;   				
				case "Move":
					$.get("check_user_ajax.php", function(response){					
						response = jQuery.trim(response);
						if (response == 'Y') {
							var this_msg = 'Move?<span class="answer">(Move up)</span><span class="answer">(Move down)</span>';
							var this_event = $(event.target);
							MsgLink(this_msg, this_event);
						} else {
							MsgRight(this_text);
							var destination = $('a[name="top"]').offset().top;
							$("html").animate({ scrollTop: destination}, 1000);
						}
					});
					break;
				case "Vote":
					$.get("check_user_ajax.php", function(response){					
						response = jQuery.trim(response);
						if (response == 'Y') {
							var this_msg = 'Vote?<span class="answer">(Vote up)</span><span class="answer">(Vote down)</span>';
							var this_event = $(event.target);
							MsgLink(this_msg, this_event);
						} else {
							MsgRight(this_text);
							var destination = $('a[name="top"]').offset().top;
							$("html").animate({ scrollTop: destination}, 1000);
						}
					});
					break;
				case "+":
					$.get("check_user_ajax.php", function(response){					
						response = jQuery.trim(response);
						if (response == 'Y') {
							var this_msg = 'Vote?<span class="answer">(Vote up)</span>';
							var this_event = $(event.target).closest('.xfolkentry');
							this_event = this_event.find('.l_actions li');
							this_event = this_event.filter(":contains('Vote')");
							MsgLink(this_msg, this_event);
						} else {
							MsgRight(this_text);
							var destination = $('a[name="top"]').offset().top;
							$("html").animate({ scrollTop: destination}, 1000);
						}
					});
					break;
				case "-":
					$.get("check_user_ajax.php", function(response){					
						response = jQuery.trim(response);
						if (response == 'Y') {
							var this_msg = 'Vote?<span class="answer">(Vote down)</span>';
							var this_event = $(event.target).closest('.xfolkentry');
							this_event = this_event.find('.l_actions li');
							this_event = this_event.filter(":contains('Vote')");
							MsgLink(this_msg, this_event);
						} else {
							MsgRight(this_text);
							var destination = $('a[name="top"]').offset().top;
							$("html").animate({ scrollTop: destination}, 1000);
						}
					});
					break;   	   	   					   
				 case "Flag":
      				$.get("check_user_ajax.php", function(response){					
						response = jQuery.trim(response);
						if (response == 'Y') {
							var this_msg = 'Flag?<span class="answer">Spam (advertising, misleading, off topic)</span><span class="answer">Offensive (nudity, profanity, violence)</span>';
							this_msg =  this_msg + '<span class="answer">Not working (incorrect URL)</span>';
							var this_event = $(event.target);
							MsgLink(this_msg, this_event);
							$('.l_action_box').css('height', '10em');
						} else {
							MsgRight(this_text);
							var destination = $('a[name="top"]').offset().top;
							$("html").animate({ scrollTop: destination}, 1000);
						}
					});
					break;
				case "Share":
					var loading_text = 'Loading...<div class="loading">'; 
					loading_text = loading_text + '<img src="../img/throbber.gif" alt="Loading" border="0" id="loading" /></div>';
					var this_action = $(event.target);
					MsgLink(loading_text, this_action);
					$(col_name + ' .l_action_box .loading').show();
					$.post('share_link_ajax.php', {
						'l_id' : $(event.target).closest('.xfolkentry').attr('id')
						}, function(response){
							response = jQuery.trim(response);
							if (response != '') {
								$('.l_action_box').css('height', '17em');
								MsgLink(response, 'N');
							} else {
								var edit_error = '<div class="msg_right"><img src="../img/close.png" alt="Close" border="0" id="close_msg" />';
								edit_error = edit_error + '<p>Could not share. Please try again.</p></div>';
								MsgLink(edit_error, 'N');
							}
					}, "html");
					break;
			}
		}
	});
});
// Submit Forms
$(document).ready(function() {
	$(col_name).click(function(event) {
		if ($(event.target).is('#save_edit')) {
			SubmitEditLlog();
		}
		if ($(event.target).is('#save_edit_link')) {
			var l_id = $(event.target).closest('.xfolkentry').attr('id');
			SubmitEditLink(l_id);
		}
		if ($(event.target).is('#save_link')) {
			SubmitAddLink();
		}
		if ($(event.target).is('#cast_vote')) {
			var llog_id = $('#llog_id').val();
			var l_id = $('#l_id').val();
			var where = $('#where').val();
			var selected_term = $('#select_search_term').val();
			if (selected_term) {
				$('#selected_search_term').val(selected_term);
			}
			var this_search_term = $('#selected_search_term').val();
			SubmitVoteLink(llog_id, l_id, where, this_search_term);
		}
	});
});
// Prevents forms from being submitted
$(document).ready(function() {
	$('input').live("keypress", function(e) {             
		 if (e.keyCode == '13') {
		 	if ($(this).closest('form').attr('name') == 'edit_linklog') {
				SubmitEditLlog();
				return false;
			}
			if ($(this).closest('form').attr('name') == 'add_link') {
				SubmitAddLink();
				<!-- enter does not submit the form --> 
				return false;
			}
			if ($(this).closest('form').attr('name') == 'edit_link') {
				var l_id = $(this).closest('.xfolkentry').attr('id');
				SubmitEditLink(l_id);
				return false;
			}
		 }
 	});     
}); 
// Helper function - Display message right
function MsgRight(text) {
	// Remove any previous text if any
	RemoveMsg();
	$(col_name).prepend(text);
	$('.linklog').fadeTo("slow", 0.33);
}
// Helper function - Display loading
function DisplayLoading(target) {
	if (target == 'link') {
		MsgLink('<div class="loading"><img src="../img/throbber.gif" alt="Loading" border="0" id="loading" />Processing...</div>', 'N');
		$(col_name + ' .l_action_box .loading').show();
	} else {
		$(col_name + ' .loading').remove();
		MsgRight('<div class="loading"><img src="../img/throbber.gif" alt="Loading" border="0" id="loading" /></div>');
		$(col_name + ' .loading').show();
	}	
}
// Helper function - Display linklog
function DisplayLinklog(existing_id, msg_text) {
	if (existing_id > 0) {
		if (msg_text) {
			$.post('linklog_ajax.php', {'llog_id': existing_id, 'msg_text': msg_text}, function(return_data) {
				$(col_name).html(return_data);
			}, "text");
		} else {
			$.post('linklog_ajax.php', {'llog_id': existing_id}, function(return_data) {
				$(col_name).html(return_data);
			}, "text");
		}
	$(col_name + ' .loading').hide();
	}
}
// Helper function - Display link - Works only with Edit
function DisplayLink(l_id) {
	$.post('link_ajax.php', {'l_id': l_id}, function(return_data) {
		var curr = $('.l_action_box').closest('.xfolkentry');
		return_data = jQuery.trim(return_data);
		if (return_data != '') {
			curr.replaceWith(return_data);
			var this_msg = 'The Link has been Updated.';
			var this_event = $('#' + l_id);
			this_event = this_event.find('.l_actions li');
			this_event = this_event.filter(":contains('Edit')");
			MsgLink(this_msg, this_event);
		} else {
			$(col_name + ' .l_action_box .loading').hide();
			var update_error = '<div class="l_action_msg">Error. Could not display link. Please try again.';
			update_error = update_error + '<span id="close_msg">(hide)</span></div>'
			$('.l_action_box').css('height', '8em');
			MsgLink(update_error, 'N');		
		}
	}, "text");
}
// Helper function - Submit Edit linklog
function SubmitEditLlog() {
	var llog_title = $('input[name="title"]').val();
	var llog_description = $('textarea[name="description"]').val();
	var loading_text = '<div class="ajax_form"><h1>Loading...</h1><div class="loading">'; 
	loading_text = loading_text + '<img src="../img/throbber.gif" alt="Loading" border="0" id="loading" /></div></div>';
	MsgRight(loading_text);
	$(col_name + ' .ajax_form .loading').show();
	$.post('update_linklog_ajax.php', {
		'open_llog_id': $(llog_id_input).val(),
		'title': llog_title,
		'description': llog_description
		}, function(response){
			response = jQuery.trim(response);
			if (response != '') {
				var existing_id = $(llog_id_input).val();
				var update_msg = '<div class="ajax_form">' + response + '</div>';
				// Success msg coming from update_linklog_ajax.php
				DisplayLinklog(existing_id, update_msg);
			} else {
				var update_error = '<div class="msg_right"><img src="../img/close.png" alt="Close" border="0" id="close_msg" />';
				update_error = update_error + '<p>Could not save. Please try again.</p></div>';
				MsgRight(update_error);
			}
	}, "text");
}
// Helper function - Submit Edit link
function SubmitEditLink(l_id) {
	var l_title = $('input[name="title"]').val();
	var l_description = $('textarea[name="description"]').val();
	var l_url = $('input[name="url"]').val();
	DisplayLoading('link');
	$.post('update_link_ajax.php', {
		'l_id': l_id,
		'title': l_title,
		'description': l_description,
		'open_llog_id': $(llog_id_input).val(),
		'url': l_url
		}, function(response){
			response = jQuery.trim(response);
			if (response != '') {
				if (response > 0) {
					var l_id = response;
					DisplayLink(l_id);
				} else {
					MsgLink(response, 'N');
				}
			} else {
				$(col_name + ' .l_action_box .loading').hide();
				var update_error = '<div class="l_action_msg">Could not save. Please try again.';
				update_error = update_error + '<span id="close_msg">(hide)</span></div>';
				$('.l_action_box').css('height', '8em');
				MsgLink(update_error, 'N');
			}
	}, "text");
}
// Helper function - Submit Add link
function SubmitAddLink() {
	var l_title = $('input[name="title"]').val();
	var l_description = $('textarea[name="description"]').val();
	var l_url = $('input[name="url"]').val();
	var loading_text = '<div class="ajax_form"><h1>Loading...</h1><div class="loading">'; 
	loading_text = loading_text + '<img src="../img/throbber.gif" alt="Loading" border="0" id="loading" /></div></div>';
	MsgRight(loading_text);
	$(col_name + ' .ajax_form .loading').show();
	$.post('add_link.php', {
		'title': l_title,
		'description': l_description,
		'open_llog_id': $(llog_id_input).val(),
		'url': l_url
		}, function(response){
			response = jQuery.trim(response);
			if (response != '') {
				var existing_id = $(llog_id_input).val();
				var update_msg = '<div class="ajax_form">' + response + '</div>';
				// Success msg coming from update_linklog_ajax.php
				DisplayLinklog(existing_id, update_msg);
			} else {
				var update_error = '<div class="msg_right"><img src="../img/close.png" alt="Close" border="0" id="close_msg" />';
				update_error = update_error + '<p>Could not save. Please try again.</p></div>';
				MsgRight(update_error);
			}
	}, "text");
}
// Helper function - Submit Vote link
function SubmitVoteLink(llog_id, l_id, where, selected_search_term) {
	DisplayLoading('link')
	$.post('cast_vote_ajax.php', {
		'l_id': l_id,
		'search_term': selected_search_term,
		'llog_id': llog_id,
		'where': where
		}, function(response){
			response = jQuery.trim(response);
			if (response != '') {
				if (response == 'same') {
					$(col_name + ' .l_action_box .loading').hide();
					$('.l_action_box').css('height', '8em');
					var vote_msg = '<div class="l_action_msg">Thank you! Your vote has been recorded.';
					var vote_msg = vote_msg + '<span id="close_msg">(hide)</span></div>';
					MsgLink(vote_msg, 'N');
				} else {
					if (response == 'up') {
						$(col_name + ' .l_action_box .loading').hide();
						$('.l_action_box').css('height', '8em');
						var vote_msg = '<div class="l_action_msg">Thank you! Your vote has been recorded and the link has moved up.';
						var vote_msg = vote_msg + '<span id="close_msg">(hide)</span></div>';
						MsgLink(vote_msg, 'N');
						var curr = $('.l_action_box').closest('.xfolkentry');
						var prev = curr.prev();
						var this_clone = curr.clone();
						MoveUp(curr, prev, this_clone);
					}
					if (response == 'down') {
						$(col_name + ' .l_action_box .loading').hide();
						$('.l_action_box').css('height', '8em');
						var vote_msg = '<div class="l_action_msg">Thank you! Your vote has been recorded and the link has moved down.';
						var vote_msg = vote_msg + '<span id="close_msg">(hide)</span></div>';
						MsgLink(vote_msg, 'N');
						var curr = $('.l_action_box').closest('.xfolkentry');
						var next = curr.next();
						var this_clone = curr.clone();
						MoveDown(curr, next, this_clone);
					} 
					if  (response != 'up' && response != 'down') {
						$(col_name + ' .l_action_box .loading').hide();
						$('.l_action_box').css('height', '8em');
						var voted = '<div class="l_action_msg">' + response;
						var voted = voted + '.<span id="close_msg">(hide)</span></div>';
						MsgLink(voted, 'N');
					}
				}
			} else {
				$(col_name + ' .l_action_box .loading').hide();
				var update_error = '<div class="l_action_msg">Could not get vote. Please try again.';
				update_error = update_error + '<span id="close_msg">(hide)</span></div>';
				$('.l_action_box').css('height', '8em');
				MsgLink(update_error, 'N');
			}
	}, "text");
}		
// Helper function - Display message under link
function MsgLink(text, this_event) {
	var open_div = '<div class="l_action_box"><div class="l_action_msg">';
	var close_div = '<span id="close_msg">(hide)</span></div></div>';
	if (this_event == 'N') {
		$('.l_action_box').html(text);
	} else {
		// Remove any previous text if any
		RemoveMsg();
		this_event.closest('.l_actions').append(open_div + text + close_div);
		this_event.attr('class', 'selected_action');
	}
}	
// Helper function - Remove open message
function RemoveMsg(restore) {
	// Remove any open message 
	var this_right = $(col_name).find(".msg_right").text();
	if (this_right != '') {
		$('.msg_right').remove();
		if (restore == 'Y') {
			$('.linklog').fadeTo("fast", 1);
		}	
	}
	var this_form = $(col_name).find(".ajax_form").text();
	if ( this_form != '') {
		$('.ajax_form').remove();
		if (restore == 'Y') {
			$('.linklog').fadeTo("fast", 1);
		}	
	}
	var this_link = $(col_name).find(".l_action_box").text();
	if (this_link != '') {
		$('.l_action_box').remove();
		$('.selected_action').attr('class', '');
	}	
}
// Helper function - Move link up
function MoveUp(curr, prev, this_clone) {
	curr.css({position: "relative", top: 0, left : 0});
	curr.animate({"top": "-=8em"}, "slow", function () {
		prev.before(this_clone);
		curr.remove();
	});
}
// Helper function - Move link down
function MoveDown(curr, next, this_clone) {
	curr.css({position: "relative", top: 0, left : 0});
	curr.animate({"top": "+=8em"}, "slow", function () {
		next.after(this_clone);
		curr.remove();
	});
}
// Helper function - Save Preferences
function SavePreferences() {
	$('#search_preferences').css('color', '#707070');
	$('#search_preferences').text('Processing...');
	$('#search_preferences').css('cursor', 'default');
	$.post('set_preferences_ajax.php', {
		'add_link': $('#add_link_mode').text(),
		'filter': $('#filter_search').text()
		}, function(response) {
			response = jQuery.trim(response);
			if (response != '') {
				$('#search_preferences').text('Preferences:');
				$('#search_preferences').css('cursor', 'pointer');
			} else {
				$('#search_preferences').css('color', '#dd3c10');
				$('#search_preferences').text('Error. Try again.');
				$('#search_preferences').css('cursor', 'pointer');		
			}
	}, "text");
}
// Helper function - HideLinklog
function HideLinklog(existing_id) {
	var this_id = '#' + existing_id;
	var count =  $('input[name="linklog_count"]').val();
	$(this_id).hide();
	if (count > 0) {
		count = count - 1;
		if (count > 1) {
			$('#llog_count').text('You are following ' + count + ' linklogs');
			$('input[name="linklog_count"]').val(count);
		} else {
			$('#llog_count').text('You are following ' + count + ' linklog');
			$('input[name="linklog_count"]').val(count);
		}
	}
}
// Helper function - ShowLinklog
function ShowLinklog(existing_id) {
	var this_id = '#' + existing_id;
	var count =  $('input[name="linklog_count"]').val();
	$(this_id).show();
	count = ++count;
	if (count > 1) {
		$('#llog_count').text('You are following ' + count + ' linklogs');
		$('input[name="linklog_count"]').val(count);
	} else {
		$('#llog_count').text('You are following ' + count + ' linklog');
		$('input[name="linklog_count"]').val(count);
	}
}