var Menu = {
    create: function() {
        this.make({target: 'menu',
                   files: [ '.'
			  , 'climbing/2009/'
			  , 'diving/'
			  , 'soccer/'
			  , 'travel/'
			  , 'doc/'
			  , 'material/' ],
                   items: [ 'ホーム'
			  , 'クライミング日記'
			  , 'ダイビングのログ'
			  , 'サッカー'
			  , '旅'
			  , '執筆'
			  , '発表資料' ]});
    }, 

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

    suffix: '.html',

    completeSuffix: false,

    nextString: '次へ',

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

	if (args.completeSuffix !== undefined) {
            this.completeSuffix = args.completeSuffix;
	}

        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(String(page)), tbody);
            }
        }
    },

    getPath: function(file) {
	if (file.match(/^http:/)) {
	    return file;
	} else if (this.completeSuffix == false || file == '.') {
	    return this.prefix + file;
	} else {
            return this.prefix + file + this.suffix;
	}
    },

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

