/********************************************************************************************************************
* 翻訳言語引数URLリプレイス
* myLanguageChange.getLanguage()             : <f> 現在の言語取得
* myLanguageChange.setLanguage()             : <f> 翻訳言語セット
* myLanguageChange.getDomain()               : <f> 現在のドメイン取得
* myLanguageChange.setDomain()               : <f> ドメインセット
* myLanguageChange.replaceHref()             : <f> 言語チェンジ
* 
* URLの引数（lng）を元にURLに言語用引数を付与する。対象は内部リンクのみ
* リプレイスを拒否したい場合個別にclass内に"nolng"を記述する。
* 【必須ライブラリ】
*    <script type="text/javascript" src="http://www.google.com/jsapi"></script>
*    <script type="text/javascript" src="js/jquery.min.js"></script>
**********************************************************************************************************************/
// コンストラクタ
var myLanguageChange = function(conf){
    /****************************************************************
    * conf.lng                   : <i> 言語(en, cn, ko...)
    * conf.domain                : <i> ドメイン名（配列）
    *****************************************************************/
    this.conf = {
        lng    : conf.lng    ? conf.lng    : "",
        domain : conf.domain ? conf.domain : ""
    };
}
// 現在の言語取得
myLanguageChange.prototype.getLanguage =  function(){
    /****************************************************************
    * this.lng                   : <o> 現在の言語
    *****************************************************************/
    return this.lng;
}
// 翻訳言語セット
myLanguageChange.prototype.setLanguage =  function(lng){
    /****************************************************************
    * lng                   : <i> 言語(en, cn, ko...)
    *****************************************************************/
    if(!lng) return;
    this.conf = {
        lng    : lng    ? lng    : ""
    }
}
// 現在の言語取得
myLanguageChange.prototype.getDomain =  function(){
    /****************************************************************
    * this.lng                   : <o> 現在のドメイン
    *****************************************************************/
    var domain = "";
    var kanma = "";
    for(i=0; i < this.domain.length; i++ ){
        domain += kanma + this.domain[i];
        kanma = ",";
    }
    return domain;
}
// 翻訳言語セット
myLanguageChange.prototype.setDomain =  function(domain){
    /****************************************************************
    * domain                   : <i> ドメイン名（配列）
    *****************************************************************/
    if(!domain) return;
    this.conf = {
        domain    : domain    ? domain    : ""
    }
}

// 言語URLリプレイス
myLanguageChange.prototype.replaceHref =  function(){
    var lng     = this.conf.lng;
    var domain  = this.conf.domain;
    var href;
    if(lng && this.conf.domain.length > 0){
        $('a[class!="nolng"]').each(function() {
            if(!$(this).attr('href')) return true;

            if($(this).attr('href').match(/http/)){
                for(i=0; i < domain.length; i++ ){
                    if($(this).attr('href').match('/'+domain[i]+'/')){

                        if($(this).attr('href').match(/[\?]/)){
                            if($(this).attr('href').match(/[#]/)){
                                href = $(this).attr('href').replace(/#/, "&"+lng+"#");
                                $(this).attr('href', href);
                            }else{
                                href = $(this).attr('href').replace(/$/, "&"+lng);
                                $(this).attr('href', href);
                            }
                        }else{
                            if($(this).attr('href').match(/[#]/)){
                                href = $(this).attr('href').replace(/#/, "?"+lng+"#");
                                $(this).attr('href', href);
                            }else{
                                href = $(this).attr('href').replace(/$/, "?"+lng);
                                $(this).attr('href', href);
                            }
                        }
                    }
                }
            }else{
                if($(this).attr('href').match(/[\?]/)){
                    if($(this).attr('href').match(/[#]/)){
                        href = $(this).attr('href').replace(/#/, "&"+lng+"#");
                        $(this).attr('href', href);
                    }else{
                        href = $(this).attr('href').replace(/$/, "&"+lng);
                        $(this).attr('href', href);
                    }
                }else{
                    if($(this).attr('href').match(/[#]/)){
                        href = $(this).attr('href').replace(/#/, "?"+lng+"#");
                        $(this).attr('href', href);
                    }else{
                        href = $(this).attr('href').replace(/$/, "?"+lng);
                        $(this).attr('href', href);
                    }
                }
            }
        });
    }
}


