var domain = document.domain;
var isIndex = false;
var locate = window.location.toString();
	
if(locate.length == (locate.lastIndexOf("/") + 1) ||
	locate.indexOf("/index.") != -1 ){
	isIndex = true;
}

String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g, "");
}

function getCookie(name){
var cname = name + "=";
var dc = document.cookie;
if (dc.length != 0) 
{
	begin = dc.indexOf(cname);
	if(begin != -1) 
	{
		begin += cname.length;
		end = dc.indexOf(";", begin);
		if(end == -1) end = dc.length;
		return unescape(dc.substring(begin, end));
	}
}
return null;
}

function setCookie(name, value, expires) {
	document.cookie = name + "=" + escape(value) + ((expires == null) ? "" : "; expires=" + expires.toGMTString()) + "; path=/";
}

function setActiveStyleSheet(title, reset) {
  var i, a, main, j;
  j=0
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
			a.disabled = true;
			j++;
			if (title == null && j==1) a.disabled = false;
      if(a.getAttribute("title") == title) {
				a.disabled = false;
			}
		}
	}
  //document.all.HideTextSize.value = title;
	//alert(title);
  if (reset == 1) {
		//alert("W->" + title);
		setCookie("wstyle", title , null);
	
	  //createCookie("wstyle", title, 365);
  }
}

function fontline(name)
{
var cname1 = name + "=";
var dc1 = document.cookie;
var tt="";
if (dc1.length != 0) 
{
	begin = dc1.indexOf(cname1);
	if(begin != -1) 
	{
		begin += cname1.length;
		end = dc1.indexOf(";", begin);
		if(end == -1) end = dc1.length;
		
	   tt=unescape(dc1.substring(begin, end));
	}
}


 if (tt=='') tt='SmallText';
 	var fss = document.getElementById("fss");
	var fsm = document.getElementById("fsm");
	var fsl = document.getElementById("fsl");
       fss.src='images/font_s.gif';
       fsm.src='images/font_m.gif';
       fsl.src='images/font_l.gif';

       if (tt=='SmallText')
           fss.src='images/font_s_on.gif';
		if (tt=='MediumText')
           fsm.src='images/font_m_on.gif';
		if (tt=='LargeText')
           fsl.src='images/font_l_on.gif';
		   
    
return null;

}



function Sfontline(name,t1)
{
var cname1 = name + "=";
var dc1 = document.cookie;
var tt="";
var tt1=t1;
if (dc1.length != 0) 
{
	begin = dc1.indexOf(cname1);
	if(begin != -1) 
	{
		begin += cname1.length;
		end = dc1.indexOf(";", begin);
		if(end == -1) end = dc1.length;
		
	   tt=unescape(dc1.substring(begin, end));
	}
}
//alert(tt);
if  (tt1=='小')
{       if (tt=='SmallText')
             return '<u>小</u>';
	else
	     return '小';
}

if  (tt1=='中')
{       if (tt=='MediumText')
	     return '<u>中</u>';
	else
	     return '中';
}

if  (tt1=='大')
{       if (tt=='LargeText')
	     return '<u>大</u>';
	else
	     return '大';
}
    
return null;

}

function goNext(){
	$('#inn1').css("display" , "none");
	$('#inn2').css("display" , "block");
}
function goPre(){
	$('#inn1').css("display" , "block");
	$('#inn2').css("display" , "none");
}
// JavaScript Document
var rNum; 
rNum = Math.random();
rNum = Math.round(rNum*2);

function showImgArea(randNum){
	if(randNum != 0 ){
		document.getElementById("img_area0").style.display = "none";
		document.getElementById("img_area"+randNum).style.display = "block";
	}
}

function showStandingBar(sid){
	if(sid == 1){
		document.getElementById("standingBar2").style.display = "none";
		document.getElementById("standingBar1").style.display = "block";
	}else{
		document.getElementById("standingBar1").style.display = "none";
		document.getElementById("standingBar2").style.display = "block";
	}
}

function hideThis(divId){
	document.getElementById("columnText"+divId).style.display = "none";
	document.getElementById("columnHide"+divId).style.display = "block";
}

function showThis(divId){
	for(var i =0 ; i <3 ; i ++){
		if(divId == i){
			document.getElementById("columnText"+i).style.display = "block";
			document.getElementById("columnHide"+i).style.display = "none";
		}else{
			hideThis(i);
		}
	}
}

function goQuery(){
	document.getElementById('queryForm').submit();
}

function goStanding(){
	var standOpt = document.getElementById('season_standing');
	var standPage = "standing.html";
	for(var z =0 ; z < standOpt.options.length ; z ++){
		if(standOpt.options[z].selected){
			standPage = standOpt.options[z].value;
		}
	}
	location.href = standPage;
}
function rand(min, max){
 return max ? min + rand(max - min) : Math.random() * ++min << .5;
};

