private void LoadCategory(SingleCategoryViewModel model, string categoryId)
        {
            Category c = MTApp.CatalogServices.Categories.Find(categoryId);
            if (c != null)
            {
                if (c.Bvin != string.Empty)
                {
                    string destination = UrlRewriter.BuildUrlForCategory(new CategorySnapshot(c), MTApp.CurrentRequestContext.RoutingContext);

                    if (c.ImageUrl.StartsWith("~") | c.ImageUrl.StartsWith("http://"))
                    {
                        model.IconUrl = ImageHelper.SafeImage(Url.Content(c.ImageUrl));
                    }
                    else
                    {
                        model.IconUrl = ImageHelper.SafeImage(Url.Content("~/" + c.ImageUrl));
                    }

                    model.LinkUrl = destination;
                    model.AltText = c.MetaTitle;
                    model.Name = c.Name;

                    if (c.SourceType == CategorySourceType.CustomLink)
                    {
                        model.OpenInNewWindow = c.CustomPageOpenInNewWindow;
                    }
                }
            }
        }
        //
        // GET: /ContentBlocks/CategoryRotator/
        public ActionResult Index(ContentBlock block)
        {
            var profiler = MvcMiniProfiler.MiniProfiler.Current;
            using (profiler.Step("Block:CategoryRotator"))
            {
                SingleCategoryViewModel model = new SingleCategoryViewModel();

                bool showInOrder = false;
                if (block != null)
                {
                    showInOrder = block.BaseSettings.GetBoolSetting("ShowInOrder");

                    int nextIndex;
                    if (Session[block.Bvin + "NextImageIndex"] != null)
                    {
                        nextIndex = (int)Session[block.Bvin + "NextImageIndex"];
                    }
                    else
                    {
                        nextIndex = 0;
                    }

                    List<ContentBlockSettingListItem> settings = block.Lists.FindList("Categories");

                    if (settings.Count != 0)
                    {
                        if (settings.Count > nextIndex)
                        {
                            LoadCategory(model, settings[nextIndex].Setting1);
                        }
                        else if (nextIndex >= settings.Count)
                        {
                            if (showInOrder)
                            {
                                nextIndex = 0;
                            }
                            else
                            {
                                nextIndex = MerchantTribe.Web.RandomNumbers.RandomInteger(settings.Count - 1, 0);
                            }
                            LoadCategory(model, settings[nextIndex].Setting1);
                        }

                        if (showInOrder)
                        {
                            nextIndex += 1;
                        }
                        else
                        {
                            nextIndex = MerchantTribe.Web.RandomNumbers.RandomInteger(settings.Count - 1, 0);
                        }
                        Session[block.Bvin + "NextImageIndex"] = nextIndex;
                    }

                }

                return View(model);
            }
        }
        public string Render(MerchantTribe.Commerce.MerchantTribeApplication app, dynamic viewBag, MerchantTribe.Commerce.Content.ContentBlock block)
        {
            SingleCategoryViewModel model = new SingleCategoryViewModel();

            bool showInOrder = false;
            if (block != null)
            {
                showInOrder = block.BaseSettings.GetBoolSetting("ShowInOrder");
                
                List<ContentBlockSettingListItem> settings = block.Lists.FindList("Categories");

                int nextIndex = GetProductIndex(settings.Count -1);                

                if (settings.Count != 0)
                {
                    if (settings.Count > nextIndex)
                    {
                        LoadCategory(model, settings[nextIndex].Setting1, app);
                    }
                    else if (nextIndex >= settings.Count)
                    {
                        if (showInOrder)
                        {
                            nextIndex = 0;
                        }
                        else
                        {
                            nextIndex = MerchantTribe.Web.RandomNumbers.RandomInteger(settings.Count - 1, 0);
                        }
                        LoadCategory(model, settings[nextIndex].Setting1, app);
                    }

                    if (showInOrder)
                    {
                        nextIndex += 1;
                    }
                    else
                    {
                        nextIndex = MerchantTribe.Web.RandomNumbers.RandomInteger(settings.Count - 1, 0);
                    }                    
                }

            }

            return RenderModel(model);
        }
        private List<SingleCategoryViewModel> PrepSubCategories(List<CategorySnapshot> snaps)
        {
            List<SingleCategoryViewModel> result = new List<SingleCategoryViewModel>();

            int columnCount = 1;

            foreach (CategorySnapshot snap in snaps)
            {
                SingleCategoryViewModel model = new SingleCategoryViewModel();

                model.LinkUrl = UrlRewriter.BuildUrlForCategory(snap, 
                                                                MTApp.CurrentRequestContext.RoutingContext);
                model.IconUrl = MerchantTribe.Commerce.Storage.DiskStorage.CategoryIconUrl(
                                                                MTApp, 
                                                                snap.Bvin, 
                                                                snap.ImageUrl, 
                                                                Request.IsSecureConnection);
                model.AltText = snap.Name;
                model.Name = snap.Name;


                bool isLastInRow = false;
                bool isFirstInRow = false;
                if ((columnCount == 1))
                {
                    isFirstInRow = true;
                }

                if ((columnCount == 3))
                {
                    isLastInRow = true;
                    columnCount = 1;
                }
                else
                {
                    columnCount += 1;
                }

                model.IsFirstItem = isFirstInRow;
                model.IsLastItem = isLastInRow;

                result.Add(model);
            }

            return result;
        }
        public string RenderModel(SingleCategoryViewModel model)
        {
            StringBuilder sb = new StringBuilder();
            
            sb.Append("<div class=\"categoryrotator\"><div class=\"decoratedblock\"><div class=\"blockcontent\">");
            sb.Append("<a href=\"" + model.LinkUrl + "\" title=\"" + HttpUtility.HtmlEncode(model.AltText) + "\">");
            sb.Append("<img src=\"" + model.IconUrl + "\" alt=\"" + HttpUtility.HtmlEncode(model.AltText) + "\" /><br />");
            sb.Append("<span>" + HttpUtility.HtmlEncode(model.Name) + "</span></a>");                      
            sb.Append("</div></div></div>");

            return sb.ToString();
        }