
var group = {
	
	tab: function(tabName, groupId) {
		jQuery('ul.inboxTabs li').removeClass('act');
		if (tabName == 'forum_topic' || tabName == 'forum_new_topic')
		{
			jQuery('ul.inboxTabs li#forum').addClass('act');
		} else {
			jQuery('ul.inboxTabs li#'+tabName).addClass('act');
		}
		if (groupId.constructor != Array)
		{
			var params = {id: groupId};
		} else {
			var params = {id: groupId[0], page: groupId[1]};
		}
		
		jQuery.get(
			site_url + 'ajax/group/view/'+ tabName + '.php', 
			params,
			function(data) {
				jQuery('#contentArea').html(data);
			}
		 );		
	}, 
	
	
	createTopic: function()
	{
		var valid = true;
		jQuery('#error_area')[0].innerHTML = '';
		// validate group name
		if (jQuery.trim(jQuery('#topic_name').val()).length == 0)
		{
			jQuery('#topic_name_label')[0].style['color'] = 'red';
			jQuery('#topic_name')[0].focus();
			jQuery('#error_area')[0].innerHTML += '<p>Please enter <strong>Topic Subject</strong></p>';
			valid = false;
		}
		else if (jQuery('#topic_name').val().length > 255) 
		{
			jQuery('#topic_name_label')[0].style['color'] = 'red';
			jQuery('#topic_name')[0].focus();
			jQuery('#error_area')[0].innerHTML += '<p><strong>Topic Subject</strong> is too long, please type not more than 255 symbols</p>';
			valid = false;
		}
		else
		{
			jQuery('#topic_name_label')[0].style['color'] = '#666666';
		}
	
		// validate description
		if (jQuery.trim(jQuery('#topic_message').val()).length == 0)
		{
			jQuery('#topic_message_label')[0].style['color'] = 'red';
			jQuery('#topic_message')[0].focus();
			jQuery('#error_area')[0].innerHTML += '<p>Please enter <strong>Your Message</strong></p>';
			valid = false;
		}
		else
		{
			jQuery('#topic_message_label')[0].style['color'] = '#666666';
		}
	
	
		if (!valid)
		{
			jQuery('#error_area').show();
		}
		else
		{
			jQuery('#error_area').hide();
			
			jQuery.post(
				site_url + 'ajax/group/view/forum_new_topic.php', 
				jQuery('#topic_form').serializeArray(), 
				function(data) { 
					if (data.constructor != Array) { 
						group.tab('forum_topic', data);
					} else {
						alert('You have no credentials to post new topic in this group.');
						group.tab('forum', data[0]);
					}
				},
				'json'
			);
			
		}
	},
	
	post: function()
	{
		var valid = true;
		jQuery('#error_area')[0].innerHTML = '';
		// validate group name
		
	
		// validate description
		if (jQuery.trim(jQuery('#post_message').val()).length == 0)
		{
			
			jQuery('#post_message')[0].focus();
			jQuery('#error_area')[0].innerHTML += '<p>Please enter <strong>Your Message</strong></p>';
			valid = false;
		}
	
		if (!valid)
		{
			jQuery('#error_area').show();
		}
		else
		{
			jQuery('#error_area').hide();
			
			jQuery.post(
				site_url + 'ajax/group/view/forum_topic.php', 
				jQuery('#post_form').serializeArray(), 
				function(data) { 
					group.tab('forum_topic', data);
				}, 
				'json'
			);
			
		}
	},
	
	deletePost: function(post_id, topic_id, page)
	{
		jQuery.post(site_url + 'ajax/group/view/delete_post.php', {id: post_id}, function(data) { 
			if (data == 'refresh')
				group.tab('forum_topic', [topic_id, page]); 
			else
				group.tab('forum', [data, 1]);
			jconfirm.popup.hide();
		});		
	}
	
}