function getVote(){
	$.ajax({url:'/pbl/rss/vote.jsp?s=2', 
		   dataType:'xml',
		   success: function(xml){
			var opt = '';
			var count = $(xml).find("quest").length;
			if(count > 0){
				var rndIndex = rand(1,count);
				var quest = $(xml).find("quest")[rndIndex-1];
				$('#s').val($(quest).attr("sn"));
				$('#voteQuest').text($(quest).attr("title"));
				$('#voteDuration').text($(quest).attr("duration"));
					
					$(quest).find("options").each(function(){
						opt += '<tr>';
						opt += '<td width="1%" valign="top" class="gray13c"><input name="vote_option" id="vote_option" type="radio" value="' + $(this).attr("sn") +  '" /></td>';
						opt += '<td align="left" class="gray13c">'+$(this).attr("title")+'</td>';
						opt += '</tr>';
					});
					$('#voteOption').append(opt);
					$('#voteArea').css("display","block");
			}
			}
			});
}

function send_vote(){
	var sid = document.getElementById("s").value;
	var opt = document.getElementsByName("vote_option");
	var voted = false;
	var poststr = "";
	
	for(var i = 0 ; i < opt.length ; i ++){
		if(opt[i].checked){
			poststr = "o="+opt[i].value;
			voted=true;
		}
	}
	poststr += "&s="+sid;
	if(voted){
		var httpRequest = reqType();
		httpRequest.onreadystatechange = processRequest;
		httpRequest.open('POST', '/go_vote.jsp', false);
		httpRequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
		httpRequest.send(poststr);
function processRequest() {
	if (httpRequest.readyState == 4) {
		if (httpRequest.status == 200) {
			return true;
		} else {
			return false
		}
	}
}
		var response = httpRequest.responseText;
		if(response.trim() == "success"){
			//alert("您神聖的一票已送出\n感謝您~");
			view_result(sid);
		}else{
			alert(response);
		}
	}else{
		alert("尚未選取選項,\n請返回重新選擇!!");
	}
}

function processRequest() {
	if (httpRequest.readyState == 4) {
		if (httpRequest.status == 200) {
			return true;
		} else {
			return false
		}
	}
}

function view_result(sid){
	
	var winPop = window.open( ((isIndex)?'k.html?nextpage=':'') +"/vote.jsp?s=" + sid , "vote_result" , "width=720 ,  height=470 , top= 100 , left=100 , toolbar=0, menubar=0, scrollbars=0, resizable=0,location=0, status=0");
	
	winPop.focus();
}

function changeTeam(tid){
	if(tid != ""){
		location.href="?s="+tid;
	}
}

function getTxtLiveSchedule(){
	$.ajax({ url: 'xml/txtLiveGame.xml',
		   dataType:'xml',
		   success: function(xml){
		   var count = $(xml).find("info").length;
		   if(count >0){
			    var i = 0;
		   		var append = "";
				$(xml).find("info").each(function(){
						if(i%2 == 0){
							append += "<tr";
							if(i%4==0){
								append += " class=\"td2\">";
							}else{
								append += ">";
							}
						}
						append+= "<td width=\"50%\"";
						if(i%2==0){
							append+= " class=\"div_m\"";
						}
						append+="><a href=\"" + $(this).attr("file") +"\" class=\"txt_blue\">" + $(this).attr("title") +"</a></td>";		
						if(i%2 == 1){
							append += "</tr>";
						}

						i++;
				});
				if(count%2==1){
					append += "<td width=\"50%\">&nbsp;</td></tr>";
				}
				$('#listTb').append(append);
				$('#textLiveBar').css("display","block");
		   }
			
		   }
		   
	});
}

var hideRows = true;
function showHide(){
	if(hideRows){
		$('#tbData tr:gt(4)').css("display" , "");
		$('#hideControl').attr("src" , "images/btn_moreclose.gif");
		hideRows = false;
	}else{
		$('#tbData tr:gt(4)').css("display" , "none");
		$('#hideControl').attr("src" , "images/btn_moreopen.gif");
		hideRows = true;
	}
}

var arr_currentLayer = new Array("guest1","hdn1","cell1");
//用來紀錄作用中頁籤的頁面ID 預設是第一個頁面
var arr_currentDiv = new Array("g1","hdna","d1");
//showLayer()函式，參數為觸發該函式的元素及所屬頁面ID
	
function arr_showLayer(obj,div,arr_id,cs_name){
	//若目前的currentLayer不等於obj.id 表示要換頁面
	if(arr_currentLayer[arr_id]!=obj.id) {
		//先改變上一個頁籤及頁面的樣式
		arr_showIt(arr_currentLayer[arr_id],arr_currentDiv[arr_id],false,cs_name);
		//重設currentLayer及currentDiv的值
		arr_currentLayer[arr_id] = obj.id;
		arr_currentDiv[arr_id] = div;
		//最後再改變目前頁籤及頁面的樣式
		arr_showIt(arr_currentLayer[arr_id],arr_currentDiv[arr_id],true,cs_name);
	}
}
//showIt()函式，參數為currentLayer、currentDiv及模式
//mode為true時 則表示目前作用中 所以要顯示
//mode為false時 則表示目前非作用中 所以要隱藏
function arr_showIt(o,d,mode,cs_name){
	var obj = document.getElementById(o);
	var div = document.getElementById(d);
	//改變className則表示改變該元素所用的class
	obj.className = (mode?"":"in")+cs_name;
	div.style.display = mode?"block":"none";
}
