jQuery(function(){

	var from = jQuery('#routes-from');
	var to = jQuery('#routes-to');
	var submit = jQuery('#routes-submit');
	var domain = 'http://www.routes.tomtom.com/';

	jQuery([to,from]).each(function(){

		var that = this;
		var org  = that.val();
		var cur = null;

		that.isModified = function () {
			cur = that.val();
			return cur && cur != org;
		};

		that.getLocation = function () {
			return that.isModified() ? cur + '/' : '';
		};

		that.showExample = function () {
			that.val(org);
		}

		that.hideExample = function () {
			that.val('');
		};

		that.focus(function(){
			if(!that.isModified()) {
				that.hideExample();
			}
		});

		that.blur(function(){
			if(!that.isModified()) {
				that.showExample();
			}
		});

	});

	function getRoutesAPI () {
		return from.isModified() && to.isModified() ? 'route/' : 'map/';
	};

	submit.click(function(event){
		event.preventDefault();
		event.stopPropagation();
		document.location.href = domain + getRoutesAPI () + from.getLocation() + to.getLocation();
	});

});
