var BASE = '';
//BASE = '/roi';

var ROI = {
	landingAgenzie: function()
	{
		var landingAgList = I('landing_agenzie_list');
		
		//landingAgList.removeChilds();
		
		$('div.ag-item', landingAgList).each(function(item)
		{
			if (Agent.engine == 'trident') 
			{
				item.remove();
			}
			else 
			{
				item.morph({
					styles: {
						opacity: [1, 0]
					},
					time: 0.5,
					onFinish: function()
					{
						item.remove();
					}
				});
			}
		});
		
		Ajax.get({
			url: BASE + '/index.php?mdu=landing/ajax-agenzie&section=' + ROI.section,
			onFinish: function(data)
			{
				data = JSON.decode(data);
				
				if(data && data.success)
				{
					data.agenzie.each(function(agenzia, i)
					{
						var item = N(['div',
							{
								className: 'ag-item',
								style: {
									position: 'absolute',
									left: 360,
									top: (130 * i),
									cursor: 'pointer'
								},
								innerHTML: agenzia.html,
								onclick: function(event)
								{
									document.location = agenzia.url;
									
									return event.stop();
								}
							}
						], landingAgList);
						
						item.morph({
							styles: {
								left: ['px', ACTUAL, 0]
							},
							effect: EE.outCubic,
							time: 0.5,
							wait: (0.2 * i)
						});
					});
				}
				
				setTimeout(ROI.landingAgenzie, 7000);
			}
		});
	},
	landingEvidenza: function()
	{
		var landingEvList = I('landing_evidenza_list');
		
		$('div.ev-item', landingEvList).each(function(item)
		{
			if (Agent.engine == 'trident') 
			{
				item.remove();
			}
			else 
			{
				item.morph({
					styles: {
						opacity: [1, 0]
					},
					time: 0.5,
					onFinish: function()
					{
						item.remove();
					}
				});
			}
		});
		
		Ajax.get({
			url: BASE + '/index.php?mdu=landing/ajax-evidenza&section=' + ROI.section,
			onFinish: function(data)
			{
				data = JSON.decode(data);
				
				if(data && data.success)
				{
					data.immobili.each(function(immobile, i)
					{
						var item = N(['div',
							{
								className: 'ev-item',
								style: {
									position: 'absolute',
									left: 0, //(176 * (i % 3)),
									top: (220 * i), //(134 * Math.floor(i / 3)),
									opacity: 0
								},
								innerHTML: immobile.html
							}
						], landingEvList);
						
						item.morph({
							styles: {
								opacity: [0, 1]
							},
							effect: EE.outCubic,
							time: 0.5,
							wait: (0.2 * i)
						});
					});
					
					/*
					if(data.immobili.length < 3)
					{
						I('landing_evidenza_btn').hide();
					}
					else
					{
						I('landing_evidenza_btn').show();
					}*/
				}
				
				setTimeout(ROI.landingEvidenza, 7000);
			}
		});
	},
	ricercaEvidenza: function()
	{
		var landingEvList = I('ricerca_evidenza_list');
		
		$('div.ev-item', landingEvList).each(function(item)
		{
			if (Agent.engine == 'trident') 
			{
				item.remove();
			}
			else 
			{
				item.morph({
					styles: {
						opacity: [1, 0]
					},
					time: 0.5,
					onFinish: function()
					{
						item.remove();
					}
				});
			}
		});
		
		Ajax.get({
			url: BASE + '/index.php?mdu=ricerca/ajax-evidenza&section=' + ROI.section,
			onFinish: function(data)
			{
				data = JSON.decode(data);
				
				if(data && data.success)
				{
					if (!data.immobili.length) 
					{
						I('ricerca_evidenza').hide();
					}
					else 
					{
					
						data.immobili.each(function(immobile, i)
						{
							var item = N(['div', {
								className: 'ev-item',
								style: {
									position: 'absolute',
									left: (176 * (i % 3)),
									top: 0,
									opacity: 0
								},
								innerHTML: immobile.html
							}], landingEvList);
							
							item.morph({
								styles: {
									opacity: [0, 1]
								},
								effect: EE.outCubic,
								time: 0.5,
								wait: (0.2 * i)
							});
						});
						
						if (data.immobili.length < 3) 
						{
							I('ricerca_evidenza_btn').hide();
						}
						else 
						{
							I('ricerca_evidenza_btn').show();
						}
					}
				}
				else
				{
					I('ricerca_evidenza').hide();
				}
			}
		});
	},
	ricercaSimili: function()
	{
		var ricercaSim = I('ricerca_simili');
		
		$('div.ev-item', ricercaSim).each(function(item)
		{
			if (Agent.engine == 'trident') 
			{
				item.remove();
			}
			else 
			{
				item.morph({
					styles: {
						opacity: [1, 0]
					},
					time: 0.5,
					onFinish: function()
					{
						item.remove();
					}
				});
			}
		});
		
		Ajax.get({
			url: BASE + '/index.php?mdu=ricerca/ajax-simili',
			onFinish: function(data)
			{
				data = JSON.decode(data);
				
				if(data && data.success)
				{
					data.immobili.each(function(immobile, i)
					{
						var item = N(['div',
							{
								className: 'ev-item',
								style: {
									position: 'absolute',
									left: (176 * (i % 3)),
									top: (134 * Math.floor(i / 3)),
									opacity: 0
								},
								innerHTML: immobile.html
							}
						], ricercaSim);
						
						item.morph({
							styles: {
								opacity: [0, 1]
							},
							effect: EE.outCubic,
							time: 0.5,
							wait: (0.2 * i)
						});
					});
					
					if (data.immobili.length == 6) 
					{
						setTimeout(ROI.ricercaSimili, 7000);
					}
				}
			}
		});
	},
	agenzieEvidenza: function()
	{
		var agenzieEvList = I('agenzie_in_evidenza_list');
		
		$('div.ev-item', agenzieEvList).each(function(item)
		{
			if (Agent.engine == 'trident') 
			{
				item.remove();
			}
			else 
			{
				item.morph({
					styles: {
						opacity: [1, 0]
					},
					time: 0.5,
					onFinish: function()
					{
						item.remove();
					}
				});
			}
		});
		
		Ajax.get({
			url: BASE + '/index.php?mdu=landing/ajax-evidenza&section=' + ROI.section,
			onFinish: function(data)
			{
				data = JSON.decode(data);
				
				if(data && data.success)
				{
					data.immobili.each(function(immobile, i)
					{
						var item = N(['div',
							{
								className: 'ev-item',
								style: {
									position: 'absolute',
									left: (176 * (i % 2)),
									top: (134 * Math.floor(i / 2)),
									opacity: 0
								},
								innerHTML: immobile.html
							}
						], agenzieEvList);
						
						item.morph({
							styles: {
								opacity: [0, 1]
							},
							effect: EE.outCubic,
							time: 0.5,
							wait: (0.2 * i)
						});
					});
				}
				
				if(data.immobili.length < 6)
				{
					if(data.immobili.length == 0)
					{
						I('agenzie_in_evidenza').hide();
					}
					
					I('landing_evidenza_btn').hide();
				}
				else
				{
					I('landing_evidenza_btn').show();
				}
			}
		});
	}
};

