Ext.namespace('CC');
Ext.namespace('CC.b');
Ext.namespace('CC.b.mod');

CC.b.mod.pricelist = function(){

	return{
	
		config : {},
	
		init : function(){
			 CC.f.mod.pricelist.init();
		}
	}

}();

Ext.namespace('CC.f');
Ext.namespace('CC.f.mod');

CC.f.mod.pricelist = function(){
	
	return{
		
		ajaxUrl : CC_baseURL +'pricelist/',
		
		grids : {},
		formItems : [],
		windows: [],
		controlPanel : {},
		fromChanged :false,
		firstEditedRow : -1,
		lastRowIndex : -1,
		lastColumnIndex : -1,
		
		
		init : function(){
			eval( 'this.'+ CC.b.mod.pricelist.config.start_event +'();' );
		},
		
		base : function () {
		
		var tbarConfig = [ 
					//this.controlPanel,
					{xtype:'tbfill'},
					{
		        		text : 'Save',
		        		cls:'x-btn-text-icon',
		        		icon:'/images/icons/save.png', 
		        		id : 'saveButton',
		      			handler : CC.f.mod.pricelist.savePrices
	        		},
	      			{xtype: 'tbtext', text: '&nbsp;&nbsp;&nbsp;&nbsp;'},
					{
		        		xtype : 'checkbox',
		        		boxLabel : 'AutoSave',
		        		id : 'autoSave',
		        		checked : true,
		        		width: 80
	        		}
        		];
        	
        	//modulNameParam, gridName, ajaxUrl, primaryField, tbarConfig
			CC.help.grid.initGrid(
				'pricelist', 
				{
					dataIndex : 'Base',
					ajaxUrl : 'extBase',
					primaryField: 'ID'
				}, 
				tbarConfig
			);
			this.grids.Base.grid.getSelectionModel().on('rowselect',this.checkAutoSave);
			CC.main.layout.center.add(this.grids.Base.grid);
			CC.main.layout.center.doLayout();
			this.grids.Base.grid.on('beforeedit', this.updateDateFromEditor );
			this.grids.Base.grid.on('cellclick', this.cellClick );
		},
		
		
		checkAutoSave : function(grid){
			if(Ext.getCmp('autoSave').getValue() == true){
				CC.f.mod.pricelist.savePrices();
			}
		},
		
		savePrices : function(){
			var records = CC.f.mod.pricelist.grids.Base.grid.getStore().getModifiedRecords();
			var data = {};
			var typeTemp = null;
			var dataTemp = {};
			var indexStart = null;
			var indexEnd = null;
			var dataIndex = 0;
			var firstRow = null;
			var id = null;
			var sel = null;
			if (records.length > 0){
				//itt kell �ssze�ll�tani a postolni k�v�nt adatokat
				for(var i = 0, len = records.length; i < len; i++){			
					for(j in records[i].data){
						if (j.indexOf('_CURRENCY') > 0){
								typeTemp = null;
								typeTemp = j.split('_');
								if(records[i].data[typeTemp[0]] != ''){
								//kell a currency
								indexStart = records[i].data[typeTemp[0]+'_CURRENCY'].indexOf('@');
								indexEnd = records[i].data[typeTemp[0]+'_CURRENCY'].indexOf('$');
								id = records[i].data[typeTemp[0]+'_CURRENCY'].substr(indexStart+1,indexEnd-indexStart-1);						
								sel = document.getElementById('@'+id+'$');	
								
								//be kell �ll�tani a selectet
								/*var selectEnd = records[i].data[typeTemp[0]+'_CURRENCY'].indexOf('<option');
								firstRow = records[i].data[typeTemp[0]+'_CURRENCY'].substr(0,selectEnd);
								alert(sel.innerHTML);
								records[i].data[typeTemp[0]+'_CURRENCY'] = firstRow+sel.innerHTML+'</select>';*/
								dataTemp = {};
								dataTemp['PACKING_ID'] = records[i].data.ID;
								dataTemp['LIST_ID'] = CC.b.mod.pricelist.config.extra.typesAll[typeTemp[0]+'_VALUE'];
								dataTemp['CURR_ID'] = records[i].data[typeTemp[0]+'_CURRENCY'];;
								dataTemp['VALUE'] = records[i].data[typeTemp[0]+'_VALUE'];
								data[dataIndex]=dataTemp;
								dataIndex++;
								
							}
						}
					}	 
				}
				
				var param ={
					singleInstance : true,
					url: CC.f.mod.pricelist.ajaxUrl +'extSavePrices',
					params:{
						formdata: Ext.encode( data )
					},
					success: function(result){
						CC.f.mod.pricelist.grids.Base.grid.getStore().commitChanges();
					}
				};	
				CC.help.ajax.doAjax(param);				
			}
		},
		
		
		updateGrid: function(from){
			CC.f.mod.pricelist.grids.Base.grid.getStore().reload({params: {interval: Ext.getCmp('from').getValue()}});
		},
		
		changeCurr : function(packingId, priceListId){
			var record = this.grids.Base.grid.getStore().getAt(CC.f.mod.pricelist.lastRowIndex);
			record.dirty = true; 
			record.store.modified.push(record);		 
		},

		updateDateFromEditor : function(cell){
			 if(Ext.getCmp('autoSave').getValue() == true){	
				if(CC.f.mod.pricelist.firstEditedRow == -1){
					CC.f.mod.pricelist.firstEditedRow = cell.row;
					CC.f.mod.pricelist.lastRowIndex = cell.row;
				}else{
					if(cell.row != CC.f.mod.pricelist.lastRowIndex){
						CC.f.mod.pricelist.savePrices();
					}
					CC.f.mod.pricelist.lastRowIndex = cell.row;
				}
			}	
		},
		
		cellClick : function(grid, rowIndex){
			CC.f.mod.pricelist.lastRowIndex = rowIndex;
		},
		
		currency_render_change : function( value, metadata, record, colIndex, rowIndex, store ) {
			return CC.b.mod.pricelist.config.grids.Base.currTypes[value];
		},
		
		autoFill : function(id){
			
		}
	
	}
	
	
}();