var tcr={
	prices: {
    'taxi': {day: {start: 7.5, km: 1.95}, night: {start: 7.5, km: 2.05}},
    'van': {day: {start: 10.5, km: 2.25}, night: {start: 12.2, km: 2.35}},
    'free': 2
  }
};

tcr.form=new Class({
		
	Implements: [Events,Options],
	
	initialize: function(handle,options) {
		this.setOptions(options);
		this.handle=handle;
		this.form=new Form(handle,{
			onBlur: this.onChange.bind(this),
			onValid: this.onValid.bind(this),
			onInvalid: this.onInvalid.bind(this),
			onSubmit: this.onSubmit.bind(this),
			onComplete: this.onComplete.bind(this)		
		})
	},
	
	onChange: function(el) {
		if (!el) return true;
		el=$(($type(el)=='string' ? el : el.id));
		if (!el) return true;
	},
	
	onValid: function(prop) {
		var id=prop.element.id+'_error';
		var el=$(id);
		if (el)
			{
			prop.element.removeClass('invalid');
			el.dispose();
			}
	},
	
	onInvalid: function(prop) {
		var id=prop.element.id+'_error';
		var el=$(id);
	  if (el)
	  	{el.set('html',prop.message);}
	  else
	  	{el=new Element('label',{id: id, 'class': 'validate', 'tween': {duration: 200}, 'opacity': 0, html: prop.message}).injectAfter(prop.element);}
		el.tween('opacity',1);
	  prop.element.addClass('invalid');
		id=prop.element.id+'_valid';
		el=$(id);
		if (el) {el.dispose();}
	},
	
	onSubmit: function() {
		this.form.send();
		this.fireEvent('onSubmit',this);
	},
	
	onComplete: function() {
		$(this.handle).set('html','<h2>Bericht is verzonden</h2><p>We hebben uw formulier ontvangen. Wij nemen z.s.m. contact met u op.</p>');
		this.fireEvent('onComplete',this);
	}
	
});


tcr.route=new Class({

	Extends: wmo.fx.box,
	
	Implements: [Events,Options],

	options: {
		padding: 0,
		closeButton: false,
		resizeOnShow: false,
		title: false,
		width: 640, 
		height: 480,
		reload: false,
		adopt: 'ritplanner'
	},
		
	initialize: function() {
	
		this.parent();
		try {
			this.geo=new google.maps.Geocoder();
			this.directions=new google.maps.DirectionsService();
			this.form=new Form(this.options.adopt,{
				onBlur: this.onBlur.bind(this),
				onValid: function(prop) {prop.element.removeClass('invalid').set('title','');},
				onInvalid: function(prop) {prop.element.addClass('invalid').set('title',prop.message);},
				onSubmit: this.onSubmit.bind(this)
			});
			$('result').set('opacity',0);		
			}
		catch(err) {};
	},
	
	show: function() {
		this.parent();
		(function(){this.form.setFocus();}).delay(100,this);
	},
	
	onBlur: function(el) {
		var prefix=el.get('name').substr(0,4)=='from' ? 'from' : 'to';
		//console.log(prefix);
		var f=this.form;
		var value='Nederland, '+f.getValue(prefix+'City')+', '+f.getValue(prefix+'Address')+' '+f.getValue(prefix+'AddressNumber');
		//console.log(value);
		this.findAddress(value,prefix+'Google');
	},
	
	onSubmit: function() {
	},
	
	findAddress: function(value,id) {
		var t=this;
    this.geo.geocode( { 'address': value}, function(results, status) {
      if (status == google.maps.GeocoderStatus.OK) {
      	//console.log(results[0]);
      	var address=results[0].address_components;
      	var zip=address[6] ? address[6].long_name : '';
      	var html=address.length>1 ? address[1].long_name+' '+address[0].long_name+'<br>'+zip+'  '+address[2].long_name : '&nbsp;';
      	$(id).set('html',html);
      	t[id]=address.length>1 ? results[0] : null;
      	t.calcRoute();
      } else {
        //alert("Geocode was not successful for the following reason: " + status);
        return false;
      }
    });
	},
	
	calcRoute: function() {
		var f=this.form;
		var fromCity=f.getValue('fromCity').trim().length!=0;
		var toCity=f.getValue('toCity').trim().length!=0;	
		if (fromCity && toCity && this.fromGoogle && this.toGoogle)
	    {
	    $('result').tween('opacity',1);
	    var request = {
	        origin: this.fromGoogle.geometry.location, 
	        destination: this.toGoogle.geometry.location,
	        travelMode: google.maps.DirectionsTravelMode.DRIVING
	    };
	    this.directions.route(request, function(response, status) {
	      if (status == google.maps.DirectionsStatus.OK) {
	        //console.log(response.routes[0].legs[0]);
	        var route=response.routes[0].legs[0];
	        var distance=(route.distance.value/1000).toFixed();
	        var html='De afstand is ongeveer <b>'+distance+' km</b>.<br>De reisduur is geschat op <b>'+route.duration.text+'</b>';
	        distance=distance-tcr.prices.free;
	        distance=distance<0 ? 0 : distance; 
	        var vehicle=f.getValue('vehicle') ? 'van' : 'taxi';
	        var moment=f.getValue('moment') ? 'night' : 'day';
	        //console.log('km '+prices[vehicle][moment].start+', prijs '+prices[vehicle][moment].km+', afstand '+distance);
	        var price=tcr.prices[vehicle][moment].start+(tcr.prices[vehicle][moment].km*distance);
	        //html+='<p>De geschatte prijs voor deze rit is <b>'+(price.toCurrency())+'</b>';
	        $('googleDistance').set('html',html);
	        $('tcrPrice').set('text','Indicatie ritprijs: '+price.toCurrency());
	      }
	    });
		}
	else
		{$('result').tween('opacity',0);}	
	}	
	
});


