async void OnLoadPreviousGemsClicked (object sender, EventArgs e)
		{
			try
			{
				CommunityGemsDetails firstItem =   communityGems.resultarray.First ();

				CommunityGemsObject gemsObj =  App.Settings.GetCommunityGemsObject ();
				int firstRendererItemIndex = gemsObj.resultarray.FindIndex (itm => itm.gem_id == firstItem.gem_id);;//gemsObj.resultarray.IndexOf ( lastItem );
				if (firstRendererItemIndex > 0 && ( firstRendererItemIndex + 1 ) < gemsObj.resultarray.Count )
				{
					reachedEnd = false;
					reachedFront = false;
					int itemCountToCopy = MAX_ROWS_AT_A_TIME;
					communityGems = null;
					communityGems = new CommunityGemsObject ();
					communityGems.resultarray = new List<CommunityGemsDetails> ();
					gemsObj.resultarray.RemoveRange( firstRendererItemIndex, gemsObj.resultarray.Count - firstRendererItemIndex );
					if( firstRendererItemIndex > MAX_ROWS_AT_A_TIME )
						gemsObj.resultarray.RemoveRange( 0, firstRendererItemIndex - MAX_ROWS_AT_A_TIME );

					communityGems.resultarray = gemsObj.resultarray;

					gemsObj = null;

					masterStackLayout.Children.Clear();

					masterScroll.Content = null;
					GC.Collect();

					await DownloadMedias();

					RenderGems ( communityGems, false );
				}
				else
				{
					reachedFront = true;
				}

			} 
			catch (Exception ex) 
			{
				DisplayAlert ( Constants.ALERT_TITLE, "Low memory error.", Constants.ALERT_OK );
			}
		}
		async void OnLoadMoreGemsClicked (object sender, EventArgs e)
		{
			try 
			{

				CommunityGemsDetails lastItem =   communityGems.resultarray.Last ();

				CommunityGemsObject gemsObj =  App.Settings.GetCommunityGemsObject ();
				int lastRendererItemIndex = gemsObj.resultarray.FindIndex (itm => itm.gem_id == lastItem.gem_id);;//gemsObj.resultarray.IndexOf ( lastItem );
				if (lastRendererItemIndex > 0 && ( lastRendererItemIndex + 1 ) < gemsObj.resultarray.Count )
				{
					reachedEnd = false;
					reachedFront = false;
					int itemCountToCopy = gemsObj.resultarray.Count - lastRendererItemIndex;
					itemCountToCopy = (itemCountToCopy > MAX_ROWS_AT_A_TIME) ? MAX_ROWS_AT_A_TIME : itemCountToCopy;
					communityGems = null;
					communityGems = new CommunityGemsObject ();
					communityGems.resultarray = new List<CommunityGemsDetails> ();

					communityGems.resultarray = gemsObj.resultarray.Skip (lastRendererItemIndex).Take (itemCountToCopy).ToList();

					gemsObj = null;


					masterStackLayout.Children.Clear();

					masterScroll.Content = null;
					GC.Collect();


					await DownloadMedias();

					if( itemCountToCopy < MAX_ROWS_AT_A_TIME )
						RenderGems ( communityGems, true );
					else
						RenderGems ( communityGems, false );
				}
				else
				{
					reachedEnd = true;
				}

			} 
			catch (Exception ex)
			{
				DisplayAlert ( Constants.ALERT_TITLE, "Low memory error.", Constants.ALERT_OK );
			}

		}
		void RenderGems( CommunityGemsObject gemsObject, bool prevButtonNeeded )
		{
			try
			{
				/*Button loadPreviousGems = new Button();
				loadPreviousGems.BackgroundColor = Color.Transparent;
				loadPreviousGems.TextColor = Constants.BLUE_BG_COLOR;
				loadPreviousGems.Text = "Load previous gems";
				loadPreviousGems.FontSize = 12;
				loadPreviousGems.BorderWidth = 0;
				loadPreviousGems.BorderColor = Color.Transparent;
				loadPreviousGems.Clicked += OnLoadPreviousGemsClicked;
				masterStackLayout.Children.Add( loadPreviousGems );*/

				foreach (var item in gemsObject.resultarray )
				{
					CustomLayout masterStack = new CustomLayout();
					masterStack.WidthRequest = App.screenWidth * 102 / 100;
					masterStack.ClassId = "masterstack" + item.gem_id;
					masterStack.BackgroundColor = Color.White;// Color.FromRgb(244, 244, 244);

					#region TOOLS LAYOUT

					StackLayout toolsLayout = new StackLayout();
					toolsLayout.BackgroundColor = Color.White;
					toolsLayout.Spacing = App.screenWidth*.10;
					toolsLayout.Orientation = StackOrientation.Horizontal;
					//toolsLayout.WidthRequest = App.screenWidth * 95 / 100;
					toolsLayout.HeightRequest = App.screenHeight * .05;
					toolsLayout.HorizontalOptions = LayoutOptions.Center;
					toolsLayout.Padding = new Thickness(10, 0, 10, 0);

					likeButtonTap = new TapGestureRecognizer();
					Image likeButton = new Image();
					string likeSource = ( item.like_status == 1 ) ? "icn_liked.png" : "icn_like.png";
					likeButton.Source = likeSource;
					likeButton.WidthRequest = Device.OnPlatform(15, 15, 15);
					likeButton.HeightRequest = Device.OnPlatform(15, 15, 15);
					likeButton.VerticalOptions = LayoutOptions.Center;
					likeButton.ClassId = item.gem_id + "&&"  + item.gem_type;
					likeButton.GestureRecognizers.Add(likeButtonTap);

					Label likeLabel = new Label
					{
						Text = "Like",
						FontFamily = Constants.HELVERTICA_NEUE_LT_STD,
						TextColor = Color.Gray,
						VerticalOptions = LayoutOptions.Center,
						FontSize = Device.OnPlatform(12, 12, 15),
						ClassId = item.gem_id + "&&" + item.gem_type
					};
					likeLabel.GestureRecognizers.Add(likeButtonTap);

					Label likeCount = new Label
					{
						Text = ( item.likecount > 0 ) ? item.likecount.ToString() : "",
						FontFamily = Constants.HELVERTICA_NEUE_LT_STD,
						TextColor = Color.Blue,
						VerticalOptions = LayoutOptions.Center,
						FontSize = Device.OnPlatform(12, 12, 15)
					};
					StackLayout likeLayout = new StackLayout{Children = {likeCount, likeButton, likeLabel}, Orientation = StackOrientation.Horizontal, Spacing = 5};


					toolsLayout.Children.Add( likeLayout  );

//					toolsLayout.Children.Add(likeCount);
//					toolsLayout.Children.Add(likeButton);
//					toolsLayout.Children.Add(likeLabel);

					likeButtonTap.Tapped += async (object tapSender, EventArgs eTap) => 
					{
						try
						{
							string gemID = "";
							Image likeImg = tapSender as Image;
							Label like = tapSender as Label;
							Label likeCountLabel = new Label();
							if (likeImg != null)
							{
								int likeImgIndex = likeLayout.Children.IndexOf( likeImg );
								if( likeImgIndex > 0 )
								{
									likeCountLabel =(Label) likeLayout.Children[ likeImgIndex - 1 ];
								}
								if (likeImg.ClassId != null)
									gemID = likeImg.ClassId;
							}

							if (like != null)
							{
								int labelIndex = likeLayout.Children.IndexOf( like );
								if( labelIndex > 0 )
								{
									likeImg =(Image) likeLayout.Children[ labelIndex - 1 ];
									likeCountLabel =(Label) likeLayout.Children[ labelIndex - 2 ];
								}
								if (like.ClassId != null)
									gemID = like.ClassId;
							}

							//likeButtonTap.Tapped -= OnLikeButtonTapped;
							progressBar.ShowProgressbar("Requesting...   ");
							/////////////// for testing /////////////

							string[] delimiters = { "&&" };
							string[] clasIDArray = gemID.Split(delimiters, StringSplitOptions.None);
							string selectedGemID = clasIDArray [0];
							string selectedGemType = clasIDArray [1];

							LikeResponse likeRes = await ServiceHelper.LikeGem( selectedGemID, selectedGemType );
							if( likeRes != null )
							{
								string source = ( likeRes.like_status == 1 ) ? "icn_liked.png" : "icn_like.png";
								likeImg.Source = source;
								if( likeCountLabel != null )
									likeCountLabel.Text =  ( likeRes.likecount > 0 ) ? likeRes.likecount.ToString() : "";
							}
							progressBar.HideProgressbar();

						}
						catch (Exception ex)
						{
							progressBar.HideProgressbar();
							DisplayAlert(Constants.ALERT_TITLE, "Could not process the request now.", Constants.ALERT_OK);
						}

						progressBar.HideProgressbar();
					};


					Image shareButton = new Image();
					shareButton.Source = Device.OnPlatform("share.png", "share.png", "//Assets//share.png");
					shareButton.WidthRequest = Device.OnPlatform(15, 15, 15);
					shareButton.HeightRequest = Device.OnPlatform(15, 15, 15);
					shareButton.VerticalOptions = LayoutOptions.Center;
					shareButton.ClassId = item.gem_id;
					shareLabel = new Label
					{
						Text = "Share",
						FontFamily = Constants.HELVERTICA_NEUE_LT_STD,
						TextColor = Color.Gray,
						VerticalOptions = LayoutOptions.Center,
						FontSize = Device.OnPlatform(12, 12, 15),
						ClassId = item.gem_id
					};
					shareButtonTap = new TapGestureRecognizer();
					shareButtonTap.Tapped += OnShareButtonTapped;
					shareButton.GestureRecognizers.Add(shareButtonTap);
					shareLabel.GestureRecognizers.Add(shareButtonTap);
					toolsLayout.Children.Add( new StackLayout{Children = {shareButton, shareLabel}, Orientation = StackOrientation.Horizontal, Spacing = 5});

//					toolsLayout.Children.Add(shareButton);
//					toolsLayout.Children.Add(shareLabel);



					Image commentButton = new Image();
					commentButton.Source = Device.OnPlatform("icon_cmnt.png", "icon_cmnt.png", "//Assets//icon_cmnt.png");
					commentButton.WidthRequest = Device.OnPlatform(15, 15, 15);
					commentButton.HeightRequest = Device.OnPlatform(15, 15, 15);
					commentButton.VerticalOptions = LayoutOptions.Center;
					commentButton.ClassId = item.gem_id + "&&"  + item.gem_type;
					Label commentsLabel = new Label
					{
						VerticalOptions = LayoutOptions.Center,
						FontFamily = Constants.HELVERTICA_NEUE_LT_STD,
						TextColor = Color.Gray,
						FontSize = Device.OnPlatform(12, 12, 15),
						ClassId = item.gem_id + "&&"  + item.gem_type
					};
					if( item.comment_count > 0 )
					{
						commentsLabel.Text = "Comments (" + item.comment_count.ToString() + ")";
					}
					else
					{
						commentsLabel.Text = "Comments";
					}

					commentButtonTap = new TapGestureRecognizer();
					commentButtonTap.Tapped += OnCommentButtonTapped;
					//commentButton.GestureRecognizers.Add(commentButtonTap);
					commentsLabel.GestureRecognizers.Add(commentButtonTap);


					toolsLayout.Children.Add( new StackLayout{Children = {commentButton, commentsLabel}, Orientation = StackOrientation.Horizontal, Spacing = 5});
//
//					toolsLayout.Children.Add(commentButton);
//					toolsLayout.Children.Add(commentsLabel);


					#endregion


					#region  title, description
					Image profileImage = new Image();
					profileImage.Source = (item.profileimg != null) ? Constants.SERVICE_BASE_URL + item.profileimg : "avatar.jpg";
					profileImage.WidthRequest = 60;
					profileImage.HeightRequest = 60;
					profileImage.Aspect = Aspect.Fill;
					profileImage.ClassId = item.user_id;
					TapGestureRecognizer profileImageTag = new TapGestureRecognizer();
					profileImageTag.Tapped += ProfileImageTag_Tapped;
					profileImage.GestureRecognizers.Add(profileImageTag);

					Label userName = new Label();
					userName.Text = item.firstname;
					userName.TextColor = Color.Black;
					userName.WidthRequest = App.screenWidth * 90 / 100;
					userName.FontAttributes = FontAttributes.Bold;
					userName.FontSize = Device.OnPlatform(14, 15, 12);
					userName.FontFamily = Constants.HELVERTICA_NEUE_LT_STD;

					title = new Label();
					title.Text = item.gem_datetime;
					title.TextColor = Color.Black;
					title.WidthRequest = App.screenWidth * 90 / 100;
					title.FontSize = Device.OnPlatform(12, 12, 12);
					title.FontFamily = Constants.HELVERTICA_NEUE_LT_STD;

					description = new Label();
					description.WidthRequest = App.screenWidth * .80;
					description.Text = item.gem_details;
					description.TextColor = Color.Black;
					description.FontFamily = Constants.HELVERTICA_NEUE_LT_STD;
					description.FontSize = Device.OnPlatform(12, 15, 15);

					CustomImageButton menuButton = new CustomImageButton
					{
						ImageName = Device.OnPlatform("downarrow.png", "downarrow.png", "//Assets//downarrow.png"),
						Text = string.Empty,
						HorizontalOptions = LayoutOptions.End,
						BackgroundColor = Color.Transparent,
						WidthRequest = 30,
						HeightRequest = 20,
						ClassId = item.gem_id
					};

		
					menuButton.Clicked += GemMenuButton_Clicked;

					CustomImageButton followButton = new CustomImageButton();
					//followButton.Text = "Follow";
					followButton.ImageName = "follow.png";
					followButton.TextColor = Color.White;
					followButton.HeightRequest = 20;
					followButton.BackgroundColor =  Color.FromRgb(8, 135, 224);
					followButton.WidthRequest = 60;
					followButton.ClassId = item.user_id;
					followButton.IsEnabled = true;
					if( item.follow_status > 0 )
					{
						followButton.ImageName = "follow_disable.png";	
						followButton.IsEnabled = false;
					}
					followButton.Clicked += async (object fsender, EventArgs fe) => 
					{
						CustomImageButton btn = fsender as CustomImageButton;
						btn.ImageName = "follow_disable.png";	
						btn.IsEnabled = false;
						if( btn != null && btn.ClassId != null )
						{
							if( currentUser != null )
							{
								progressBar.ShowProgressbar( "Loading...." );
								FollowResponse resp = await ServiceHelper.SendFollowRequest( currentUser.UserId.ToString(), btn.ClassId.ToString() );
								if( resp != null && resp.code == "400" )
								{
									progressBar.ShowToast( "Already sent reqeust." );
								}
								progressBar.HideProgressbar();
							}

						}
					};

					// masterStack.AddChildToLayout(pageTitle, 1, 1);
					//masterStack.AddChildToLayout(menuButton, 79, 1);
					masterStack.AddChildToLayout(profileImage, 4, 1);
					masterStack.AddChildToLayout(userName, 25, 3);
					masterStack.AddChildToLayout(title, 25, 7);

					if( (item.user_id != currentUser.UserId.ToString() ) && item.can_follow == "1")
					{
						masterStack.AddChildToLayout(followButton, 80, 3 );
					}

					TapGestureRecognizer moreTap = new TapGestureRecognizer();
					moreTap.Tapped += async (object senderr, EventArgs ee) =>
					{
						Image more = senderr as Image;
						if (more != null)
						{
							IProgressBar progress = DependencyService.Get<IProgressBar>();
							progress.ShowProgressbar("Loading more medias..");
							App.masterPage.IsPresented = false;
							CommunityGemsDetails gemInfo = communityGems.resultarray.FirstOrDefault(itm => itm.gem_id == more.ClassId);
							if (gemInfo != null)
							{
								List<PurposeColor.Constants.MediaDetails> mediaPlayerList = new List<PurposeColor.Constants.MediaDetails>();

								List<string> listToDownload = new List<string>();

								foreach (var mediaItem in  gemInfo.gem_media )
								{
									if(string.IsNullOrEmpty(mediaItem.gem_media))
									{
										continue;
									}

									if (mediaItem.media_type == "png" || mediaItem.media_type == "jpg" || mediaItem.media_type == "jpeg") 
									{

										listToDownload.Add(Constants.SERVICE_BASE_URL+ mediaItem.gem_media);
										string fileName = System.IO.Path.GetFileName(mediaItem.gem_media);
										mediaItem.gem_media = App.DownloadsPath + fileName;
										mediaPlayerList.Add(new PurposeColor.Constants.MediaDetails() { ImageName = mediaItem.gem_media, ID = item.gem_id, MediaType = mediaItem.media_type, Url = mediaItem.gem_media });
									}
									else if( mediaItem.media_type == "mp4" || mediaItem.media_type == "3gpp" )
									{
										mediaItem.gem_media = Constants.SERVICE_BASE_URL + mediaItem.gem_media ;
										mediaPlayerList.Add(new PurposeColor.Constants.MediaDetails() { ImageName = mediaItem.video_thumb, ID = item.gem_id, MediaType = mediaItem.media_type, Url = mediaItem.gem_media });
									}
									else
									{
										mediaItem.gem_media = Constants.SERVICE_BASE_URL + mediaItem.gem_media ;
										mediaPlayerList.Add(new PurposeColor.Constants.MediaDetails() { ImageName = mediaItem.gem_media, ID = item.gem_id, MediaType = mediaItem.media_type, Url = mediaItem.gem_media });
									}



								}

								// down load files //
								if (listToDownload != null && listToDownload.Count > 0) {
									IDownload downloader = DependencyService.Get<IDownload>();
									await downloader.DownloadFiles(listToDownload);
								}

								await Navigation.PushAsync(new CommunityMediaViewer(mediaPlayerList));
							}

							progress.HideProgressbar();
						}

					};
					Image moreImg = new Image();
					moreImg.Source = "more.png";
					moreImg.HorizontalOptions = LayoutOptions.End;
					moreImg.VerticalOptions = LayoutOptions.End;
					moreImg.GestureRecognizers.Add(moreTap);
					moreImg.ClassId = item.gem_id;

					#endregion

					StackLayout bottomAndLowerControllStack = new StackLayout
					{
						Orientation = StackOrientation.Vertical,
						BackgroundColor = Color.Transparent,
						Spacing = 1,
						Padding = new Thickness(0, 5, 0, 5),
						WidthRequest = App.screenWidth * 102 / 100
					};

					BoxView paddingBoxView = new BoxView();
					paddingBoxView.WidthRequest = App.screenWidth * 3 / 100;
					paddingBoxView.BackgroundColor = Color.Transparent;

					bottomAndLowerControllStack.Children.Add(new StackLayout { Orientation = StackOrientation.Horizontal , WidthRequest = App.screenWidth * 102 / 100, Children = { paddingBoxView, description } });

					#region MEDIA LIST

					if ( item.gem_media != null )
					{
						GemMedia gemMedia = (item.gem_media.Count > 0) ? item.gem_media[0] : null;
						if( gemMedia != null )
						{
							TapGestureRecognizer videoTap = new TapGestureRecognizer();
							videoTap.Tapped += OnGemTapped;

							IDownload downloader = DependencyService.Get<IDownload>();

							string fileName = Path.GetFileName( Constants.SERVICE_BASE_URL + gemMedia.gem_media ); 
							string localFilePath = Device.OnPlatform( downloader.GetLocalFileName( fileName ), App.DownloadsPath + fileName, "" );


							Image img = new Image();
							bool isValidUrl = (gemMedia.gem_media != null && !string.IsNullOrEmpty(gemMedia.gem_media)) ? true : false;
							string source = (isValidUrl) ?  localFilePath : null;
							string fileExtenstion = Path.GetExtension(source);
							bool isImage = (fileExtenstion == ".png" || fileExtenstion == ".jpg" || fileExtenstion == ".jpeg") ? true : false;
							img.WidthRequest = App.screenWidth * 102 / 100;// * 90 / 100;
							img.HeightRequest = App.screenWidth * 80 / 100;
							img.Aspect = Aspect.Fill;

							img.ClassId = null;
							if ( gemMedia.gem_media != null && gemMedia.media_type == "mp4")
							{
								img.ClassId = Constants.SERVICE_BASE_URL + gemMedia.gem_media ;
								source = Constants.SERVICE_BASE_URL + gemMedia.video_thumb;
							}
							else if ( gemMedia.gem_media != null && (gemMedia.media_type == "3gpp" || gemMedia.media_type == "aac" ))
							{
								img.ClassId = Constants.SERVICE_BASE_URL + gemMedia.gem_media;
								source = Device.OnPlatform("audio.png", "audio.png", "//Assets//audio.png");
							}
							else if (gemMedia.gem_media != null && gemMedia.media_type == "wav")
							{
								img.ClassId = source;
								source = Device.OnPlatform("audio.png", "audio.png", "//Assets//audio.png");
							}

							if( source != null )
							{
								img.Source = source;
								img.GestureRecognizers.Add(videoTap);
								var indicator = new ActivityIndicator { Color = new Color(.5), };
								indicator.SetBinding(ActivityIndicator.IsRunningProperty, "IsLoading");
								masterStack.AddChildToLayout(indicator, 45, 30);

							/*	CustomLayout imgContainer = new CustomLayout();
								imgContainer.WidthRequest = App.screenWidth * 90 / 100;
								imgContainer.HeightRequest = App.screenWidth * 90 / 100;
								imgContainer.Children.Add(img);
								if( item.gem_media != null && item.gem_media.Count > 1 )
									imgContainer.AddChildToLayout(moreImg, 75, 75, (int)imgContainer.WidthRequest, (int)imgContainer.HeightRequest);*/

								Grid grid = new Grid
								{
									WidthRequest = App.screenWidth * 102 / 100,
								//	HeightRequest = App.screenWidth * 80 / 100,
									VerticalOptions = LayoutOptions.FillAndExpand,
									HorizontalOptions = LayoutOptions.FillAndExpand,
									RowDefinitions = 
									{
										new RowDefinition { Height = new GridLength( img.HeightRequest / 3 ) },
										new RowDefinition { Height = new GridLength( img.HeightRequest / 3 ) },
										new RowDefinition { Height = new GridLength( img.HeightRequest / 3 ) },

									},
									ColumnDefinitions = 
									{
										new ColumnDefinition { Width = new GridLength( img.WidthRequest / 3 ) },
										new ColumnDefinition { Width = new GridLength( img.WidthRequest / 3 ) },
										new ColumnDefinition { Width = new GridLength( img.WidthRequest / 3 ) },

									}
								};
								
								if ( gemMedia != null && gemMedia.media_type == "mp4")
								{

									Image play = new Image();
									play.Source = "video_play.png";
									play.Aspect = Aspect.AspectFit;
									play.WidthRequest = 75;
									play.HeightRequest = 75;
									play.HorizontalOptions = LayoutOptions.Center;
									play.VerticalOptions = LayoutOptions.Center;
									play.ClassId =  img.ClassId;
									play.GestureRecognizers.Add(videoTap);

									BoxView box = new BoxView();
									box.BackgroundColor = Color.Red;
									box.WidthRequest = 100;
									box.HeightRequest = 100;

									grid.Children.Add( img, 0, 0 );
									Grid.SetColumnSpan(img, 3);
									Grid.SetRowSpan( img, 3 );
									grid.Children.Add(play, 1, 1);
									if( item.gem_media.Count > 1 )
									grid.Children.Add(moreImg, 2, 2);
									bottomAndLowerControllStack.Children.Add(grid);
								}
								else
								{	
									grid.Children.Add( img, 0, 0 );
									Grid.SetColumnSpan(img, 3);
									Grid.SetRowSpan( img, 3 );
									if( item.gem_media.Count > 1 )
										grid.Children.Add(moreImg, 2, 2);
									bottomAndLowerControllStack.Children.Add(grid);
								}
							}

						}

					}

					#endregion


					bottomAndLowerControllStack.Children.Add(toolsLayout);
					masterStack.AddChildToLayout(bottomAndLowerControllStack, 0, 12);
					//  masterStack.AddChildToLayout( moreImg, 65, Device.OnPlatform( 30, 20, 20 ) );

					//masterStack.AddChildToLayout(spaceOffsetlayout, 1, 85);
					/// bottomAndLowerControllStack.Children.Add(spaceOffsetlayout);

	

					masterScroll.HeightRequest = App.screenHeight - 20;
					masterScroll.WidthRequest = App.screenWidth * 102 / 100;// * 90 / 100;


					masterStackLayout.Children.Add(masterStack);


				}


				if( prevButtonNeeded )
				{
					Button backToTop = new Button();
					backToTop.BackgroundColor = Color.Transparent;
					backToTop.TextColor = Constants.BLUE_BG_COLOR;
					backToTop.Text = "Go back to previous page";
					backToTop.FontSize = 12;
					backToTop.BorderWidth = 0;
					backToTop.BorderColor = Color.Transparent;
					backToTop.ClassId = "prev page";
					backToTop.Clicked += (object sender, EventArgs e) => 
					{
						OnLoadPreviousGemsClicked(  masterScroll, EventArgs.Empty );
					};
					masterStackLayout.Children.Add ( backToTop );
				}
				/*	Button loadMoreGems = new Button();
				loadMoreGems.BackgroundColor = Color.Transparent;
				loadMoreGems.TextColor = Constants.BLUE_BG_COLOR;
				loadMoreGems.Text = "Load more gems";
				loadMoreGems.FontSize = 12;
				loadMoreGems.BorderWidth = 0;
				loadMoreGems.BorderColor = Color.Transparent;
				loadMoreGems.Clicked += OnLoadMoreGemsClicked;*/

				BoxView transBox = new BoxView();
				transBox.HeightRequest = 125;
				transBox.WidthRequest = App.screenWidth * 80 / 100;
				transBox.BackgroundColor = Color.Transparent;
				//masterStackLayout.Children.Add(loadMoreGems);
				masterStackLayout.Children.Add(transBox);
				masterScroll.Content = masterStackLayout;
			}
			catch (Exception ex) 
			{
				
			}
		}
		async  void OnAppearing(object sender, EventArgs e)
		{
			try
			{

				/*communityGems.resultarray = new List<CommunityGemsDetails>();
				communityGems.resultarray.Add( new CommunityGemsDetails{ firstname = "test", gem_datetime = "2014 feb 14", gem_id = "68", user_id = "2"  } );*/


				if (communityGems != null)
					return;


				progressBar.ShowProgressbarWithCancel( "Downloading gems....", OnCancelProgress );
				//progess.ShowProgressbar( "lloading gems..." );

				User user = null;
				IsNavigationFrfomGEMS = modelObject.fromGEMSPage;
				try
				{
					user = App.Settings.GetUser();
				}
				catch (Exception ex)
				{
					var test = ex.Message;
				}



				communityGems = await ServiceHelper.GetCommunityGemsDetails();

				if( communityGems == null || communityGems.resultarray == null )
				{
					var success = await DisplayAlert(Constants.ALERT_TITLE, "Error in fetching gems", Constants.ALERT_OK, Constants.ALERT_RETRY);
					if (!success) 
					{
						OnAppearing (sender, EventArgs.Empty);
						return;
					} 
					else
					{
						if (Device.OS != TargetPlatform.WinPhone)
							communityGems = App.Settings.GetCommunityGemsObject();

					}
				}
				else
				{
					App.Settings.DeleteCommunityGems();
					App.Settings.SaveCommunityGemsDetails( communityGems );
				}

				/*if(!await DownloadMedias())
				{
					DisplayAlert( Constants.ALERT_TITLE, "Media download failed..", Constants.ALERT_OK );
					return;
				}*/


				if( communityGems.resultarray.Count > MAX_ROWS_AT_A_TIME )
				{
					communityGems.resultarray.RemoveRange( MAX_ROWS_AT_A_TIME, communityGems.resultarray.Count - MAX_ROWS_AT_A_TIME );
				}

				progressBar.HideProgressbarWithCancel();

				await DownloadMedias();



				RenderGems( communityGems, false );


			} 
			catch (Exception ex) 
			{
				string err = ex.Message;
			}
		}
		public void Dispose()
		{
			masterLayout = null;
			progressBar = null;
			//masterStack = null;
			title = null;
			description = null;
			mediaList = null;
			shareLabel = null;
			if (shareButtonTap != null)
			{
				shareButtonTap.Tapped -= OnShareButtonTapped;
				shareButtonTap = null;
			}
			if (commentButtonTap != null)
			{
				commentButtonTap.Tapped -= OnCommentButtonTapped;
				commentButtonTap = null;
				likeButtonTap = null;
			}

			gemMenuContainer = null;
			modelObject = null;
			masterStackLayout = null;
			communityGems = null;
			GC.Collect();
		}
		public bool SaveCommunityGemsDetails( CommunityGemsObject gemsObject )
		{
			try
			{
				if (gemsObject == null || gemsObject.resultarray == null)
				{
					return false;
				}

				List<CommunityGemsDetailsDB> listCommunityGemsDetails = new List<CommunityGemsDetailsDB>();
				List<GemMedia> listGemMedia = new List<GemMedia>();

				foreach (var item in gemsObject.resultarray)
				{


					if( item != null )
					{
						CommunityGemsDetailsDB detail = new CommunityGemsDetailsDB();
						detail.firstname = item.firstname;
						detail.gem_datetime = item.gem_datetime;
						detail.gem_details = item.gem_details;
						detail.gem_id =  item.gem_id;
						detail.gem_title = item.gem_title;
						detail.likecount = item.likecount;
						detail.like_status = item.like_status;
						detail.profileimg = item.profileimg;
						detail.share_status = item.share_status;
						detail.user_id = item.user_id;
						detail.gem_type = item.gem_type;
						listCommunityGemsDetails.Add( detail );
					}


					foreach (var gemMedia in item.gem_media)
					{
						GemMedia media = new GemMedia();
						media = gemMedia;
						listGemMedia.Add(media);
					}

				}

				Connection.InsertAll(listCommunityGemsDetails);
				Connection.InsertAll(listGemMedia);

				listGemMedia.Clear();
				listCommunityGemsDetails.Clear();

				listGemMedia = null;
				listCommunityGemsDetails = null;

				return true;
			}
			catch (Exception ex)
			{

				return false;
			}
		}
		public CommunityGemsObject GetCommunityGemsObject()
		{
			try
			{
				CommunityGemsObject masterObject = new CommunityGemsObject();

				List<CommunityGemsDetailsDB> listCommunityDetails = new List<CommunityGemsDetailsDB>();
				List<GemMedia> listGemMedia = new List<GemMedia>();

				listCommunityDetails = (from t in Connection.Table<CommunityGemsDetailsDB>() select t).ToList();
				listGemMedia = (from t in Connection.Table<GemMedia>() select t).ToList();

				masterObject.resultarray = new List<CommunityGemsDetails>();

				foreach (var item in listCommunityDetails)
				{
					CommunityGemsDetails resultArray = new CommunityGemsDetails();
					resultArray.gem_media = new List<GemMedia>();

					resultArray.user_id = item.user_id;
					resultArray.like_status = item.like_status;
					resultArray.likecount = item.likecount;
					resultArray.gem_id = item.gem_id;
					resultArray.gem_title = item.gem_title;
					resultArray.gem_details = item.gem_details;
					resultArray.gem_datetime = item.gem_datetime;
					resultArray.share_status = item.share_status;
					resultArray.firstname = item.firstname;
					resultArray.profileimg = item.profileimg;
					resultArray.gem_type = item.gem_type;


					// Title
					foreach (var mediaObject in listGemMedia)
					{
						if (mediaObject.gem_id == item.gem_id)
							resultArray.gem_media.Add(mediaObject);
					}

					masterObject.resultarray.Add(resultArray);
				}

				return masterObject;
			}
			catch (Exception)
			{

				return null;
			}

		}