/* session.js

Enthaelt Funktionen fuer die Sitzung

*/

var Merker = new Class({
	options: {
	},

	initialize: function() {
		this.data = new Array();
	},

	merke: function(id) {
		this.data.merge([id]);
		return this.data.length;
	},

	unmerke: function(id) {
		if (this.data.contains(id)) {
			this.data.remove(id);
		}
		return this.data.length;
	},

	toggle: function(id) {
		if (this.data.contains(id)) {
			this.data.remove(id);
		} else {
			this.data.merge([id]);
		}
		return this.data.length;
	},

	length: function() {
		return this.data.length;
	},

	serialize: function() {
		return this.data.join(':');
	},

	unserialize: function(text) {
		this.data = text.split(':');
	}
});

Merker.implement(new Options);


var Session = new Class({
	options: {
	},

	initialize: function() {
		this.hemdstoffMerker = new Merker;
		this.konfektionsstoffMerker = new Merker;
	},

	speichere: function() {
		Cookie.set('hsm', this.hemdstoffMerker.serialize());
		Cookie.set('ksm', this.konfektionsstoffMerker.serialize());
	},

	lade: function() {
		if (Cookie.get('hsm')) {
			this.hemdstoffMerker.unserialize(Cookie.get('hsm'));
		}
		if (Cookie.get('ksm')) {
			this.konfektionsstoffMerker.unserialize(Cookie.get('ksm'));
		}
	},
	
	length: function() {
		return this.hemdstoffMerker.data.length + this.konfektionsstoffMerker.data.length;
	},

	serialize: function() {
		var rueckgabe = '';

		if (this.hemdstoffMerker.data.length > 0) {
			rueckgabe = 'h' + this.hemdstoffMerker.data.join(':h');
		}

		if (this.konfektionsstoffMerker.data.length > 0) {
			if (rueckgabe.length > 0) {
				rueckgabe = rueckgabe + ':k' + this.konfektionsstoffMerker.data.join(':k');
			} else {
				rueckgabe = 'k' + this.konfektionsstoffMerker.data.join(':k');
			}
		}

		return rueckgabe;
	}
});

Session.implement(new Options);
