Ext.define('Ext.ux.InfoPanel', {
	extend: 'Ext.panel.Panel',
	alias: 'widget.infopanel',
	bodyCls: "x-infopanel-body",
	minWidth: 200,
	minHight: 100,
	isNotification: true,
	shadow: false,
	autoHide: false,
	pinEvent: "none",
	hideDelay: 7000,
	showPin: false,
	pinned: false,
	bringToFront: true,
	focusOnToFront: false,
	bodyPadding: 5,
	textAlign: 'left',
	statics: {
		info: function(title, html, queue) {
			var config;
			if (arguments.length == 1 && Ext.isObject(title)) {
				config = title;
			}
			else {
				config = {};
				config.title = title;
				config.html = html;
				config.queue = queue;
			}
			config.isFloatingInfo = true;
			if (config.floating !== false && !Ext.isDefined(config.autoHide)) {
				config.autoHide = true;
			}
			if (!config.alignmentEl && config.floating !== false) {
				if (!config.queue) {
					config.queue = Ext.ux.InfoPanelQueue.defaultQueue;
				}
				if (Ext.isString(config.queue)) {
					config.queue = Ext.ux.InfoPanelQueue.queues[config.queue];
				}
				return config.queue.add(config);
			}
			else {
				return Ext.create("Ext.ux.InfoPanel", config);
			}
		}
	},
	toFront: function() {
		this.setZIndex(this.zIndexManager.zseed);
		if (this.modal) {
			this.zIndexManager.showModalMask(this);
		}
	},
	initComponent: function() {
		var noqueue = false;
		this.addCls("x-infopanel");
		this.afterHideAnimate = Ext.Function.bind(this.afterHideAnimate, this);
		if (!this.alignmentEl && this.initialConfig.floating !== false && this.isFloatingInfo) {
			if (!this.queue) {
				this.queue = Ext.ux.InfoPanelQueue.defaultQueue;
				noqueue = true;
			}
			if (Ext.isString(this.queue)) {
				this.queue = Ext.ux.InfoPanelQueue.queues[this.queue];
			}
			if (noqueue && this.queue && !this.queue.container) {
				this.queue.add(this);
			}
		}
		else {
			delete this.queue;
			if (this.alignmentEl) {
				this.alignmentEl = Ext.get(this.alignmentEl);
			}
		}
		if ((this.alignmentEl || !this.queue || !this.queue.container) && this.initialConfig.floating !== false && this.isFloatingInfo) {
			this.floating = true;
			this.renderTo = Ext.getBody();
		}
		if (this.ui && this.ui != "default") {
			this.nui = this.ui;
			this.addCls("x-infopanel-" + this.ui);
			this.ui = "default";
		}
		if (this.floating) {
			this.addCls("x-infopanel-floating");
		}
		if (this.iconCls) {
			this.addCls("x-infopanel-icon");
		}
		this.callParent(arguments);
		if (noqueue && this.queue && this.queue.container) {
			this.queue.add(this);
		}
	},
	_getXY: function() {
		if (this._xy) {
			return this._xy;
		}
		return Ext.dom.Element.prototype.getXY.call(this);
	},
	hide: function(animate) {
		var me = this,
			config, hideFx = this.queue && this.queue.fx.hide;
		if (this._parentHide || animate === false) {
			delete this._parentHide;
			this.wasHidden = true;
			this.callParent(arguments);
			return this;
		}
		if (this._hiding) {
			return this;
		}
		if (hideFx && this.queue && this.queue.enableAnimations) {
			this._hiding = true;
			if (this.rendered) {
				config = {
					duration: hideFx.duration,
					listeners: {
						afteranimate: this.afterHideAnimate
					}
				};
				if (hideFx.easing) {
					config.easing = hideFx.easing;
				}
				if (hideFx.opacity) {
					config.to = {
						opacity: 0
					};
				}
				this.el.animate(config);
			}
		}
		else {
			if (this.alignmentEl) {
				this.animateHide();
				return;
			}
			if (this._destroyAfterHide) {
				this.destroy(false);
			}
			else {
				this.callParent(arguments);
			}
		}
		return this;
	},
	afterHideAnimate: function() {
		var me = this;
		if (me._destroyAfterHide) {
			me.destroy(false);
		}
		else {
			delete me._hiding;
			me._parentHide = true;
			me.hide();
			me.el.setOpacity(1, false);
		}
	},
	destroy: function(animate) {
		if (!this.hidden && this.rendered && animate !== false) {
			this._destroyAfterHide = true;
			this.hide();
		}
		else {
			if (this.queue) {
				this.queue.remove(this, animate);
				delete this.queue;
			}
			else {
				Ext.Array.remove(Ext.ux.InfoPanelQueue.noneQueue.items, this);
			}
			if (this.hideTask) {
				this.hideTask.cancel();
			}
			this.callParent(arguments);
		}
	},
	stopHiding: function() {
		if (this.header && this.showPin) {
			this.pin();
		}
		else {
			this.pinned = true;
			if (this.autoHide) {
				this.hideTask.cancel();
			}
		}
	},
	onRender: function() {
		this.callParent(arguments);
		this.body.addCls("x-infopanel-body-" + this.textAlign);
		if (this.bringToFront && this.floating) {
			this.toFront();
		}
		if (this.alignmentEl) {
			this.x = -10000;
		}
		if (this.autoHide) {
			this.hideTask = new Ext.util.DelayedTask(this.destroy, this);
			this.el.hover(this.onOver, this.onOut, this);
		}
		if (this.header) {
			this.header.addCls("x-infopanel-header x-panel-header-light");
		}
	},
	animateShow: function() {
		this.el.setOpacity(0, false);
		this.el.fadeIn();
	},
	animateHide: function() {
		this.el.fadeOut({
			listeners: {
				afteranimate: this.afterHideAnimate
			}
		});
	},
	restartHideTask: function() {
		if (this.hideTask) {
			this.hideTask.cancel();
			this.hideTask.delay(this.hideDelay);
			this.delayed = false;
		}
	},
	onOver: function() {
		if (!this.pinned) {
			this.hideTask.cancel();
			this.delayed = true;
		}
	},
	onOut: function() {
		if (!this.pinned) {
			this.hideTask.delay(this.hideDelay);
			this.delayed = false;
		}
	},
	pin: function(e, toolEl, owner, tool) {
		this.tools.unpin.hide();
		this.tools.pin.show();
		this.hideTask.cancel();
		this.pinned = true;
	},
	unpin: function(e, toolEl, owner, tool) {
		this.tools.pin.hide();
		this.tools.unpin.show();
		this.destroy();
		this.pinned = false;
	},
	afterLayout: function() {
		this.callParent(arguments);
		if (this.alignmentEl && !this._aligned) {
			this._aligned = true;
			var xy = this.getAlignToXY(this.alignmentEl, this.alignment || "tr-tr", [this.offsetX || 0, this.offsetY || 0]);
			this.x = xy[0];
			this.y = xy[1];
			this.setXY(xy);
			this.animateShow();
		}
	},
	afterRender: function() {
		this.callParent(arguments);
		if (this.showPin) {
			this.addTool({
				type: "unpin",
				itemId: "unpin",
				handler: this.pin,
				hidden: this.pinned,
				hideMode: "display",
				scope: this
			});
			this.addTool({
				type: "pin",
				itemId: "pin",
				handler: this.unpin,
				hidden: !this.pinned,
				hideMode: "display",
				scope: this
			});
		}
		if (this.pinEvent !== "none") {
			this.el.on(this.pinEvent, this.stopHiding, this);
			this.on(this.pinEvent, this.stopHiding, this);
		}
		if (this.autoHide && !this.delayed && !this.pinned) {
			this.hideTask.delay(this.hideDelay);
		}
	},
	setUI: function(ui) {
		if (ui == "default") {
			this.callParent(arguments);
			return;
		}
		if (this.nui) {
			this.removeCls("x-infopanel-" + this.nui);
		}
		this.nui = ui;
		if (this.nui) {
			this.addCls("x-infopanel-" + this.nui);
		}
	}
});
Ext.onReady(function() {
	Ext.MessageBox.info = Ext.ux.InfoPanel.info;
});
