google.load('maps','2');
var initialized = 0;
var reinit = 0;
var sep;
var actualImg = 0;
var actualId = 0;
var map;
var bounds;
var icon = new Array();
var icon_array = new Array('ulice','ulice-all','firmy-a-sluzby','lekarne-nemocnice','institucie','kulturne-strediska','sportove-strediska','banky-a-bankomaty','autobusy-zsr-mhd-taxi','foto','bankomat','firma-detail','realita-detail','bank','pobocka','nv_reality','realita-update','photo','pobocka-detail');
var markersArray = new Array();
var showArray = new Array();
var overlayNumber = '';
var _sep_next = '';
var _id_i = 0;
var showed = 0;
function initialize_map() {
	if ((_sep != 'realita-detail' || (_sep == 'realita-detail' && (document.location.hash == '#mapa' || default_tab_ini == 3)) || reinit) && !initialized) {
		if ((_sep != 'firma-detail' || (_sep == 'firma-detail' && (document.location.hash == '#mapa' || default_tab_ini == 4)) || reinit) && !initialized) {
			map = new google.maps.Map2(document.getElementById('maps'));
			map.setCenter(new google.maps.LatLng(mapLat,mapLng),mapZoom);
			map.addMapType(G_PHYSICAL_MAP);
			map.addControl(new GLargeMapControl3D());
			map.addControl(new GMenuMapTypeControl());
			GEvent.addListener(map,'moveend',mapLoaded);
			mapLoaded();
			initialized = 1;
		}
	}
}
function initializeAdminMap() {
	map = new google.maps.Map2(document.getElementById('maps'));
	map.setCenter(new google.maps.LatLng(mapLat,mapLng),mapZoom);
	map.addMapType(G_PHYSICAL_MAP);
	map.addControl(new GLargeMapControl3D());
	map.addControl(new GMenuMapTypeControl());
	if (!_position) {
		GEvent.addListener(map,'click',showMarker);
		GEvent.addListener(map,'dblclick',jumpZoom);
	}
	else {
		GEvent.addListener(map,'move',getCenter);
	}
	mapLoadedAdmin();
}
if (_sep != 'realita-update' && _sep != 'photo') google.setOnLoadCallback(initialize_map);
else google.setOnLoadCallback(initializeAdminMap);
function mapLoaded() {
	bounds = map.getBounds();
	for (var i in icon_array) {
		if (icon[i] == null) {
			icon[icon_array[i]] = new GIcon();
			icon[icon_array[i]].image = homePage + '/virtualne/img/g_icon/g_icon_' + icon_array[i] + '.gif';
			icon[icon_array[i]].iconSize = new GSize(14, 19);
			icon[icon_array[i]].iconAnchor = new GPoint(0, 19);
			icon[icon_array[i]].shadow = homePage + '/virtualne/img/g_icon/g_icon_shadow.png';
			icon[icon_array[i]].shadowSize = new GSize(22, 19);
			icon[icon_array[i]].infoWindowAnchor = new GPoint(14, 0);
		}
	}
	readData(_sep,id_item);
	reinit = 0;
}


function mapLoadedAdmin() {
	marker = new GMarker(new google.maps.LatLng(mapLat,mapLng),{draggable:true});
	map.addOverlay(marker);
	marker.hide();
	GEvent.addListener(marker,'drag',actualizeData);
	initData();
}
function readData(_sep,id_item) {
	get_ajax_data(_sep,id_item,bounds.getSouthWest().lat() + ';' + bounds.getNorthEast().lat() + ';' + bounds.getSouthWest().lng() + ';' + bounds.getNorthEast().lng());
}
function showMarker() {
	if (marker.isHidden()) {
		marker.show();
		marker.setPoint(map.getCenter());
	}
}
function position_marker(x,y) {
	if (parseFloat(x)) {
		marker.show();
		marker.setPoint(new google.maps.LatLng(x,y));
		map.setCenter(marker.getPoint());
		map.setZoom(mapZoomDetail);
	}
	else {
		marker.hide();
		map.setCenter(new google.maps.LatLng(mapLat,mapLng),mapZoom);
	}
}
function actualizeData() {
	var point = marker.getPoint();
	updateData(point.lat(),point.lng());
}
function updateData(x,y) {
	document.getElementById('map_lat').value = x;
	document.getElementById('map_lng').value = y;
}
function jumpZoom() {
	if (!marker.isHidden()) {
		map.setCenter(marker.getPoint());
		map.setZoom(mapZoomDetail);
	}
}
function getCenter() {
	var position = map.getCenter();
	updateData(position.lat(),position.lng());
	document.getElementById('map_lat').value = position.lat();
	document.getElementById('map_lng').value = position.lng();
}
function jumpMap(x,y,zoom) {map.setCenter(new GLatLng(x,y),zoom);}
function initData() {
	update_mark(document.getElementById('map_lat').value,document.getElementById('map_lng').value);
}
function update_mark(x,y) {
	position_marker(x,y);
	updateData(x,y);
}



