function persistPageviewInCookie(siteId, locale, pageId, config) {
	//if(typeof(console) !== 'undefined' && console != null) { console.log('[persistPageviewInCookie] config: ' + config); }

	var numEntries = 330; // default number of page ids to store
	
	var hash = null;
	if(config) {
		var configParams = config.split('|');
		if(configParams) {
			hash = {};
			configParams.each(function(item) {
				var splitted = item.split(':');
				if(splitted && splitted[0] != 'undefined' && splitted[1] != 'undefined') {
					var key = splitted[0].strip();
					var val = splitted[1].strip();
					hash[key]=val;
					if(key == 'number') { numEntries = val; }
				}
			});
		}		
	}
	
	var cookie = new Cookie(siteId+'#'+locale+'#pageviews', hash);
	var val = cookie.getValue();
	if(val == null) { val = ''; }
	if(val.indexOf('p'+pageId+'#' >= 0)) {
		val = val.replace('p'+pageId+'#', '');
	}
	val = 'p'+pageId+'#'+val;
	if(((val.length)/6)>numEntries) {
		val = val.substring(0, val.length-1);
		val = val.substring(0, val.lastIndexOf('#')+1);
	}
	cookie.setValue(val, false);
}

var Cookie = Class.create({

	options: {},

	initialize: function(name, options) {
		this.name = name;
		this.options = {
			expires: '',	// in hours
			path: '/',		// cookie path
			domain: '',		// cookie domain
			secure: ''		// secure ?
		};	
		Cookie.options = Object.extend(this.options, options || {});
		if (this.options.expires != '') {
			var date = new Date();
			date = new Date(date.getTime() + (this.options.expires * 1000 * 3600));
			this.options.expires = '; expires=' + date.toGMTString();
		}
		if (this.options.path != '') {
			this.options.path = '; path=' + escape(this.options.path);
		}
		if (this.options.domain != '') {
			this.options.domain = '; domain=' + escape(this.options.domain);
		}
		if (this.options.secure == 'secure') {
			this.options.secure = '; secure';
		} else {
			this.options.secure = '';
		}
	},

	setValue: function(value, doEscape) {
		var cookie_str = this.name + "=" + (doEscape ? escape(value) : value);
		try {
			document.cookie = cookie_str + this.getOptions();
		} catch (e) {
			return false;
		}
		return true;
	},

	getValue: function() {
		var cookies = document.cookie.match(this.name + '=(.*?)(;|$)');
		if (cookies) {
			return (unescape(cookies[1]));
		} else {
			return null;
		}		
	},
	
	getOptions: function() {
        return (this.options.expires ? this.options.expires : '') + (this.options.path ? this.options.path : '') + (this.options.domain ? this.options.domain : '') + (this.options.secure ? this.options.secure : '');
    }
	
});
