// Script to set/read cookie to set personal preferences
/* 
DATE		DESCRIPTION
----		-----------
11-29-2003	First iteration; set theme. Notice that the function is called in this page 
				(after the function, so it exists..)
01-18-2004	Added path - "/blogger" so it will set for all underneath it; I wasn't aware of this
			(otherwise, sets for directory only - I'm still not sure what happens if I want to set
			it for directory A and C but not B....)
*/


///////////////////////////////////////
// GENERIC cookie set; only cookieName and cookieValue required non-null)
function setCookie(cookieName, cookieValue, cookiePath, cookieExpires) {
	
	cookieValue = escape(cookieValue); // encodes for spaces and so on
	
	// default date to six months from now
	if (cookieExpires == "") {
		var nowDate = new Date();
		nowDate.setMonth(nowDate.getMonth() + 6);
		cookieExpires = nowDate.toGMTString();
	} // end no expiration 
		
	// make path empty string
	cookiePath = "/blogger";
		
	// set / update cookie
	document.cookie = cookieName + "=" + cookieValue + ";expires=" + cookieExpires + ";Path=" + cookiePath;
	//alert (document.cookie); 
	
	
} // END setCookie()

///////////////////////////////////////////
// GENERIC get cookie value, given the name
// returns value or NULL if no cookie exists

function getCookieValue(cookieName) {
	var cookieValue = document.cookie;
	var cookieStartsAt = cookieValue.indexOf(" " + cookieName + "=");
	
	  if (cookieStartsAt == -1)
	  {
	      cookieStartsAt = cookieValue.indexOf(cookieName + "=");
	  }
	
	   if (cookieStartsAt == -1)
	   {
	      cookieValue = null; // no cookie, set to null so default displays
	   }
	   else
	   {
	      cookieStartsAt = cookieValue.indexOf("=", cookieStartsAt) + 1;
	      var cookieEndsAt = cookieValue.indexOf(";", cookieStartsAt);
	      if (cookieEndsAt == -1)
		      cookieEndsAt = cookieValue.length;
	    	  cookieValue = unescape(cookieValue.substring(cookieStartsAt, cookieEndsAt));
	   }

	   return cookieValue;

} // END getCookieValue()





////////////////////////////////////////////
// Function - onLoad event OR called after user selection - to SET the theme value
// 	 and set properly (may be null onLoad, so set to default)
//	 If passed value is null, also set cookie - DON'T otherwise
// 
function setTheme(themeValue) {
		
	// if theme value is null, use default stylesheet and set cookie
	//	 (mainly for the onload call to this function, for users w/o cookie)
	if (themeValue == null) {
		themeValue = "blog_default"; // set to default style sheet
		// set cookie to store the value
		//setCookie("theme", themeValue, "", "");		
	}
	//alert ("theme value is: " + themeValue);
	
	// call function to set the sheet
	setActiveStyleSheet(themeValue);
	

} // end setTheme

////////////////////////////////////////////
// Function - 
// 	 Set/update cookie and call setActiveStyleSheet() 
//		to apply user sheet choice
// 
function updateTheme(updateValue) {
	
	// call function to set active sheet
	setActiveStyleSheet(updateValue);
	
	// set cookie to store this puppy
	setCookie("theme",updateValue,"","");


} // end updateTheme


// STOLEN FROM http://www.alistapart.com/articles/alternate/  -- Thanks Jeff, saved me time
function setActiveStyleSheet(title) {
   var i, a, main;
   for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
     if(a.getAttribute("rel").indexOf("style") != -1
        && a.getAttribute("title")) {
       a.disabled = true;
       if(a.getAttribute("title") == title) a.disabled = false;
     }
   }
} // END setActiveStyleSheet - to pull in correct style sheet

////////////////////////////////////////
// ONLOAD call to get cookie value for theme
// 		(could be null...)
setTheme(getCookieValue('theme'));