﻿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, 3000);
}

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=185&h=145', 'w=476&h=284&all=0');
}

function ZoomImage()
{
	BigImage.href = BigImage.style.backgroundImage.replace('url("', '').replace('w=476&h=284&all=0', 'w=700&h=600&all=0').replace('url(', '').replace('w=476&h=284&all=0', 'w=800&h=800&all=0');
}

function PrintImage(ImmobileId) {
	if (BigImage != null)
		document.getElementById('imgStampa').src = BigImage.style.backgroundImage.replace('url(', '').replace('w=476&h=284&all=0)', 'w=476&h=284&all=0');
	else
		document.getElementById('imgStampa').src = '/resource/loghi/' + ImmobileId + '.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=185&h=145', 'w=476&h=284&all=0');
	divControl.style.display = 'block';
	if (arr.length > 2) {
		//divControl.style.display = 'block';
		idIntervalGallery = setInterval(PlayGallery, 3000);
		PlayGallery();
	}
	//else
		//divControl.style.display = 'none';
}

var geocoder = new GClientGeocoder();

function GetMap(IndirizzoComune, Provincia, IsGeoref, ImmobileId) {
	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 = '/resource/loghi/map/' + ImmobileId + '.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 = '/resource/loghi/nomap/' + ImmobileId + '.png';
		iconColor.iconSize = new GSize(150, 50);
		iconColor.shadowSize = new GSize(150, 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, ImmobileId)
				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());
				}
			}
		);
};
