private void doSamples()
        {
            //To ensure proper updating. Hack
            TwitterItem[] tmpTweets = new TwitterItem[sampleTweets.Count()];
            sampleTweets.CopyTo(tmpTweets);

            listBox1.ItemsSource = tmpTweets;
        }
 private void sanitizeSample( TwitterItem tmpTwt)
 {
     for (int i = 0; i < badWords.Length; i++)
     {
         int lastFind = tmpTwt.Message.ToLower().IndexOf(badWords[i]);
         while (lastFind != -1)
         {
             if (lastFind > -1)
             {
                 tmpTwt.Message = tmpTwt.Message.Replace(badWords[i], badWords[i][0] + "***");
             }
             lastFind = tmpTwt.Message.ToLower().IndexOf(badWords[i], lastFind + 1);
         }
     }
 }
        private void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                return; //ToDo: better error handling. print a message
            }
            else
            {
                string twitterResults = e.Result;

                JObject root  = JObject.Parse(e.Result);
                JArray tweetsJArray = JArray.Parse(root["results"].ToString());
                List<TwitterItem> tweets = new List<TwitterItem>();

                string date = "";
                //todo: this part is sort of hacky way of finding the date.
                if (root["next_page"] != null)
                {
                   date = root["next_page"].ToString().Substring(root["next_page"].ToString().IndexOf("until%3") + 8, 10);
                }
                else if (root["refresh_url"]!=null)
                {
                    date = root["refresh_url"].ToString().Substring(root["refresh_url"].ToString().IndexOf("until%3") + 8, 10);
                }
                else if(root["previous_page"]!=null)
                {
                    date = root["previous_page"].ToString().Substring(root["previous_page"].ToString().IndexOf("until%3") + 8, 10);
                }
                DateTime localSearchDate = dateToday;
                if (date != "")
                {
                    if (date.Substring(date.Length - 1) == "&")
                    {
                        date = date.Substring(0, date.Length - 1);
                    }
                    localSearchDate = DateTime.ParseExact(date, "yyyy-MM-d", CultureInfo.InvariantCulture);  //"Sat, 28 Jan 2012 05:27:42 +0000"//"ddd, dd MMM yyyy HH:mm:ss zzz"
                }

                foreach (JObject tweet in tweetsJArray)
                {
                    tweetTotalCount[(dateToday - localSearchDate).Days]++;

                    TwitterItem tempTweet = new TwitterItem();
                    tempTweet.ImageSource = (tweet["profile_image_url"]).ToString();
                    tempTweet.Message = tweet["text"].ToString();
                    tempTweet.Username = tweet["from_user"].ToString()+":";
                    tempTweet.hourCreated = Int32.Parse(tweet["created_at"].ToString().Substring((tweet["created_at"].ToString().IndexOf(':')-2),2));

                    //Set mood and mood score
                    //decide if this tweet is angry, sad, or happy
                    tmpHappy = wordCount(happyWords, tempTweet.Message);
                    tmpAngry = wordCount(angryWords, tempTweet.Message);
                    tmpSad = wordCount(sadWords, tempTweet.Message);

                    if (tmpHappy == tmpAngry && tmpHappy == tmpSad)
                    {
                        tweetTypeCount[(int)TweetType.Nuetral, (dateToday-localSearchDate).Days]++;
                        tempTweet.Mood = TweetType.Nuetral;
                        tempTweet.MoodScore = tmpHappy; //all tmps equal so just choose arbitrary one
                        if (tmpHappy == 0 && (sampleTweets.Count%20==0)) //mod 12 just to prevent too many samples
                        {
                            sampleTweets.Insert(0, tempTweet); //nuetral ones are added to the end of samples
                        }
                        else if (tmpHappy == 0 && (sampleTweets.Count % 11 == 0)) //mod 12 just to prevent too many samples
                        {
                            sampleTweets.Add(tempTweet); //nuetral ones are added to the end of samples
                        }
                    }
                    else if (tmpHappy >= tmpAngry && tmpHappy >= tmpSad) //ordering of ifs skews to favour happy: ie. happy:2 angry:2 sad:1 would be happy
                    {
                        tweetTypeCount[(int)TweetType.Happy,(dateToday-localSearchDate).Days]++; //set back to *2 and 0:1
                        tempTweet.Mood = TweetType.Happy;
                        tempTweet.MoodScore = tmpHappy;
                        if (bestTweet[(int)TweetType.Happy] < tmpHappy)
                        {
                            sampleTweets.Insert(0, tempTweet);
                            bestTweet[(int)TweetType.Happy] = tmpHappy;
                        }
                        else if (bestTweet[(int)TweetType.Happy] <= tmpHappy && tweetTotalCount[(dateToday - localSearchDate).Days] % 2 == 0)
                        {
                            sampleTweets.Add( tempTweet); ;
                            bestTweet[(int)TweetType.Happy] = tmpHappy;
                        }
                    }
                    else if (tmpAngry >= tmpHappy && tmpAngry >= tmpSad)
                    {
                        tweetTypeCount[(int)TweetType.Angry,(dateToday-localSearchDate).Days]++;
                        tempTweet.Mood = TweetType.Angry;
                        tempTweet.MoodScore = tmpAngry;
                        if (bestTweet[(int)TweetType.Angry] < tmpAngry)
                        {
                            sampleTweets.Insert(0, tempTweet);
                            bestTweet[(int)TweetType.Angry] = tmpAngry;
                        }
                        else if ((bestTweet[(int)TweetType.Angry] <= tmpAngry && tweetTotalCount[(dateToday - localSearchDate).Days] % 2 == 0))
                        {
                            sampleTweets.Add(tempTweet);
                            bestTweet[(int)TweetType.Angry] = tmpAngry;
                        }
                    }
                    else if (tmpSad >= tmpHappy && tmpSad >= tmpAngry)
                    {
                        tweetTypeCount[(int)TweetType.Sad,(dateToday-localSearchDate).Days]++;
                        tempTweet.Mood = TweetType.Sad;
                        tempTweet.MoodScore = tmpSad;
                        if (bestTweet[(int)TweetType.Sad] < tmpSad)
                        {
                            sampleTweets.Insert(0,tempTweet);
                            bestTweet[(int)TweetType.Sad] = tmpSad;
                        }
                        else if (bestTweet[(int)TweetType.Sad] <= tmpSad && tweetTotalCount[(dateToday - localSearchDate).Days] % 2 == 0)
                        {
                            sampleTweets.Add( tempTweet);
                            bestTweet[(int)TweetType.Sad] = tmpSad;
                        }
                    }

                     tempTweet.ImageMood= "Images/" + tempTweet.Mood.ToString().ToLower() + "Facesm.png";
                }

                if (Int32.Parse(root["page"].ToString()) < numPages )
                {
                    nextPage(localSearchDate, root["max_id_str"].ToString(), Int32.Parse(root["page"].ToString()));
                }
                else{
                    threadDayComplete[(dateToday-localSearchDate).Days]=true;
                }

              //if all threads done
                bool allDone = true;
                for (int i = 0; i < threadDayComplete.Length; i++)
                {
                    allDone = (allDone && threadDayComplete[i]);
                }
                if (allDone)
                {
                    allThreadsDone();
                }
            }
        }