protected void bAjaxPostback_Click(object sender, EventArgs e)
        {
            try
            {
                aqufitEntities entities = new aqufitEntities();
                Affine.Data.Managers.IDataManager dataMan = Affine.Data.Managers.LINQ.DataManager.Instance;
                long pId = -1;
                switch (hiddenAjaxAction.Value)
                {
                case "loadPhoto":
                    pId = Convert.ToInt64(hiddenAjaxValue.Value);
                    string url = SetupPhoto(pId);
                    RadAjaxManager1.ResponseScripts.Add("Aqufit.Page.Actions.LoadImage('" + url + "', " + lPhotoNum.Text + ");");
                    break;

                case "makeProfile":
                    pId = Convert.ToInt64(hiddenAjaxValue.Value);
                    Affine.Utils.ImageUtil.MakeProfileFromPhoto(UserSettings.Id, pId, Server.MapPath("~"));
                    string profileUrl = ResolveUrl("~") + UserSettings.UserName;
                    RadAjaxManager1.ResponseScripts.Add("top.location.href='" + profileUrl + "'; Aqufit.Page.atiLoading.remove();");
                    break;

                case "tagPhoto":
                    Affine.Data.Helpers.PhotoTag json = JsonExtensionsWeb.FromJson <Affine.Data.Helpers.PhotoTag>(hiddenAjaxValue.Value);
                    pId = Convert.ToInt64(hiddenPhotoId.Value);
                    // TODO: security
                    dataMan.TagPhoto(UserSettings.Id, json.FriendId, pId, json.Top, json.Left, json.Width, json.Height);
                    RadAjaxManager1.ResponseScripts.Add("radalert('<div style=\"width: 100%; height: 100%; padding: 0px;\"><span style=\"color: #FFF;\"><img src=\"" + ResolveUrl("~/DesktopModules/ATI_Base/resources/images/iCheck.png") + "\" align=\"absmiddle\"/> Photo has been tagged</span></div>', 330, 100, 'Success');");
                    break;

                case "addComment":
                    pId = Convert.ToInt64(hiddenPhotoId.Value);
                    string            comment = hiddenAjaxValue.Value;
                    AttachmentComment pc      = dataMan.SavePhotoComment(UserSettings.Id, pId, comment);
                    string            j       = new { UserName = pc.UserSetting.UserName, UserKey = pc.UserSetting.UserKey, Comment = pc.Comment }.ToJson();
                    RadAjaxManager1.ResponseScripts.Add("Aqufit.Page.Actions.appendComment('" + j + "'); ");
                    break;

                case "deletePhoto":
                    pId = Convert.ToInt64(hiddenPhotoId.Value);
                    dataMan.DeletePhoto(UserSettings.Id, pId);
                    Response.Redirect(ResolveUrl("~") + UserSettings.UserName + "/photos", true);
                    //RadAjaxManager1.ResponseScripts.Add("alert('"+pId+"');");
                    break;

                case "deleteAlbum":
                    long aId = Convert.ToInt64(hiddenAlbumKey.Value);
                    dataMan.DeleteAlbum(UserSettings.Id, aId);
                    Response.Redirect(ResolveUrl("~") + UserSettings.UserName + "/photos", true);
                    //RadAjaxManager1.ResponseScripts.Add("alert('"+pId+"');");
                    break;

                case "deleteTag":
                    long       tId = Convert.ToInt64(hiddenAjaxValue.Value);
                    User2Photo u2p = entities.User2Photo.FirstOrDefault(p => p.Id == tId && (p.UserSettingsKey == UserSettings.Id || p.TaggerUserSettingsKey == UserSettings.Id));
                    if (u2p != null)
                    {
                        entities.DeleteObject(u2p);
                        entities.SaveChanges();
                        RadAjaxManager1.ResponseScripts.Add("radalert('<div style=\"width: 100%; height: 100%; padding: 0px;\"><span style=\"color: #FFF;\"><img src=\"" + ResolveUrl("~/DesktopModules/ATI_Base/resources/images/iCheck.png") + "\" align=\"absmiddle\"/> Tag has been removed</span></div>', 330, 100, 'Success');");
                    }
                    RadAjaxManager1.ResponseScripts.Add("Aqufit.Page.atiLoading.remove(); ");
                    break;
                }
            }
            catch (Exception ex)
            {
                // TODO: better error handling
                RadAjaxManager1.ResponseScripts.Add(" alert('" + ex.Message + "'); ");
            }
        }
        private string SetupPhoto(long pId)
        {
            string         ret      = string.Empty;
            aqufitEntities entities = new aqufitEntities();

            hiddenNextPhotoId.Value = "-1";
            hiddenPrevPhotoId.Value = "-1";
            panelPhotoList.Visible  = false;
            panelPhoto.Visible      = true;
            Photo[] photos    = null;
            Photo   photoPrev = null;

            if (hiddenAlbumKey.Value == string.Empty)
            {
                // lets first check that the photo is not in any album?
                Album album = entities.UserAttachments.OfType <Photo>().Include("Album").FirstOrDefault(p => p.Id == pId).Album;
                if (album != null)
                {
                    hiddenAlbumKey.Value = "" + album.Id;
                }
            }
            if (hiddenAlbumKey.Value != string.Empty)
            {
                long aId = Convert.ToInt64(hiddenAlbumKey.Value);
                IQueryable <Photo> photoQuery = entities.UserAttachments.OfType <Photo>().Include("UserSetting").OrderBy(p => p.Id).Where(p => p.Album.Id == aId);
                int total = photoQuery.Count();
                litPhotoCount.Text = "" + total;
                photoQuery         = photoQuery.Where(p => p.Id >= pId);
                total++;    // avoid saying 0 of 4 ....
                lPhotoNum.Text = "" + (total - photoQuery.Count());
                photos         = photoQuery.Take(2).ToArray();
                photoPrev      = entities.UserAttachments.OfType <Photo>().Include("UserSetting").OrderByDescending(p => p.Id).FirstOrDefault(p => p.Id < pId && p.Album.Id == aId);
            }
            else
            {
                IQueryable <Photo> photoQuery = entities.User2Photo.OrderBy(p => p.Photo.Id).Where(p => p.UserSettingsKey == ProfileSettings.Id).Select(p => p.Photo).OfType <Photo>();
                int total = photoQuery.Count();
                litPhotoCount.Text = "" + photoQuery.Count();
                photoQuery         = photoQuery.Where(p => p.Id >= pId);
                total++;    // avoid saying (0 of 4) ....
                lPhotoNum.Text = "" + (total - photoQuery.Count());
                photos         = photoQuery.Take(2).ToArray();
                User2Photo u2p = entities.User2Photo.Include("Photo").OrderByDescending(p => p.Photo.Id).FirstOrDefault(p => p.Photo.Id < pId);
                if (u2p != null)
                {
                    photoPrev = (Photo)u2p.Photo;
                }
            }
            if (photos.Length > 0)
            {
                if (photos[0].UserSetting.Id == UserSettings.Id)
                {
                    liDeletePhoto.Visible = true;
                }

                ret = photos[0].ImageUri;
                hiddenPhotoId.Value = "" + photos[0].Id;
                // setup tags...
                pId = photos[0].Id; // yes this is needed
                User2Photo[] photoTags = entities.User2Photo.Where(p => p.Photo.Id == pId).ToArray();
                string       js        = string.Empty;
                js += "Aqufit.Page.atiPhotoTagger.clearTags();";
                foreach (User2Photo u2p in photoTags)
                {
                    User person = entities.UserSettings.OfType <User>().FirstOrDefault(u => u.Id == u2p.UserSettingsKey);
                    js += "Aqufit.Page.atiPhotoTagger.addTagElement(" + person.Id + "," + u2p.Id + ",'" + person.UserName + " (" + person.UserFirstName + " " + person.UserLastName + ")'," + u2p.Top + ", " + u2p.Left + ", " + u2p.Width + ", " + u2p.Height + ");";
                }
                if (!Page.IsPostBack && !Page.IsCallback)
                {
                    this.StartupFunction = "Aqufit.Page.Actions.onLoadHelper = function(){ " + js + " };";
                }
                else
                {
                    RadAjaxManager1.ResponseScripts.Add("$(function(){ " + js + " });");
                }
            }
            if (photos.Length > 1)
            {
                hiddenNextPhotoId.Value = "" + photos[1].Id;
            }

            if (photoPrev != null)
            {
                hiddenPrevPhotoId.Value = "" + photoPrev.Id;
            }
            return(ret);
        }