function showMarks(data,_sep,id_item) {
	if (data != '') {
		var dat = data.split(';--;');
		for (var x=0;x<dat.length;x++) {
			dt = dat[x].split(';-;');
			if (markersArray[dt[0]] == null) markersArray[dt[0]] = new Array();
			if (markersArray[dt[0]][id_item] == null) markersArray[dt[0]][id_item] = new Array();
			if (markersArray[dt[0]][id_item][dt[1]] == null) {
				markersArray[dt[0]][id_item][dt[1]] = new GMarker(new google.maps.LatLng(dt[3],dt[4]),{icon:icon[dt[0]],title:dt[2]});
				map.addOverlay(markersArray[dt[0]][id_item][dt[1]]);
				//if (dt[0] != 'firma-detail' && dt[0] != 'bank' && dt[0] != 'ulice-all') {
				if (dt[0] != 'bank' && dt[0] != 'ulice-all') {
					if (_sep == 'foto') overlayNumber = '1';
					initGOverlay(dt[0] + ';-;' + id_item + ';-;' + dt[1],overlayNumber);
				}
				if (dt[0] == 'firma-detail' && _sep_next == 'firma-detail') {
					map.setCenter(new GLatLng(dt[3],dt[4]),mapZoomDetail);
					markersArray[dt[0]][id_item][dt[1]].openInfoWindowHtml(dt[5]);
				}
				if (dt[0] == 'pobocka-detail' && _sep_next == 'pobocka-detail' && _id_i == dt[1]) {
					map.setCenter(new GLatLng(dt[3],dt[4]),mapZoomDetail);
					markersArray[dt[0]][id_item][dt[1]].openInfoWindowHtml(dt[5]);
				}
				if (dt[0] == 'ulice-all') {
					ai = document.getElementById('mtgt_unnamed_' + actualImg);
					ai.setAttribute('onclick','window.location = ' + String.fromCharCode(39) + 'http://' + dt[5] + '/info-mapa/ulice.html#content' + String.fromCharCode(39));
					actualImg++;
				}
			}
		}
		if (_sep == 'foto') readData('foto-list',id_item);
	}
}
function initGOverlay(sepString,overlayNumber) {
	sep = sepString.split(';-;');
	ai = document.getElementById('mtgt_unnamed_' + actualImg);
	ai.setAttribute('onclick','customOverlay(ai,homePage + "/virtualne/info-mapa-detail.php?_sep=" + sep[0] + "&kat=" + sep[1] + "&id_item=" + sep[2],overlayNumber);');
	customOverlay(ai,homePage + '/virtualne/info-mapa-detail.php?_sep=' + sep[0] + '&kat=' + sep[1] + '&id_item=' + sep[2] + '&nopos=1',overlayNumber);
	actualImg++;
}
function showFirmPosition(id_f,x_pos,y_pos) {
	map.setCenter(new GLatLng(x_pos,y_pos),mapZoomDetail);
	if (markersArray[_sep][id_item][id_f] != null) {
		actualId = id_f;
		get_ajax_data('firm-position',id_f,'');
	}
	else window.setTimeout('showFirmPosition(' + id_f + ',' + x_pos + ',' + y_pos + ')',500);
	return false;
}
function showFirmPositionReal(data,id) {markersArray[_sep][id_item][id].openInfoWindowHtml(data);}
function showBankomatPosition(id_b) {
	if (markersArray['bankomat'][id_item][id_b] != null) {
		actualId = id_b;
		get_ajax_data('bankomat-position',id_b,'');
	}
	return false;
}
function showBankomatPositionReal(data,id) {markersArray['bankomat'][id_item][id].openInfoWindowHtml(data);}
function showImgList(data) {
	document.getElementById('imp-gal').innerHTML = '<ul>' + data + '</ul>';
	customOvA();
	if (!showed && location.hash.substr(0,5) == '#idf_') showPosition(location.hash.substr(5));
}
function showPosition(id) {
	actualId = id;
	get_ajax_data('foto-position',id,'');
}
function showPositionReal(data,id) {
	window.onerror = function() {return true;}
	data = data.split(';-;');
	map.setCenter(new google.maps.LatLng(data[1],data[2]),mapZoom);
	markersArray[_sep][id_item][id].openInfoWindowHtml(data[0]);
	showed = 1;
	windowd.onerror = function() {return false;}
}
function hideActual() {if (actualId) markersArray[_sep][id_item][actualId].closeInfoWindow();}
function showDetail(data) {
	data = data.split(';-;');
	map.setCenter(new GLatLng(data[3],data[4]),mapZoomDetail);
}

