
/*//////////////////////////// DOMREADY ////////////////////////////
_______________________________________________________________________________________________________ */

window.addEvent('domready', function() { 	

	/* ==== COMMON BEHAVIOURS ==== */

	/* ==== CUSTOM PAGES ==== */

	// handle request form on contact page
	if (document.getElement('#pageContatti')) {
		var myContactGmap = new contactGmap();
		var myContactForm = new contactForm(); 
	}

	// handle google querying on calendar page
	if (document.getElement('#pageCalendario')) {
		var myCalendarGcal = new calendarGcal();
	}

	/* ==== INTERNET EXPLORER OVERRIDES ==== */

	if (Browser.ie){}

});
		

/*//////////////////////////// FUNCTIONS ////////////////////////////
_______________________________________________________________________________________________________ */


/*//////////////////////////// CLASSES ////////////////////////////
_______________________________________________________________________________________________________ */

var contactGmap = new Class({

	initialize: function() {
	
		// store behaviour objects
		this.gbox = document.getElement('#gmap');

		// get map center and zoom level
		this.lat = parseFloat(this.gbox.get('data-lat'));
		this.lng = parseFloat(this.gbox.get('data-lng'));
		this.zlv = parseInt(this.gbox.get('data-zoom'));
		this.wdt = parseInt(this.gbox.get('data-width'));
		this.hgt = parseInt(this.gbox.get('data-height'));

		// set location point
	    this.map_latlng = new google.maps.LatLng(this.lat,this.lng);

		// add the map to the page
		this.add_gmap();

		// add the pois to the  map
		this.add_poi();

	},

	add_gmap: function() {
	

		// set map options
		var map_options = {
			center: this.map_latlng,
			zoom: this.zlv,
			disableDefaultUI: false,
			scrollwheel: false,
			mapTypeId: google.maps.MapTypeId.ROADMAP
		};
		
		// add the map wrapper, set dimensions and inject in the gmap box
		var map_wrapper = new Element('div').setStyles({ width: this.wdt, height: this.hgt}).inject(this.gbox);
	
		// create the google map
		this.gmap = new google.maps.Map(map_wrapper, map_options);

	},

	add_poi: function() {
	
		// build the poi marker
		var poi_marker = new google.maps.Marker({
			icon: '/media/img/interface/marker_gmap.png',
			position: this.map_latlng,
			title: '',
			map: this.gmap
		},this);

		// open google maps when user clicks the marker
		google.maps.event.addListener(poi_marker, 'click', (function() {
			window.open('http://maps.google.it/maps?q='+this.lat+','+this.lng+'&hl=it&sll='+this.lat+'&z=16');
		}).bind(this),this);

	}

})

var contactForm = new Class({

	initialize: function() {
	
		// store behaviour objects
		this.form = document.getElement('form');
		this.formfields = this.form.getElements('input,textarea');
		this.formsubmit = this.form.getElement('button[type=submit]');
		this.formfeedback = this.form.getElement('#feedback');
		
		// add the highlight behaviour to input elements
		this.formfields.each(function(el){
			el.addEvents({
				focus: this.set_highlight_focused.pass(el,this),
				click: this.set_highlight_focused.pass(el,this),
				blur: this.unset_highlighted
			},this);
		},this);
		
		// handle form submission
		this.formsubmit.addEvent('click', this.send_form.bind(this));
		this.form.addEvent('keydown', function(e){ if (e.key == 'enter') { this.send_form(e); } }.bind(this));

	},

	unset_highlighted: function() {
		this.form.getElements('.focused').removeClass("focused");
	},

	set_highlight_focused: function(el) {
		this.unset_highlighted();
		el.getParent('.field').addClass("focused");
	},

	send_form: function(event) {
	
		// stop propagation
		event.stop();

		// avoid multiple submissions
		if(this.form.hasClass('disabled')) { return; }

		// temporarely disable submit button
		this.form.addClass('disabled');
		
		// remove previous not-valid messages
		this.form.getElements('.field.notvalid').removeClass('notvalid');
		
		// give user a feedback
		this.formfeedback.set('text','invio messaggio in corso...').setProperty('class','processing');
		
		// submit form via ajax
		new Request.JSON({ url: '/script/php/send_contactform.php', data: "mode=ajax"+"&"+ this.form.toQueryString(), onComplete: this.show_feedback.bind(this) }).send();

	},

	show_feedback: function(response){

		if(response.status=='success') {
		
			// give user a feedback
			this.formfeedback.set('text','messaggio inviato correttamente').setProperty('class','success');

			// delayed execution
			(function(){ 

				// reset feedback message
				this.formfeedback.set('text','').setProperty('class','');

				// remove previous not-valid messages
				this.form.getElements('.field.notvalid').removeClass('notvalid');
				
				// reset form
				this.form.reset();

				// re-enable submit button, at last...
				this.form.removeClass('disabled');

			}).delay(5000,this);
		
		} else if(response.status=='notvalid') {
		
			Object.each(response.validation,function(status,name) {
				
				// get field element
				var fld = this.form.getElement('input[name='+name+']').getParent('.field');
				var msg = fld.getElement('i');

				// highlight field as not valid
				fld.addClass('notvalid');
				
				// update field message
				if(status.required) {
					msg.set('text','obbligatorio');
				} else {
					if(status.format) {
						msg.set('text','formato non valido');
					}
				}


			},this);

			// give user a feedback
			this.formfeedback.set('text','attenzione: verificare i dati inseriti').setProperty('class','error');

			// re-enable submit button, at last...
			this.form.removeClass('disabled');

		} else {

			// give user a feedback
			this.formfeedback.set('text','errore imprevisto: prego riprovare').setProperty('class','error');

			// re-enable submit button, at last...
			this.form.removeClass('disabled');

		}

	}

})

var calendarGcal = new Class({

	initialize: function() {
	
		// store behaviour objects
		this.gcal = document.getElement('#gcalendar');
		this.fbck = new Element('span').inject(this.gcal);

		// get calendars and start/stop dates
		this.cals = this.gcal.get('data-cals');
		this.startmin = this.gcal.get('data-startmin');
		this.startmax = this.gcal.get('data-startmax');

		// add the events list to the page
		this.get_events();

	},

	get_events: function(){
	
		// get calendar events via AJAX request
		var myRequest = new Request({
		
			url: '/script/php/get_calendar.php',

			// callback functions
			onRequest: this.show_loading.bind(this),
			onSuccess: this.show_events.bind(this),
			onFailure: this.show_error.bind(this),
		
			data: {
				mode: 'ajax',
				cals: this.cals,
				startmin: this.startmin,
				startmax: this.startmax
			}

		}).send();
	},
	
	show_loading: function(){
		this.fbck.set('text', 'caricamento in corso').className = 'processing';
	},

	show_events: function(response){
		this.gcal.empty().set('html',response);	
		// console.log(response);
	},

	show_error: function(){
		this.fbck.set('text', 'errore di collegamento: riprovare più tardi').className = 'error';
	}

})

