CantRemember = {
	Index: {
		_getDone: function() {
			var root = this._cookies.get('root') || {};
			return root.index_done || false;
		},
		_setDone: function(b) {
			//	for posterity
			var root = this._cookies.get('root') || {};
			root.index_done = b;
			this._cookies.put('root', root);
		},
		_to: null,

		//	http://www.lalit.org/lab/jsoncookies
		//	once a day
		_cookies: new CookieJar({
			expires: 86400,
			path: '/'
		}),
		
		show: function(node, b) {
			if (! node) { return; }
			if (arguments.length < 2) { b = true; }
			//	better for cascading than node.show()
			Element.setStyle(node, { 'display': (b ? 'block' : 'none') });
		},
		title: function(s) {
			document.title = s;
		},
		
		anim: function() {
			var _this = this;
			
			var seq = [];
			var nextIn = function(sec) {
				if (_this._getDone()) { return; }
				_this._to = seq.shift().delay(sec);
			}
			var title = this.title;
			var show = this.show;
	
			seq.push(function() {
				title('cant');
				show($('f_cant'));
				nextIn(0.5);
			});
			seq.push(function() {
				title('cant re');
				show($('f_re'));
				nextIn(0.3);
			});
			seq.push(function() {
				title('cant remem');
				show($('f_mem'));
				nextIn(0.5);
			});
			seq.push(function() {
				title('cant remember');
				show($('f_ber'));
				nextIn(0.6);
			});
			seq.push(function() {
				title('cant remember .');
				show($('f_dot'));
				nextIn(1.0);
			});
			seq.push(function() {
				show($('f_edu'));
				nextIn(1.0);
			});
			seq.push(function() {
				title('cant remember .');
				show($('f_edu'), false);
				show($('f_edu_'));
				nextIn(0.4);
			});
			seq.push(function() {
				show($('f_edu_'), false);
				show($('f_mil'));
				nextIn(1.0);
			});
			seq.push(function() {
				show($('f_mil'), false);
				show($('f_mil_'));
				nextIn(0.8);
			});
			seq.push(function() {
				title('cant remember . com');
				show($('f_mil_'), false);
				show($('f_com'));
				nextIn(0.6);
			});
			seq.push(function() {
				_this.finish();
			});
			
			title('umm ...');
			show($('frames'));
			nextIn(0.5);
		},

		finish: function() {
			var _this = this;
			
			this._setDone(true);
			if (this._to) { window.clearTimeout(this._to); }
			
			['edu','mil'].each(function(n) {
				_this.show($('f_' + n), false);
				_this.show($('f_' + n + '_'), false);
			});
			['cant','re','mem','ber','dot','com'].each(function(n) {
				var node = $('f_' + n);
				_this.show(node);
				Element.addClassName(node, 'sure');
			});
		},
		reset: function() {
			var _this = this;
			
			$$('div.frame').each(function(node) {
				_this.show(node, false);
				Element.removeClassName(node, 'sure');
			});
		},
		
		init: function() {
			var _this = this;
			
			if (this._getDone()) {
				this.finish();
			}
			else {
				document.observe('dom:loaded', this.anim.bind(this));
			}
			
			var f = function() {
				if (_this._getDone()) {
					_this._setDone(false);
					_this.reset();
					_this.anim();
				}
				else {
					_this.finish();
				}
			};

			$('logo').observe('click', f);
			$$('div.frame').each(function(node) {
				node.observe('click', f);
			});
		}
	}
}

CantRemember.Index.init();
