
var RedPointsCounter = new Class({
	
	initialize: function( actionUrl ) {
		
		this.actionUrl = actionUrl;		
	
	},
	
	reload: function() {
			
		new Request({
			url: 	this.actionUrl,
			onSuccess: function ( jsResponse ) {
				var response = processResponse( jsResponse );
				
				if ( response.success ) {
					$( 'RedPointsCounter' ).getParent('div').set( 'html', response.result );
				}
				
			}
		})
		.get();
	}
});

var RedPointsPanel = new Class({
	
	Extends:	ListAndFormPanel,
	
	initialize: function( formElement ) {
		this.parent( formElement );
		
		this.currentOrder = { 'field': 'date', 'direction': 'DESC' };
		
		this.counter = new RedPointsCounter( formElement.action.replace( 'update=', 'reloadCounter=' ) );
		
		this.rowIdentifier = 'eventId';
	},

	clearInitialState: function() {
		this.initialState[ 'date'        ] = '';
		this.initialState[ 'pointsGiven' ] = '';
		this.initialState[ 'fineGiven'   ] = '';
		this.initialState[ 'regionId'    ] = '';
		this.initialState[ 'eventId'     ] = '';		
	},
	
	loadInitialState: function( loadedData ) {
		this.initialState[ 'date'        ] = loadedData.date;
		this.initialState[ 'pointsGiven' ] = loadedData.pointsGiven;
		this.initialState[ 'fineGiven'   ] = loadedData.fineGiven;
		this.initialState[ 'regionId'    ] = loadedData.regionId;
		this.initialState[ 'eventId'     ] = loadedData.eventId;
	},

	onSuccessfulUpdate: function( updateResult ) {
		this.parent( updateResult );
		
		this.reload();
		this.counter.reload();
		
		this.hideForm();
	},
	
	reorder: function( field ) {
		
		var direction = 'ASC';
		
		if ( this.currentOrder.field == field && this.currentOrder.direction == 'ASC' ) {
			direction = 'DESC';
		}		
		
		this.currentOrder.field = field;
		this.currentOrder.direction = direction;
		
		this.reload();
	}
	
});


var VehiclesPanel = new Class({
	
	Extends:	ListAndFormPanel,
	
	initialize: function( formElement ) {
		this.parent( formElement );
		
		this.rowIdentifier = 'vehicleId';
		// this.elements.erase( formElement.getElement( "input[name='photo']") );
	},
	
	showForm: function() {
		this.parent();
		
		this.submitButton.setStyle( 'display', 'block' );
		this.footer.setStyle( 'height', 45 );
	},
	
	hideForm: function() {
		this.parent();
		
		this.submitButton.setStyle( 'display', 'none' );
		this.footer.setStyle( 'height', 10 );
	},
	
	clearInitialState: function() {
		this.initialState[ 'vehicleId'       ] = '';
		this.initialState[ 'type'     ] = '';
		this.initialState[ 'producer' ] = '';
		this.initialState[ 'model'   ] = '';
		this.initialState[ 'year'   ] = '';
		
		this.recreateFileInput();
		
		this.formElement.getElement( "fieldset.photoActionRadios" ).setStyle( "display", "none" );
	},
	
	recreateFileInput: function() {
		var oldFileInput = this.formElement.getElement( "input[name=photo]" );
		var newFileInput = new Element( 'input', { 
			'class': 'fileUpload',
			'type': 'file',
			'name': 'photo'
		});
		newFileInput.replaces( oldFileInput );
		
	},
	
	hideFileInput: function() {
		var fi = this.formElement.getElement( "input[name=photo]" );
		fi.setStyle( 'display', 'none' );
		fi.set( 'disabled', true );
	},
	
	showFileInput: function() {
		var fi = this.formElement.getElement( "input[name=photo]" );
		fi.setStyle( 'display', 'inline' );
		fi.set( 'disabled', false );
	},
	
	loadInitialState: function( loadedData ) {
		this.initialState[ 'type' ] = loadedData.type;
		this.initialState[ 'model'   ] = loadedData.model;
		this.initialState[ 'producer'    ] = loadedData.producer;
		this.initialState[ 'year'    ] = loadedData.year;
		this.initialState[ 'vehicleId'          ] = loadedData.vehicleId;
		
		this.recreateFileInput();
		
		if ( loadedData.pictureUrl != null ) {
			var fieldset = this.formElement.getElement( "fieldset.photoActionRadios" );
			fieldset.getElement( "img" ).set( 'src', loadedData.pictureUrl );
			fieldset.setStyle( "display", "block" );
						
			fieldset.getElements( 'input[name=photoAction]' ).each( function(el) {
				el.set( 'disabled', false );
				if ( el.get ( 'value' ) == 'NOACTION' ) {
					el.set( 'checked', true );
				}
			});
			
			fieldset.getParent( 'tr' ).getPrevious( 'tr.photoUploadLabelRow' ).setStyle( "display", "none" );
			
			this.hideFileInput();
		}
		else {
			var fieldset = this.formElement.getElement( "fieldset.photoActionRadios" );
			
			fieldset.getElements( 'input[name=photoAction]' ).each( function(el) {
				el.set( 'disabled', true );
			});
			
			fieldset.getParent( 'tr' ).getPrevious( 'tr.photoUploadLabelRow' ).setStyle( "display", "table-row" );
			
			fieldset.setStyle( "display", "none" );
		}
		
	},
	
	onSuccessfulUpdate: function( updateResult ) {
		this.parent( updateResult );		
		this.reload();
		this.hideForm();		
	},
	

	afterReload: function() {
		milkbox.reloadGalleries();		
	},
	
	
	showSubmitButton: function() {},	
	hideSubmitButton: function() {}

	
});