document.getElementsByTagName('html')[0].id = 'hasjs';
Effect.DefaultOptions.duration = 0.25;

MMM = {
	create_modal: function() {
		if ($('modal')) return true;
		m = document.createElement('div');
		m.setAttribute('id', 'modal');
		m.setAttribute('style', 'display:none')
		$('teh_owners').insertAfter(m);
	}
}

Vehicle = {
	forward: function(e) {
		slug = $F('vehicle_slug');
		if (slug == '') {
			return true;
		}	else if (slug == '_') {
			window.location = '/account';
		} else {
			window.location = '/vehicles/' + $F('vehicle_slug');
		}
	},
	
	create_history: function(e) {
		Element.toggle('new_history_submit');
		Element.toggle('new_history_loader');
		new Ajax.Request($('new_history_form').action, {asynchronous:true, evalScripts:true, parameters:Form.serialize($('new_history_form'))}); 
		return false;
	},
	
	toggle_histories: function(e) {
		id = $('show_histories') ? 'show_histories' : 'hide_histories';
		Element.hide(id);
		Element.show('histories_loader');
		new Ajax.Request($(id).href, {asyncronous:true, evalScripts:true, method:'get', onComplete:function () {Vehicle.reset_history_toggle();}});
	},
	
	reset_history_toggle: function() {
		id = $('show_histories') ? 'show_histories' : 'hide_histories';
		Event.observe(id, 'click', function() { Vehicle.toggle_histories(); return false; });
	},
	
	toggle_projection_form: function() {
		Element.toggle('projected_date_value');
		Element.toggle('projected_date_form');
	},
	
	update_projection_date: function() {
		Element.hide('submit_projection_date');
		Element.hide('cancel_edit_projection');
		Element.show('update_projection_loader');
		new Ajax.Request($('projection_date_form').action, {asynchronous:true, evalScripts:true, parameters:Form.serialize($('projection_date_form')), onComplete:function() {Event.observe('reset_projection_date', 'click', Vehicle.reset_projection_date)}}); 
		return false;
	},
	
	reset_projection_date: function() {
		Element.hide('submit_projection_date');
		Element.hide('cancel_edit_projection');
		Element.show('update_projection_loader');
		new Ajax.Request($('projection_date_form').action, {asynchronous:true, evalScripts:true, parameters:'vehicle[projection_date]='});
	},
	
	load_history_confirm_form: function() {
		Element.hide('delete_history');
		Element.show('delete_loader');
		new Ajax.Request($('delete_history').href, {asynchronous:true, evalScripts:true, onComplete:function() {Vehicle.prepare_history_confirm_form();}});
	},
	
	load_rename_form: function(id) {
		Element.hide('vactions-' + id);
		Element.show('vextras-' + id);
		new Ajax.Request('/vehicles/' + id + '/edit', {asynchronous:true, evalScripts:true, onComplete:function() {Vehicle.prepare_rename_form(id);}});
	},
	
	load_delete_form: function(id) {
		Element.hide('vactions-' + id);
		Element.show('vextras-' + id);
		new Ajax.Request('/vehicles/' + id + '/confirm', {asynchronous:true, evalScripts:true, onComplete:function() {Vehicle.prepare_delete_form(id);}});
	},
	
	prepare_history_confirm_form: function() {
		Element.hide('delete_button'); 
		Element.show('delete_form'); 
		Event.observe('delete_confirm_form', 'submit', Vehicle.delete_history);
		Event.observe('cancel_delete', 'click', Vehicle.reset_delete_button);
	},
	
	prepare_rename_form: function(id) {
		Element.hide('vtitle-' + id);
		Element.show('vform-' + id);
		Element.hide('vloading-' + id);
		Element.show('vcancel-' + id);
		Event.observe('vcancel-' + id, 'click', function() { Vehicle.reset_rename_form(id); return false; });
		Event.observe("vehicleform-" + id, 'submit', function() { Vehicle.rename(id); return false; });
	},
	
	prepare_delete_form: function(id) {
		Element.hide('vextras-' + id);
		Element.show('vehicledelete-' + id);
		Event.observe('vcanceldelete-' + id, 'click', function() { Vehicle.reset_delete_form(id); return false; });
		Event.observe("deleteform-" + id, 'submit', function() { Vehicle.destroy(id); return false; });
	},
	
	rename: function(id) {
		$('vehicleform-submit-' + id).setAttribute('disabled', 'true');
		new Effect.Opacity('vehicleform-submit-' + id, {duration:0.1, from:1.0, to:0.3});
		new Ajax.Request($('vehicleform-' + id).action, {asynchronous:true, evalScripts:true, parameters:Form.serialize($('vehicleform-' + id))});
	},
	
	destroy: function(id) {
		Element.hide('vehicledelete-' + id);
		Element.show('vextras-' + id);
		new Ajax.Request($('deleteform-' + id).action, {asynchronous:true, evalScripts:true, method:'delete', parameters:Form.serialize($('deleteform-' + id))});
	},
	
	update_slugs: function(old, slug) {
		$('vehicle-' + old).setAttribute("id", "vehicle-" + slug);
		$('vtitle-' + old).setAttribute("id", "vtitle-" + slug);
		$('vform-' + old).setAttribute("id", "vform-" + slug);
		$('vrename-' + old).setAttribute("id", "vrename-" + slug);
		$('vdelete-' + old).setAttribute("id", "vdelete-" + slug);
		$('vactions-' + old).setAttribute("id", "vactions-" + slug);
		$('vextras-' + old).setAttribute("id", "vextras-" + slug);
		$('vloading-' + old).setAttribute("id", "vloading-" + slug);
		$('vcancel-' + old).setAttribute("id", "vcancel-" + slug);
		$('vehicledelete-' + old).setAttribute("id", "vehicledelete-" + slug);
	},
	
	reset_rename_form: function(id) {
		Element.show('vtitle-' + id); 
		Element.hide('vform-' + id);
		Element.show('vloading-' + id);
		Element.hide('vcancel-' + id);
		Element.hide('vextras-' + id);
		Element.show('vactions-' + id);
	},
	
	reset_delete_form: function(id) {
		Element.hide('vehicledelete-' + id);
		Element.show('vactions-' + id);
	},
	
	reset_delete_button: function() {
		Element.hide('delete_form');
		Element.show('delete_history');
		Element.hide('delete_loader');
		Element.show('delete_button');
		return false;
	},
	
	delete_history: function() {
		Element.hide('delete_form');
		Element.show('delete_button');
		new Ajax.Request($('delete_confirm_form').action, {asynchronous:true, evalScripts:true, method:'delete', parameters:Form.serialize($('delete_confirm_form'))}); 
		return false;
	}
	
}