Event.add(window, 'load', function()
{
	// Ottengo la sezione attuale
	var section = 'residenziale';
	$('#menu li').each(function(li)
	{
		if(li.hasClass('active'))
		{
			section = li.id.replace('menu_', '');
		}
	});
	
	ROI.section = section;
	
	
	if(I('landing_rapidissima_q'))
	{
		I('landing_rapidissima_q').addEvent('focus', function()
		{
			if(this.value.trim() == 'Inserisci la parola che vuoi cercare')
			{
				this.value = '';
			}
		});
	}
	
	// LANDING TOP
	if(I('landing_top'))
	{
		$('#landing_step_1_map div').each(function(div)
		{
			div.addEvent('click', function(event)
			{
				var id = this.id.replace('_btn', ''), input = I(id);
				
				if(input.value == 1)
				{
					this.removeClass('active');
					
					input.value = 0;
				}
				else
				{
					this.addClass('active');
					
					input.value = 1;
				}
				
				calcAccuracy(1);
				
				return event.stop();
			}).addEvent('mouseover', function()
			{
				this.addClass('hover');
			}).addEvent('mouseout', function()
			{
				this.removeClass('hover');
			});
		});
		
		var allTiposBtn = $('#landing_step_2_tipos div');
		
		allTiposBtn.each(function(div)
		{
			div.addEvent('click', function(event)
			{
				var id = this.id.replace('_btn', ''), input = I(id);
				
				if(id == 'ls2_t5')
				{
					if (input.value == 1) 
					{
						I('landing_step_2').removeClass('terreno');
					}
					else
					{
						I('landing_step_2').removeClass('licenze');
						I('landing_step_2').addClass('terreno');
					}
				}
				else if(id == 'ls2_t6')
				{
					if (input.value == 1) 
					{
						I('landing_step_2').removeClass('licenze');
					}
					else
					{
						I('landing_step_2').removeClass('terreno');
						I('landing_step_2').addClass('licenze');
					}
				}
				else
				{
					I('landing_step_2').removeClass('licenze terreno');
				}
				
				if(input.value == 1)
				{
					this.removeClass('active');
					
					input.value = 0;
				}
				else
				{
					allTiposBtn.each(function(div)
					{
						var id = div.id.replace('_btn', ''), input = I(id);
						
						div.removeClass('active');
						input.value = 0;
					});
					
					this.addClass('active');
					
					input.value = 1;
				}
				
				calcAccuracy();
				
				return event.stop();
			}).addEvent('mouseover', function()
			{
				this.addClass('hover');
			}).addEvent('mouseout', function()
			{
				this.removeClass('hover');
			});
		});
		
		
		// Back / GO
		I('landing_step_1_go').addEvent('click', function()
		{
			I('landing_top_cnt').morph({
				styles: {
					left: ['px', ACTUAL, -950]
				},
				effect: EE.outCubic
			});
		});
		
		I('landing_step_2_back').addEvent('click', function()
		{
			I('landing_top_cnt').morph({
				styles: {
					left: ['px', ACTUAL, 0]
				},
				effect: EE.outCubic
			});
		});
		
		if (I('landing_step_2_go')) 
		{
			I('landing_step_2_go').addEvent('click', function()
			{
				I('landing_top_cnt').morph({
					styles: {
						left: ['px', ACTUAL, -1900]
					},
					effect: EE.outCubic
				});
			});
		}
		
		if (I('landing_step_3_back')) 
		{
			I('landing_step_3_back').addEvent('click', function()
			{
				I('landing_top_cnt').morph({
					styles: {
						left: ['px', ACTUAL, -950]
					},
					effect: EE.outCubic
				});
			});
		}
		
		if (I('landing_step_2_btn')) 
		{
			I('landing_step_2_btn').addEvent('click', function()
			{
				$('#landing_top form')[0].submit();
			});
		}
		
		I('landing_step_3_btn').addEvent('click', function()
		{
			$('#landing_top form')[0].submit();
		});
		
		var step2selects = $('#landing_step_2 select');
		
		step2selects[0].addEvent('change', function()
		{
			if (this.val() == '1') 
			{
				I('landing_step_2_f5_vendita').className = '';
				I('landing_step_2_f5_affitto').className = 'hide';
				
				I('landing_step_2').removeClass('affitto');
			}
			else 
			{
				I('landing_step_2_f5_vendita').className = 'hide';
				I('landing_step_2_f5_affitto').className = '';
				
				I('landing_step_2').addClass('affitto');
			}
		});
		
		step2selects.each(function(select)
		{
			select.addEvent('change', function()
			{
				calcAccuracy();
			});	
		});
		
		$('#landing_step_3 select').each(function(select)
		{
			select.addEvent('change', function()
			{
				calcAccuracy();
			});	
		});
		
		$('#landing_step_3 input').each(function(input)
		{
			input.addEvent('click', function()
			{
				calcAccuracy();
			});	
		});
		
		
		function calcAccuracy(ref)
		{
			// MAP
			var map = false, tipos = false, total = 0, step3 = 0, zones = [];
			
			$('#landing_step_1_map input').each(function(input)
			{
				if(input.value == 1)
				{
					map = true;
					
					zones.push(input.name.replace('z', ''));
				}	
			});
			
			if(map)
			{
				total += 20;
			}
			
			// TIPOS
			$('#landing_step_2_tipos input').each(function(input)
			{
				if(input.value == 1)
				{
					tipos = true;
				}	
			});
			
			if(tipos)
			{
				total += 10;
			}
			
			// CONTRACT & CO
			$('#landing_step_2 select').each(function(select)
			{
				if(select.value > 0)
				{
					total += 10;
				}
			});
			
			
			// STEP 3
			$('#landing_step_3 select').each(function(select)
			{
				if(select.value > 0)
				{
					step3++;
				}
			});
			
			$('#landing_step_3 input').each(function(input)
			{
				if(input.checked)
				{
					step3++;
				}
			});
			
			if(step3 == 9)
			{
				total += 20;
			} else if(step3 >=4)
			{
				total += 10;
			}
			
			// MAKE IT
			
			$('#landing_top .accu-a').each(function(accu)
			{
				$('span', accu)[0].replaceChilds(total);
				$('.bar div', accu)[0].style.width = total + '%';
			});
			
			
			if (ref) 
			{
				// Aggiorno il contatore delle tipologie
				zones = zones.join('-');
				
				Ajax.get(
				{
					url: BASE + '/index.php?mdu=landing/ajax-search-typos',
					post: 
					{
						zones: zones,
						section: ROI.section
					},
					onFinish: function(t)
					{
						t = JSON.decode(t);
						
						if (t && t.success) 
						{
							var types = t.types, tipo;
							
							
							tipo = I('ls2_t1_btn');
							
							if (tipo) 
							{
								tipo.replaceChilds(tipo.getText().replace(/\(.*\)/i, '(' + (types[1] || 0) + ')'));
							}
							
							tipo = I('ls2_t2_btn');
							
							if (tipo) 
							{
								tipo.replaceChilds(tipo.getText().replace(/\(.*\)/i, '(' + (types[2] || 0) + ')'));
							}
							
							tipo = I('ls2_t3_btn');
							
							if (tipo) 
							{
								tipo.replaceChilds(tipo.getText().replace(/\(.*\)/i, '(' + (types[3] || 0) + ')'));
							}
							
							tipo = I('ls2_t4_btn');
							
							if (tipo) 
							{
								tipo.replaceChilds(tipo.getText().replace(/\(.*\)/i, '(' + (types[4] || 0) + ')'));
							}
							
							tipo = I('ls2_t5_btn');
							
							if (tipo) 
							{
								tipo.replaceChilds(tipo.getText().replace(/\(.*\)/i, '(' + (types[5] || 0) + ')'));
							}
							
							tipo = I('ls2_t6_btn');
							
							if (tipo) 
							{
								tipo.replaceChilds(tipo.getText().replace(/\(.*\)/i, '(' + (types[6] || 0) + ')'));
							}
						}
					}
				});
			}
		}
	}
	
	$('div.ag-item, div.im-item, div.src-item').each(function(item)
	{
		var a = $('.go a', item)[0];
		
		if(a)
		{
			item.css('cursor', 'pointer').addEvent('click', function(event)
			{
				document.location = a.href;
				
				return event.stop();
			});
		}
		
		if(item.className == 'src-item')
		{
			var ag = $('.agenzia a', item)[0];
			
			if(ag)
			{
				ag.addEvent('click', function(event)
				{
					document.location = this.href;
					
					event.stop();
				});
			}
			
		}
		
	});
	
	
	// LANDING
	
	if(I('landing_agenzie_list'))
	{
		ROI.landingAgenzie();
	}
	
	if(I('landing_evidenza_list'))
	{
		ROI.landingEvidenza();
		
		/*I('landing_evidenza_list').after(['div', {
			id: 'landing_evidenza_btn',
			style: {
				display: 'none'
			},
			onclick: function()
			{
				ROI.landingEvidenza();
			}
		}]);*/
	}
	
	// RICERCA
	if(I('ricerca_evidenza_list'))
	{
		ROI.ricercaEvidenza();
		
		I('ricerca_evidenza_list').after(['div', {
			id: 'ricerca_evidenza_btn',
			style: {
				display: 'none'
			},
			onclick: function()
			{
				ROI.ricercaEvidenza();
			}
		}]);
	}
	
	if(I('ricerca_bottom_left'))
	{
		$('#ricerca_b_f2 select')[0].addEvent('change', function()
		{
			if (this.val() == '1') 
			{
				I('ricerca_b_f6_vendita').className = '';
				I('ricerca_b_f6_affitto').className = 'hide';
			}
			else 
			{
				I('ricerca_b_f6_vendita').className = 'hide';
				I('ricerca_b_f6_affitto').className = '';
			}
		});
	}
	
	if(I('ricerca_simili'))
	{
		ROI.ricercaSimili();
	}
	
	// RICERCA RAPIDA
	if(I('rapida_form'))
	{
		$('#ricerca_r_f4 select')[0].addEvent('change', function()
		{
			if (this.val() == '1') 
			{
				I('ricerca_r_f8_vendita').className = '';
				I('ricerca_r_f8_affitto').className = 'hide';
			}
			else 
			{
				I('ricerca_r_f8_vendita').className = 'hide';
				I('ricerca_r_f8_affitto').className = '';
			}
		});
		
		$('#ricerca_r_f1 select')[0].addEvent('change', function()
		{
			if (this.val() == '3') 
			{
				I('rapida_form').className = 'commerciale';
				I('ricerca_r_f2_1').name = '';
				I('ricerca_r_f2_2').name = 'tipologia';
			}
			else 
			{
				I('rapida_form').className = 'residenziale';
				I('ricerca_r_f2_1').name = 'tipologia';
				I('ricerca_r_f2_2').name = '';
			}
		});
	}
	
	// LISTA AGENZIE
	
	if(I('agenzie_in_evidenza_list'))
	{
		ROI.agenzieEvidenza();
		
		I('agenzie_in_evidenza_list').after(['div', {
			id: 'landing_evidenza_btn',
			style: {
				display: 'none'
			},
			onclick: function()
			{
				ROI.agenzieEvidenza();
			}
		}]);
	}
	
	
	// AGENZIA
	if (I('agenzia_map')) 
	{
		if (GBrowserIsCompatible()) 
		{
			var map = new GMap2(document.getElementById('agenzia_map')), geo = new GClientGeocoder();
			
			/*
			var iconaAffitto = new GIcon(G_DEFAULT_ICON);
			iconaAffitto.image = base + '/tpl/default/img/icona_gmaps_affitti.png';
			iconaAffitto.iconSize = new GSize(42, 70);
			iconaAffitto.shadowSize = new GSize(0, 0);
			iconaAffitto.iconAnchor = new GPoint(21, 35);
			iconaAffitto.infoWindowAnchor = new GPoint(21, 35);
			iconaAffitto.imageMap = [0, 15, 42, 15, 42, 55, 0, 55, 0, 0];*/
			
			var address = I('agenzia_map_address').val(), title = I('agenzia_map_title').val();
			
			geo.getLatLng(address + ', Italia', function(location)
			{
				if (location) 
				{
					var marker = new GMarker(location, {
						title: title
					});
					
					/*
					GEvent.addListener(marker, 'click', function()
					{
						// Passo le informazioni
						
						marker.openInfoWindowTabsHtml([new GInfoWindowTab(immobile.title, immobile.html)], {
							maxWidth: 200
						});
					});*/
					
					map.addOverlay(marker);
					
					var lat = location.lat();
					var lng = location.lng();
					
					var ui = map.getDefaultUI();
					ui.maptypes.satellite = false;
					ui.maptypes.hybrid = false;
					
					map.setMapType(G_HYBRID_MAP);
					map.setCenter(new GLatLng(lat, lng), 13);
					map.setUI(ui);
				}
			});
		}
	}
	
	// IMMOBILE
	if (I('immobile_map')) 
	{
		var address = I('immobile_map_address').val().trim(), title = I('immobile_map_title').val().trim();
		
		if (address && window.GBrowserIsCompatible && GBrowserIsCompatible()) 
		{
			var map = new GMap2(document.getElementById('immobile_map')), geo = new GClientGeocoder();
			
			/*
			var iconaAffitto = new GIcon(G_DEFAULT_ICON);
			iconaAffitto.image = base + '/tpl/default/img/icona_gmaps_affitti.png';
			iconaAffitto.iconSize = new GSize(42, 70);
			iconaAffitto.shadowSize = new GSize(0, 0);
			iconaAffitto.iconAnchor = new GPoint(21, 35);
			iconaAffitto.infoWindowAnchor = new GPoint(21, 35);
			iconaAffitto.imageMap = [0, 15, 42, 15, 42, 55, 0, 55, 0, 0];*/
			
			geo.getLatLng(address + ', Italia', function(location)
			{
				if (location) 
				{
					var marker = new GMarker(location, {
						title: address
					});
					
					/*
					GEvent.addListener(marker, 'click', function()
					{
						// Passo le informazioni
						
						marker.openInfoWindowTabsHtml([new GInfoWindowTab(immobile.title, immobile.html)], {
							maxWidth: 200
						});
					});*/
					
					map.addOverlay(marker);
					
					var lat = location.lat();
					var lng = location.lng();
					
					var ui = map.getDefaultUI();
					ui.maptypes.satellite = false;
					ui.maptypes.hybrid = false;
					
					map.setMapType(G_HYBRID_MAP);
					map.setCenter(new GLatLng(lat, lng), 13);
					map.setUI(ui);
				}
			});
		}
	}
	
	// Gallery immobile
	var imagesAList = $('#immobile_images_thumbs a');
	
	if (imagesAList.length) 
	{
		var first = false;
		
		imagesAList.each(function(a)
		{
			var id = a.id.split('_')[1];
			
			if(!first)
			{
				first = true;
				
				I('image_' + id).show();
			}
			
			a.addEvent('click', function(event)
			{
				$('#immobile_images div.image').each(function(img)
				{
					img.hide();
				});
				
				I('image_' + id).show();
				
				return event.stop();
			});
			
			
		});
		
		I('immobile_images').show();
		I('immobile_images_thumbs').show();
	}
	
	// LI Info immobile
	var immInfoLi = 0;
	$('#immobile_scheda div.info li').each(function(li)
	{
		if(immInfoLi % 2 === 0)
		{
			li.addClass('clear');
		}
		
		immInfoLi++;
	});
	
	
	// RICHIESTA
	if(I('richiesta_form'))
	{
		function makeLabel(field, text)
		{
			I(field).addEvent('focus', function()
			{
				var value = this.value;
				
				if(value == text)
				{
					this.value = '';
				}
			}).addEvent('blur', function()
			{
				var value = this.value;
				
				if(!value.trim())
				{
					this.value = text;
				}
			}).value = text;
		}
		
		makeLabel($('#richiesta_f1 input')[0], 'Nome e Cognome');
		makeLabel($('#richiesta_f2 input')[0], 'Zona in cui cerchi');
		makeLabel($('#richiesta_f3 input')[0], 'Tipologia di immobile che cerchi');
		makeLabel($('#richiesta_f4 input')[0], 'Metri ( min / max)');
		makeLabel($('#richiesta_f5 input')[0], 'Prezzo ( max )');
		makeLabel($('#richiesta_f6 input')[0], 'Numero di telefono');
		makeLabel($('#richiesta_f7 input')[0], 'Tua Email');
		makeLabel($('#richiesta_f8 textarea')[0], 'Comunicazioni aggiuntive');
	}
	
	if(I('invio_immobile'))
	{
		if(I('photos_cnt'))
		{
			var iii = 0;
			
			I('photos_cnt').append(['button', {
				type: 'button',
				style: {
					margin: '10px 0px 10px 170px'
				},
				click: function(event)
				{
					iii++;
					
					this.before(['div', {
						className: 'field'
					}, ['div', {
						className: 'label'
					}, 'Immagine ' + iii], ['div', {
						className: 'data'
					}, ['input', {
						type: 'file',
						name: 'photos_' + iii,
						id: 'roi_photos_' + iii,
						className: 'file'
					}], ['br'], 'Titolo: ', ['input', {
						type: 'text',
						name: 'photos_title_' + iii,
						id: 'roi_photos_title_' + iii,
						className: 'text'
					}]]]);
					
					return event.stop();
				}
			}, 'Aggiungi foto']);
		}
	}
	
	if (!imagesAList.length) 
	{
		Require.module('lightshow', function()
		{
			LightShow.init();
		});
	}
});
