if (!Ext.EMC) {
	Ext.namespace('Ext.EMC');
}
/**
 * @class Ext.EMC.ux.Leadspace (Singleton) Leadspace functionality
 **/
Ext.require('Ext.fx.Anim');
Ext.EMC.Leadspace = function () {
    var _instance;
    
    return {
        state : null,
        getInstance : function () {
            if (!_instance){ 
                // constructor
                var leadspace = Ext.get(Ext.query('.lead-space')[0]);
				if (!leadspace) return null;
				
                this.animationEnabled = leadspace.hasCls('lead-space-collapsable');

                this.els = {
					container : leadspace
                };
                _instance = this;
            }
            return _instance;
        }
    };
   
}();

Ext.EMC.Leadspace.init = function (properties) {
    this.config = { collapsedHeight: 97, expandedHeight: 265};
    Ext.apply(this.config, properties);
    
    this.state = 'expand';
	if (!this.animationEnabled) return;
    
};
Ext.EMC.Leadspace.animate = function (targetState) {
	if (!this.animationEnabled) return;
    if (targetState === this.state) return this;
    var _this = this;
    var container = this.els.container;
	var animDuration = 500;
    if (targetState == 'collapse') {
		container.select('.hide-on-collapse').each(function(){
			Ext.get(this.dom).animate({opacity: 0, duration: animDuration});
		});
        container.setStyle({
			overflow : 'hidden'
		}).animate({
            'min-height': this.config.collapsedHeight,
            height: this.config.collapsedHeight,
            duration: animDuration,
			callback: function(){ 
				_this.state = 'collapse';
				container.addCls('lead-space-collapsed');
			}
        });
        
        
    } else {
        container.select('.hide-on-collapse').each(function(){
			Ext.get(this.dom).animate({opacity: 1, duration: animDuration});
        });
        container.animate({
            'min-height': this.config.expandedHeight,
            height: this.config.expandedHeight,
            duration: animDuration,
			callback: function(){ 
				_this.state = 'expand';
				container.setStyle({ overflow : 'visible' });
			},
			listeners: {
				beforeanimate: function(){
					container.removeCls('lead-space-collapsed');
				},
				scope: this
			}
        });
    }
};