tcr.site=new Class({
	
	initialize: function(hasVideo) {
		this.footer=$('footer');
		this.content=$('content');
		this.window=$(window);
		window.addEvent('resize',this.resize.bind(this));
		this.windowHeight=0;
		this.footerHeight=$('footer').getSize().y+20;
		this.resize();
		tcr.slide=new wmo.fx.simpleSlide('.foto',{startSlideOnInitialize: !hasVideo, fxDuration: 1600, periodical: 6400});
		this.setMenu();
		if ($('ritplanner')) {this.route=new tcr.route();}
	},

	resize: function() {
		var ws=this.window.getSize();
		//$('gradient').setStyles({width: ws.x, height: ws.y});
		if (this.windowHeight!=ws.y)
			{
			this.windowHeight=ws.y;
			var cc=this.content.getCoordinates();
			//var fc={y: 160};//footer.getSize();
			this.footer.setStyle('position',(ws.y-(this.footerHeight+cc.bottom)>0) ? 'fixed' : 'relative');
			//var fc=this.footer.getCoordinates();
			//footer.set({styles: {height: (ws.y<160 ? 160 : ws.y)-cc.bottom-14}});
			}
	},
	
	setMenu: function() {
		var menu=$('menu');
		var active='';
		menu.getElements('ul.submenu').set('opacity',0.9);
		menu.getElements('ul li.main').addEvents({
			'mouseenter': function(e) {
				var el=$(e.target);
				var tag=el.get('tag');
				var el=(tag=='li' ? el.getElement('a') : tag=='span' ? el.getParent() : el);
				el=el.addClass('active').getNext();
				if (el)
					{el.addClass('active');}
			},
			'mouseleave': function(e) {
				menu.getElements('.active').removeClass('active');
			}
		});
	}
		
});

if (hasVideo==undefined)
	{var hasVideo=false;}
window.addEvent('domready',function(){tcr.site=new tcr.site(hasVideo);});//, 'resize': tcr.site.resize.bind(tcr.site.resize)});

