﻿//---------------------------------------------------------------------------
// クエリ解析クラス
//---------------------------------------------------------------------------
var Query = Class.create();
Query.prototype = {
  //---------------------------------------------------------------------------
  // 変数
  //---------------------------------------------------------------------------
  Name: new Array(),
  Value: new Array(),
  Keys: 0,
  initialize: function() {
  },
  //---------------------------------------------------------------------------
  // 変数を追加する(内部用)
  //---------------------------------------------------------------------------
  AddString: function(name, value) {
    this.Name[this.Keys]  = name;
    this.Value[this.Keys] = value;
    this.Keys++;
  },
  //---------------------------------------------------------------------------
  // 指定された変数の登録番号を返す(内部用)
  //---------------------------------------------------------------------------
  SearchString: function(name) {
    var num = -1;

    for (ct = 0; ct <= this.Keys; ct++) {
        if (name == this.Name[ct]) {
            num = ct;
            break;
        }
    }
    return num;
  },
  //---------------------------------------------------------------------------
  // 指定された変数の値を返す
  //---------------------------------------------------------------------------
  GetString: function(name) {
    var value = '';

    if ((num = this.SearchString(name)) >= 0) {
        value = this.Value[num];
    }
    return value;
  },
  //---------------------------------------------------------------------------
  // 変数を追加・修正する
  //---------------------------------------------------------------------------
  SetString: function(name, value) {
    if(name.length == 0) {
        return 0;
    }

    if((num = this.SearchString(name)) >= 0) {
        this.Value[num] = value;
    } else {
        this.AddString(name, value);
    }
    return 0;
  },
  //---------------------------------------------------------------------------
  // 変数を削除する
  //---------------------------------------------------------------------------
  DeleteString: function(name) {
    if((num = this.SearchString(name)) >= 0) {
        this.Name[num]  = '';
        this.Value[num] = '';
    }
    return 0;
  },
  //---------------------------------------------------------------------------
  // クエリ文字列を解析する
  //---------------------------------------------------------------------------
  CreateQueryStringList: function() {
    var name  = '';
    var value = '';
    var flag  = false;

    for(ct = 1; ct <= location.search.length - 1; ct++) {
        ch = location.search.charAt(ct);
        if(ch == '?') {
            flag = false;
            continue;
        }
        if(ch == '=') {
            flag = true;
            if(value.length > 0) {
                name = value;
            }
            value = '';
            continue;
        }
        if((ch == '&') || (ch == '#')) {
            flag = false;
            if((name.length > 0) && (value.length > 0)) {
                this.AddString(name, value);
            }
            name  = '';
            value = '';
            continue;
        }
        value += ch;
    }
    if(value.length > 0) {
        if(flag == true) {
            this.AddString(name, value);
        }
    }
    return 0;
  }
} 

