/***************************************
 *
 * Groups library to manipulate the groups display
 *
 ***************************************/

function CGroups()
{
	this.removeTopic = function( title , groupid , topicid ){
		var ajaxCall	= 'jax.call("community","groups,ajaxShowRemoveDiscussion", "' + groupid + '","' + topicid + '");';
		
		cWindowShow( ajaxCall , title , 350 , 150 );
	}
	
	this.removeBulletin = function( title , groupid , bulletinid ){
		var ajaxCall	= 'jax.call("community","groups,ajaxShowRemoveBulletin", "' + groupid + '","' + bulletinid + '");';
		
		cWindowShow( ajaxCall , title , 350 , 150 );
	}
	
	this.unpublishGroup	= function( groupId ){
		jax.call( 'community' , 'groups,ajaxUnpublishGroup' , groupId );
	}

	this.saveJoinGroup	= function( groupid ){
		jax.call('community' , 'groups,ajaxSaveJoinGroup' , groupid);
	}

	this.redirect	= function ( url ){
		document.location.href= url;
	}

	this.leaveGroup		= function( groupid ){
		jax.call('community', 'groups,ajaxLeaveGroup' , groupid );
	}

	this.showLeaveGroup	= function( groupid ){
		var ajaxCall	= 'jax.call("community" , "groups,ajaxShowLeaveGroup" , "' + groupid + '");';

		cWindowShow( ajaxCall , 'Leave Group' , 450 , 170 );
	}

	this.joinGroup	= function( groupid ){
		var ajaxCall	= 'jax.call("community" , "groups,ajaxShowJoinGroup" , "' + groupid + '", location.href );';

		cWindowShow( ajaxCall , 'Join Group' , 450 , 170 );
	}

	this.addNews	= function ( groupId ){
		var ajaxCall = "jax.call('community', 'groups,ajaxAddNews', '" + groupId + "');";
		cWindowShow(ajaxCall, 'Add News Item', 450, 230);
	}

	this.saveNews	= function( groupid ){
		var title		= jQuery('#news-title').val();
		var description	= jQuery('#news-description').val();

		jax.call('community' , 'groups,ajaxSaveNews' , groupid , title , description );
	}

	this.insertNews	= function ( html ){
		cWindowHide();

		if( jQuery('#community-groups-news-items div.empty') == null )
		{
			jQuery('#community-groups-news-items').append(html);
		}
		else
		{
			jQuery('#community-groups-news-items').append(html);
			jQuery('#community-groups-news-items div.empty').remove();
		}
	}

	this.editGroup	= function(){

		// Check if input is already displayed
		jQuery('#community-group-info div div.cdata').each(function(){
			// Test if the next div is cinput

			if(jQuery(this).next().html() && jQuery(this).css('display') != 'none' )
				jQuery(this).css('display' , 'none');
			else
				jQuery(this).css('display' , 'block');
		})

		jQuery('#community-group-info div .cinput').each(function(){
			if(jQuery(this).css('display') == 'none')
				jQuery(this).css('display' , 'block');
			else
				jQuery(this).css('display' , 'none');
		})

		if(jQuery('div#community-group-info-actions').css('display') != 'none')
			jQuery('div#community-group-info-actions').css('display' , 'none');
		else
			jQuery('div#community-group-info-actions').css('display' , 'block');
	}

	this.manageGroup = function()
	{

	}

	this.saveGroup	= function( groupid ){
		var name		= jQuery('#community-group-name').val();
		var description	= jQuery('#community-group-description').val();
		var website		= jQuery('#community-group-website').val();
		var category	= jQuery('#community-group-category').val();
		var approvals	= jQuery("input[@name='group-approvals']:checked").val();
		
		jax.call('community' , 'groups,ajaxSaveGroup' , groupid , name , description , website , category , approvals);

	}

	this.updateGroup	= function( groupName , groupDescription , groupWebsite , groupCategory){

		// Re-update group data
		jQuery('#community-group-data-name').html( groupName );
		jQuery('#community-group-data-description').html( groupDescription );
		jQuery('#community-group-data-website').html( groupWebsite );
		jQuery('#community-group-data-category').html( groupCategory );

		cgroups.editGroup();
	}

	this.removeMember	= function( memberId , groupId ){
		jax.call('community' , 'groups,ajaxRemoveMember' , memberId , groupId );
	}
}

cgroups	= new CGroups();