/**
 * @author 3Devil
 */

Ext.namespace('Ext.ux.plugins');
 
Ext.ux.plugins.PriceCombo = function(config) {
    Ext.apply(this, config);
};
 
Ext.extend(Ext.ux.plugins.PriceCombo, Ext.util.Observable, {
    init:function(combo) {
        Ext.apply(combo, {
            tpl:  '<tpl for=".">'
                + '<div class="x-combo-list-item">'
				+ '<tpl if="parseInt(' + combo.priceField + ') !== 0">'
				+ '<div class="ux-price-combo-item">'
				+ '<tpl if="' + combo.priceField + ' &gt; 0">+</tpl>'
				+ '{[fm.usMoney(values.' + combo.priceField + ')]}'
				+ '</div>'
				+ '</tpl>'
                + '{' + combo.displayField + '}'
                + '</div></tpl>',
				
            onRender:combo.onRender.createSequence(function(ct, position) {
                // adjust styles
                this.wrap.applyStyles({position:'relative'});
 
                // add div for icon
                this.price = Ext.DomHelper.append(this.el.up('div.x-form-field-wrap'), {
                    tag: 'div',
					style:'position:absolute;right:20px; top:2px',
					html:parseInt(this.store.query(this.valueField, this.getValue()).itemAt(0).get(this.priceField)) !== 0 ? 
						(parseInt(this.store.query(this.valueField, this.getValue()).itemAt(0).get(this.priceField)) > 0 ? '+' : '') +
						Ext.util.Format.usMoney(this.store.query(this.valueField, this.getValue()).itemAt(0).get(this.priceField)) : ''
                });
            }), // end of function onRender
 
            setPrice:function() {
				if (this.price !== undefined) {
					var rec = this.store.query(this.valueField, this.getValue()).itemAt(0);
					if (rec && parseInt(rec.get(this.priceField)) !== 0) {
	                    this.price.innerHTML = (parseInt(rec.get(this.priceField)) > 0 ? '+' : '') + Ext.util.Format.usMoney(rec.get(this.priceField));
					} else {
						this.price.innerHTML = '';
					}
				}
            }, // end of function setIconCls
 
            setValue:combo.setValue.createSequence(function(value) {
                this.setPrice();
            })
        });
    } // end of function init
}); // end of extend