﻿if (typeof(FOH) == "undefined") FOH = {};





FOH.Cookies = Class.create();
FOH.Cookies.prototype = {
	data: [],

	initialize: function() {
		this.read();
	},
	
	resetData: function() {
		this.data = this.data.uniq();
		this.string = this.data.join(',');
	},

	getCookieName: function() {
		var path = document.location.toString();
		path = path.replace(/http:\/\/[^\/]*/, '');
		path = path.replace(/#.*/, '');
		
		return path;
	},

	read: function() {
		var cookies = document.cookie.split(';');
		
		for (var i=0; i<cookies.length; i++) {
			var cookie = cookies[i];

			if (cookie.match(this.getCookieName())) {
				var seen = cookie.substring(cookie.indexOf('=')+1, cookie.length).split(',');
				for (var j=0; j<seen.length; j++) {
					if (seen[j] != '') this.data.push(seen[j]);
				}

				this.resetData();
				return;
			}
		}
	},
	
	set: function(id) {
		// set cookie to expire in 1 week
		var date = new Date();
		date.setDate(date.getDate()+7);
		
		var path = this.getCookieName();

		// comma delimited to sort later
		this.string = this.data.join(',');
		if (this.string.match(id) == null) {
			this.string = (this.string != '') ? this.string+','+id : id;
		}

		// set the cookie
		document.cookie = path+'='+this.string+'; expires='+date.toGMTString()+'; path='+path+'; domain=apple.com';

		// remember we added it		
		this.data.push(id);
		this.resetData();
	},
	
	isSet: function(id) {
		if (!this.string) this.resetData();
		return this.string.match(id);
	}

};
