﻿//<![CDATA[

var amsll;
var ams;

function cpeAddMapspot(anev,alat,alng,aikon,asize,ainfo,aleir,aspid,alm,ahide,aclick,ater,atpic,amstip,isizex,isizey,ianchx,ianchy,iwanchx,iwanchy) {
	amsll = new GLatLng(alat, alng);
	//alert(aspid);
	ams = createMapspot(amsll, ainfo, anev, false, aikon, asize, aleir, aspid, alm, ahide, aclick, ater, atpic,amstip,isizex,isizey,ianchx,ianchy,iwanchx,iwanchy); //, '<div>xxx<b>bbb</b>1</div>');
    //if (typeof cpemmgr == 'undefined') cpemap.addOverlay(ams);
	return ams;
}

function cpeAddMapspots(amnew,ashow) {
	if (typeof ashow == 'undefined') { ashow = 1 };
    //alert(cpemmgr);
	if (typeof cpemmgr == 'undefined') {
		for (var i in amnew) {
			//if (ashow) amnew[i].hide();
			if (!amnew[i].cpeMapon) {
				cpemap.addOverlay(amnew[i]);
				if (typeof amnew[i].cpeTerulet != 'undefined') cpemap.addOverlay(amnew[i].cpeTerulet);
				if (typeof amnew[i].cpeTeruletKep != 'undefined') cpemap.addOverlay(amnew[i].cpeTeruletKep);
			};
		};
	}
	else {
		cpemmgr.addMarkers(amnew,1);
		if (ashow) cpemmgr.show();
	}
}

function cpeShowMapspot(all,azoom) {
	if (typeof all == 'undefined') {
	  all = new GLatLng(46.2527302, 20.1492096);
	 }
	if (typeof azoom == 'undefined') {
	  azoom = 12;
	 }
	cpemap.setCenter(all, azoom);
}

function cpeShowMapspots(amspots) {
	if (typeof cpemmgr == 'undefined') {
		for (var i in amspots) {
			if (!amspots[i].cpeMapon) {
				cpemap.addOverlay(amspots[i]);
				if (typeof amspots[i].cpeTerulet != 'undefined') cpemap.addOverlay(amspots[i].cpeTerulet);
				if (typeof amspots[i].cpeTeruletKep != 'undefined') cpemap.addOverlay(amspots[i].cpeTeruletKep);
			};			
			amspots[i].show();
			if (typeof amspots[i].cpeTerulet != 'undefined') amspots[i].cpeTerulet.show();
			if (typeof amspots[i].cpeTeruletKep != 'undefined') { if ((aktlangui!='en')||(cpemap.getZoom()>13)) { amspots[i].cpeTeruletKep.show(); } else { amspots[i].cpeTeruletKep.hide(); }; };
		}
	}
	else {
		cpemmgr.show();
	}
}

function cpeHideMapspots(amspots) {
	if (typeof amspots == 'undefined') {
		amspots = mapspots;
		if (typeof mapspotsnewsw != 'undefined') cpeHideMapspots(mapspotsnewsw);
		if (typeof mapspotsnewswim != 'undefined') cpeHideMapspots(mapspotsnewswim);
		if (typeof mapspotsnewswia != 'undefined') cpeHideMapspots(mapspotsnewswia);
		if (typeof nyilaknewsw != 'undefined') cpeHideMapspots(nyilaknewsw);
		$('#unkezvez').remove();
	};
	if (typeof cpemmgr == 'undefined') {
		for (var i in amspots) {
			amspots[i].hide();
			if (typeof amspots[i].cpeTerulet != 'undefined') amspots[i].cpeTerulet.hide();
			if (typeof amspots[i].cpeTeruletKep != 'undefined') amspots[i].cpeTeruletKep.hide();
		};
		//cpemap.clearOverlays();
	}
	else {
		/*1*/cpemmgr.clearMarkers();
		//2//cpemmgr.hide();
	};
}

function cpeClearMapspots() {
	mapspots.length = 0;
	if (typeof cpemmgr != 'undefined') {
		cpemmgr.clearMarkers();
		cpemmgr.refresh();
	}
	cpemap.clearOverlays();
}

function cpeNewMapspot(atitle,plat,plng) {
	if (typeof plat == 'undefined') { alat = 46.26227 } else { alat = plat; };
	if (typeof plng == 'undefined') { alng = 20.169688 } else { alng = plng; };
	cpemap.closeInfoWindow();
	amsll = new GLatLng(alat, alng);
	ams = createMapspot(amsll, atitle, atitle, true); //, '<div>xxx<b>bbb</b>1</div>');
    /*if (typeof cpemmgr == 'undefined') */cpemap.addOverlay(ams);
	if (typeof plat != 'undefined') { cpemap.setCenter(amsll, 14); } else { gotoAddr('6721 Szeged, Széchenyi tér 1.'); };
	return ams;
}