User = {
	
	authenticate: function(e) {
		if ($F('openid_url') == '' && $F('recovery_email') == 'you@yourdomain.com') {
			new Ajax.Request('/sessions', {asynchronous:true, evalScripts:true, parameters:Form.serialize($('login_form'))}); 
			return false;
		} else if ($('recovery_email').value != 'you@yourdomain.com') {
			new Ajax.Request('/forgot', {asynchronous:true, evalScripts:true, parameters:Form.serialize($('login_form')), method:'post'});
			return false;
		}		
	},
	
	preselect_location: function(e) {
		a = $F('user_email');
		if (a.substr(-6) == '.co.uk') {
			$('user_location_id').selectedIndex = 3;
		}
		if (a.substr(-3) == '.ca') {
			$('user_location_id').selectedIndex = 2;
		}
	},
	
	set_email_box: function() {
		if (!$F('user_email') || $F('user_email') == 'you@yourdomain.com') {
			$('user_email').className = 'blur';
			$('user_email').value = 'you@yourdomain.com';
		}
	},
	
	edit_email: function() {
		if ($F('user_email') == 'you@yourdomain.com') $('user_email').value = '';
		$('user_email').className = '';
	},
	
	set_login_box: function() {
		if (!$F('login') || $F('login') == 'you@yourdomain.com') {
			$('login').className = 'blur';
			$('login').value = 'you@yourdomain.com';
		}
	},
	
	edit_login: function() {
		if ($F('login') == 'you@yourdomain.com') $('login').value = '';
		$('login').className = '';
	},
	
	set_recovery_box: function() {
		if (!$F('recovery_email') || $F('recovery_email') == 'you@yourdomain.com') {
			$('recovery_email').className = 'blur';
			$('recovery_email').value = 'you@yourdomain.com';
		}
	},
	
	edit_recovery: function() {
		if ($F('recovery_email') == 'you@yourdomain.com') $('recovery_email').value = '';
		$('recovery_email').className = '';
	},
	
	set_vehicle_box: function(id) {
		if (!$F(id) || $F(id) == "e.g. Ford Focus, Blue Nova, Dodge Viper, The Big Wagon") {
			$(id).className = 'blur';
			$(id).value = "e.g. Ford Focus, Blue Nova, Dodge Viper, The Big Wagon";
		}
	},
	
	edit_vehicle: function(id) {
		if ($F(id) == "e.g. Ford Focus, Blue Nova, Dodge Viper, The Big Wagon") $(id).value = '';
		$(id).className = '';
	},
	
	verify_signup: function() {
		if ($F('user_vehicle_string') == "e.g. Ford Focus, Blue Nova, Dodge Viper, The Big Wagon") {
			$('user_vehicle_string').value = '';
		}
	},
	
	prepare_login_form: function() {
		if (!$('openid_login')) return false;
		if ($('openid_login').className == 'preferred') {
            // $('openid_toggle').innerHTML = 'Use Password';
			Element.toggle('password_login');
			Element.toggle('password_recovery');
		} else {
			Element.toggle('openid_login');
			Element.toggle('password_recovery');
		}
	},
	
	toggle_login: function() {
		Element.toggle('openid_login');
		Element.toggle('password_login');
		// $('openid_toggle').innerHTML = $('openid_toggle').innerHTML == 'Use OpenID' ? 'Use Password' : 'Use OpenID';
	},
	
	show_recovery: function() {
		Element.hide('openid_login');
		Element.hide('password_login');
		Element.show('password_recovery');
	},
	
	hide_recovery: function() {
		// Element.hide('openid_login');
		Element.show('password_login');
		Element.hide('password_recovery');
	},
	
	prepare_twitter_help: function() {
		if (!$('twitter_help')) return false;
		Element.hide('twitter_help');
	},
	
	toggle_twitter_help: function() {
		if ($('twitter_toggle').innerHTML == 'How can I send you my data from Twitter?') {
			$('twitter_toggle').innerHTML = 'Thanks, I got it!';
			Effect.BlindDown('twitter_help');
		} else {
			$('twitter_toggle').innerHTML = 'How can I send you my data from Twitter?';
			Effect.BlindUp('twitter_help');
		}
	},
	
	confirm_password: function() {
		if ($F('user_password') && $F('user_password_confirmation')) {
			if ($F('user_password') == $F('user_password_confirmation')) {
				$('password_confirmation').innerHTML = 'Confirmed';
				$('password_confirmation').className = 'confirmed';
			} else {
				$('password_confirmation').innerHTML = 'Not a Match';
				$('password_confirmation').className = 'unconfirmed';
			}
		} else {
			$('password_confirmation').innerHTML = 'Just to be sure';
			$('password_confirmation').className = '';
		}
	}
}

