﻿var ArrFoto;
var ArrPlanimetria;
var indexGallery;
var idIntervalGallery;
var BigImage;
var BigMap;
var tipo;
var divControl;
var divFoto;
var divPlanimetria;

function InitGallery(idArrFoto, idArrPlanimetria) {
	BigImage = document.getElementById('BigImage');
	BigMap = document.getElementById('BigMap');
	ArrFoto = document.getElementById(idArrFoto).value.split(';');
	ArrPlanimetria = document.getElementById(idArrPlanimetria).value.split(';');
	indexGallery = 0;
	divControl = document.getElementById('divControl');
	divFoto = document.getElementById('divFoto');
	divPlanimetria = document.getElementById('divPlanimetria');
	
	if (ArrFoto.length > 1)
		ChangeTipo('f');
	else
		ChangeTipo('p');
}

function PlayGallery() {
	if (idIntervalGallery != -1) {
		indexGallery++;
		ChangeGallery();
	}
	else
		idIntervalGallery = setInterval(PlayGallery, 6000);
}

function ChangeGallery(idsmall,action) {
	if (action == 'click') {
		document.getElementById('divControl').style.display = 'block';
		BigMap.style.display = 'none';
		BigImage.style.display = 'block';
	}

	var len = (tipo == 'f' ? ArrFoto.length : ArrPlanimetria.length);
	if ((indexGallery + 1) >= len)
		indexGallery = 0;
	else if (indexGallery < 0)
		indexGallery = len - 2;

	var id = ArrGallery();
	if (idsmall != null) {
		id = idsmall;
		clearInterval(idIntervalGallery);
		idIntervalGallery = -1;
	}
	BigImage.style.backgroundImage =
				document.getElementById(id).style.backgroundImage.replace('w=100&h=100', 'w=410&h=439');
}

function ZoomImage() {
	BigImage.href = BigImage.style.backgroundImage.replace('url("', '').replace('w=410&h=439")', 'w=600&h=500').replace('url(', '').replace('w=410&h=439)', 'w=600&h=500');
}

function PrintImage() {
	if (BigImage != null)
		document.getElementById('imgStampa').src = BigImage.style.backgroundImage.replace('url(', '').replace('w=410&h=439)', 'w=410&h=439');
	else
		document.getElementById('imgStampa').src = '/img/comuni/eurocasa-logo.gif';
}

function ArrGallery() {
	return (tipo == 'f' ? ArrFoto[indexGallery] : ArrPlanimetria[indexGallery]);
}

function PrevGallery() {
	clearInterval(idIntervalGallery);
	idIntervalGallery = -1;
	indexGallery--;
	ChangeGallery();
}

function NextGallery() {
	clearInterval(idIntervalGallery);
	idIntervalGallery = -1;
	indexGallery++;
	ChangeGallery();
}

function PauseGallery() {
	clearInterval(idIntervalGallery);
	idIntervalGallery = -1;
}

function ChangeTipo(t) {
	BigMap.style.display = 'none';
	BigImage.style.display = 'block';
	tipo = t;
	PauseGallery();
	var arr = (tipo == 'f' ? ArrFoto : ArrPlanimetria);
	if (tipo == 'f') {
		divFoto.style.display = 'block';
		divPlanimetria.style.display = 'none';
	}
	else
	{
		divFoto.style.display = 'none';
		divPlanimetria.style.display = 'block';
	}
	BigImage.style.backgroundImage = document.getElementById(arr[0]).style.backgroundImage.replace('w=100&h=100', 'w=410&h=439');
	divControl.style.display = 'block';
	if (arr.length > 2) {
		//divControl.style.display = 'block'; //////////////////
		idIntervalGallery = setInterval(PlayGallery, 6000);
		PlayGallery();
	}
	//else ///////////////
		//divControl.style.display = 'none'; ///////////
}

var geocoder = new GClientGeocoder();

function GetMap(IndirizzoComune, Provincia, IsGeoref) {
	document.getElementById('divControl').style.display = 'none';
	document.getElementById('BigImage').style.display = 'none';
	document.getElementById('BigMap').style.display = 'block';
	document.getElementById('divFoto').style.display = 'none'; ////////////////
	document.getElementById('divPlanimetria').style.display = 'none'; /////////////
	BigMap = document.getElementById('BigMap');
	
	var iconColor = new GIcon();
	if (IsGeoref) {
	    iconColor.shadow = '/img/struttura/immobili/scheda-immobile/mappa.png';
		iconColor.iconSize = new GSize(70, 50);
		iconColor.shadowSize = new GSize(70, 50);
		iconColor.iconAnchor = new GPoint(32, 50);
		iconColor.infoWindowAnchor = new GPoint(30, 50);
	}
	else {
	    iconColor.shadow = '/img/struttura/immobili/scheda-immobile/nomappa.png';
		iconColor.iconSize = new GSize(170, 50);
		iconColor.shadowSize = new GSize(170, 50);
		iconColor.iconAnchor = new GPoint(82, 50);
		iconColor.infoWindowAnchor = new GPoint(80, 50);
	}

	geocoder.getLatLng
		(
			IndirizzoComune,
			function(point) {
				if (!point)
					GetMap(Provincia, Provincia, BigMap, IsGeoref)
				else {
					map = new GMap2(BigMap);

					map.setCenter(point, eval(16));

					var marker = new GMarker(point, iconColor);

					map.addOverlay(marker);

					map.addControl(new GSmallMapControl());
					map.addControl(new GMapTypeControl());

					var givenmaptypes = map.getMapTypes();
					map.setMapType(givenmaptypes[2]);
				}
			}
		);
};
