var Menu = {
    create: function() {
        this.make({target: 'menu',
                   files: [ ''
			  , 'search.html'
			  , 'info/release/'
			  , 'relnote/release.html'
			  , 'http://www.mew.org/mailman/listinfo'
			  , 'download'
			  , 'feature/'
			  , 'git/'
			  , 'bugreport/'
			  , 'http://www.wikihouse.com/mew/'
			  , 'link/' ],
                   items: [ 'トップ'
			  , '検索'
			  , 'マニュアル'
			  , 'リリースノート'
			  , 'ML'
			  , 'ダウンロード'
			  , '拡張機能'
			  , 'Git'
			  , 'バグレポート'
			  , 'FAQ'
			  , '関連リンク' ]});
    }, 

    prefix: location.hostname.match(/mew.org/) ? 'http://' + location.host + '/ja/' : 'http://localhost/ja/',

    nextString: '次へ',

    make: function(args) {
        if (args.directory) {
            this.prefix += args.directory;
        }

        var menu = document.getElementById(args.target);
        var table = document.createElement('table');
        var tbody = document.createElement('tbody');
        var len = args.files.length;

        for (var i = 0; i < len; i++ ) {
            this.add(args.items[i], this.getPath(args.files[i]), tbody);
        }
        if (args.next) {
            this.addNext(args, tbody);
        }

        table.appendChild(tbody);
        menu.appendChild(table);
    },

    add: function(name, url, tbody) {
        var tr = document.createElement('tr');
        var td = document.createElement('td');
        var a = document.createElement('a');
        a.appendChild(document.createTextNode(name));
        a.setAttribute('href', url);
        td.appendChild(a);
        tr.appendChild(td);
        tbody.appendChild(tr);
    },

    addNext: function(args, tbody) {
        var regex = /([0-9]+).html/;
        var result = location.href.match(regex);
        if (result === null) {
            this.add(this.nextString, this.getPath('1'), tbody);
        } else {
            var page = parseInt(result[1], 10);
            var lastpage = parseInt(args.files[args.files.length - 1], 10);
            if (page != lastpage) {
                page++;
                this.add(this.nextString, this.getPath(page), tbody);
            }
        }
    },

    getPath: function(file) {
	if (file.match(/^http:/)) {
	    return file;
	} else {
            return this.prefix + file;
	}
    },

    transfer: function(Sub) {
        for (var prop in this) {
            if (! Sub[prop]) {
                Sub[prop] = this[prop];
            }
        }
    }
};

