/* Adapted from http://www.phpied.com/json-javascript-cookies/ */

// Read a cookie with the given name's value. Returns null if no such named cookie can be found.
function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

// Sets a globally available sidebar_prefs variable that loads and saves JSON to a cookie.
var sidebar_prefs = {

    // default the following blocks to open:
    data: {
      actionsblock : true,
      infoblock : true,
      mediablock : true,
      categoriesblock : true
    },

    // Look for the 'sidebar_prefs' function, read it into the data hash and return
    load: function () {
      cookie_data = JSON.parse(unescape(readCookie('sidebar_prefs')));
      if (cookie_data)
      { 
        this.data = cookie_data; 
      }
      return this.data;
    },

    // Save the data hash a JSON string in the 'sidebar_prefs' cookie.
    save: function (expires, path) {
        var d = expires || new Date(2020, 02, 02);
        var p = path || '/';
        document.cookie = 'sidebar_prefs=' + escape(JSON.stringify(this.data))
                          + ';path=' + p
                          + ';expires=' + d.toUTCString();
    },

    // Deletes the cookie by setting a date in the past on it.
    del: function () {
        this.save(new Date(2000,01,01));
    }

}
sidebar_prefs.load();
