private static bool InCacheFolder(HtmlResources resources, string file)
 {
     // Check if the file is contained in any of the cache folders
     if (file.StartsWith(resources.StyleSheetFolder) || file.StartsWith(resources.ScriptFolder) || file.StartsWith(resources.ImageFolder))
     {
         return true;
     }
     else
     {
         return false;
     }
 }
        // TODO: Add support for value of *.js or *.css
        public static MvcHtmlString Resource(this HtmlHelper html, string value)
        {
            int depth = GetDepth(html);
            //throw new Exception(layout);
            if (ConfigurationManager.AppSettings["ScriptsFolder"] != null)
            {
                scriptsFolder = ConfigurationManager.AppSettings["ScriptsFolder"];
            }
            if (ConfigurationManager.AppSettings["StyleSheetFolder"] != null)
            {
                cssFolder = ConfigurationManager.AppSettings["StyleSheetFolder"];
            }

            var url = new UrlHelper(html.ViewContext.RequestContext);
            var server = html.ViewContext.RequestContext.HttpContext.Server;
            var resources = (HtmlResources)html.ViewData["Resources"];
            if (resources == null)
            {
                resources = new HtmlResources();
                html.ViewData["Resources"] = resources;
            }

            // Make sure the paths exist
            Directory.CreateDirectory(server.MapPath(scriptsFolder));
            Directory.CreateDirectory(server.MapPath(cssFolder));

            FileInfo info = new FileInfo(server.MapPath(value));
            if (info.Exists)
            {
                // Find the path diffrence so we can fix up included resources in fx css
                resources.PathOffset[value] = GetPathOffset(url.Content(value.Substring(0, value.Length - info.Name.Length)), url.Content(scriptsFolder));

                if (value.EndsWith(".js"))
                {
                    // Ensure that list exists.
                    if (!resources.Scripts.Keys.Contains(depth))
                    {
                        resources.Scripts.Add(depth, new List<string>());
                    }

                    if (!resources.Scripts[depth].Contains(value))
                    {
                        // Note the latest date a file was changed.
                        if (DateTime.Compare(resources.LatestScriptFile, info.LastWriteTime) < 0)
                        {
                            resources.LatestScriptFile = info.LastWriteTime;
                        }

                        // Minify the script file if necessary.
                        if (resources.Minify && string.IsNullOrEmpty((string)html.ViewContext.HttpContext.Session["ResourceHelper.NoMinifying"]))
                        {
                            string origname = info.Name.Substring(0, info.Name.LastIndexOf('.'));
                            if (origname.EndsWith(".min"))
                            {
                                // The resource is pre-minified. Skip.
                                resources.Scripts[depth].Add(value);
                            }
                            else if (!resources.Debug && File.Exists(server.MapPath(scriptsFolder + origname + ".min" + info.Extension)) && DateTime.Compare(File.GetLastWriteTime(server.MapPath(scriptsFolder + origname + ".min" + info.Extension)), info.LastWriteTime) >= 0)
                            {
                                if (DateTime.Compare(resources.LatestScriptFile, info.LastWriteTime) < 0)
                                {
                                    resources.LatestScriptFile = File.GetLastWriteTime(server.MapPath(scriptsFolder + origname + ".min" + info.Extension));
                                }
                                // We have already minified the file. Skip.
                                resources.Scripts[depth].Add(scriptsFolder + origname + ".min" + info.Extension);
                            }
                            else
                            {
                                // TODO: Try to fix up relative paths if we move the script file to another location
                                // Minify file.
                                string filename = scriptsFolder + origname + ".min" + info.Extension;
                                MinifyFile(server.MapPath(filename), server.MapPath(value));
                                //File.WriteAllText(server.MapPath(filename), Yahoo.Yui.Compressor.JavaScriptCompressor.Compress(File.ReadAllText(server.MapPath(value))));
                                resources.LatestScriptFile = File.GetLastWriteTime(server.MapPath(filename));

                                // Insert the path to the minified file.
                                resources.Scripts[depth].Add(filename);

                                // File changed named because we are using the mimified version
                                resources.PathOffset[filename] = resources.PathOffset[value];
                            }
                        }
                        else
                        {
                            resources.Scripts[depth].Add(value);
                        }
                    }
                }
                else if (value.EndsWith(".css"))
                {
                    // ENsure that list exists.
                    if (!resources.Stylesheets.Keys.Contains(depth))
                    {
                        resources.Stylesheets.Add(depth, new List<string>());
                    }
                    if (!resources.Stylesheets[depth].Contains(value))
                    {
                        // Note the latest date a file was changed.
                        if (DateTime.Compare(resources.LatestCSSFile, info.LastWriteTime) < 0)
                            resources.LatestCSSFile = info.LastWriteTime;

                        resources.Stylesheets[depth].Add(value);
                    }
                }
            }
            else
            {
                if (resources.Strict)
                {
                    throw new FileNotFoundException(String.Format("Could not find file {0}", value), value);
                }
            }
            return null;
        }
        private static HtmlResources GetResources(HtmlHelper html)
        {
            // Store state in the ViewData //TODO: Implment some kind of IIS caching so we don't do this for every page load
            var resources = (HtmlResources)html.ViewContext.HttpContext.Items["Resources"];

            if (resources == null)
            {
                resources = new HtmlResources();
                html.ViewContext.HttpContext.Items["Resources"] = resources;
            }
            return resources;
        }