var root_url='http://www.rin-go.biz/'; // 非常重要


/*---------------------- Character list scroll control --------------------*/
if(typeof(lsTop) == 'undefined')
    var lsTop=0;
    var list = document.getElementById('character-list').getElementsByTagName('a');
if(lsTop==0)
    list[0].style.display='none';
if(lsTop+6 == list.length-1)
    list[list.length-1].style.display='none';
    /* 暫時解決老子問題 */
    if(location.href.indexOf('character=laozi')!=-1)
	scrollDown();
function scrollDown(){
    /* list 的 0 是上捲，最後一個誓下捲鈕 */
    if(lsTop+6 == list.length-1)
	return;
    list[1+lsTop].style.display='none';
    list[lsTop+6].style.display='block';
    lsTop++;
    list[0].style.display='block';
    if(lsTop+6 == list.length-1)
	list[list.length-1].style.display='none';
}

function scrollUp(){
    if(lsTop == 0)
	return;
    lsTop--;
    list[1+lsTop].style.display='block';
    list[lsTop+6].style.display='none';
    list[list.length-1].style.display='block';
    if(lsTop == 0)
	list[0].style.display='none';
}
/*-------------- End Of Character list scroll Control -----------*/

/*-------------- Dymatic load tag page instead ------------------*/
var tabs = document.getElementById('tab-container').getElementsByTagName('a');
var active_tab;
// Get now character
// alert(location.href.match(new RegExp("character=(.*?)")));
// 我太廢了，寫不出 RegExp Orz
var pos,character;

if((pos = location.href.indexOf('character='))!=-1){
    character = location.href.substr(pos+10);
    if((pos = character.indexOf('&'))!=-1)
    character = character.substr(0,pos);
}
else
    var character ='confucius'; // 孔子為預設

// Find active tab
for(var i=0;i<tabs.length;i++){
    if(tabs[i].className.indexOf('active') != -1){
	active_tab = tabs[i];
	break;
    }
}

// Bundle events
tabs[0].onclick=function(){
    return change_tab(this,'intro');
    };
tabs[1].onclick=function(){
    return change_tab(this,'pixalart');
};
tabs[2].onclick=function(){
    return change_tab(this,'skill');
};

function change_tab(that,target){
    if(that === active_tab)
	return false;
    that.blur();
    if(window.XMLHttpRequest)
        http = new XMLHttpRequest();
    else
	http = new ActiveXObject("Microsoft.XMLHTTP");
    if(window.XMLHttpRequest)
        var http = new XMLHttpRequest();
    else
	var http = new ActiveXObject("Microsoft.XMLHTTP");
        http.open('GET',root_url + '/character_genTab.php?tab=' + target+'&character='+character);
        http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;");
        http.onreadystatechange = function(){
            if(http.readyState!=4)
		return 0;
            if(http.status!=200)
		alert('與伺服器取得內容發生錯誤，可能是您的電腦網路連線中斷或伺服器異常\n請稍後再試，如果無法解決，請與網站擔當聯繫');
	    else{
		document.getElementById('intro-content').innerHTML=http.responseText;
		if(active_tab === tabs[1])
		    pix_panel_init();
	    }
    };
        http.send("");
    active_tab.className = 'tab';
    active_tab = that;
    that.className = 'tab active';
    check_intro_bg_righttop();
    return false;
}
check_intro_bg_righttop();
function check_intro_bg_righttop(){
    if(active_tab !== tabs[0])
	document.getElementById('intro-bg-right-top').style.display='block';
    else
	document.getElementById('intro-bg-right-top').style.display='none';
    }

/*-------- Pixalart Panel ---------------*/
var active_bg_button=false;
var active_fg_button=false;
if(active_tab === tabs[1])
    pix_panel_init();
function pix_panel_init(){
    if(active_tab !== tabs[1])
	return ;
    var pix_panel = document.getElementById('pixalart-panel');
    pix_panel.getElementsByTagName('fieldset')[0].getElementsByTagName('button')[0].click();
    pix_panel.getElementsByTagName('fieldset')[1].getElementsByTagName('button')[0].click();
}
function change_bg(that,file){
    document.getElementById('pixalart-bg').style.backgroundImage='url(image/characters/share_bg/'+file+')';
    that.disabled = 'disabled';
    if(active_bg_button)
	active_bg_button.disabled='';
    active_bg_button=that;
}

function change_pixalart(that,file){
    document.getElementById('pixalart-img').src='image/characters/'+character+'/'+file;
    that.disabled = 'disabled';
    if(active_fg_button)
	active_fg_button.disabled='';
    active_fg_button=that;
}