/*   funkcie pre ulice */
function initStreets(pis) {document.getElementById(pis).style.display = 'block';}
function changeStreets(pis,obj) {
	var alls = document.getElementsByTagName('OL');
	for (var x=0;x<alls.length;x++) if (alls[x].id.substr(0,4) == 'pis-') alls[x].style.display = 'none';
	initStreets(pis);
	alls = document.getElementsByTagName('A');
	for (x=0;x<alls.length;x++) if (alls[x].id.substr(0,5) == '-pis-') alls[x].className = 'fero';
	document.getElementById('-' + pis).className = 'selected';
}
function showStreet(x,y,name) {
	if (markersArray[_sep] == null) markersArray[_sep] = new Array();
	if (markersArray[_sep][0] == null) markersArray[_sep][0] = new Array();
	if (markersArray[_sep][0][0] == null) {
		markersArray[_sep][0][0] = new GMarker(new google.maps.LatLng(x,y),{icon:icon[_sep],title:name});
		map.addOverlay(markersArray[_sep][0][0]);
	}
	else markersArray[_sep][0][0].setPoint(new GLatLng(x,y));
	map.setCenter(new GLatLng(x,y),mapZoomDetail);
}
/*   funkcie pre ulice */

function customOvA() {
	/* custom overlay */
	$("a.custom1[rel]").overlay({
		top: 0,
		expose: {
			color: '#666666',
			opacity: 0.5,
			loadSpeed: 0,
			closeSpeed: 0
		},
		speed: 'fast',
		zoomType: 'div',
		close: 1,
		onBeforeLoad: function() {
          	var wrap = this.getContent().find('.content-wrap');
			wrap.load(this.getTrigger().attr("href"));

          	var file = this.getTrigger().attr('href');
          	var temp = file.substr(file.indexOf('?')+1);
          	temp = temp.split('=');
           	get_ajax_data('spreadDivVirt',temp[1] + ';-;' + temp[0],'overlay1');
          },
          onLoad: function () {
			var pop = document.getElementById('popis');
			if (pop) pop.style.width = p_width + 'px';
          
          	hideActual();
			$('a.custom').click(function () {
				$('a.custom1[rel]').each(function () {
					$(this).overlay().close();
				});
			});
          }
	});
}
	/* /custom overlay */

/* function for google GMarker */
var actual_obj = 0;
function customOverlay(obj,odkaz,overlayNumber) {
	$(obj).overlay({
		top: 0,
		expose: {
			color: '#666666',
			opacity: 0.5,
			loadSpeed: 0,
			closeSpeed: 0
		},
		close: 1,
		speed: 'fast',
		zoomType: 'div',
		target: '#overlay' + overlayNumber,
		onBeforeLoad: function() {
          	var wrap = this.getContent().find('.content-wrap');
			wrap.load(odkaz);

          	var file = odkaz;
          	var temp = file.substr(file.indexOf('&id_item=')+9);
          	temp = temp.split('&');
           	get_ajax_data('spreadDivVirt',temp[0] + ';-;idf','overlay1');
		},
		onLoad: function() {
			actual_obj = $(obj).overlay();
		}
	});
}
function closeOverlay1() {
	if (actual_obj) {
		actual_obj.close();
		actual_obj = 0;
	}
	$('a.custom1').each(function () {
		$(this).overlay().close();	
	});
}