function cpeDelMapspot(pms) {
	if (typeof cpemmgr == 'undefined') {
		cpeDestroyMapspot(pms);
	}
	else {
		cpemmgr.removeMarker(pms);
	}
}

function gotoAddr(acim) {
  if (!acim) acim = document.getElementById('s_cim_str').value;
  cpegeo.getLocations(acim, setMapToAddr);
 }

 function gotoLatLng() {
  var alat = document.getElementById('s_lat_str').value;
  var alng = document.getElementById('s_lng_str').value;
  setAms(alat,alng);
 }
 
function setMapToAddr(response) {
  if (!response || response.Status.code != 200) { alert("Nincs meg ez a cím!");	}
  else {
			setAms(response.Placemark[0].Point.coordinates[1], response.Placemark[0].Point.coordinates[0]);
			setEdit(ams);
		}
	}
  
function setAms(alat,alng) {
  var alatlng = new GLatLng(alat,alng);
  ams.setLatLng(alatlng);
  cpemap.setCenter(alatlng);
  cpeLatLngAltChg(alatlng);
 }

function setEdit(pms) {
  if (pms.cpeSpid=='') {
	document.getElementById('s_lat_str').value = pms.getLatLng().lat().toString();
	document.getElementById('s_lng_str').value = pms.getLatLng().lng().toString();
	cpeLatLngAltChg(pms.getLatLng());
  }
  else {
	if ((typeof pnunidk[pms.cpeSpid].cpeIranyMutato != 'undefined') && ((pms.getLatLng().lat() != pnunidk[pms.cpeSpid].cpeIranyMutato.getVertex(0).lat()) || (pms.getLatLng().lng() != pnunidk[pms.cpeSpid].cpeIranyMutato.getVertex(0).lng()))) {
		var adlat = pnunidk[pms.cpeSpid].cpeIranyMutato.getVertex(1).lat() + (pms.getLatLng().lat() - pnunidk[pms.cpeSpid].cpeIranyMutato.getVertex(0).lat());
		var adlng = pnunidk[pms.cpeSpid].cpeIranyMutato.getVertex(1).lng() + (pms.getLatLng().lng() - pnunidk[pms.cpeSpid].cpeIranyMutato.getVertex(0).lng());
		pnunidk[pms.cpeSpid].cpeIranyMutato.insertVertex(0,pms.getLatLng());
		pnunidk[pms.cpeSpid].cpeIranyMutato.deleteVertex(1);
		pnunidk[pms.cpeSpid].cpeIranyMutato.insertVertex(1,new GLatLng(adlat,adlng));
		pnunidk[pms.cpeSpid].cpeIranyMutato.deleteVertex(2);
	};
	if ((typeof pnunidk[pms.cpeSpid].cpeIranyMutatoGen != 'undefined') && ((pms.getLatLng().lat() != pnunidk[pms.cpeSpid].cpeIranyMutatoGen.getVertex(0).lat()) || (pms.getLatLng().lng() != pnunidk[pms.cpeSpid].cpeIranyMutatoGen.getVertex(0).lng()))) {
		var adlat = pnunidk[pms.cpeSpid].cpeIranyMutatoGen.getVertex(1).lat() + (pms.getLatLng().lat() - pnunidk[pms.cpeSpid].cpeIranyMutatoGen.getVertex(0).lat());
		var adlng = pnunidk[pms.cpeSpid].cpeIranyMutatoGen.getVertex(1).lng() + (pms.getLatLng().lng() - pnunidk[pms.cpeSpid].cpeIranyMutatoGen.getVertex(0).lng());
		pnunidk[pms.cpeSpid].cpeIranyMutatoGen.insertVertex(0,pms.getLatLng());
		pnunidk[pms.cpeSpid].cpeIranyMutatoGen.deleteVertex(1);
		pnunidk[pms.cpeSpid].cpeIranyMutatoGen.insertVertex(1,new GLatLng(adlat,adlng));
		pnunidk[pms.cpeSpid].cpeIranyMutatoGen.deleteVertex(2);
	};
	
	if (((pms.getLatLng().lat().toString() != document.getElementById('h_lat_str_'+pms.cpeSpid).value) || (pms.getLatLng().lng().toString() != document.getElementById('h_lng_str_'+pms.cpeSpid).value) || (document.getElementById('h_direction_override_'+pms.cpeSpid).value != pms.cpeDirectionManual))) {
		for (i in pnunidk[pms.cpeSpid].cpeNyilakKi) {
			//console.log(pnunidk[pms.cpeSpid].cpeNyilakKi[i].cpeStid);
			//console.log(pnunidk[pms.cpeSpid].cpeNyilakKi[i].getVertex(0).lat());
			//console.log(pnunidk[pms.cpeSpid].cpeNyilakKi[i].getVertex(1).lat());
			pnunidk[pms.cpeSpid].cpeNyilakKi[i].insertVertex(0,pms.getLatLng());
			pnunidk[pms.cpeSpid].cpeNyilakKi[i].deleteVertex(1);
			//console.log(pnunidk[pms.cpeSpid].cpeNyilakKi[i].getVertex(0).lat());
			//console.log(pnunidk[pms.cpeSpid].cpeNyilakKi[i].getVertex(1).lat());
			if (typeof pnunidk[pms.cpeSpid].cpeNyilakKi[i].cpeRwFixed == 'undefined') {
				nyiluzemmodact(pnunidk[pms.cpeSpid].cpeNyilakKi[i].cpeCelid,((typeof pnunidk[pms.cpeSpid].cpeNyilakKi[i].cpeCelLatLng == 'undefined') ? 1 : 0),((typeof pnunidk[pms.cpeSpid].cpeNyilakKi[i].cpeCelLatLng == 'undefined') ? 0 : pnunidk[pms.cpeSpid].cpeNyilakKi[i].cpeCelLatLng),pms.cpeSpid,1);
				nyiluzemmodact(pnunidk[pms.cpeSpid].cpeNyilakKi[i].cpeCelid,((typeof pnunidk[pms.cpeSpid].cpeNyilakKi[i].cpeCelLatLng == 'undefined') ? 1 : 0),((typeof pnunidk[pms.cpeSpid].cpeNyilakKi[i].cpeCelLatLng == 'undefined') ? 0 : pnunidk[pms.cpeSpid].cpeNyilakKi[i].cpeCelLatLng),pms.cpeSpid,1);
			};
			//unmapactionsmod(true,"nd",pms.cpeSpid,pnunidk[pms.cpeSpid].cpeNyilakKi[i].cpeCelid);
			//unmapactionsmod(true,"ni",pms.cpeSpid,pnunidk[pms.cpeSpid].cpeNyilakKi[i].cpeCelid);
			//$("#ckbnyilact").val(unmapactions);
		};
		for (i in pnunidk[pms.cpeSpid].cpeNyilakBe) {
			//console.log(pnunidk[pms.cpeSpid].cpeNyilakBe[i].cpeStid);
			//console.log(pnunidk[pms.cpeSpid].cpeNyilakBe[i].getVertex(0).lat());
			//console.log(pnunidk[pms.cpeSpid].cpeNyilakBe[i].getVertex(1).lat());
			pnunidk[pms.cpeSpid].cpeNyilakBe[i].insertVertex(1,pms.getLatLng());
			pnunidk[pms.cpeSpid].cpeNyilakBe[i].deleteVertex(2);
			//console.log(pnunidk[pms.cpeSpid].cpeNyilakBe[i].getVertex(0).lat());
			//console.log(pnunidk[pms.cpeSpid].cpeNyilakBe[i].getVertex(1).lat());
			if (typeof pnunidk[pms.cpeSpid].cpeNyilakBe[i].cpeRwFixed == 'undefined') {
				nyiluzemmodact(pms.cpeSpid,1,0,pnunidk[pms.cpeSpid].cpeNyilakBe[i].cpeCelid,1);
				nyiluzemmodact(pms.cpeSpid,1,0,pnunidk[pms.cpeSpid].cpeNyilakBe[i].cpeCelid,1);
			};
			//unmapactionsmod(true,"nd",pnunidk[pms.cpeSpid].cpeNyilakBe[i].cpeSpid,pms.cpeSpid);
			//unmapactionsmod(true,"ni",pnunidk[pms.cpeSpid].cpeNyilakBe[i].cpeSpid,pms.cpeSpid);
			//$("#ckbnyilact").val(unmapactions);
		};
	};
	
	document.getElementById('h_lat_str_'+pms.cpeSpid).value = pms.getLatLng().lat().toString();
	document.getElementById('h_lng_str_'+pms.cpeSpid).value = pms.getLatLng().lng().toString();
	document.getElementById('h_lat_dir_'+pms.cpeSpid).value = pms.cpeIranyMutato.getVertex(1).lat().toString();
	document.getElementById('h_lng_dir_'+pms.cpeSpid).value = pms.cpeIranyMutato.getVertex(1).lng().toString();
	//console.log(pms.cpeSpid);
	//console.log(document.getElementById('h_lat_str_'+pms.cpeSpid).value);
	document.getElementById('h_direction_'+pms.cpeSpid).value = pms.cpeDirectionNext;
	document.getElementById('h_direction_override_'+pms.cpeSpid).value = pms.cpeDirectionManual;

  };
 }

function hibaadd(phibastr) {
  document.getElementById("hibalista").value += phibastr + "\n";
};
//]]>