Event.addBehavior({
  '#vehicle_slug':   								function() { Event.observe(this, 'change', Vehicle.forward.bind(this)); },
	'#login_form':     								function() { Event.observe(this, 'submit', User.authenticate.bind(this)); },
	'#projection_date_form':     			function() { Event.observe(this, 'submit', Vehicle.update_projection_date.bind(this)); },
	'#new_history_form':  						function() { Event.observe(this, 'submit', Vehicle.create_history.bind(this)); },
	'#show_histories:click': 					function() { Vehicle.toggle_histories(); return false; },
	'#change_projection_date:click': 	function() { Vehicle.toggle_projection_form(); return false; },
	'#cancel_edit_projection:click': 	function() { Vehicle.toggle_projection_form(); return false; },
	'#reset_projection_date:click': 	function() { Vehicle.reset_projection_date(); return false; },
	'#delete_history:click': 					function() { Vehicle.load_history_confirm_form(); return false; },
	'#first_signup #user_email:blur': function() { User.preselect_location(); },
	'#user_email': 										function() { User.set_email_box(); Event.observe(this, 'focus', User.edit_email); Event.observe(this, 'blur', User.set_email_box); },
	'#login': 												function() { User.set_login_box(); Event.observe(this, 'focus', User.edit_login); Event.observe(this, 'blur', User.set_login_box); },
	'#user_vehicle_string,#vehicle_vehicle_string': function() { User.set_vehicle_box(this.id); Event.observe(this, 'focus', function() {User.edit_vehicle(this.id)}); Event.observe(this, 'blur', function() {User.set_vehicle_box(this.id)}); },
	'#user_password,#user_password_confirmation': function() { Event.observe(this, 'blur', User.confirm_password); },
	'.vrename:click': 								function() { Vehicle.load_rename_form(this.id.substr(8)); return false; },
	'.vdelete:click': 								function() { Vehicle.load_delete_form(this.id.substr(8)); return false; },
	'#user_form': 										function() { Event.observe(this, 'submit', User.verify_signup); },
	'#openid_toggle:click': 					function() { User.toggle_login(); return false;},
	'#password_toggle:click': 					function() { User.toggle_login(); return false;},
	'#recovery_show:click': 					function() { User.show_recovery(); return false;},
	'#recovery_hide:click': 					function() { User.hide_recovery(); return false;},
	'#twitter_toggle:click': 					function() { User.toggle_twitter_help(); return false;},
	'#recovery_email': 								function() { User.set_recovery_box(); Event.observe(this, 'focus', User.edit_recovery); Event.observe(this, 'blur', User.set_recovery_box); }							
});

Event.observe(window, 'load', function() {
	User.prepare_login_form();
	User.prepare_twitter_help();
});