var TT = {
	global : null,
	form : 'applicationForm',
	init : function () {
		TT.global = (function () {
			return this;
		})();
		var form = TT.form = document.forms[TT.form];
		if (form) {
			jQuery('#footnotes').hide();
			jQuery(form).addClass('jsform');
			TT.tooltip.init();
			TT.step.init();
			TT.defaultValue.init();
		}
	},
	defaultValue : {
		init : function () {
			var inp = TT.form.getElementsByTagName('INPUT'),
				l = inp.length,
				el;
			while (l--) {
				el = inp[l];
				if (el.title) {
					el.value = el.title;
					el.onfocus = TT.defaultValue.focus;
					el.onblur  = TT.defaultValue.blur;
				}
				el = null;
			}
		},
		focus : function () {
			if (this.value === this.title) {
				this.value = '';
			}
		},
		blur : function () {
			if (!this.value) {
				this.value = this.title;
			}
		}
	},
	step : {
		current : null,
		cache : [],
		count : null,
		init : function () {
			var form = TT.form;
			var jForm = jQuery(form);
			var steps = jForm.children('div.step'),
				l = TT.step.count = steps.length;
			var id;
			if (l > 1) {
				var current, el;
				TT.step.current = 0;
				for (var i = 0; i < l; i++) {
					current = steps[i];
					id = current.id;
					el = jQuery(current).next();
					if ((i + 1) < l) {
						var button = document.createElement('INPUT');
						button.type = 'button';
						el.append(button);
						button.value = 'stap ' + (i + 2);
						button.className = 'nextstep';
					}
					TT.step.cache.push({
						id : id,
						trigger : current,
						element : el,
						valid : false
					});
					if (i > 0) {
						el.hide();
					}
				}
				jForm.click(TT.step.delegate);
				jForm.submit(
					function () {
						return TT.validate.fragment() ? true : false;
					}
				);
			}
		},
		delegate : function (e) {
			var target = e.target,
				step = TT.step,
				cache = step.cache,
				current = step.current,
				valid,
				obj;
			if ('nextstep' === target.className) {
				valid =	TT.validate.fragment(),
				obj = cache[current];
				if (valid) {
					obj.valid = true;
					jQuery(obj.trigger).addClass('expand');
					step.expand();
				} else {
					obj.valid = false;
					jQuery(obj.trigger).removeClass('expand');
				}
			} else {
				var parent = target.parentNode,
					id = target.parentNode.id;
				if (id && ('step' === id.substring(0, 4))) {
					// valid =	TT.validate.fragment();
					valid = true;
					if (valid) {
						var l = cache.length;
						while (l--) {
							obj = cache[l];
							if ((obj.id === id) && (l !== current) && obj.valid) {
								TT.step.expand(l);
								var i;
								for (i = l + 1; i < cache.length; i++) {
									obj = cache[i];
									obj.valid = false;
									jQuery(obj.trigger).removeClass('expand');
								}
								break;
							}
						}
					}
				}
			}
		},
		expand : function (i) {
			jQuery(TT.step.cache[TT.step.current].element).slideUp(
				200,
				function () {
					TT.step.current = (('undefined' !== typeof i) ? i : (TT.step.current + 1));
					jQuery(TT.step.cache[TT.step.current].element).slideDown(200);
				}
			);
		}
	},
	tooltip : {
		container : null,
		text : null,
		triggers : null,
		footnotes : {},
		init : function () {
			var footnotes = document.getElementById('footnotes');
			if (footnotes) {
				footnotes = footnotes.getElementsByTagName('LI');
				var i, l, item, id;
				for (i = 0, l = footnotes.length; i < l; i++) {
					item = footnotes[i];
					id = item.id;
					if (id) {
						TT.tooltip.footnotes[id] = item.innerHTML;
					}
				}
				var form = TT.form;
				var triggers = jQuery(form).find('img.infotooltip');
				for (i = 0, l = triggers.length; i < l; i++) {
					item = jQuery(triggers[i]);
					item.mouseover(TT.tooltip.show);
					item.mouseout(TT.tooltip.hide);
					item.parent().focus(TT.tooltip.show);
					item.parent().blur(TT.tooltip.hide);
				}
				var container = document.createElement('DIV');
				var tooltipsDiv = document.getElementById("tooltips")
				tooltipsDiv.appendChild(container);
				container.className = 'tooltip';
				var arrow = document.createElement('DIV');
				container.appendChild(arrow);
				arrow.className = 'arrow';
				var text = document.createElement('DIV');
				container.appendChild(text);
				text.className = 'text';
				TT.tooltip.container = jQuery(container);
				TT.tooltip.text = jQuery(text);
				TT.tooltip.arrow = jQuery(arrow);
			}
		},
		show : function (e) {
			var a = ('a' === this.nodeName.toLowerCase());
			var href = (a && this.href) || this.parentNode.href,
				tooltip,
				text,
				offset,
				jThis;
			if (href && (href = href.split('#')[1]) && (href = TT.tooltip.footnotes[href])) {
				tooltip = TT.tooltip.container;
				text = TT.tooltip.text;
				text.html(href);
				offset = jQuery(this).offset();
				tooltip.css({
					'top' : (offset.top - ((tooltip.height() / 2) - (this.offsetHeight / 2))) + 'px',
					'left' : (offset.left + this.offsetWidth + 5) + 'px'
				});
				tooltip.fadeIn(300, function() {
					var height = TT.tooltip.text[0].offsetHeight;
					var arrow = TT.tooltip.arrow;
					arrow[0].style.height = height + 'px';
				});
			}
		},
		hide : function (e) {
			var a = ('a' === this.nodeName.toLowerCase());
			var href = (a && this.href) || this.parentNode.href,
				tooltip;
			if (href && (href = href.split('#')[1]) && (href = TT.tooltip.footnotes[href])) {
				tooltip = TT.tooltip.container;
				tooltip.fadeOut(100);
			}
		}
	},
	validate : {
		fields : function (el) {
			var current = TT.step.cache[TT.step.current].element;
			var inputs =  current.find('INPUT');
			var selects = current.find('SELECT');
			var objects = [];
			var i, l, item;
			for (i = 0, l = inputs.length; i < l; i++) {
				item = inputs[i];
				if (item.type == 'checkbox') {
					if (item.checked) {
						objects.push(item);
					}
				} else {
					objects.push(item);
				}
			}
			for (i = 0, l = selects.length; i < l; i++) {
				objects.push(selects[i]);
			}
			return objects;
		},
		fragment : function() {
			TT.global.errorList = '';
			var objects = TT.validate.fields();
			for (var i = 0, l = objects.length, obj; i < l; i++) {
				obj = objects[i];
				var validationMethod = obj.getAttribute('validate');
				if (validationMethod) {
					validationMethod = validationMethod.replace(/this/, 'obj');
					var validationResult = TT.global.eval(validationMethod);
				}
				var required = obj.getAttribute('required');
				if (required === 'true') {
					validateRequired(obj);
				}
			}
			// errorList is global!
			var errors = TT.global.errorList;
			if (errors) {
				alert(errors);
				return false;
			} else {
				return true;
			}
		}
	}
};
