// JavaScript Document

var map;
var gdir;
var geocoder = null;
var addressMarker;

var winScroll;

function handleErrors(){
   if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
	 alert("No corresponding geographic location could be found for one of the specified addresses. This may be due to the fact that the address is relatively new, or it may be incorrect.\nError code: " + gdir.getStatus().code);
   else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)
	 alert("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: " + gdir.getStatus().code);
   
   else if (gdir.getStatus().code == G_GEO_MISSING_QUERY)
	 alert("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\n Error code: " + gdir.getStatus().code);
	 
   else if (gdir.getStatus().code == G_GEO_BAD_KEY)
	 alert("The given key is either invalid or does not match the domain for which it was given. \n Error code: " + gdir.getStatus().code);

   else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
	 alert("A directions request could not be successfully parsed.\n Error code: " + gdir.getStatus().code);
	
   else alert("An unknown error occurred.");
   
}

window.addEvent('domready', function() {
	
	$$('#nav li a').each(function(a) {
		
		a.set('morph',{duration:200})
		
		a.status = {
			normal:{
				'background-position':'0 0',
				'border-color':'#96BBC5',
				'color':a.getStyle('color')
			},
			hover:{
				'background-position':'0 -35px',
				'border-color':'#09253A',
				'color':'#fff'
				
			}
		}
		a.set('styles',a.status.normal)
		.set('events', {
			 
			 'mouseover':function() {this.morph(a.status.hover)},
			 'mouseout':function() {this.morph(a.status.normal)}
			 
		});
		
		winScroll = new Fx.Scroll(window,{duration:500});
		
		
	});
	$$('#start_address').addEvent('keydown', function() {
													 
		if (this.get('value').trim().length>0) this.getNext('input[type=submit]').erase('disabled');
		else this.getNext('input[type=submit]').set('disabled','disabled');											 
	});

	if ($('get_directions')) {
		$('get_directions').addEvent('submit', function() {
		
			gdir.load("from: " + $('start_address').get('value') + " to: 44.063471,9.923573")
			
			return false;
		
		});
	}
	
	
	if ($('submitBlock')) {
		
	
		new Element('input',{
		
			'type':'submit',
			'class':'submit',
			'value':'Invia'
		
		}).inject($('submitBlock'));

	
		
	}
	
	
	if ($('no_js_email')) $('no_js_email').set('styles',{'display':'none'});
	
	$$('#contactForm')
	.set('styles',{'display':'block'})
	.addEvent('submit', function() {
	
		this.getElements('.required input, .required textarea')
		.each(function(i) { 
			
			if (i.get('value').trim().length==0) i.getParent().addClass('error')
			else i.getParent().removeClass('error');

		});
		
		if (!$('email').get('value').trim().contains('@')) $('email').getParent().addClass('error')
		else $('email').getParent().removeClass('error');
	
		if (this.getElements('.error').length>0) return false;
		
	
	});
	
	$$('#email').addEvent('change', function() {
		if (!this.get('value').trim().contains('@')) this.getParent().addClass('error')
		else this.getParent().removeClass('error');
	});
	
	$$('.photo_preview_home ul li a img')
	.set('morph', {duration:300})
	.set('events', {
		
		'mouseover':function() {
			this.morph({'border-color':'#09253a'});
		},
		'mouseout':function() {
			this.morph({'border-color':'#8ca4a7'});
		}
		
	});
	
});

window.addEvent('load', function() {
								 
	if ($('map')) {

		var map = new GMap2($("map"));
        map.setCenter(new GLatLng(44.063471,9.923573), 16);
		map.setMapType(G_HYBRID_MAP);

		map.addControl(new GSmallZoomControl());
		
		var point = new GLatLng(44.063471,9.923573);
		map.addOverlay(new GMarker(point));
		
		gdir = new GDirections(map,$('directions_text'));	
        GEvent.addListener(gdir, "error", handleErrors);
        GEvent.addListener(gdir, "load", function() {
			winScroll.toElement('map');
			$('directions_text').highlight();
		}
		);		
	}
	
	
});
