private void take_ping_picture(object sender, System.EventArgs e)
        {
            haveTheFlag = true;

            string fName = "pingPicture.jpg";
            Bitmap saveBmp = null;
            try
            {

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

                if (config.getProfile(bubble.profileInUse).pingStatsStamp)
                {

                    statistics.movementResults stats = new statistics.movementResults();
                    stats = statistics.statsForCam(CameraRig.activeCam, bubble.profileInUse, "Ping");

                    lst.Add(stats.avgMvStart.ToString());
                    lst.Add(stats.avgMvLast.ToString());
                    lst.Add(stats.mvNow.ToString());
                    lst.Add(Convert.ToBoolean(CameraRig.rigInfoGet(bubble.profileInUse, CameraRig.rig[CameraRig.activeCam].cameraName, CameraRig.infoEnum.alarmActive)) ? "On" : "Off");
                    lst.Add(config.getProfile(bubble.profileInUse).pingInterval.ToString() + " Mins");

                }

                imageText stampArgs = new imageText();

                if (config.getProfile(bubble.profileInUse).pingAll)
                {

                    mosaic mos = new mosaic();
                    int imgHeight = 0;
                    int imgWidth = 0;

                    //set the height and width to the largest image
                    foreach (rigItem item in CameraRig.rig)
                    {

                        if (item.cam.pubFrame.Height > imgHeight || item.cam.pubFrame.Width > imgWidth)
                        {

                            imgHeight = item.cam.pubFrame.Height;
                            imgWidth = item.cam.pubFrame.Width;

                        }

                    }

                    foreach (rigItem item in CameraRig.rig)
                    {

                        mos.addToList(bubble.resizeImage(item.cam.pubFrame, imgWidth, imgHeight));

                    }

                    stampArgs.bitmap = (Bitmap)mos.getMosaicBitmap(4).Clone();

                }
                else//if (config.getProfile(bubble.profileInUse).pingStatsStamp)
                {

                    stampArgs.bitmap = (Bitmap)camera.pubFrame.Clone();

                }

                stampArgs.type = "Ping";
                stampArgs.backingRectangle = config.getProfile(bubble.profileInUse).pingTimeStampRect;
                stampArgs.stats = lst;

                saveBmp = bubble.timeStampImage(stampArgs);

                ImageCodecInfo jgpEncoder = GetEncoder(ImageFormat.Jpeg);
                System.Drawing.Imaging.Encoder myEncoder = System.Drawing.Imaging.Encoder.Quality;
                EncoderParameters myEncoderParameters = new EncoderParameters(1);
                EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, config.getProfile(bubble.profileInUse).pingCompression);
                myEncoderParameters.Param[0] = myEncoderParameter;
                saveBmp.Save(bubble.tmpFolder + fName, jgpEncoder, myEncoderParameters);

                Bitmap thumb = bubble.GetThumb(saveBmp);
                thumb.Save(bubble.tmpFolder + bubble.tmbPrefix + fName, ImageFormat.Jpeg);

                saveBmp.Dispose();
                thumb.Dispose();
                bubble.logAddLine("Image saved: " + fName);
                //bubble.pingError = false;
                haveTheFlag = false;

            }
            catch (Exception)
            {
                haveTheFlag = false;
                //bubble.pingError = true;
                bubble.logAddLine("Error in saving image: " + fName);
            }
        }
        public static void movementPublish()
        {
            int emailToProcess = new int();
            int ftpToProcess = new int();
            bool spamStopEmail = false;

            emailToProcess = imagesFromMovement.emailToProcess();
            ftpToProcess = imagesFromMovement.ftpToProcess();

            teboDebug.writeline(teboDebug.movementPublishVal + 1);
            pulseEvent(null, new EventArgs());

            if (!Graph.dataExistsForDate(time.currentDate()))
            {
                teboDebug.writeline(teboDebug.movementPublishVal + 2);
                movStats.Clear();
                moveStatsInitialise();
                Graph.updateGraphHist(time.currentDate(), bubble.movStats);
            }

            //we have images to process however the option is set to not load to ftp site and not email images
            if (ftpToProcess + emailToProcess > 0
                && !config.getProfile(bubble.profileInUse).sendNotifyEmail
                && !config.getProfile(bubble.profileInUse).loadImagesToFtp)
            {
                teboDebug.writeline(teboDebug.movementPublishVal + 3);
                logAddLine("Email and ftp set to OFF(see images folder), files created: " + emailToProcess.ToString());
                imagesFromMovement.listsClear();
                //imagesToProcess.Clear();
                Graph.updateGraphHist(time.currentDate(), bubble.movStats);
                if (graphToday()) { redrawGraph(null, new EventArgs()); }
            }

            //we have images to load to the ftp site and the option is set to load to ftp site
            if (config.getProfile(bubble.profileInUse).loadImagesToFtp && ftpToProcess > 0)
            {

                //ftp images - start
                if (config.getProfile(bubble.profileInUse).loadImagesToFtp)
                {
                    teboDebug.writeline(teboDebug.movementPublishVal + 5);
                    try
                    {
                        teboDebug.writeline(teboDebug.movementPublishVal + 6);
                        pulseEvent(null, new EventArgs());

                        int tmpInt = 0;

                        ArrayList ftpArrList = imagesFromMovement.toFtp(ftpToProcess);

                        //foreach (string img in imagesToProcess)
                        foreach (string img in ftpArrList)
                        {

                            teboDebug.writeline(teboDebug.movementPublishVal + 7);
                            logAddLine("Uploading to ftp site");
                            ftp.Upload(imageFolder + img, config.getProfile(bubble.profileInUse).ftpRoot, config.getProfile(bubble.profileInUse).ftpUser, config.getProfile(bubble.profileInUse).ftpPass);
                            imagesFromMovement.ftpConfirmed(img);

                            tmpInt++;

                            if (tmpInt > 4)
                            {
                                tmpInt = 0;
                                pulseEvent(null, new EventArgs());
                            }

                        }

                    }
                    catch { }
                    if (!config.getProfile(bubble.profileInUse).sendNotifyEmail)
                    {
                        teboDebug.writeline(teboDebug.movementPublishVal + 8);
                        //imagesToProcess.Clear();
                    }
                }
                //ftp images - end

            }

            //Images to process are more than will fit in one email
            //or we have images to process and the email notify interval time has passed
            if (
                config.getProfile(bubble.profileInUse).sendNotifyEmail &&
                (emailToProcess >= config.getProfile(bubble.profileInUse).maxImagesToEmail ||
                (emailToProcess > 0 && (time.secondsSinceStart() - lastProcessedTime) >
                config.getProfile(bubble.profileInUse).emailNotifyInterval))
                && !mail.SpamAlert(config.getProfile(bubble.profileInUse).emailIntelEmails,
                                   config.getProfile(bubble.profileInUse).emailIntelMins,
                                   config.getProfile(bubble.profileInUse).EmailIntelOn,
                                   time.secondsSinceStart())
                )
            {

                if (mail.spamStopped)
                {

                    mail.spamStopped = false;
                    spamStopEmail = true;

                }

                teboDebug.writeline(teboDebug.movementPublishVal + 4);
                logAddLine("Images to process: " + emailToProcess.ToString());
                bubble.fileBusy = true;
                Graph.updateGraphHist(time.currentDate(), bubble.movStats);
                if (graphToday()) { redrawGraph(null, new EventArgs()); }

                if (config.getProfile(bubble.profileInUse).sendNotifyEmail)
                {
                    teboDebug.writeline(teboDebug.movementPublishVal + 9);

                    ArrayList emailArrList = imagesFromMovement.toEmail(emailToProcess);
                    int imagesToEmail = emailToProcess;

                    //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
                    while (imagesToEmail > 0)
                    {
                        teboDebug.writeline(teboDebug.movementPublishVal + 10);
                        try
                        {

                            teboDebug.writeline(teboDebug.movementPublishVal + 11);
                            mail.clearAttachments();

                            //the time trigger has caused these emails to be sent
                            //or the despamificator has been switched on and the time has elapsed with the mosaic option selected
                            if (emailToProcess < config.getProfile(bubble.profileInUse).maxImagesToEmail || (spamStopEmail && !config.getProfile(bubble.profileInUse).EmailIntelStop))
                            {
                                teboDebug.writeline(teboDebug.movementPublishVal + 12);

                                //send mosaic
                                if (config.getProfile(bubble.profileInUse).sendMosaicImages || (spamStopEmail && !config.getProfile(bubble.profileInUse).EmailIntelStop))
                                {

                                    mosaic mos = new mosaic();

                                    mos.clearList();

                                    for (int i = 0; i < emailToProcess; i++)
                                    {
                                        mos.addToList(thumbFolder + tmbPrefix + emailArrList[i].ToString());
                                        imagesFromMovement.emailConfirmed(emailArrList[i].ToString());
                                    }

                                    imagesToEmail = 0;

                                    string rand = new Random(time.secondsSinceStart()).Next(99999).ToString();

                                    pulseEvent(null, new EventArgs());

                                    if (!spamStopEmail)
                                    {

                                        mos.saveMosaicAsJpg(config.getProfile(bubble.profileInUse).mosaicImagesPerRow,
                                                            thumbFolder + rand + mosaicFile,
                                                            config.getProfile(bubble.profileInUse).alertCompression);

                                    }
                                    else
                                    {

                                        mos.saveMosaicAsJpg(10,
                                                            thumbFolder + rand + mosaicFile,
                                                            config.getProfile(bubble.profileInUse).alertCompression);

                                    }

                                    mos.clearList();

                                    mail.attachments.Add(thumbFolder + rand + mosaicFile);

                                }

                                //send thumbs or fullsize
                                else
                                {

                                    teboDebug.writeline(teboDebug.movementPublishVal + 13);

                                    for (int i = 0; i < emailArrList.Count; i++)
                                    {

                                        imagesFromMovement.emailConfirmed(emailArrList[i].ToString());
                                        if (config.getProfile(bubble.profileInUse).sendThumbnailImages) emailArrList[i] = thumbFolder + tmbPrefix + emailArrList[i];
                                        if (config.getProfile(bubble.profileInUse).sendFullSizeImages) emailArrList[i] = imageFolder + emailArrList[i];

                                    }

                                    imagesToEmail = 0;

                                    pulseEvent(null, new EventArgs());
                                    mail.attachments.AddRange(emailArrList.GetRange(0, (emailArrList.Count)));

                                }

                                teboDebug.writeline(teboDebug.movementPublishVal + 14);

                            }

                            //the quantity trigger has caused these emails to be sent
                            else
                            {
                                teboDebug.writeline(teboDebug.movementPublishVal + 15);

                                //send mosaic
                                if (config.getProfile(bubble.profileInUse).sendMosaicImages)
                                {

                                    mosaic mos = new mosaic();

                                    mos.clearList();

                                    for (int i = 0; i < (int)(config.getProfile(bubble.profileInUse).maxImagesToEmail); i++)
                                    {

                                        mos.addToList(thumbFolder + tmbPrefix + emailArrList[i].ToString());
                                        imagesFromMovement.emailConfirmed(emailArrList[i].ToString());
                                        imagesToEmail--;

                                    }

                                    string rand = new Random(time.secondsSinceStart()).Next(99999).ToString();

                                    pulseEvent(null, new EventArgs());
                                    mos.saveMosaicAsJpg(config.getProfile(bubble.profileInUse).mosaicImagesPerRow,
                                                                 thumbFolder + rand + mosaicFile,
                                                                 config.getProfile(bubble.profileInUse).alertCompression);

                                    mos.clearList();

                                    mail.attachments.Add(thumbFolder + rand + mosaicFile);

                                }

                                //send thumbs or fullsize
                                else
                                {

                                    teboDebug.writeline(teboDebug.movementPublishVal + 16);

                                    for (int i = 0; i < (int)(config.getProfile(bubble.profileInUse).maxImagesToEmail); i++)
                                    {

                                        imagesFromMovement.emailConfirmed(emailArrList[i].ToString());
                                        if (config.getProfile(bubble.profileInUse).sendThumbnailImages) emailArrList[i] = thumbFolder + tmbPrefix + emailArrList[i].ToString();
                                        if (config.getProfile(bubble.profileInUse).sendFullSizeImages) emailArrList[i] = imageFolder + emailArrList[i].ToString();
                                        imagesToEmail--;

                                    }

                                    pulseEvent(null, new EventArgs());
                                    teboDebug.writeline(teboDebug.movementPublishVal + 17);
                                    mail.attachments.AddRange(emailArrList.GetRange(0, (int)(config.getProfile(bubble.profileInUse).maxImagesToEmail)));

                                }

                            }

                            try
                            {
                                teboDebug.writeline(teboDebug.movementPublishVal + 18);
                                graphSeq++;
                                graphCurrent.Save(tmpFolder + "graphCurrent" + graphSeq.ToString() + ".jpg", ImageFormat.Jpeg);
                            }
                            catch
                            {
                                logAddLine("Error saving graph for emailing;");
                            }

                            teboDebug.writeline(teboDebug.movementPublishVal + 19);
                            pulseEvent(null, new EventArgs());

                            mail.addAttachment(tmpFolder + "graphCurrent" + graphSeq.ToString() + ".jpg"); ;
                            logAddLine("graphCurrent" + graphSeq.ToString() + ".jpg" + " added to email");
                            Thread.Sleep(500);
                            logAddLine("Sending Email");

                            mail.sendEmail(
                                           config.getProfile(bubble.profileInUse).sentBy,
                                           config.getProfile(bubble.profileInUse).sendTo,
                                           config.getProfile(bubble.profileInUse).mailSubject,
                                           config.getProfile(bubble.profileInUse).mailBody,
                                           config.getProfile(bubble.profileInUse).replyTo,
                                           (config.getProfile(bubble.profileInUse).sendThumbnailImages ||
                                           config.getProfile(bubble.profileInUse).sendFullSizeImages ||
                                           config.getProfile(bubble.profileInUse).sendMosaicImages),
                                           time.secondsSinceStart(),
                                           config.getProfile(bubble.profileInUse).emailUser,
                                           config.getProfile(bubble.profileInUse).emailPass,
                                           config.getProfile(bubble.profileInUse).smtpHost,
                                           config.getProfile(bubble.profileInUse).smtpPort,
                                           config.getProfile(bubble.profileInUse).EnableSsl
                                           );

                            //string[] newdet = new string[2];

                            emailToProcess = imagesFromMovement.emailToProcess();
                            imagesToEmail = emailToProcess;

                        }
                        catch { }
                    }//while (imagesToProcess2.emailToProcess() != 0)

                    teboDebug.writeline(teboDebug.movementPublishVal + 20);
                    pulseEvent(null, new EventArgs());

                    lastProcessedTime = time.secondsSinceStart();
                    FileManager.WriteFile("log");
                    bubble.logAddLine("Log data saved.");
                    FileManager.WriteFile("graph");
                    bubble.logAddLine("Graph data saved.");
                    bubble.logAddLine("Config data saved.");
                    FileManager.WriteFile("config");
                    bubble.fileBusy = false;
                    Thread.Sleep(500);

                    if (spamStopEmail)
                    {

                        spamStopEmail = false;

                    }

                }

            }

            teboDebug.writeline(teboDebug.movementPublishVal + 21);
            pulseEvent(null, new EventArgs());

            teboDebug.writeline(teboDebug.movementPublishVal + 22);
            Thread.Sleep(1000);
        }