I figured that, but it wasn't working. My guess is that I've not changed the right thing.. I've never worked with cookies before so I have no idea how they work really.
I might as well attach the two parts of the script. Do I need to also make a new copy of the cookie.js?
Protoswitcher.js
Code:
var ProtoSwitcher = Class.create({
initialize: function(container, styles, options) {
this.container = $(container);
this.styles = $A(styles);
this.body = $$('body')[0];
this.options = Object.extend({
label : '',
callback : null
}, options || {});
this.buildSwitcher();
},
buildSwitcher: function() {
this.switcher = new Element('div', { id:'protoswitcher' }).update(this.options.label);
this.styleOptions = new Element('ul', { id:'style-options' });
this.container.insert(this.switcher);
this.switcher.insert(this.styleOptions);
this.temp= [];
this.styles.each(function(style, index) {
this.temp[index] = new Element('li', { id:style }).update(style);
this.styleOptions.insert(this.temp[index]);
}.bind(this));
this.loadPreferences();
this.setPreferences();
},
loadPreferences: function() {
var cookie = Cookie.get('protoswitcher');
if (cookie=='' || cookie==null) {
cookie = this.styles[0];
}
this.body.className = cookie;
this.currentStyle = cookie;
},
setPreferences: function () {
this.temp.each(function(element) {
element.observe('click', function(event) {
var newStyle = element.readAttribute('id');
if (newStyle!= this.currentStyle) {
this.replaceClass(this.body, this.currentStyle, newStyle);
Cookie.set('protoswitcher', newStyle, 365);
this.currentStyle = newStyle;
}
Event.stop(event);
}.bind(this));
}.bind(this));
},
replaceClass: function (elem, old_class, new_class) {
$(elem).addClassName(new_class);
$(elem).removeClassName(old_class);
}
});
And the cookie.js
Code:
var Cookie = {
set: function(name, value, daysToExpire) {
var expire = '';
if (daysToExpire != undefined) {
var d = new Date();
d.setTime(d.getTime() + (86400000 * parseFloat(daysToExpire)));
expire = '; expires=' + d.toGMTString();
}
return (document.cookie = escape(name) + '=' + escape(value || '') + expire + '; path=/');
},
get: function(name) {
var cookie = document.cookie.match(new RegExp('(^|;)\\s*' + escape(name) + '=([^;\\s]*)'));
return (cookie ? unescape(cookie[2]) : null);
},
// get: function(key) {
// var keyEquals = key+"=";
// var value = false;
// document.cookie.split(';').invoke('strip').each(function(s){
// if (s.startsWith(keyEquals)) {
// value = unescape(s.substring(keyEquals.length, s.length));
// throw $break;
// }
// });
// return value;
// },
erase: function(name) {
var cookie = Cookie.get(name) || true;
Cookie.set(name, '', -1);
return cookie;
},
accept: function() {
if (typeof navigator.cookieEnabled == 'boolean') {
return navigator.cookieEnabled;
}
Cookie.set('_test', '1');
return (Cookie.erase('_test') === '1');
}
};
Basically I want to have two switchers. One for some colours, and one for the position of a userbar. That's the goal, anyway
.
Bookmarks