﻿/**
* Copyright (c) 2011 eSolutionsGroup
* All rights reserved.
* 
* Author: Timothy Grant Vogelsang <tvogelsang@esolutionsgroup.ca>
*/

Ext.form.Field.override( {
	initIndicator : function () {
		if (this.indicatorEl) {
			this.alignIndicator();
			return;
		}

		var f = function () {
			if (!Ext.isEmpty(this.indicatorText, false) || 
					!Ext.isEmpty(this.indicatorIconCls, false)) {

				if (!this.indicatorEl) {
					var elp = this.getErrorCt();

					if (!elp) {
						return;
					}

					this.on("hide", function () {
						this.hideIndicator();
					} );

					this.on("invalid", function () {
						if (this.msgTarget === "side" && this.errorIcon && this.errorIcon.isVisible()) {
							this.hideIndicator();
						}
					} );

					this.on("show", function () {
						this.showIndicator();
					} );
					this.on("valid", function () {
						if (this.msgTarget === "side") {
							this.showIndicator();
						}
					} );

					this.indicatorEl = elp.createChild( {
						cls : "x-form-indicator " + (this.indicatorCls || "") + (this.indicatorCls ? " " : "") + (this.indicatorIconCls || ""), 
						html : this.indicatorText || "",
						style : this.indicatorIconCls ? "padding-left: 18px;" : ""
					} );

					if (this.ownerCt) {
						this.ownerCt.on("afterlayout", this.alignIndicator, this);
						this.ownerCt.on("expand", this.alignIndicator, this);
						this.ownerCt.on("beforeremove", function() {
							if (this.ownerCt) {
								this.ownerCt.un("afterlayout", this.alignIndicator, this);
								this.ownerCt.un("expand", this.alignIndicator, this);
							}
						}, this);
					}

					this.on("resize", this.alignIndicator, this);
					this.on("autosize", this.alignIndicator, this);
					this.on("destroy", function () {
						Ext.destroy(this.indicatorEl);
					}, this);
				}

				this.alignIndicator();

				if (this.indicatorTip) {
					this.indicatorEl.dom.qtip = this.indicatorTip;
				}

				this.showIndicator();

				this.indicatorElIsVisible = true;

				if (this.hidden) {
					this.hideIndicator();
				}
			}
		};

		if (this.rendered) {
			f.call(this);
		} else {
			this.on("render", f, this);
		}
	}
} );
