/*
 * Read from SVN revision 38700 on Thu Jan 07, 2010 at 06:02 AM by r.clein.
 */
function formPost() {
	path = (typeof arguments[0] == 'string') ? arguments[0] : location.href.replace(/http(s)?:\/\//, '').replace(/^[^\/]*/, '');
	hash = (arguments[1] && typeof arguments[1] == 'object') ? arguments[1] : arguments[0] || {};
	f = new Element('form', { method: 'post', action: path });
	for(o in hash) {
		i = new Element('input', { type: 'hidden', name: o, value: hash[o] });
		f.insert({ bottom: i });
	}
	$$('body')[0].insert({ bottom: f });
	f.submit();
}

function postFormWith(form, hash) {
	f = $(form);
	for(o in hash) {
		i = new Element('input', { type: 'hidden', name: o, value: hash[o] });
		f.insert({ bottom: i });
	}
	f.submit();
}

function safeEnter(e) {
	var elm = e.element();
	switch(e.keyCode) {
		case Event.KEY_RETURN:
			e.stop();
			if(elm.getAttribute('safeEnter_InAutoComplete') && elm.getAttribute('safeEnter_InAutoComplete') == 'true') {
				elm.setAttribute('safeEnter_InAutoComplete', 'false');
			} else if(elm.up('form')) {
				elm.up('form').submit();
			}
			break;

		case Event.KEY_UP:
		case Event.KEY_DOWN:
		case Event.KEY_PAGEUP:
		case Event.KEY_PAGEDOWN:
			elm.setAttribute('safeEnter_InAutoComplete', 'true');
			break;

		case Event.KEY_ESC:
			elm.setAttribute('safeEnter_InAutoComplete', 'false');
			break;
	}
}

Event.observe(window, 'load', function() {
	$$('form.jt_common_form input').each(function(e) {
		Event.observe(e, 'keypress', safeEnter);
	});
});

/* Class: Tip {{{
 */
var Tip = Class.create({
	initialize: function() {
		this.options = Object.extend({
			selector: 'tip',
			attribute: 'title',
			prefix: 'jt',
			'class': 'tip_div',
			img: ''
		}, arguments[0] || {});

		this.showHandler = this.show.bindAsEventListener(this);
		this.hideHandler = this.hide.bindAsEventListener(this);
		this.handleMouseMove = this.mouseMove.bindAsEventListener(this);
		this.tip_text = '';
		this.tip = new Element('div', { id: this.options.prefix+'_tip', 'class': this.options['class'] }).hide();
		this.pointer = $(new Image());
		this.pointer.src = this.options.img;
		this.bounds = {};
		this.target = null;
		this.open = false;

		$$('body')[0].insert({ bottom: this.tip });

		this.apply();
	},
	/* Function: apply() {{{
	 * Apply the event observers to our chosen elements.
	 */
	apply: function() {
		$$('.'+this.options.selector).each(function(elm) {
			Event.observe(elm, 'mouseover', this.showHandler);
			//Event.observe(elm, 'mouseout', this.hideHandler);
		}.bind(this));
	}, // }}}
	/* Function: show() {{{
	 * Show the tip.
	 */
	show: function(e) {
		if(this.open) return;
		this.target = Event.element(e);
		if(this.target = this.upToClass(this.target, this.options.selector)) {
			if(this.tip_text = this.target.getAttribute(this.options.attribute)) {
				this.target.setAttribute(this.options.attribute, '');

				target_loc = this.target.cumulativeOffset();
				this.bounds = {
					top: target_loc.top-1,
					left: target_loc.left-1,
					bottom: target_loc.top + this.target.getHeight()+1,
					right: target_loc.left + this.target.getWidth()+1
				}

				this.tip.setStyle({ width: '' });
				this.tip.show();
				this.open = true;
				this.tip.update(this.tip_text);
				this.tip.insert({ bottom: this.pointer });

				if(this.tip.getWidth() > 300) this.tip.setStyle({ width: '300px' });
				this.tip.setStyle({
					left: Math.floor(this.target.cumulativeOffset().left - (this.tip.getWidth() / 2) + (this.target.getWidth() / 2)) + 'px',
					top: Math.floor(this.target.cumulativeOffset().top - this.tip.getHeight() - 7) + 'px'
				});
				this.pointer.setStyle({
					top: this.tip.getHeight()-4+'px',
					left: this.tip.getWidth()/2-6+'px'
				});
				//this.tip.setStyle({
				//	left: Math.floor(target.cumulativeOffset().left + target.getWidth() + 5) + 'px',
				//	top: Math.floor(target.cumulativeOffset().top - ((this.tip.getHeight() - target.getHeight()) / 2)) + 'px'
				//});
				//this.tip.down('img').setStyle({
				//	top: Math.floor((this.tip.getHeight() / 2) - 6) + 'px'
				//});

				Event.observe(document, 'mousemove', this.handleMouseMove);
			}
		}
	}, // }}}
	/* Function: hide() {{{
	 * Hide the tip.
	 */
	hide: function() {
		this.target.setAttribute(this.options.attribute, this.tip_text);
		this.tip.hide();
		this.open = false;
		Event.stopObserving(document, 'mousemove', this.handleMouseMove);
	}, // }}}
	/* Function: upToClass() {{{
	 */
	upToClass: function(elm, class_name) {
		if(elm.className.match(new RegExp(class_name))) return elm;
		else if(!elm.tagName.match(/body/i)) this.upToClass(elm.up(), class_name);
	}, // }}}
	/* Function: mouseMove() {{{
	 */
	mouseMove: function(e) {
		var coords = {
			x: e.pageX || e.clientX || e.x,
			y: e.pageY || e.clientY || e.y
		}
		if( coords.x > this.bounds.left &&
			coords.x < this.bounds.right &&
			coords.y > this.bounds.top &&
			coords.y < this.bounds.bottom)
			return;
		else this.hide();
	}
}); // }}}

