cs.pro.ResurfacePanel = Ext.extend(Ext.FormPanel, {

	initComponent: function()
	{
		this.resurfaceOptions = new cs.pro.ResurfaceOptions();

		Ext.apply(this, {
			title: 'Resurfacer',
			frame: true,
			autoHeight: true,
			items:[
				this.resurfaceOptions
			]
		});

		cs.pro.ResurfacePanel.superclass.initComponent.apply(this, arguments);
	}
});

cs.pro.ResurfaceOptions = Ext.extend(Ext.form.FieldSet, {

	getResurface: function()
	{
		var file;
		if((file = cs.pro.viewport.getSelectedFile()) == false) return;
		
		Ext.Ajax.request({
			url: 'json/pro_job/resurface',
			params: {
				file: file.get('id'),
				gap: this.gap.getValue(),
				res: this.res.getValue(),
				dec: this.dec.getValue()
			},
			callback: this.gotResurface,
			scope: this
		});
	},
	
	gotResurface: function(options, success, response)
	{
		if(response = cs.common.lib.errorFilter(success, response, this.getResurface))
		{
			//desc = 'Resurface. Gap Fill = ' + this.gap.getValue() + ', Resolution = ' + this.res.getValue() + ', Decimation = ' + this.dec.getValue();
			cs.pro.viewport.addTab(response.filename, 'prosurface', response.job);
		}
	},

	initComponent: function()
	{
		this.gap = new cs.common.SliderPanel({
			label: 'Gap Fill:',
			startValue: 4,
			minValue: 2,
			maxValue: 9,
			listeners: {
				sliderChanged: {
					scope: this,
					fn: this.gapUpdated 
				}
			}
		});

		this.res = new cs.common.SliderPanel({
			label: 'Resolution:',
			startValue: 8,
			maxValue: 15,
			listeners: {
				sliderChanged: {
					scope: this,
					fn: this.resUpdated 
				}
			}
		});
		
		this.dec = new cs.common.SliderPanel({
			label: 'Decimation',
			startValue: 8,
			maxValue: 10
		});

		this.begin = new Ext.Button({
			text: 'Process Selected File',
			handler: this.getResurface,
			scope: this
		});

		Ext.apply(this, {
			title: 'Processing Options',
			autoHeight: true,
			style: 'padding-left: 10px; padding-right: 10px;',
			items: [
				this.gap,
				this.res,
				this.dec
			],
			buttons: [
				this.begin
			]
		});

		cs.pro.ResurfaceOptions.superclass.initComponent.apply(this, arguments);
	},
	
	gapUpdated: function(scope, value)
	{
		resValue = this.res.getValue();
		switch(value)
		{
			case 9:
				if(resValue < 2) this.res.setValue(2);
				break;
			
			case 10:
				if(resValue < 7) this.res.setValue(7);
				break;
		}
	},
	
	resUpdated: function(scope, value)
	{
		gapValue = this.gap.getValue();
		if(value < 7)
		{
			if(value < 2)
			{
				if(gapValue > 8) this.gap.setValue(8);
			}
			else
			{
				if(gapValue > 9) this.gap.setValue(9); 
			}
		}
	}
});
