// open, close and align customize menu
var opencustom = 0;

function getobject(obj) {
  if (document.getElementById)
    return document.getElementById(obj)
  else if (document.all)
    return document.all[obj]
}

function getposOffset(what, offsettype) {
  var obj_id = document.getElementById(what)
  var totaloffset=(offsettype=="left")? obj_id.offsetLeft : obj_id.offsetTop;
  var parentEl=obj_id.offsetParent;

  while (parentEl!=null) {
    totaloffset=(offsettype=="left")? totaloffset+parentEl.offsetLeft : totaloffset+parentEl.offsetTop;
    parentEl=parentEl.offsetParent;
  }
  return totaloffset;
}

function pullcustom(obj) {
//  if (document.images['custom'].onclick) {
//  if (document.getElementById('custom').onclick) {
//    if (opencustom) {
//      hidecustom(obj)
//      opencustom = 0
//    } else {
      showcustom(obj)
//      opencustom = 1;
//    }
//  }
  ChkLastSetting();
}

function chkbrowser(obj) {
  if (document.all)
    showcustom(obj)
  else if (document.getElementById) 
    showcustom(obj)
}

function showcustom(obj) {
  aligncustom(obj)
  document.getElementById('custom_panel').style.visibility="visible";
}

function hidecustom(obj) {
  document.getElementById('custom_panel').style.visibility="hidden";
  opencustom = 0;
}

function aligncustom(obj) {
  var cust, alignX

  cust = getobject("custom_panel")
  cust.x = getposOffset(obj, "left")
  cust.y = getposOffset(obj, "top")

//  cust.style.left = cust.x + 5 + "px"
  if (navigator.appName!="Netscape")
	alignX = 473
  else
	alignX = 540

  if (document.body.clientWidth > 955)
    cust.style.left = Math.round((document.body.clientWidth - 955) / 2) + (alignX) + "px"
  else
    cust.style.left = alignX + "px"
  cust.style.top = cust.y + 13 + "px"
}

// Customize menu control
//var FontSizeArray = new Array("Small","Medium","Large");
var FontSizeArray = new Array("Small","Medium");
var ThemesArray = new Array("Blue","Yellow","Green","Purple","Grey");
var FontSize,Themes
var FontSize_tagID, FontSize_CSS;
var Themes_tagID, Themes_CSS;

function getDefault(id){
  switch(id){
	case "FontSize" :
	  return 0;
	case "Themes" :
	  return 0;
  }
}

function page_load(){
  if(GetCookie('FontSize')!=null) {
	FontSize_tagID = document.getElementById("FontSize")
	FontSize_CSS = "../stylesheet/format" +FontSizeArray[GetCookie('FontSize')] + ".css"
	if (FontSize_tagID)
	  FontSize_tagID.setAttribute("href", FontSize_CSS)
  }
//  if(GetCookie('Themes')!=null) {
//	Themes_tagID = document.getElementById("Themes")
//	Themes_CSS = "../stylesheet/Themes" +ThemesArray[GetCookie('Themes')] + ".css"
//	if (Themes_tagID)
//	  Themes_tagID.setAttribute("href", Themes_CSS)
//  }
}

page_load();

function ChkLastSetting() {
//  if(GetCookie('FontSize')!=null) {
//	FontSize = GetCookie('FontSize');
//  } else {
//	FontSize = getDefault("FontSize");
//  }
  if(GetCookie('Themes')!=null) {
	Themes = GetCookie('Themes');
  } else {
	Themes = getDefault("Themes");
  }
  DisplaySetting();
}

function DisplaySetting(){
//  for(i=0;i<FontSizeArray.length;i++){
//	if(i==FontSize)
//	  document.getElementById("Img_Font_" + FontSizeArray[i]).src="../images/"+FontSizeArray[i]+"_on.gif";
//	else
//	  document.getElementById("Img_Font_" + FontSizeArray[i]).src="../images/"+FontSizeArray[i]+"_off.gif";
//  }	
  for(i=0;i<ThemesArray.length;i++){
	if(i==Themes)
	  document.getElementById("Img_Themes_" + ThemesArray[i]).src="../images/"+ThemesArray[i]+"_on.gif";
	else
	  document.getElementById("Img_Themes_" + ThemesArray[i]).src="../images/"+ThemesArray[i]+"_off.gif";
  }
}

function SetCSS(id,name){
  if(id=="FontSize"){
	FontSize = name;
	for(var i=0;i<FontSizeArray.length;i++){
	  if(i==name)
		document.getElementById("Img_Font_" + FontSizeArray[i]).src="../images/"+FontSizeArray[i]+"_on.gif";
	  else
		document.getElementById("Img_Font_" + FontSizeArray[i]).src="../images/"+FontSizeArray[i]+"_off.gif";
	}
  }
	
  if(id=="Themes"){        
	Themes = name;
	for(var i=0;i<ThemesArray.length;i++){
	  if(i==name)
		document.getElementById("Img_Themes_" + ThemesArray[i]).src="../images/"+ThemesArray[i]+"_on.gif";
	  else
		document.getElementById("Img_Themes_" + ThemesArray[i]).src="../images/"+ThemesArray[i]+"_off.gif";
	}
  }
  
  SaveAppearance();
}

function SaveAppearance(){
//    if(!CookieEnable()){   
//        alert("对不起，您的浏览器的Cookie功能被禁用，请开启!");
//        return;
//    }
  SetCookie ("FontSize", FontSize);
  SetCookie ("Themes", Themes);
  document.getElementById("FontSize").href="../stylesheet/format" +FontSizeArray[FontSize] + ".css";
//	document.getElementById("Themes").href="../stylesheet/Themes" +ThemesArray[Themes] + ".css";
  ChkLastSetting();
}

function CancelAppearance(){
}

function ResetAppearance(){
  FontSize = getDefault("FontSize"); 
  Themes = getDefault("Themes");
}

function SetCookie(name,value)//两个参数，一个是cookie的名子，一个是值
{
  var Days = 365; //此 cookie 将被保存 365 天
  var exp  = new Date();    //new Date("December 31, 9998");
  exp.setTime(exp.getTime() + Days*24*60*60*1000);
  document.cookie = name + "="+ escape (value) + ";path=/;expires=" + exp.toGMTString();
}
function GetCookie(name)//取cookies函数        
{
  var arr = document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"));
  if(arr != null) return unescape(arr[2]); return null;
}
function DelCookie(name)//删除cookie
{
  var exp = new Date();
  exp.setTime(exp.getTime() - 1);
  var cval=GetCookie(name);
  if(cval!=null) document.cookie= name + "="+cval+";path=/;expires="+exp.toGMTString();
}

function CookieEnable()   {   
  var result=false;   
  if(navigator.cookiesEnabled)  return true;  
    
  SetCookie("CookieEnable","true");
  if(GetCookie("CookieEnable")!=null)
	result=true; 
  DelCookie("CookieEnable");      
  return result;
}

