// mtl.js

// 英語版と日本語版を切り替える

$(function() {
    var href = location.href.replace(/[\#].*$/, '');
    var lang = 'ja';

    // 英語ページを制作してあるページのリスト
	var list = [
		'/about/$',
		'/about/scope/$',
		'/about/history/$',
		'/about/collaboration/$',
		'/about/technology/$',
		'/about/culture/$',
		'/about/publicity/$',
		'/contact.html$',
		'/access/$'
	];

    // 現在 Google Translate 経由でアクセスしている場合は URL を補正する
    var usegoogle = true;
    if (location.host.search(/translate.google/) > -1) {
        var query = '&' + location.search;
        var pos = query.search(/&u=/);
        if (pos > -1 ) {
            href = query.substr(pos+3).replace(/&.*$/, '');
            href = decodeURIComponent(href);
            lang = 'en';
        }
    }

    //『index.html』は不要
    href = href.replace(/\/index.html$/, '/');

    // 英語ページがあるページかチェック
    for (var i=0; i<list.length; i++) {
        if (href.search(list[i]) > -1) {
            usegoogle = false;
            break;
        }
    }

    //『-en.html』なら既に英語ページだ
    if (href.search(/-en.html/) > -1) {
        lang = 'en';
        usegoogle = false;
    }

    // リンク先 URL を決定する
    var jlink = href;
    var elink = href;
    if (usegoogle) {
        var gt = 'http://translate.google.com/translate?hl=en&sl=ja&tl=en&u=';
        elink = gt + encodeURIComponent(href);
    } else {
        jlink = href.replace(/\/index-en.html/, '/').replace(/-en.html/, '.html');
        elink = href.replace(/\/$/, '/index.html').replace(/(-en)?.html$/, '-en.html');
    }

    // トップページの英語版は、About トップにする
    if (jlink.search(/^http:\/\/[^\/]+\/?$/) > -1) {
        jlink = (jlink+'/').replace(/\/\/$/, '/');
        elink = jlink + 'about/index-en.html';
    }

    // リンクのタグを追加する
    var j = $('<a/>').attr('target', '_top').attr('href', jlink).attr('title', 'Japanese').text('Japanese');
    var e = $('<a/>').attr('target', '_top').attr('href', elink).attr('title', 'English').text('English');
    $('.english-link').empty().append(j).append(' | ').append(e);
    // $('body').addClass('lang-'+lang);
});

