$(function() {

	$('#invite-link').click(function() {
		$(this).select();
		return;
	});
	$('#profile-link').click(function() {
		$(this).select();
		return;
	});

	$('form#delete-bulletin').submit(function(){
    return confirm('Are you sure you would like to delete the selected bulletin(s)?');
  });
	
	
	$("div#loading").hide();
	// This is for the paging links
	var paging_page = 1;
	
	function own_groups_topics_to_page(page) {
		var id = page - 1;
		
		if (paging_page == page) return false;
		
		// Let's enable the other paging links
		for (j=0; j<$("div#paging a").size(); j++) {
			if (id != j) $("div#paging a").eq(j).attr("class","enabled");
		}
		
		// Let's set the data variables
		var infoPieces = $("div#paging a").eq(id).attr("name").split(",");
		var currentPage = infoPieces[0];
		var queryLimit = infoPieces[1];
		var pagesTotal = infoPieces[2];
		var forumId = infoPieces[3];
		var groupId = infoPieces[3];
		
		// Let's display/hide the previous/links
		if (id >= 1) $("span.previous").show(); else $("span.previous").hide();
		if (id != pagesTotal-1) $("span.next").show(); else $("span.next").hide();
		
		// Let's do the behind-the-scenes-call
		window.scroll(0, 0);
		$("div#loading").show(); 
		$("div#manage_topics").hide();
				
		$.get("/services/mygroup_topics/"+ groupId + "/" + forumId + "/" + queryLimit + "/" + currentPage, function(data) {
			if ($("topics topic", data).size() > 0) {
				var table_content = '';
				
				$("topics topic", data).each(function(i) {
					var topic = $(this);
					
					table_content += '<tr><td><p class="manage-topic-title"><a href="/forum/topic/' + topic.find("id").text() + '">' + topic.find("title").text() + '</a></p><p class="manage-topic-meta"><span class="manage-meta-when">Posted <b>' + topic.find("posted").text() + '</b></span><span class="manage-meta-replies">';
					
					if (topic.find("reply_count").text() != 0) table_content += '<a href="/forum/topic/' + topic.find("id").text() + '/#replies">';
					
					table_content += topic.find("reply_count").text();
					
					table_content += (topic.find("reply_count").text() == 1 ? ' Reply' : ' Replies');
					
					if (topic.find("reply_count").text() != 0) table_content += '</a>';
																			 
					table_content += '</span></p></td><td><span class="manage-topic-status" id="status_' + topic.find("id").text() + '">' + topic.find("status").text() + '</span></td><td>';
					
					if (topic.find("is_open").text() == 1) table_content += '<a href="#" class="close_topic" topic_id="' + topic.find("id").text() + '">Close</a>';
					
					table_content += '</td></tr>';
					
					if (i == $("topics topic", data).size()-1) {
						$("div#loading").hide();
						
						table_content = '<table><colgroup><col id="col_topic_details" /><colgroup id="colgp_topic_status"><col id="col_topic_status"/><col id="col_topic_close"/></colgroup></colgroup><thead><tr><th abbr="Topic Details" scope="col">Topic Details</th><th abbr="Status" scope="colgroup" colspan="2">Status</th></tr></thead><tbody>' + table_content + '</tbody></table>';
						
						$("div#manage_topics").empty().append(table_content).show();
					}
				});
			} else {
				$("div#loading").hide();
				$("div#manage_topics").show();
			}
		});
		
		$("div#paging a").eq(id).attr("class","disabled");
		paging_page = page;
	}
	
	function forum_search_to_page(page) {
		var id = page - 1;
		
		if (paging_page == page) return false;
		
		// Let's enable the other paging links
		for (j=0; j<$("div#paging a").size(); j++) {
			if (id != j) $("div#paging a").eq(j).attr("class","enabled");
		}
		
		// Let's set the data variables
		var infoPieces = $("div#paging a").eq(id).attr("name").split(",");
		var currentPage = infoPieces[0];
		var queryLimit = infoPieces[1];
		var pagesTotal = infoPieces[2];
		var group_id = infoPieces[3];
		var urlVars = infoPieces[4];
		
		// Let's display/hide the previous/links
		if (id >= 1) $("span.previous").show(); else $("span.previous").hide();
		if (id != pagesTotal-1) $("span.next").show(); else $("span.next").hide();
		
		// Let's do the behind-the-scenes-call
		window.scroll(0, 0);
		$("div#loading").show(); 
		$("div#search_topics").hide();
				
		$.get("/services/group_forum_search/" + queryLimit + "/" + currentPage + "/" + group_id + "?" + urlVars, function(data) {
			if ($("topics topic", data).size() > 0) {
				var table_content = '';
				
				$("topics topic", data).each(function(i) {
					var topic = $(this);
					
					table_content += '<li class="clearfix"><div class="div-left"><img class="left profile-photo-sm" src="' + topic.find("poster_profile_photo").text() + '" alt="' + topic.find("poster_full_name").text() + '\'s profile photo" /></div><div class="div-right"><p class="topic-title"><a href="/forum/topic/' + topic.find("id").text() + '">' + topic.find("title").text() + '</a></p><p class="topic-meta"><span class="meta-replies">';
					
					if (topic.find("reply_count").text() != 0) table_content += '<a href="/forum/topic/' + topic.find("id").text() + '/#replies">';
					
					table_content += topic.find("reply_count").text();
					table_content += (topic.find("reply_count").text() == 1 ? " Reply" : " Replies");
														
					if (topic.find("reply_count").text() != 0) table_content += '</a>';
					
					
					table_content += '</span><span class="meta-poster">Posted by <a href="/people/profiles/' + topic.find("poster_id").text() + '">' + topic.find("poster_full_name").text() + '</a>';
					
					if (topic.find("group").text() != "" && topic.find("administered_by_poster") == 1) table_content += '<img class="group-admin-img" src="' + topic.find("group_admin_icon").text() + '" />';
					
					
					table_content += '</span><span class="meta-when-where">' + topic.find("created").text() + ' in <a href="/forum/search?cat=' + topic.find("cat_id").text() + '">' + topic.find("cat_title").text() + '</a></span></p></div></li>';
					
					if (i == $("topics topic", data).size()-1) {
						$("div#loading").hide();
						
						table_content = '<ul>' + table_content + '</ul>';
						
						$("div#search_topics").empty().append(table_content).show();
					}
				});
			} else {
				$("div#loading").hide();
				$("div#search_topics").show();
			}
		});
		
		$("div#paging a").eq(id).attr("class","disabled");
		paging_page = page;
	}
	
	$("span.next").click(function() {
		if ($("div#paging a").eq(paging_page).attr("id") == "group-own-topics") {
			own_groups_topics_to_page(paging_page+1);
		} else if ($("div#paging a").eq(paging_page).attr("id") == "group-private-forum-search-results") {
			forum_search_to_page(paging_page+1);
		}
	});
	
	$("span.previous").click(function() {
		if ($("div#paging a").eq(paging_page-2).attr("id") == "group-own-topics") {
			own_groups_topics_to_page(paging_page-1);
		} else if ($("div#paging a").eq(paging_page-2).attr("id") == "group-private-forum-search-results") {
			forum_search_to_page(paging_page-1);
		}
	});
	
	$("div#paging a").each(function(i) {
		$("div#paging a").eq(i).click(function() {			
			if ($("div#paging a").eq(i).attr("id") == "group-own-topics") {
				own_groups_topics_to_page(i+1);
			} else if ($("div#paging a").eq(i).attr("id") == "group-private-forum-search-results") {
				forum_search_to_page(i+1);
			}
		});
	});
	
});


	function flag(event){
		var id=$(event.target).attr('id');

		if(id=='flag_topic'){
			var replyId=null;
			var formAction=defaultFlagAction;
			$(event.target).hide();
			var insertAfterEl=$(event.target).parent();
			$('#flag_entity').html('topic');
			$(".lnk_flag").show();
			$('.flag_entity').text('topic');
		}else{
			var replyId=$(event.target).attr('reply_id');
			var formAction=defaultFlagAction+'/'+replyId;
			$("#cancel_flag").attr('reply_id',replyId);		
			var insertAfterEl=$(event.target);
			$('#flag_entity').html('reply');	
			$(".lnk_flag").show();	
			$('.flag_entity').text('reply');		
		}
		insertAfterEl.after($('#flag_form'));
		var flagForm=$('form_flag');


		$('#form_flag').attr('action',formAction);
		$('#flag_form').show();
		$('#form_flag TEXTAREA').get(0).focus();
		hideReply(event);
	}
	function hideFlag(event){
		$('#flag_form').hide();
		$(".lnk_flag").show();
		if(replyId=$(event.target).attr('reply_id')){
			$("#flag_reply_"+replyId).show();
		}else{
			$("#flag_topic").show();
		}

		return;
	}

	function reply(event){
		$(event.target).after($('#reply_form'));
		$(event.target).hide();
		hideFlag(event);
		$('#reply_form').show();
		$('#reply_form TEXTAREA').get(0).focus();	
		$("#cancel_reply").show();
	}
	function hideReply(event){
		$(event.target).hide();
		$('.lnk_reply').show();
		$('#reply_form').hide();
		
	}

	function toggleRecentPosts(event){
		var target=$(event.target);
		var id=target.attr('id');
		var recentReplies=$("#recent_replies");
		var recentTopics=$("#recent_topics");
		if(id=='show_recent_topics'){
			recentReplies.hide();		
			$("#show_recent_replies").removeClass('active');
			$("#show_recent_topics").addClass('active');
			recentTopics.show();
			return false;
		}else{
			recentTopics.hide();	
			$("#show_recent_topics").removeClass('active');
			$("#show_recent_replies").addClass('active');
			recentReplies.show();		
			return false;
		}
	}

	function closeTopic(event){
		var topicId=$(event.target).attr('topic_id');
		if(!topicId) return false;
		var statusEl=$('#status_'+topicId);
		
		var url=closeTopicUrl+'/'+topicId;
		var target=$(event.target);
		
		if (confirm("Are you sure you would like to close this topic to new replies?")) {
			$.post(url,{'id':topicId},function(xml){
					if($('response', xml).size() == 0) {
						alert('An error occured. Please try again.');
					}else{
						var status=$('success', xml).text();
						if(status==1){
							statusEl.text('Closed');
							target.remove();
						}else{
							alert('The topic could not be closed. Try again');
						}
					}
			},"xml");
		} else return false;
	}

	function toggleOptions(event){
		$('#more_options').toggle();
		$('#less_options').toggle();
		$('#options').toggle();
		if($('#options').css('display')=='none'){
			firstOpt=$('#categories').find('option').eq(0).attr('selected','selected');
			$('#notempty').attr('checked','');
			$('#open').attr('checked','');
		}
	}