var _ = require('lodash');
var Posts;

module.exports = function(nico) {
  var exports = {};
  var Categories = {};

  exports.reader = function(post) {
    var filepath = post.meta.filepath.toLowerCase();
    if (filepath.indexOf('components') === 0) {
      post.template = post.meta.template = 'component';
    } else {
      post.template = post.meta.template = (post.meta.template || 'page');
    }
    if (filepath === 'readme.md') {
      post.filename = post.meta.filename = 'index';
      post.template = post.meta.template = 'home';
    }
    if (filepath.indexOf('/demo/') > 0) {
      post.template = post.meta.template = 'code';
    }
    return post;
  };

  exports.filters = {
    find_category: function(posts, cats) {
      if (typeof cats === 'string') {
        cats = [cats];
      }
      var ret = [];
      Object.keys(posts).forEach(function(key) {
        var item = posts[key];
        if (cats.indexOf(item.meta.category) >= 0) {
          ret.push(item);
        }
      });
      ret = ret.sort(function(a, b) {
        a = a.meta.order || 10;
        b = b.meta.order || 10;
        return parseInt(a, 10) - parseInt(b, 10);
      });
      return ret;
    },
    get_categories: function(posts, post) {
      var rootDirectory = post.directory.split('/')[0];
      var categories = Categories[rootDirectory] || _.uniq(Object.keys(posts).map(function(key) {
        var item = posts[key];
        if (item.directory.split('/')[0] === post.directory.split('/')[0]) {
          return item.meta.category;
        }
      })).filter(function(n){ return n != undefined });
      categories = categories.sort(function(a, b) {
        return a.length - b.length;
      })
      Categories[rootDirectory] = categories;
      return categories;
    },
    find_demo_in_component: function(pages, directory) {
      var ret = [];
      Object.keys(pages).forEach(function(key) {
        var page = pages[key];
        if (key.indexOf(directory + '/demo/') === 0) {
          ret.push(page);
        }
      });
      ret = ret.sort(function(a, b) {
        if (/index$/i.test(a.filename)) {
          a.meta.order = 1;
        }
        if (/index$/i.test(b.filename)) {
          b.meta.order = 1;
        }
        a = a.meta.order || 100;
        b = b.meta.order || 100;
        return parseInt(a, 10) - parseInt(b, 10);
      });
      return ret;
    },
    // For Debug
    console: function(target) {
      console.log(target);
    },
    parsePost: function(filepath) {
      return nico.sdk.post.read(filepath);
    },
    odd: function(items) {
      return items.filter(function(item, i) {
        return (i+1)%2 === 1;
      });
    },
    even: function(items) {
      return items.filter(function(item, i) {
        return (i+1)%2 === 0;
      });
    },
    rootDirectoryIs: function(directory, rootDirectory) {
      return directory.split('/')[0] === rootDirectory;
    }
  };

  return exports;
};

@@ -23,19 +23,8 @@
 h');
-%0Avar Posts;
 %0A%0Amo
@@ -97,16 +97,233 @@
 es = %7B%7D;
+%0A  var Posts = %5B%5D;%0A%0A  function getAllPosts(pages) %7B%0A    if (Posts && Posts.length %3E 0) %7B%0A      return Posts;%0A    %7D%0A    Object.keys(pages).map(function(key) %7B%0A      Posts.push(pages%5Bkey%5D);%0A    %7D);%0A    return Posts;%0A  %7D
 %0A%0A  expo
@@ -1030,26 +1030,26 @@
 ;%0A      
-Object.key
+getAllPost
 s(posts)
@@ -1070,45 +1070,15 @@
 ion(
-key) %7B%0A        var item = posts%5Bkey%5D;
+post) %7B
 %0A   
@@ -1099,20 +1099,20 @@
 indexOf(
-item
+post
 .meta.ca
@@ -1146,20 +1146,20 @@
 et.push(
-item
+post
 );%0A     
@@ -1519,26 +1519,26 @@
  _.uniq(
-Object.key
+getAllPost
 s(posts)
@@ -1555,45 +1555,15 @@
 ion(
-key) %7B%0A        var item = posts%5Bkey%5D;
+item) %7B
 %0A   
@@ -1993,34 +1993,34 @@
 = %5B%5D;%0A      
-Object.key
+getAllPost
 s(pages).for
@@ -2025,35 +2025,36 @@
 orEach(function(
-key
+post
 ) %7B%0A        var 
@@ -2053,46 +2053,25 @@
     
-var page = pages%5Bkey%5D;%0A        if (key
+if (post.filepath
 .ind
@@ -2126,19 +2126,19 @@
 t.push(p
-age
+ost
 );%0A     

improve site perfermance