$(function() {
	$("div#loading").hide();
	// This is for the paging links
	var paging_page = 1;
	
	function 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 queryTotal = infoPieces[2];
		var pagesTotal = infoPieces[3];
		var queryString = 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
		$.get("/services/mysearch/forum/" + queryLimit + "," + currentPage + "," + queryString, function(data) {
			if ($("search result", data).size() > 0) {
				var table_content = "<tr><th>Topic title</th><th>Status</th><th>Number of replies</th><th>Posted by</th><th>Date</th><th>Category</th></tr>";
								
				$("search result", data).each(function() {
					var result = $(this);
					
					table_content += '<tr><td>' + (result.find("is_open").text() == 1 ? '<b>' : '') + '<a href="/forum/topic/' + result.find("topic_id").text() + '">' + result.find("topic_title").text() + '</a>' + (result.find("is_open").text() == 1 ? '</b>' : '') + '</td><td>' + result.find("topic_status").text() + '</td><td>' + result.find("reply_count").text() + '</td><td><a href="/people/profiles/' + result.find("poster_id").text() + '" rel="external"><img class="profile-photo" style="display: block; height: 30px; width: 30px;" align="left" src="' + result.find("poster_profile_photo").text() + '" alt="' + result.find("poster_fullname").text() + '\'s profile photo" /></a><h6><a href="/people/profiles/' + result.find("poster_id").text() + '" rel="external">' + result.find("poster_fullname").text() + '</a></h6><p style="color: #777; font-size: 11px; line-height: 1.5em;">' + result.find("poster_headline").text() + '</p><p>' + result.find("poster_city").text() + ', ' + result.find("poster_country_display").text() + '<img src="' + result.find("poster_country_flag").text() + '" alt="' + result.find("poster_country_display").text() + '" /></p></td><td>' + result.find("date").text() + '</td><td>' + result.find("category").text() + '</td></tr>';
				});
				
				$(".forum-search-results").html(table_content);
			}
		});
		
		$("div#paging a").eq(id).attr("class","disabled");
		paging_page = page;
	}
	
	function own_forum_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];
		
		// 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/myforum_topics/" + 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 urlVars = 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#search_topics").hide();
				
		$.get("/services/forum_search/" + queryLimit + "/" + currentPage + "?" + 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") == "forum-own-topics") {
			own_forum_topics_to_page(paging_page+1);
		} else if ($("div#paging a").eq(paging_page).attr("id") == "forum-search") {
			forum_search_to_page(paging_page+1);
		} else {
			search_to_page(paging_page+1);
		}
	});
	
	$("span.previous").click(function() {
		if ($("div#paging a").eq(paging_page-2).attr("id") == "forum-own-topics") {
			own_forum_topics_to_page(paging_page-1);
		} else if ($("div#paging a").eq(paging_page-2).attr("id") == "forum-search") {
			forum_search_to_page(paging_page-1);
		} else {
			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") == "forum-own-topics") {
				own_forum_topics_to_page(i+1);
			} else if ($("div#paging a").eq(i).attr("id") == "forum-search") {
				forum_search_to_page(i+1);
			} else {
				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 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','');
		}
	}
	
	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);
		
		$.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");
		
	
	
	}
	
	