//GetAuctionListingImagesFromImages
        public void GetAuctionListingImagesFromImages(long auctionlisting_id, long auction_id, AuctionListing al)
        {
            List <Image> images = dataContext.spAuction_Images(auction_id).ToList();

            al.AuctionListingImages.Clear();
            bool   res1, res2;
            string pathS = GetAuctionImagePath(auction_id);
            string pathD = GetAuctionImageDirForUser(al.Owner_ID, auctionlisting_id);

            foreach (Image img in images)
            {
                AuctionListingImage ali = new AuctionListingImage();
                ali.Auction_ID    = auctionlisting_id;
                ali.Default       = img.Default;
                ali.Order         = img.Order;
                ali.PicturePath   = img.PicturePath;
                ali.ThumbNailPath = img.ThumbNailPath;
                res1 = CopyAuctionListingImage(Path.Combine(pathS, img.PicturePath), Path.Combine(pathD, ali.PicturePath));
                res2 = CopyAuctionListingImage(Path.Combine(pathS, img.ThumbNailPath), Path.Combine(pathD, ali.ThumbNailPath));
                if (res1 && res2)
                {
                    al.AuctionListingImages.Add(ali);
                }
            }
            SubmitChanges();
        }
        //DeleteImage
        public JsonExecuteResult DeleteImage(long auctionlisting_id, long image_id)
        {
            try
            {
                AuctionListing             auc    = dataContext.AuctionListings.SingleOrDefault(A => A.ID == auctionlisting_id);
                AuctionListingImage        img    = dataContext.AuctionListingImages.SingleOrDefault(I => I.ID == image_id);
                List <AuctionListingImage> images = GetAuctionListingImages(auctionlisting_id);

                string   path = GetAuctionImageDirForUser(auc.Owner_ID, auctionlisting_id);
                FileInfo fi   = new FileInfo(Path.Combine(path, img.PicturePath));
                if (fi.Exists)
                {
                    fi.Delete();
                }
                fi = new FileInfo(Path.Combine(path, img.ThumbNailPath));
                if (fi.Exists)
                {
                    fi.Delete();
                }

                if (images.Count() > 1)
                {
                    int order = img.Order;
                    images.Where(I => I.Order > img.Order).ToList().ForEach(i => i.Order = order++);
                    img.Order = int.MaxValue;
                }
                if (img.Default && images.Count > 1)
                {
                    images.OrderBy(I => I.Order).FirstOrDefault().Default = true;
                }
                dataContext.AuctionListingImages.DeleteOnSubmit(img);
                SubmitChanges();
            }
            catch (IOException ex)
            {
                return(new JsonExecuteResult(JsonExecuteResultTypes.ERROR, ex.Message));
            }
            catch (Exception ex)
            {
                return(new JsonExecuteResult(JsonExecuteResultTypes.ERROR, ex.Message));
            }
            return(new JsonExecuteResult(JsonExecuteResultTypes.SUCCESS));
        }
        //MoveImage
        public JsonExecuteResult MoveImage(long auctionlisting_id, long image_id, bool isup)
        {
            try
            {
                AuctionListing             auc    = dataContext.AuctionListings.SingleOrDefault(A => A.ID == auctionlisting_id);
                AuctionListingImage        img    = dataContext.AuctionListingImages.SingleOrDefault(I => I.ID == image_id);
                List <AuctionListingImage> images = GetAuctionListingImages(auctionlisting_id);
                if (img == null || images.Count() == 1 || (img.Order == 1 && isup) || (img.Order == images.Count() && !isup))
                {
                    return(new JsonExecuteResult(JsonExecuteResultTypes.ERROR, String.Format("You can't move {0} this image.", ((isup) ? "up" : "down"))));
                }
                AuctionListingImage img2 = images.Where(I => ((I.Order <= img.Order && isup) || (I.Order >= img.Order && !isup)) && I.ID != img.ID).AsQueryable().OrderBy(((isup) ? "Order desc" : "Order asc")).FirstOrDefault();
                int order = img.Order;
                img.Order  = img2.Order;
                img2.Order = order;

                if (isup && img2.Default && img.Order == 1)
                {
                    img2.Default = false;
                    img.Default  = true;
                }
                else if (!isup && img.Default && img2.Order == 1)
                {
                    img2.Default = true;
                    img.Default  = false;
                }
                SubmitChanges();
            }
            catch (IOException ex)
            {
                return(new JsonExecuteResult(JsonExecuteResultTypes.ERROR, ex.Message));
            }
            catch (Exception ex)
            {
                return(new JsonExecuteResult(JsonExecuteResultTypes.ERROR, ex.Message));
            }
            return(new JsonExecuteResult(JsonExecuteResultTypes.SUCCESS));
        }
        //AddAuctionListingImage
        public long AddAuctionListingImage(long auctionlising_id, string image)
        {
            long image_id;

            try
            {
                AuctionListingImage        img    = new AuctionListingImage();
                List <AuctionListingImage> images = GetAuctionListingImages(auctionlising_id);
                img.Auction_ID    = auctionlising_id;
                img.Default       = images.Count() == 0;
                img.Order         = (images.Count() == 0) ? 1 : images[images.Count() - 1].Order + 1;
                img.PicturePath   = image;
                img.ThumbNailPath = "thmb_" + image;
                dataContext.AuctionListingImages.InsertOnSubmit(img);
                SubmitChanges();
                image_id = img.ID;
            }
            catch
            {
                image_id = -1;
            }
            return(image_id);
        }
        //SetImageAsDefault
        public JsonExecuteResult SetImageAsDefault(long auctionlisting_id, long image_id)
        {
            try
            {
                AuctionListing             auc    = dataContext.AuctionListings.SingleOrDefault(A => A.ID == auctionlisting_id);
                AuctionListingImage        img    = dataContext.AuctionListingImages.SingleOrDefault(I => I.ID == image_id);
                List <AuctionListingImage> images = GetAuctionListingImages(auctionlisting_id);

                if (img == null || images.Count() == 1 || img.Order == 1)
                {
                    return(new JsonExecuteResult(JsonExecuteResultTypes.SUCCESS));
                }
                img.Default = true;
                img.Order   = 1;
                int order = 1;
                foreach (AuctionListingImage i in images)
                {
                    if (i.ID == img.ID)
                    {
                        continue;
                    }
                    i.Order   = ++order;
                    i.Default = false;
                }
                SubmitChanges();
            }
            catch (IOException ex)
            {
                return(new JsonExecuteResult(JsonExecuteResultTypes.ERROR, ex.Message));
            }
            catch (Exception ex)
            {
                return(new JsonExecuteResult(JsonExecuteResultTypes.ERROR, ex.Message));
            }
            return(new JsonExecuteResult(JsonExecuteResultTypes.SUCCESS));
        }