
var ParkingFinderForm = new Class({
	Implements: [Options, Events],
	options: {
		aform: 'find-parking-address',
		iform: 'find-parking-intersection',
		vform: 'find-parking-venue',
		aEl: 'input[name=a]',
		iEl: 'input[name=i]',
		vEl: 'input[name=v]',
		abtn: '#find-address a',
		ibtn: '#find-intersection a',
		vbtn: '#find-venue a'
	},
	initialize: function(options){
		this.setOptions(options);
		
		this.aform = $(this.options.aform);
		this.iform = $(this.options.iform);
		this.vform = $(this.options.vform);
		
		this.abtn = $(document).getElement(this.options.abtn);
		this.ibtn = $(document).getElement(this.options.ibtn);
		this.vbtn = $(document).getElement(this.options.vbtn);
		
		if (!$defined(this.aform)) {
			return;
		}
		
		
		this.initForms();
		this.initButtons();	
	},
	
	initForms: function() {
		
		this.a = $(this.aform).getElement(this.options.aEl);
		if (this.a) {
			this.a.addEvents({
				'focus': function() {
					if (this.a.value == this.options.form_defaults.a) this.a.value='';
					if (this.clear_a && this.a.value == this.clear_a) this.i.value='';		
				}.bind(this),
				'blur': function() {
					if (this.a.value == '') this.a.value=this.options.form_defaults.a;
				}.bind(this)			
			});
		} 
		
		this.i = $(this.iform).getElement(this.options.iEl);
		if (this.i) {
			this.i.addEvents({
				'focus': function() {
					if (this.i.value == this.options.form_defaults.i) this.i.value='';
					if (this.clear_i && this.i.value == this.clear_i) this.v.value='';		
				}.bind(this),
				'blur': function() {
					if (this.i.value == '') this.i.value=this.options.form_defaults.i;
				}.bind(this)			
			});
		} 
		
		this.v = $(this.vform).getElement(this.options.vEl);
		if (this.v) {
			this.v.addEvents({
				'focus': function() {
					if (this.v.value == this.options.form_defaults.v) this.v.value='';
					if (this.clear_v && this.v.value == this.clear_v) this.v.value='';					
				}.bind(this),
				'blur': function() {
					if (this.v.value == '') this.v.value=this.options.form_defaults.v;
				}.bind(this)			
			});
		} 
		
	},
	
	aValue: function() {
		if (this.a.value == this.options.form_defaults.a) {
			return false;
		} else {
			return this.a.value;
		}
	},
	iValue: function() {
		if (this.i.value == this.options.form_defaults.i) {
			return false;
		} else {
			return this.i.value;
		}
	},
	vValue: function() {
		if (this.v.value == this.options.form_defaults.v) {
			return false;
		} else {
			return this.v.value;
		}
	},
	
	initButtons: function() {
		this.abtn.addEvent('click', function(event) {
			event.preventDefault();
			this.a.value=this.options.form_defaults.a;
			this.showButton('a');
			
			
		}.bind(this));
		this.ibtn.addEvent('click', function(event) {
			event.preventDefault();
			this.showButton('i');
			this.i.value = this.options.form_defaults.i;
			
		}.bind(this));
		this.vbtn.addEvent('click', function(event) {
			event.preventDefault();
			this.showButton('v');
			this.v.value=this.options.form_defaults.v;
			
		}.bind(this));
		
		this.defaultButton();
	},
	
	showButton: function(button) {
		this.clearButtons();
		if (button == 'a') {
			this.aform.setStyle('display', 'block');
			this.abtn.addClass('current');
		}
		if (button == 'i') {
			this.iform.setStyle('display', 'block');
			this.ibtn.addClass('current');
		}
		if (button == 'v') {
			this.vform.setStyle('display', 'block');
			this.vbtn.addClass('current');
		}
	},
	
	clearButtons: function() {
		this.aform.setStyle('display', 'none');
		this.iform.setStyle('display', 'none');
		this.vform.setStyle('display', 'none');
		this.abtn.removeClass('current');
		this.ibtn.removeClass('current');
		this.vbtn.removeClass('current');
	},
	
	defaultButton: function() {
		var qs = document.location.search.substr(1).parseQueryString();
		if (qs.i) {
			this.showButton('i');
		}
		if (qs.v) {
			this.showButton('v');
		}
	}
});