/*
 * Premium Search
 *
 * Interfaccia di ricerca web per Premium - Italiana Immobiliare
 *
 * Autore: Alfonso Tatarelli
 * ©2008 Rigel Engineering Srl 
 *
 */
 
var Premium = {
	premium_url: 'http://premium.world.it/index.php/',
	// premium_url: 'http://www.italianaimmobiliare.it/premium/index.php/',
	// premium_url: 'http://localhost/premium/index.php/',

	accessKey: '123467890123456',

	currRegione: 'Toscana',
	currProvincia: '_',
	currComune: '_',
	currZona: '_',
	currTipologia: '_',
	currContratto: '_',
	currPrezzo: '_',
	currVani: '_',
	currMq: '_',

	currTableName: 'offerte',
	countOfferte: 0,
	currPage: 0,
	pagelimit: 5,
	sort: 'prezzo',
	ord: 'asc',
	
	cod_agenzia: '',
	agenzia_mail: '',
	cod_offerta: '',

	waiting_id: 'waiting',
	regione_id: 'regione',
	provincia_id: 'provincia',
	comuni_id: 'comuni-filter',
	zone_id: 'zone',
	tipologia_id: 'tipologia',
	results_id: 'results',
	scheda_id: 'scheda',
	results_title_id: 'results-title',
	box_list_id: 'box-list',
	box_scheda_id: 'box-scheda',
	box_form_id: 'box-form',
	agenzie_list_id: 'agenzie-list',
	
	updateList: true,
	
	init: function() {
		this.clearData();
/* 		this.changeOpacity(2, false); */
/* 		this.changeOpacity(3, false); */
/* 		this.changeOpacity(4, false); */
	},
	clearData: function() {
		this.currProvincia = '_';
		this.currComune = '_';
		this.currZona = '_';
		this.currTipologia = '_';
		this.currContratto = '_';
		this.currPrezzo = '_';
		this.currVani = '_';
		this.currMq = '_';
		this.countOfferte = 0;
	},
	serializeSearch: function(segment) {
		var result;
		if(segment) { // 12
			result = this.accessKey + '/' + this.currRegione + '/' + this.currProvincia +'/' + this.currComune + '/' + this.currZona + '/' + this.currTipologia + '/' + this.currContratto +  '/' + this.currPrezzo + '/' + this.currVani + '/' + this.currMq + '/' + this.currPage + '/' + this.pagelimit; // + '/' + this.sort + '/' + this.ord;
		} else {
			result = 'key=' + this.accessKey + '&r=' + this.currRegione + '&p=' + this.currProvincia + '&c=' + this.currComune + '&z=' + this.currZona + '&t=' + this.currTipologia + '&co=' + this.currContratto + '&pr=' + this.currPrezzo + '&v=' + this.currVani + '&mq=' + this.currMq + '&page=' + this.currPage + '&limit=' + this.pagelimit;
		}
		return escape(result);
	},
	updateRegionePopup: function() {
		var url = this.premium_url+'api/comuni/getregioni/'+this.serializeSearch(true)+'/'+this.regione_id;
		new Ajax.Request(url, {
			crossSite: true,
			evalScripts: true, 
			method: 'get',
			parameters: this.serializeSearch() + '&elem='+ this.regione_id,
			onSuccess: function(transport) {
				
			},
			onLoading: function(transport) {
				
			}
		});
	},
	onChangeRegione: function(menu) {
		var oldValue = this.currRegione;
		if (menu.selectedIndex>=0) {
			var regione = (menu.options[menu.selectedIndex].value);
			this.currRegione = regione;
	
			if (regione=='') {
				this.currRegione = '_';
			}
			this.clearData();
			this.updateProvinciaPopup();
		} else {
			this.clearData();
		}
		Element.update(this.comuni_id, '');
		this.updateLayout(true);
	},
	updateProvinciaPopup: function() {
		if ((this.currProvincia != '')) {
			var url = this.premium_url+'api/comuni/getprovincie/'+this.serializeSearch(true)+'/'+this.provincia_id;
			new Ajax.Request(url, {
				crossSite: true,
				evalScripts: true, 
				method: 'get',
				parameters: this.serializeSearch() + '&elem='+ this.provincia_id,
				onSuccess: function(transport) {
					
				},
				onLoading: function(transport) {
					
				}
			});
		} else {
			$(this.provincia_id).innerHTML = '';
		}
	},
	onChangeProvincia: function(menu) {
		var oldValue = this.currProvincia;
		this.clearData();
		if (menu.selectedIndex>=0) {
			
			var provincia = (menu.options[menu.selectedIndex].value);
	
			this.clearData();
			if (provincia!='') {
				this.currProvincia = provincia;
				this.updateComuniList();
			}
			this.updateOfferteList('new');
		}
		this.updateLayout((oldValue != this.currProvincia));
	},
	updateComuniList: function() {
		if ((this.currComune != '')) {
			var url = this.premium_url+'api/comuni/getcomuni/'+this.serializeSearch(true)+'/'+this.comuni_id;
			new Ajax.Request(url, {
				crossSite: true,
				evalScripts: true, 
				method: 'get',
				parameters: this.serializeSearch() + '&elem=' + this.comuni_id,
				onSuccess: function(transport) {
					// new Effect.Pulsate('marker_2', {pulses: 2});
				}.bind(this)
			});
		}
	},
	selectComune: function(item, name) {
		var oldValue = this.currComune;
		var elements = $$('#'+this.comuni_id+' a');
		elements.map(function(document){Element.removeClassName(document, 'selected');});
		Element.addClassName(item, 'selected');
		this.currComune = name;
		this.currZona = '_';
		this.updateZonePopup();
		this.updateOfferteList('new');
		this.updateLayout((oldValue == '_'));
	},
	onChangeZone: function(menu) {
		if (menu.selectedIndex>=0) {
			var oldvalue = this.currZona;
			
			var zona = (menu.options[menu.selectedIndex].value);
	
			if (zona=='') {
				this.currZona = '_';
			} else {
				this.currZona = zona;
			}
			
			if(oldvalue != this.currZona)
				this.updateOfferteList('new');
		}
	},
	updateZonePopup: function() {
		if ((this.currZona != '')) {
			var url = this.premium_url+'api/comuni/getzone/'+this.serializeSearch(true)+'/'+this.zone_id;
			new Ajax.Request(url, {
				crossSite: true,
				evalScripts: true, 
				method: 'get',
				// parameters: this.serializeSearch() + '&elem=' + this.zone_id,
				parameters: '',
				onSuccess: function(transport) {
				}
			});
		}
	},
	onChangeTipologia: function(menu) {
		if (menu.selectedIndex>=0) {
			var oldvalue = this.currTipologia;

			var tipologia = (menu.options[menu.selectedIndex].value);
	
			if (tipologia=='') {
				this.currTipologia = '_';
			} else {
				this.currTipologia = tipologia;
			}

			if(oldvalue != this.currTipologia)
				this.updateOfferteList('new');
		}
	},
	updateTipologiaPopup: function() {
		if ((this.currZona != '')) {
			var url = this.premium_url+'api/offerte/gettipologia/'+this.serializeSearch(true)+'/'+this.tipologia_id;
			new Ajax.Request(url, {
				crossSite: true,
				evalScripts: true, 
				method: 'get',
				parameters: this.serializeSearch() + '&elem=' + this.tipologia_id,
				onSuccess: function(transport) {
				}
			});
		}
	},
	selectOption: function(item, id, value) {
		var elements = $$('#'+id+' p');
		elements.map(function(document){Element.removeClassName(document, 'selected');});
		if(!Element.hasClassName(item, 'selected')) {
			Element.addClassName(item, 'selected');
			switch (id) {
				case 'contratto-filter':
					if(value == 'affitto') {
						$('prezzo-filter-vendita').hide();
						$('prezzo-filter-affitto').show();
					} else {
						$('prezzo-filter-affitto').hide();
						$('prezzo-filter-vendita').show();
					}
					if(this.currContratto == value) {
						this.currContratto = '_';
						Element.removeClassName(item, 'selected');
						$('prezzo-filter-affitto').hide();
						$('prezzo-filter-vendita').show();
					} else {
						this.currContratto = value;
						this.currPrezzo = '_';
						elements = $$('#prezzo-filter p');
						elements.map(function(document){Element.removeClassName(document, 'selected');});
					}
					break;
				case 'prezzo-filter':
					if(this.currPrezzo == value) {
						this.currPrezzo = '_';
						Element.removeClassName(item, 'selected');
					} else {
						this.currPrezzo = value;
					}
					break;
				case 'vani-filter':
					if(this.currVani == value) {
						this.currVani = '_';
						Element.removeClassName(item, 'selected');
					} else {
						this.currVani = value;
					}
					break;
				case 'superficie-filter':
					if(this.currMq == value) {
						this.currMq = '_';
						Element.removeClassName(item, 'selected');
					} else {
						this.currMq = value;
					}
					break;
			}
		}
		this.updateOfferteList('new');
	},
	active: function(button) {
		return(Element.hasClassName(button, 'active'));
	},
	updateLayout: function(marker) {
		var clear = true;
		var html = '';
		var elements = $$('.column p');
		if (this.currRegione == '_') {
				html = '<div id="results-header" class="results-header"><div class="title-header">Selezionare una Regione ed una Provincia</div></div>';
				elements.map(function(document){Element.removeClassName(document, 'selected');});
		} else {
			if (this.currProvincia == '_') {
				html = '<div id="results-header" class="results-header"><div class="title-header">Selezionare una Provincia</div></div>';
				elements.map(function(document){Element.removeClassName(document, 'selected');});
			} else {
				if (this.currComune == '_') {
					html = '<div id="results-header" class="results-header"><div class="title-header">Selezionare un Comune</div></div>';
					elements.map(function(document){Element.removeClassName(document, 'selected');});
				} else {
					clear = false;
				}
			}
		}
/*
		if (clear) {
			Element.update(this.zone_id, '<option value="" selected>Qualsiasi zona</option>');
			Element.update(this.results_id, html);
			if(marker) {
				this.changeOpacity(2, false);
				this.changeOpacity(3, false);
				this.changeOpacity(4, false);
			}
		} else {
			if(marker) {
				this.changeOpacity(2, true);
				this.changeOpacity(3, true);
				this.changeOpacity(4, true);
			}
		}
*/
	},
	disableButton: function(button) {
		Element.removeClassName(button, 'active');
	},
	searchOffertebyCod: function(cod) {
		if (cod != '') {

			var url = this.premium_url+'api/offerte/searchby/'+this.accessKey+'/'+cod+'/'+this.results_id;

			var params = 'cod_offerta='+cod+'&elem='+this.results_id;
			new Ajax.Request(url, {
				crossSite: true,
				evalScripts: true, 
				method: 'get',
				parameters: params,
				onSuccess: function(request) {
					this.showList();
					new Effect.Pulsate('title-results', {pulses: 2});
				}.bind(this)
			});
		}
	},
	updateOfferteList: function(search) {
		if ((this.currComune != '')) {
			if((search) && (search=='new')) this.currPage = 0;
			
			var url = this.premium_url+'api/offerte/search/'+this.serializeSearch(true)+'/'+this.results_id;
			if(search) {url += '/'+search;} else {url += '/new';}
			// var params = this.serializeSearch()+'&elem='+this.results_id;
			// if(search) {params += '&search='+search;} else {params += '&search=new';}
			var params = '';
			new Ajax.Request(url, {
				crossSite: true,
				evalScripts: true, 
				method: 'get',
				parameters: params,
				onSuccess: function(request) {
					this.showList();
					new Effect.Pulsate('title-results', {pulses: 2});
/* 					new Effect.Pulsate('marker_4', {pulses: 2}); */
					// new Effect.Pulsate('marker_4', {pulses: 2, afterFinish: this.myCallBackOnFinish.bind(this)});
				}.bind(this)
			});
		}
	},
	showPage: function(page, count) {
		this.currPage = page;
		this.updateOfferteList('page');
	},
	showOfferta: function(codice) {
		if ((codice != '')) {
			var url = this.premium_url+'api/offerte/get_offerta/'+this.accessKey+'/'+codice+'/'+this.scheda_id;
			var params = 'api='+this.accessKey+'&id='+codice+'&elem='+this.scheda_id;
			new Ajax.Request(url, {
				crossSite: true,
				evalScripts: true, 
				method: 'get',
				parameters: params,
				onSuccess: function(request) {
					Effect.BlindUp('search', {duration: 0.5});
					Element.show('show-search');
					Element.hide('hide-search');
					new Effect.Appear(this.box_scheda_id, {afterFinish: function(){new Effect.ScrollTo(Premium.box_list_id,{});} });
					new Effect.Fade(this.box_list_id, {});
				}.bind(this)
			});
		}
	},
	showList: function() {
		new Effect.Appear(this.box_list_id, {});
		new Effect.Fade(this.box_scheda_id, {});
/* 			new Effect.ScrollTo(this.box_list_id,{}); */
		Element.update(this.scheda_id, '');
	},
	showForm: function(agenzia) {
		this.cod_agenzia = agenzia;
		new Effect.Appear(this.box_form_id, {afterFinish: function(){new Effect.ScrollTo(Premium.box_form_id,{});} });
	},
	closeForm: function() {
		new Effect.Fade(this.box_form_id, {afterFinish: function(){} });
		new Effect.ScrollTo(Premium.box_scheda_id,{});
	},
	sendMail: function(form) {
		var url = 'http://www.italianaimmobiliare.it/index.php/blade/send_mail/';
		var params = 'api='+this.accessKey+'&agenzia='+this.cod_agenzia+'&ag_email='+this.agenzia_mail+'&offerta='+this.cod_offerta;
		params += '&'+Form.serialize(form);
		new Ajax.Request(url, {
			evalScripts: true, 
			method: 'post',
			parameters: params,
			onSuccess: function(transport) {
				alert('Grazie! Sarete contatti prima possibile.');
				this.closeForm();
			}.bind(this)
		});
		// Event.stop(event);
	},
	showAgenzie: function() {
		var url = this.premium_url+'api/agenzie/get_agenzie/'+this.accessKey+'/'+this.agenzie_list_id;
		var params = 'api='+this.accessKey+'&elem='+this.agenzie_list_id;
		new Ajax.Request(url, {
			crossSite: true,
			evalScripts: true, 
			method: 'get',
			parameters: params,
			onSuccess: function(transport) {
			}.bind(this)
		});
	},
	sendSondaggio: function(form, target) {
		var url = 'http://www.italianaimmobiliare.it/index.php/form/send_mail/';
		var params = 'api='+this.accessKey+'&agenzia='+this.cod_agenzia+'&ag_email='+this.agenzia_mail+'&offerta='+this.cod_offerta;
		params += '&'+Form.serialize(form);
		new Ajax.Request(url, {
			evalScripts: true, 
			method: 'post',
			parameters: params,
			onSuccess: function(transport) {
				// alert('Grazie per aver collaborato alla nostra ricerca.');
				$(target).innerHTML = '<p><br />Grazie per aver collaborato alla nostra ricerca.<br /><br />Buona navigazione.<br /><br /><br /><br /><br /><br /><br /><br /><br /><br /></p>';
				// this.sendFormTools(form); //  Commentato per IE6
			}.bind(this)
		});
		this.sendFormTools(form); // aggiunto per IE6
	},
	sendFormTools: function(form) {
		var url = 'http://www.italianaimmobiliare.it/formtools/process.php';
		// var url = 'http://localhost/FormTools1_5_1/process.php';
		var params = Form.serialize(form);
		new Ajax.Request(url, {
			evalScripts: true, 
			method: 'post',
			parameters: params,
			onSuccess: function(transport) {
				// $(target).innerHTML = '<p><br />Grazie per aver collaborato alla nostra ricerca.<br /><br />Buona navigazione.<p>';
			}.bind(this)
		});
	},
	serializeElements: function() {
		var elements = $$('.element');
		return elements.map(function(document){
		  return "id[]=" + encodeURIComponent(document.id.split("_")[1]);
		}).join("&");
	},
	showWaiting: function() {
		$(this.waiting_id).show();
	},
	hideWaiting: function() {
		// $('spinner').hide();
		new Effect.Fade(this.waiting_id);
	},
	changeOpacity: function(id, on) {
		if(on){
			new Effect.Opacity('marker_'+id, {duration: 0, from:0.3, to:1.0});
		} else {
			new Effect.Opacity('marker_'+id, {duration: 0, from:1.0, to:0.3});
		}
	},
	myCallBackOnFinish: function(obj) {
		this.changeOpacity(obj.element.id, true);
	},
	getUrlTest: function(url) {
		new Ajax.Request(url, {
			evalScripts: true, 
			method: 'post',
			parameters: '',
			onSuccess: function(transport) {
				alert(transport.responseText);
			}.bind(this)
		});
	}
}

Ajax.Responders.register({
	onCreate: function() {
		if ($(Premium.waiting_id)) Premium.showWaiting();
	},
	onComplete: function() {
		if ($(Premium.waiting_id)) setTimeout(function() {Premium.hideWaiting();}, 250);
		setTimeout(function() {
			var myScripts = $$('script');
			myScripts.each(function(s){
				if ((s.src.indexOf('api') != -1) &&
					(s.src.indexOf(Premium.accessKey) != -1)) {
					if (s) s.remove();
				}
			});
		}, 5000);
	}
});

