/*--
author: PIZn
contact:www.pizn.net
date:2010.10.8
update:2010.10.9 change the cookie code.
Allright by PIZn.
--*/
/*--
onload function
--*/
function addLoadEvent(func){
		var oldonload = window.onload;
		if(typeof window.onload != 'function'){
			window.onload =func;
			}
		else{
			window.onload = function(){
				oldonload();
				func();
				}
			}
		}
		
/*--
catch the event
--*/
function ch(){
	var toolBox = document.getElementById("toolBox");
	var light = toolBox.getElementsByTagName("a");
	var off = light[2];
	var up = light[3];
	off.onclick = function (){
		changeButSty(off,up);
		changeStyleSheet();
		return false;
		}

	up.onclick = function (){
		changeButSty2(off,up);
		changeStyleSheet2();
		return false;
		}
	}	

/*--
change the bottom style
--*/
function changeButSty(obj1,obj2){
	obj1.style.display = "none";
	obj2.style.display = "block";
	var oF=obj1.style.display;
	var oS=obj2.style.display;
	setCookie("off",oF,30);
	setCookie("up",oS,30);
	}

function changeButSty2(obj1,obj2){
	obj1.style.display = "block";
	obj2.style.display = "none";
	var oF=obj1.style.display;
	var oS=obj2.style.display;
	setCookie("off",oF,30);
	setCookie("up",oS,30);
	}

/*--
change the link src to change the blog theme
--*/
function changeStyleSheet2(){
	delCookie("stylesheet",30);
	var styleSheet = document.getElementsByTagName("link")[0];;
	styleSheet.setAttribute("href","http://www.pizn.net/wp-content/themes/violet/style.css");	
	var src = styleSheet.href;	
	setCookie("stylesheet",src,30);
	}
	
function changeStyleSheet(){
	delCookie("stylesheet",30);
	var styleSheet = document.getElementsByTagName("link")[0];
	styleSheet.setAttribute("href","http://www.pizn.net/wp-content/themes/violet/night.css");	
	var src = styleSheet.href;
	setCookie("stylesheet",src,30);	
	}

//Use cookie to remenber the theme style
/*--
get the cookie
--*/
function getCookie(c_name){
	if (document.cookie.length>0)
	  {
	  c_start=document.cookie.indexOf(c_name + "=")
	  if (c_start!=-1)
		{ 
		c_start=c_start + c_name.length+1 
		c_end=document.cookie.indexOf(";",c_start)
		if (c_end==-1) c_end=document.cookie.length
		return unescape(document.cookie.substring(c_start,c_end))
		} 
	  }
	return ""
	}
/*--
set the cookie
--*/
function setCookie(c_name,value,expiredays){
	var exdate=new Date()
	exdate.setDate(exdate.getDate()+expiredays)
	document.cookie=c_name+ "=" +escape(value)+
	((expiredays==null) ? "" : ";expires="+exdate.toGMTString())
	}

/*--
remove the cookie
--*/
function delCookie(c_name,expiredays){
	var exdate = new Date();
	exdate.setDate(exdate.getDate() - expiredays-100);
	var cval = getCookie(c_name);
		if(cval != null){
			document.cookie=c_name+ "=" +
		((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
		}
	}

/*--
check the cookie and change the remenber style theme
--*/
function checkCookie(){
	cookieSrc =getCookie("stylesheet");
	turnOff =getCookie("off");
	turnOn = getCookie("up");
	if (cookieSrc!=null && cookieSrc!="" && turnOff != null && turnOff != "" && turnOn != null && turnOn != ""){
	   var styleSheet = document.getElementsByTagName("link")[0];
	   styleSheet.setAttribute("href",cookieSrc);
	   var toolBox = document.getElementById("toolBox");
	   var light = toolBox.getElementsByTagName("a");
	   light[2].style.display = turnOff;
	   light[3].style.display = turnOn;
  	}
	else {
			
		  var styleSheet = document.getElementsByTagName("link")[0];;
		  cookieSrc = styleSheet.href;
		  var toolBox = document.getElementById("toolBox");
	   	  var light = toolBox.getElementsByTagName("a");
	      turnOff = light[2].style.display;
	      turnOn = light[3].style.display; 
		  if (cookieSrc!=null && cookieSrc!="" && turnOff != null && turnOff != "" && turnOn != null && turnOn != ""){
    			setCookie('stylesheet',cookieSrc,30);
				setCookie("off",turnOff,30);
		  		setCookie("up",turnOn,30);
				
    	  }
  	}
	}
/*--
load the function
--*/
addLoadEvent(ch);
addLoadEvent(checkCookie);
