(function($, el){
	DiscussionBoard = {
		showResponseMessage: function (message){
			CustomPopup.initPrompt(message);
			CustomPopup.createPopup();			
		},
		
		showLoader: function () {
			var content = '<center><img src="http://images.goabroad.net/images/loading.gif"/> <p>Please wait...</p></center>';	
			CustomPopup.createPopUpUsingLayout1(content, 150, 150, 'loaderDimmer', 'loaderPopUp');				
		},
		
		hideLoader: function() {
			document.body.removeChild(el('loaderDimmer'));
			document.body.removeChild(el('loaderPopUp'));			
		}
	};
	
	Discussion = {		
		
        edit: function(id, title) {
			var html = ''+
				'<h4 class="header" id="popup_header"><strong>Edit Discussion</strong></h4>'+
				'<form id="template_form" action="/discussion/edit/2" method="post" onsubmit="Discussion.saveDiscussionForm(this); return false;">'+
					'<div id="popup_message" class = "confirm">'+
						'<div class="errors" style="display:none;"></div>'+
						'<ul class="form">'+
							'<li class="form_set">'+
								'<label class="basic" for="">Title <span class="required">*</span></label>'+
								'<input type="text" value="'+title+'" name="group_template_name" class="textBox_small"/>'+
							'</li>'+
						'</ul>'+
					'</div>'+
					'<div class="buttons_box" id="popup_buttons">'+
						'<p>'+
							'<input type="submit" value="Save" class="prompt_button"/>'+
							'<input type="button" onclick="jQuery(\'#edit_dimmer, #edit_pop_up\').remove();" value="Cancel" class="prompt_button"/>'+
						'</p>'+
					'</div>'+
				'</form>';
			CustomPopup.createPopUpUsingLayout1(html, 425, 150, 'edit_dimmer', 'edit_pop_up');
			
			Discussion.saveDiscussionForm = function(form) {
				jQuery('#edit_dimmer, #edit_pop_up').remove();
				$.post('/discussion/edit/'+id, $(form).serialize(),
				  function(resp)
				  {
				    if(resp.is_successful)
				    {
				      $('.discussion_title').html(resp.title);
				    }
				  }, 'json'
				);				
			}       
        },
        
		/**
		 * Activates a discussion.
		 * 
		 * @param int id The ID of the discussion
		 *
		 * @return void
		 */
		activate: function(id){
			DiscussionBoard.showLoader();
			$.post('/discussion/activate/'+id, '',
				function(response){
					var resp = eval(response);
					if (resp.isSuccessful) {
						$('#archive_'+id).css('display', '');
						$('#activate_'+id).css('display', 'none');	
					}
					DiscussionBoard.hideLoader();
					DiscussionBoard.showResponseMessage(resp.message);
				}, 'json'
			);			
		},

		/**
		 * Archives the discussion.
		 * 
		 * @param int id The ID of the discussion.
		 *
		 * @return void
		 */	
		archive: function(id){
			DiscussionBoard.showLoader();
			$.post('/discussion/archive/'+id, '',
				function(response){
					DiscussionBoard.hideLoader();
					var resp = eval(response);
					if (resp.isSuccessful) {
						$('#archive_'+id).css('display', 'none');
						$('#activate_'+id).css('display', '');	
					}
					DiscussionBoard.showResponseMessage(resp.message);
				}, 'json'
			);
		},
		
		/**
		 * Features a discussion.
		 *
		 * @param int 			id 				The ID of discussion to be featured.
		 * @param int 			mode 			The mode or the location where the action is made.
		 * @param function	callback	The function called after the featuring through ajax is done.
		 *
		 * @return void
		 */
		feature: function(id, mode, callback){
			DiscussionBoard.showLoader();
			var response_handler = function(response){
				DiscussionBoard.hideLoader();
				if (callback) {
					callback(response);
				}
				else {
					var resp = eval(response);
					if (resp.isSuccessful) {
						$('#pin_header').attr('class', 'pinned');
						$('#pin_image').css('display', '');
						$('#pin_action').css('display', 'none');
						$('#unpin_action').css('display', '');
					}
					DiscussionBoard.showResponseMessage(resp.message);
				}
			};
			
			$.post('/discussion/feature/'+id+'/'+mode, '', response_handler, 'json');			
		},
		
		/**
		 * Unfeatures a discussion.
		 *
		 * @param int 			id	 			The ID of discussion to be featured.
		 * @param int 			mode 			The mode or the location where the action is made.
		 * @param function	callback	The function called after the featuring through ajax is done.
		 *
		 * @return void
		 */		
		unfeature: function(id, mode, callback){
			DiscussionBoard.showLoader();
			var response_handler = function(response){
				DiscussionBoard.hideLoader();
				if (callback) {
					callback(response);
				}
				else {
					var resp = eval(response);
					if (resp.isSuccessful) {
						$('#pin_header').attr('class', '');
						$('#pin_image').css('display', 'none');
						$('#pin_action').css('display', '');
						$('#unpin_action').css('display', 'none');
					}
					DiscussionBoard.showResponseMessage(resp.message);
				}
			};
			
			$.post('/discussion/unfeature/'+id+'/'+mode, '', response_handler, 'json');
		},
		
		/**
		 * Adds the discussion to the knowledge base.
		 * 
		 * @param int 			id 				The ID of the discussion.
		 * @param int 			mode			The view mode for the success message.
		 * @param function	callback	The function called after the featuring through ajax is done.
		 *
		 * @return void
		 */
		addToKnowledgeBase: function(id, mode, callback){
			DiscussionBoard.showLoader();
			var response_handler = function(response){
				DiscussionBoard.hideLoader();
				if (callback) {
					callback(response);
				}
				else {
					var resp = eval(response);
					if (resp.isSuccessful) {
						$('#add_knowledge_action_'+id).css('display', 'none');
						$('#remove_knowledge_action_'+id).css('display', '');	
					}
					DiscussionBoard.showResponseMessage(resp.message);
				}
			}
			
			$.post('/discussion/add_to_knowledge_base/'+id+'/'+mode, '', response_handler, 'json');		
		},

		/**
		 * Removes a discussion from the knowledge base.
		 *
		 * @param int 		 id 					The ID of discussion to be featured.
		 * @param int 		 mode					The mode or the flag where the action comes from.
		 * @param function callback			The callback of the action
		 *
		 * @return void
		 */		
		removeFromKnowledgeBase: function(id, mode, callback){
			DiscussionBoard.showLoader();
			var response_handler = function(response){
				DiscussionBoard.hideLoader();
				if (callback) {
					callback(response);
				}
				else {
					var resp = eval(response);
					if (resp.isSuccessful) {
						$('#add_knowledge_action_'+id).css('display', '');
						$('#remove_knowledge_action_'+id).css('display', 'none');	
					}
					DiscussionBoard.showResponseMessage(resp.message);
				}
			}
			
			$.post('/discussion/remove_from_knowledge_base/'+id+'/'+mode, '', response_handler, 'json');		
		}
	};
	
	Post = {
		remove: function(id){
			DiscussionBoard.showLoader();
			$.post('/post/delete/'+id, '', 
				function(response){
					DiscussionBoard.hideLoader();
					var resp = eval(response);
					
					DiscussionBoard.showResponseMessage(resp.message);
					
					if (resp.isRedirect) {
						window.location.href = resp.url;
					} else if (resp.isSuccessful) {
						$('#'+id).css('display', 'none');
						$('#spacer_'+id).css('display', 'none');
					}
				}, 'json'
			);	
		} 
	}
	
	Topic = {
		oldPrivacy: null,
		showTopicPrivacyPreference : function(id, sg_name, pg_name){
			var privacy = $('#hidPrivacyPreference').val();
			Topic.oldPrivacy = privacy;
			var content = '<h4 class="header" ><strong>Edit Privacy Preference</strong></h4>'+
				'<div class="confirm" >'+
					'<form name="preference">'+	
						'<ul class="form">'+
						  '<li class="confirmthis">'+
						    '<input type="radio" name="radPref" id="radPref1" value="1" '+(("1" == privacy) ? 'checked="checked"' : '')+' />'+
						    '<label for="radPref1">Public</label>'+
						    '<p class="supplement">Posts can be viewed by anyone visiting your site but only members of '+pg_name+' can add posts. </p>'+
						  '</li><li class="confirmthis">'+
						    '<input type="radio" name="radPref" id="radPref2" value="2" '+(("2" == privacy) ? 'checked="checked"' : '')+' />'+
						    '<label for="radPref2">Members Only</label>'+
						    '<p class="supplement">Only members of the '+pg_name+' can view and add posts for this topic.</p>'+
						  '</li><li class="confirmthis">'+
							'<input type="radio" name="radPref" id="radPref3" value="3" '+(("3" == privacy) ? 'checked="checked"' : '')+' />'+
							'<label for="radPref3">Public but Posting for Group Only</label>'+
							'<p class="supplement">Posts can be viewed by anyone visiting your site, but only members of '+sg_name+' can add posts.</p>'+
						  '</li><li class="confirmthis">'+
							'<input type="radio" name="radPref" id="radPref4" value="4" '+(("4" == privacy) ? 'checked="checked"' : '')+' />'+
							'<label for="radPref4">Group Members Only</label><p class="supplement">Only members of '+sg_name+' can view and add posts.</p>'+
						  '</li>'+
						'</ul>'+
					'</form>'+
				'</div>'+
				'<div class="buttons_box" id="popup_buttons">'+
					'<input type="button" onclick="Topic.saveTopicPrivacySetting('+id+');" value="OK" class="prompt_button" />'+
					'<input type="button" value="Cancel" onclick="Topic.hidePrivacySetting();" class="prompt_button" />'+
				'</div>';	
			CustomPopup.createPopUpUsingLayout1(content, 550, 350, 'privacyDimmer', 'privacyPopUp');					
		},
		
		hidePrivacySetting: function(){
			document.body.removeChild(el('privacyDimmer'));
			document.body.removeChild(el('privacyPopUp'));				
		},
		
		removeDiscussionsFromKnowledgeBase: function(topicID, privacy){
			DiscussionBoard.showLoader();
			$.post('/topic/remove_discussions_from_knowledge_base/'+topicID+'/'+privacy, '', 
				function(response){
					DiscussionBoard.hideLoader();
					var resp = eval(response);
					DiscussionBoard.showResponseMessage(resp.message);
					if (resp.isSuccessful) {
						$('#hidPrivacySetting').val(rad_val);
						if ((("4" == rad_val || "3" == rad_val) && ("1" == Topic.oldPrivacy || "2" == Topic.oldPrivacy)) || (("1" == rad_val || "2" == rad_val) && ("3" == Topic.oldPrivacy || "4" == Topic.oldPrivacy))) {
							window.location.href = "";
						}
					}				
				}, 'json'
			);
		},
		
		saveTopicPrivacySetting: function(topicID){
			DiscussionBoard.showLoader();
			for (var i=0; i < document.preference.radPref.length; i++) {
				if (document.preference.radPref[i].checked) {
      		rad_val = document.preference.radPref[i].value;
      	}
   		}
			Topic.hidePrivacySetting();
			$.post('/topic/save_privacy_setting/'+topicID+'/'+rad_val, '',
				function(response){
					DiscussionBoard.hideLoader();
					var resp = eval(response);
					if (resp.isSuccessful) {
						if (resp.validate) {
							CustomPopup.initialize('Remove Discussions From The Knowledge Base?', resp.message, function(){ Topic.removeDiscussionsFromKnowledgeBase(topicID, rad_val); },'OK','1');
							CustomPopup.setJS();
							CustomPopup.createPopup();
						}
						else {
							$('#hidPrivacySetting').val(rad_val);
							DiscussionBoard.showResponseMessage(resp.message);
							if ((("4" == rad_val || "3" == rad_val) && ("1" == Topic.oldPrivacy || "2" == Topic.oldPrivacy)) || (("1" == rad_val || "2" == rad_val) && ("3" == Topic.oldPrivacy || "4" == Topic.oldPrivacy))) {
								window.location.href = "";
							}
						}
					}
					else {
						DiscussionBoard.showResponseMessage(resp.message);
					}
				}, 'json'
			);	
		}	
	};
})(jQuery, function(id) { return document.getElementById(id); });