//---------------------------------------------------------------------------
// 多言語ブラウザ自動判別処理クラス
//---------------------------------------------------------------------------
var MultiLangIndex = Class.create();
MultiLangIndex.prototype = {
  oQuery: new Query(),				 // クエリ解析クラス生成
  lang: "",							 // 現在の言語
  postAry: ["adcd", "robot", "oem"], // 事業部判定用配列
  langAry: ["/en/"],				 // サイトの言語判定用配列
  siteLang: "",						 // 現在のサイトの言語取得用

  // コンストラクタ
  initialize: function() {
	this.lang = this.getBroserLANG();
  },

  //---------------------------------------------------------------------------
  // ブラウザ言語の取得メソッド
  //---------------------------------------------------------------------------
  getBroserLANG: function() {
	if(document.all) {
      return navigator.browserLanguage.substr(0,2);	      //IE用
	} else if(document.layers) { 
	  return navigator.language.substr(0,2);             //N4用
	} else if(document.getElementById) {
	  return navigator.language.substr(0,2); //N6,Moz用
	}
  },

  //---------------------------------------------------------------------------
  // selectLangageからこのページが表示されたら文字列trueを返す。
  //---------------------------------------------------------------------------
  isSelectLang: function () {
	this.oQuery.CreateQueryStringList();
	is = this.oQuery.GetString("selectlang");
	
	return is;
  },

  //---------------------------------------------------------------------------
  // 多言語ブラウザ自動判定処理実行
  //---------------------------------------------------------------------------
  execute: function() {
	if (this.isSelectLang() == true) { // selectlanguageの場合リダイレクトは発生しない
	  return;
	}

	// 現在どの事業部のトップか調べる
	var kind = this.matchArray(this.postAry, location.pathname);
	// 現在のサイトを調べる
	this.siteLang = this.matchArray(this.langAry, location.pathname);

	this.redirect(kind);
  },

  //---------------------------------------------------------------------------
  // 現在のどのトップか取得
  // 各事業部の場合文字列を返し
  // 総合の場合nullを返す。
  //---------------------------------------------------------------------------
  matchArray: function(ary, str) {
	var kind = ary.detect(function(value, index) {
	    var reg = new RegExp(".*" + value + ".*");
		// str.indexOf(value);
		// alert(str);
		if (str.match(reg)) return true; 
	});
	// alert(kind);
	return kind;
  },

  //---------------------------------------------------------------------------
  // 転送処理
  //---------------------------------------------------------------------------
  redirect: function (kind) {
	var href = "";
	if (kind == "robot") { // ロボット事業部用
	  href = this.redirectRobot();
	} else if (kind == "oem") {
	  href = this.redirectOther("oem");
	} else if (kind == "adcd") {
	  href = this.redirectOther("adcd");
	//} else if (typeof(kind) != "undefined"){  // ロボット・総合以外の場合
	//  href = this.redirectOther();
	} else { // 総合の場合
	  href = this.redirectIndex();
	}

	if (href != "") {// 転送先が存在する場合は転送
	  location.href = href;	
	}
  },

  //---------------------------------------------------------------------------
  // 総合トップ・ロボット事業部以外の転送処理
  //---------------------------------------------------------------------------
  redirectOther: function(post) {
	var href = "";
	if (this.lang != "ja") {
	  href = "../../en/" + post +  "/index.html";		
	}
	return href;
  }, 

  //---------------------------------------------------------------------------
  // ロボット事業部転送処理
  //---------------------------------------------------------------------------  
  redirectRobot: function() {
	var href = "";
	if (this.siteLang == "/en/") { // 英語サイトの場合の転送処理
	  if (this.lang == "ja" || this.lang == "de" || this.lang == "zh" || this.lang == "ko") { // 日本語、ドイツ語、中国語、韓国語の場合、各国の言語に転送
		href = "../../" + this.lang + "/robot/index.html";
	  } else { // それ以外は転送無
	  }
	} else { // 日本語サイトの場合の転送処理
	  if (this.lang == "en" || this.lang == "de" || this.lang == "zh" || this.lang == "ko") { // 英語、ドイツ語、中国語、韓国語の場合、各国の言語に転送
		href = "../../" + this.lang + "/robot/index.html";
	  } else if (this.lang == "ja") { // 日本語の場合は転送無
	  } else { // それ以外は英語に転送
		href = "../../en/robot/index.html";
	  }
	}
	  return href;
  },

  //---------------------------------------------------------------------------
  // 総合トップ用転送処理
  //---------------------------------------------------------------------------  
  redirectIndex: function() {
	var href = "";
	if (this.lang != "ja") {
	  href = "en/index.html";		
	}
	return href;
  }
}

//---------------------------------------------------------------------------
// ページロード完了時に多言語ブラウザ自動判定処理実行
//---------------------------------------------------------------------------
Event.observe (window, 'load', function() {
  oMultiLangIndex = new MultiLangIndex(); // 多言語ブラウザ自動判別クラス生成
  oMultiLangIndex.execute();
}, false);
