/* ===============================================
 * 切換語言
 * =============================================== */
function changeLanguage(){
    var newUrl = location.pathname.replace(/(TradChinese|SimpleChinese|English)/, $('#language').val());
    location.href = newUrl;
}

/* ===============================================
 * 首頁標籤切換
 * =============================================== */
var switchTag = 0;  // 預防重疊動作
var timer;
var switchRole;

function indexTagSwitch(index){

    clearTimeout(timer);

    // 設定籤頁像素位置
    var tag1 = 0;
    var tag2 = -1000;
    var tag3 = -2000;

    // 取得目前頁面位置
    var thisPage;
    var thisPageLeft = parseInt($('#TagsPage').css('left').replace('px', ''));

    if(thisPageLeft == tag1){
        thisPage = 1;
    }else if(thisPageLeft == tag2){
        thisPage = 2;
    }else if(thisPageLeft == tag3){
        thisPage = 3;
    }

    // 控制自動滑動順序
    if((typeof index == 'undefined') && thisPage == 1){
        index = 'R';
    }else if((typeof index == 'undefined') && thisPage == 3){
        index = 'L';
    }else if(typeof index == 'undefined'){
        if ($('#TagsEvent').attr('id') == undefined && thisPage == 2){
            index = 'L';
        }else{
            index = switchRole;
        }
    }

    // 控制滑動到底後的順序
    if(index == 'L' && thisPage > 1){
        index = thisPage - 1;
    }

    if ($('#TagsEvent').attr('id') == undefined){
        if(index == 'R' && thisPage < 2){
            index = thisPage + 1;
        }
    }else{
        if(index == 'R' && thisPage < 3){
            index = thisPage + 1;
        }
    }
    var perLoop;

    // 向左滑回第一個籤頁
    if(index == 1 && switchTag == 0){
        switchTag   = 1;
        switchRole  = 'R';
        perLoop     = (thisPageLeft-tag1) / 100;
        $('#cir_1').attr('class', 'cir_click');
        $('#cir_2').attr('class', 'cir');
        $('#cir_3').attr('class', 'cir');
        $('#cir_1').css('margin-top', '15px');
        $('#cir_2').css('margin-top', '15px');
        $('#cir_3').css('margin-top', '15px');
        $('#leftButton').css('display', 'none');
        $('#rightButton').css('display', 'inline');

    // 只有兩個籤頁下像又滑動到第二個籤頁
    }else if ($('#TagsEvent').attr('id') == undefined && index == 2 && switchTag == 0){
        switchTag   = 1;
        switchRole  = 'L';
        perLoop     = (thisPageLeft-tag2) / 100;
        $('#cir_1').attr('class', 'cir');
        $('#cir_2').attr('class', 'cir_click');
        $('#cir_1').css('margin-top', '10px');
        $('#cir_2').css('margin-top', '10px');
        $('#leftButton').css('display', 'inline');
        $('#rightButton').css('display', 'none');

    // 向左或右滑動到第二個籤頁
    }else if(index == 2 && switchTag == 0){
        switchTag = 1;
        if(thisPage == 1){
            switchRole = 'R';
        }else{
            switchRole = 'L';
        }
        perLoop = (thisPageLeft-tag2) / 100;
        $('#cir_1').attr('class', 'cir');
        $('#cir_2').attr('class', 'cir_click');
        $('#cir_3').attr('class', 'cir');
        $('#cir_1').css('margin-top', '10px');
        $('#cir_2').css('margin-top', '10px');
        $('#cir_3').css('margin-top', '10px');
        $('#leftButton').css('display', 'inline');
        $('#rightButton').css('display', 'inline');

    // 向右滑動到第三個籤頁
    }else if(index == 3 && switchTag == 0){
        switchTag   = 1;
        switchRole  = 'L';
        perLoop     = (thisPageLeft-tag3) / 100;
        $('#cir_1').attr('class', 'cir');
        $('#cir_2').attr('class', 'cir');
        $('#cir_3').attr('class', 'cir_click');
        $('#cir_1').css('margin-top', '10px');
        $('#cir_2').css('margin-top', '10px');
        $('#cir_3').css('margin-top', '10px');
        $('#leftButton').css('display', 'inline');
        $('#rightButton').css('display', 'none');

    }

    for(var i=1; i<=100; i++){
        setTimeout('$("#TagsPage").css("left", "'+(thisPageLeft-i*perLoop)+'px");', i*2);
    }
    switchTag = 0;

    timer = setTimeout('indexTagSwitch();', 7000);
}

/* ===============================================
 * 首頁標籤切換
 * =============================================== */
function showContent(tag){
    if($('#'+tag).css('display') == 'inline'){
        $('#'+tag).css('display', 'none');
    }else{
        $('#'+tag).css('display', 'inline');
    }
}

/* ===============================================
 * FAQ 切換錨點
 * =============================================== */
function moveHash(tag){
    location.hash = tag.replace('C','');
    showContent(tag);
}

/* ===============================================
 * 按鈕變色
 * =============================================== */
function buttonSwich(obj){
    var Jobj    = $(document.getElementById(obj.id));
    var Jobj_L  = $('#' + Jobj.attr('id').replace(/\_.*/ ,'') + '_L');
    var Jobj_M  = $('#' + Jobj.attr('id').replace(/\_.*/ ,'') + '_M');
    var Jobj_R  = $('#' + Jobj.attr('id').replace(/\_.*/ ,'') + '_R');
    if(Jobj_L.attr('class') == 'ButtonLeft'){
        Jobj_L.attr('class', 'ButtonLeftClick');
        Jobj_M.attr('class', 'ButtonCenterClick');
        Jobj_R.attr('class', 'ButtonRightClick');
    }else{
        Jobj_L.attr('class', 'ButtonLeft');
        Jobj_M.attr('class', 'ButtonCenter');
        Jobj_R.attr('class', 'ButtonRight');
    }
}
