/*	Script: OverText.js
		Shows text over an input that disappears when the user clicks into it. The text remains hidden if the user adds a value.

		Author:
		Aaron Newton <aaron [dot] newton [at] cnet [dot] com>

		Class: OverText
		Shows text over an input that disappears when the user clicks into it. The text remains hidden if the user adds a value.

		Arguments:
		inputs - (collection) a collection of dom elements that should receive the hover text
		options - (object) an optional collection of key/value pairs

		Options:
		positionOptions - (object) data passed to <Element.setPosition>
		onTextHide - (function) callback fired when an over text is hidden (passed the over text element and the input element as arguments)
		onTextShow - (function) callback fired when an over text is shown (passed the over text element and the input element as arguments)
*/
var OverText = new Class({
	options: {
		positionOptions: {
			position:"upperLeft",
			edge:"upperLeft",
			offset: {
				x: 4,
				y: 2
			}
		},
		onTextHide: Class.empty,
		onTextShow: Class.empty
	},
	initialize: function(inputs, options) {
		this.setOptions(options);
		this.overTxts = [];
		this.overTxtEls = [];
		this.elements = $$(inputs);
		this.buildOverTxts();
		this.repositionAll();
	},
	buildOverTxts: function() {
		this.elements.each(function(el){
			if (this.overTxtEls.contains(el) || el.hasClass('overTxtSetup')) return;
			el.addClass('overTxtSetup');
			var val = el.getProperty('alt')||el.getProperty('title');
			el.setProperty('alt', '').setProperty('title','');
			if(!val) { return; }
			this.overTxtEls.push(el);
			var txt = new Element('div', {'class': 'overTxtDiv'}).setHTML(val).injectAfter(el);
			if(el.id) {
				txt.id = 'overTxtDiv_'+el.id;
			}
			el.addEvents({
				focus: this.hideTxt.pass(el, this),
				blur: this.testOverTxt.pass(el, this),
				change: this.testOverTxt.pass(el, this)
			});
			window.addEvent('resize', this.repositionAll.bind(this));
			txt.addEvent('click', this.hideTxt.pass(el, this));
			this.overTxts.push(txt);
			this.testOverTxt(el);
		}, this);
	},

	getTxtForEl: function(el) {
		try {
			return this.overTxts[this.overTxtEls.indexOf(el)];
		} catch (e){
			return null;
		}
	},
/*	Property: hideTxt
		Hides the over text for a given input.

		Arguments:
		el - (element) the input whose over text should be hidden	*/
	hideTxt: function(el){
		var txt = this.getTxtForEl(el);
		if (txt && (txt.getStyle('display') != 'none')) {
			txt.hide();
			el.focus();
			this.fireEvent('onTextHide', [txt, el]);
		}
	},
/*	Property: showTxt
		Shows the over text for a given input.

		Arguments:
		el - (element) the input whose over text should be shown	*/
	showTxt: function(el){
		var txt = this.getTxtForEl(el);
		if (txt && !(txt.getStyle('display') != 'none')) {
			txt.show();
			this.fireEvent('onTextShow', [txt, el]);
		}
	},
	testOverTxt: function(el){
		if(el.getValue()) this.hideTxt(el);
		else this.showTxt(el);
	},
/*	Property: repositionAll
		Repositions all the overTexts over their inputs.	*/
	repositionAll: function(){
		this.overTxtEls.each(this.repositionOverTxt.bind(this));
	},
/*	Property: repositionOverTxt
		Repositions a specific over text for a given element.

		Arguments:
		el - (element) the input whose over text should be repositioned
		*/
	repositionOverTxt: function (el){
		var txt = this.getTxtForEl(el);
		if (!txt) return;
		try {
			var parentPosition = el.getPosition();
			txt.setStyles({
				'left': parentPosition.x + this.options.positionOptions.offset.x,
				'top': parentPosition.y + this.options.positionOptions.offset.y
			});
			/* txt.setPosition($merge(this.options.positionOptions, {relativeTo: el})); */
			if(el.offsetHeight) this.testOverTxt(el);
			else this.hideTxt(el);
		} catch(e){
			console.log('overTxt error: ', e);
		}
	}
});
OverText.implement(new Options, new Events);
/* do not edit below this line */
/* Section: Change Log

$Source: /var/lib/cvs/sipgate20_zend_html/public/static/js/standard/OverText.class.js,v $
$Log: OverText.class.js,v $
Revision 1.2  2010-02-24 14:33:07  rotmanov
fixed login form

Revision 1.3  2008-03-10 13:44:25  rotmanov
""

Revision 1.2  2008-03-10 09:31:18  rotmanov
""

Revision 1.1  2008-03-10 09:30:48  rotmanov
""

Revision 1.2  2008/01/10 22:49:34  newtona
Fixed a typo in setAssetHref.js
OverText repositioning bug fixed
Fixed an issue with date picker that forced you to pass in options
Fixing leap year issues in date.js and date.extras.js

Revision 1.1  2008/01/04 00:49:06  newtona
date.picker: rewrote class to make use of new native date.js
date.picker.plus: allows for time and date range options
stickyWin.default.layout now has a handle option
stickyWinFx now uses the handle reference in stickyWin.default.layout by default
fixed some Fx.Sort array link issues
added datepicker assets to setAssetHref
Waiter.js: new class
OverText.js: new class
Native: date and date.extras - extends the native Date object greatly
updated make mootools 1.11 redball.common.full.js.bat to include new files
fixed a few syntax issues (semi colons) with previous commits


*/

