window.addEvent('domready', function() {
	Semantic.init();
	formHelp.init();

	//Debug.init();
	Form.init();
});

/* some kind od static tooltip attached to a form elemtn */
var formHelp = {
	container: null,
	body: null,

	init: function() {
		this.container = new Element('div', {
			'class': 'formHelp',
			'id': 'formHelpContainer',
			'styles': {
				'display': 'none',
				'position': 'absolute'
			}
		});

		this.body = new Element('span');
		this.container.adopt(this.body);
		$(document.body).adopt(this.container);
	},
	show: function(msg, x, y) {
		if (!this.container) {return;}
		this.body.set('html', msg);
		this.container.setStyles({
			display: 'block',
			left: x+'px',
			top: y+'px'
		});
	},
	hide: function() {
		if (!this.container) { return; }
		this.container.setStyles({display: 'none'});
	}
}

/*
 * uses extended xhtml attributes and classes for extra semantic
 * currently implemented:
 * <blockquote cite="source" /> -> onClick takes you to the quotation source
 * <a class="confirm" title="question" /> -> pops a confirmation wether to follow the link
 * <form class="confirm" /> -> same as above, but for forms
 * <select class="quickjump" /> -> sets selected value as new url by onChange event
 * <select class="quickjump submit" /> -> same as above, but posts the enclosing form
 */
Semantic = {
	init: function() {
		this.blockquotes();
		this.hrefs();
		this.quickjumps();
	},
	blockquotes: function() {
		quotes = $$('blockquote[cite^=http]');
		quotes.setStyle('cursor', 'pointer');
		quotes.addEvent('click', function(e) {
			e.stop();
			if (!confirm("Further information available at:\n" + this.get('cite') + "\n\nTake you there now?"))
				{ return; }
			document.location.href = this.get('cite');
		});
	},
	hrefs: function() {
		$$('a.confirm[title], form.confirm[title]').addEvent('click', function(e) {
			if (!confirm(this.get('title') + "?")) {
				e.stop();
			}
		});
	},
	quickjumps: function() {
		$$('select.quickjump').addEvent('change', function(e) {
			if (this.get('value') == '' || this.get('value') == '#') { return; }
			e.stop();

			if (this.hasClass('form')) {
				this.getParents('form')[0].submit();
			} else {
				document.location.href = this.get('value');
			}
		});
	}
}

/* toggle borders */
Debug = {
	init: function() {
		Event.Keys.shift = 16;

		window.addEvent('keydown', function(e){
			if (e.key != "shift") return;

			$$('*').each(function(el, i, array) {
				rint = Math.round(0xffffff * Math.random());
				hex = [rint >> 16, rint >> 8 & 255, rint & 255].rgbToHex();
				el.toggleClass('debug');
				if (el.hasClass('debug')) {
					el.setStyle('border-color', hex);
				}
			});

		});
	}
}

Form = {
	init: function() {
		$$('form ul.errors li, form p.success').addEvent('click', function(e) {
			this.dispose();
		});

		// disable submit buttons onSubmit
		$$('form input[type=submit], form button[type=submit]').addEvent('click', function(e) {
			//this.click();
			//this.set('disabled', 'disabled');
		});

		 $$('textarea').addEvents({
                        'click': function(e) {
                                e.stop();
                                val = this.get('html');
                                if (val.trim() != val) {
                                        this.set('html', this.get('html').clean());
                                }
                        },
                        'focus': function(e) {
                                e.stop();
                                val = this.get('html');
                                if (val.trim() != val) {
                                        this.set('html', this.get('html').clean());
                                }
                        }
		});

		$$('form dd input[type=text], form dd input[type=password], form dd input[type=radio], form dd input[type=checkbox], form dd input[type=file], form dd select, form dd textarea').addEvents({
			focus: function(e) {
				help = null;
				ypos = xpos = 0;

				this.addClass('focused');
				this.getParent().addClass('focused');

				if (this.getParent().hasClass('group')) {
					this.getParent().getParent('dd').addClass('focused');
					this.getParent().getParent('dd').getPrevious('dt').addClass('focused');
					// height fix
					this.getParent().getParent('dd').getPrevious('dt').setStyle('height', this.getParent().getParent('dd').getSize().y);
					help = this.getParent().getNext('*.help');
					xpos = this.getParent().getParent('dd').getPosition().x;
					xpos += this.getParent().getParent('dd').getSize().x;
					ypos = this.getParent().getParent('dd').getPosition().y;
				} else {
					this.getParent().getPrevious('dt').addClass('focused');
					// height fix
					this.getParent().getPrevious('dt').setStyle('height', this.getParent().getSize().y);
					xpos = this.getParent().getPosition().x;
					xpos += this.getParent().getSize().x;
					ypos = this.getParent().getPosition().y;
					help = this.getNext('*.help');
				}

				if (help) {
					formHelp.show(help.get('html'), xpos, ypos);
				}
			},
			blur: function(e) {
				this.removeClass('focused');
				this.getParent().removeClass('focused');
				if (this.getParent().hasClass('group')) {
					this.getParent().getParent('dd').removeClass('focused');
					this.getParent().getParent('dd').getPrevious('dt').removeClass('focused');
				} else {
					this.getParent().getPrevious('dt').removeClass('focused');
				}

				formHelp.hide();
			}
		});

	}

}

