$(document).ready(function(){
	
});

function beMember(id_group)
{
	$.ajax({
				type: "POST",
				url: "ajax.php?mod=front&view=ajaxGroups&act=ajaxBeMemeber",
				data: "id_group="+id_group,		   
				dataType: 'json',
				beforeSend: function(){
//					$('#mainContent').empty();
//					$('#mainContent').append('<img id="loader4" class="loader-main" src="./images/ajax-loader.gif"/>');
				},
				success: function(data)
				{
					$('.infoGroupInOut').hide();
					$('#groupMessage'+id_group).parent('span').fadeIn();
					if(data == 1)
					{
						$('#groupMessage'+id_group).html('Dołączyłeś do grupy');
						getMyGroups(id_group);
					}
					else if(data == 2)
					{
						$('#groupMessage'+id_group).html('Oczekujesz na potwierdzenie');
						getMyGroups(id_group);
					}
					else if(data == 0)
					{
						$('#groupMessage'+id_group).html('Jesteś już członkiem tej grupy');
					}
					else
					{
						$('#groupMessage'+id_group).html('Problem. Dołącz póżniej');
					}
				}
				});	
}

function stopBeMember(id_group)
{
	$.ajax({
				type: "POST",
				url: "ajax.php?mod=front&view=ajaxGroups&act=ajaxStopBeMemeber",
				data: "id_group="+id_group,		   
				dataType: 'json',
				beforeSend: function(){
//					$('#mainContent').empty();
//					$('#mainContent').append('<img id="loader4" class="loader-main" src="./images/ajax-loader.gif"/>');
				},
				success: function(data)
				{
					$('.infoGroupInOut').hide();
					$('#groupMessage'+id_group).parent('span').fadeIn();
					if(data == 1)
					{
						$('#groupMessage'+id_group).html('Opuściłeś grupę');
						getMyGroupsAfterLeaving(id_group);
					}
					else if(data == 0)
					{
						$('#groupMessage'+id_group).html('Nie jestes członkiem tej grupy');
					}
					else
					{
						$('#groupMessage'+id_group).html('Problem. Dołącz póżniej');
					}
				}
				});	
}

function getMyGroupsAfterLeaving(id_group)
{
	var count = $('#usersCountB'+id_group).val();
	newCount = count-1;

	$('#mainG'+id_group).remove();
	$('#usersCount'+id_group).html(newCount);
	$('#usersCountInput'+id_group).html('<input type="hidden" id="usersCountB'+id_group+'" value="'+newCount+'"/>');
	$('#member'+id_group).html('');
	$('#member'+id_group).prev('img').hide();
	$('#member'+id_group).prev('img').attr('src','images/Add.png');
	$('#member'+id_group).prev('img').attr('style','');
	
	$('#member'+id_group).hide();
	$('#member'+id_group).html('<a href="javascript:;" onclick="beMember('+id_group+')">Dołącz do grupy</a>');
	$('#member'+id_group).fadeIn();
	$('#member'+id_group).prev('img').fadeIn();
	
}

function getMyGroups(id_group)
{
	$.ajax({
				type: "POST",
				url: "ajax.php?mod=front&view=ajaxGroups&act=ajaxGetMainGroups",	   
				dataType: 'json',
				beforeSend: function(){
				},
				success: function(data)
				{
					//$('#myGroups').html('');
					$('#myGroups').html('');
					for(var i=0;i<data.length;i++)
					{
						$('#myGroups').append('<li class="border" id="mainG'+data[i].id_group+'"><a href="see_members_of_this_group-'+data[i].id_group+'">'+data[i].group_name+'</a></li>');
					}
					var count = $('#usersCountB'+id_group).val();
					newCount = parseInt(count) + 1;
					
					$('#usersCount'+id_group).html(newCount);
					$('#usersCountInput'+id_group).html('<input type="hidden" id="usersCountB'+id_group+'" value="'+newCount+'"/>');
					$('#member'+id_group).html('');
					
					$('#member'+id_group).prev('img').hide();
					$('#member'+id_group).prev('img').attr('src','images/delete.png');
					$('#member'+id_group).prev('img').attr('style','margin:3px');
					
					$('#member'+id_group).hide();
					$('#member'+id_group).html('<a  href="javascript:;" onclick="stopBeMember('+id_group+')">Opuść grupę</a>');
					$('#member'+id_group).fadeIn();
					$('#member'+id_group).prev('img').fadeIn();
				}
				});
}

function changeAvatarInGroupForm(id,b)
{ 
	$('#changingAvatarInGroupForm').html('<form method="post" action="change-avatar-in-group-'+id+'-'+b+'.html" enctype="multipart/form-data">\
			<table id="avatar" style="width:100%">\
				<tr>\
					<td colspan="2">\
						<input class="fileInput" type="file" name="avatar"/>\
					</td></tr><tr>\
					<td style="padding:3px 0px;">\
						<input clsas="grey" type="submit" value="zmień avatar"/>\
				</tr>\
			</table>\
			</form>');
}

function addPhotoToFavouriteInGroup(id_photo)
{
	
	$.ajax({
				type: "POST",
				url: "ajax.php?mod=front&view=ajaxPhotos&act=addToFavourite",
				data: "id_photo="+id_photo,		   
				dataType: 'json',
				success: function(data)
				{
					
					$('#fav_photo').html('');
					systemMessage('Zdjęcie zostało dodane do ulubionych','success');
				}
				});
	
}	

function addMovieToFavouriteInGroup(id_movie)
{
	
	$.ajax({
				type: "POST",
				url: "ajax.php?mod=front&view=ajaxMovies&act=addToFavourite",
				data: "id_movie="+id_movie,		   
				dataType: 'json',
				success: function(data)
				{
					
					$('#fav_movie').html('');
					systemMessage('Film został dodany do ulubionych','success');
				}
				});
	
}	

function addTextToFavouriteInGroup(id_text)
{
	
	$.ajax({
				type: "POST",
				url: "ajax.php?mod=front&view=ajaxTexts&act=addToFavourite",
				data: "id_text="+id_text,		   
				dataType: 'json',
				success: function(data)
				{
					
					$('#fav_text').html('');
					systemMessage('Tekst został dodany do ulubionych','success');
				}
				});
	
}

function addAlbumToFavouriteInGroup(id_album)
{
	
	$.ajax({
				type: "POST",
				url: "ajax.php?mod=front&view=ajaxAlbums&act=addToFavourite",
				data: "id_album="+id_album,		   
				dataType: 'json',
				success: function(data)
				{
					
					$('#fav_album').html('');
					systemMessage('Album został dodany do ulubionych','success');
				}
				});
	
}


