﻿var map;
var markers=[];

var base_icon = new GIcon();
base_icon.image = '';
base_icon.iconSize = new GSize(20,20);
base_icon.shadow = '';
base_icon.shadowSize = new GSize(0,0);
base_icon.iconAnchor = new GPoint(16,16);
base_icon.infoWindowAnchor = new GPoint(16,16);

function pokaz_ukryj(pokaz, ukryj, ukryj2, ukryj3) { 
	document.getElementById(pokaz).style.display = "block"; 
	document.getElementById(ukryj).style.display = "none";
	document.getElementById(ukryj2).style.display = "none";
	document.getElementById(ukryj3).style.display = "none";

}


function przybliz()
		{
			map.zoomIn();
		
		}
		
function oddal()
		{
			map.zoomOut();
		
		}
function mapa1()
		{
map.setMapType(G_NORMAL_MAP); 
}

function mapa2()
		{
map.setMapType(G_SATELLITE_MAP); 
}
function mapa3()
		{
map.setMapType(G_HYBRID_MAP); 
}
function mapa4()
		{
map.setMapType(G_PHYSICAL_MAP); 
}
function mapa5()
		{
//map.setMapType(mapaOSL1); 
map.setMapType(G_SATELLITE_3D_MAP);

}
function mapa6()
		{

map.setMapType(mapaOSL2);

}
function mapa7()
		{

map.setMapType(mapaOSL1);

}
function wik()
		{
var myLayer = new GLayer("org.wikipedia.pl");	
map.addOverlay(myLayer);

}	




function schowajDiv(kate, checkbox)
{			
	if(document.getElementById(checkbox).checked)
	document.getElementById(kate).style.display = "block"; 
	else
		document.getElementById(kate).style.display = "none";

}


function deg2dms(liczba,latlng)
{
	// jeśli nie określono argumentu to nie wykonujemy funkcji
	if(latlng!='lat' && latlng!='lng') return;
	
	if(liczba>0)
		var znak=0;
	else
		var znak=1;
	
	liczba = Math.abs(liczba);
	
	var d = Math.floor(liczba);
	var s = ((liczba - Math.floor(liczba))*3600);
	var m = Math.floor(s/60);
	
	s = (s - m*60).toFixed(2);
	
	if(s<10)
		s='0'+s;
	if(m<10)
		m='0'+m;
	
	if(latlng=='lat')
		var sufix=['N','S'];
	else
		var sufix=['E','W'];
		
	return d+'°'+m+'\''+s+'\'\' '+sufix[znak];
}





function GranicaGminy(){
 

	
	map.addOverlay(new GPolyline([
        new GLatLng(50.676696,18.713493),
		new GLatLng(50.6912,18.727226),
		new GLatLng(50.692886,18.739672),
		new GLatLng(50.688987,18.757954),
		new GLatLng(50.679633,18.772888),
		new GLatLng(50.677161,18.779969),
		new GLatLng(50.677297,18.782802),
		new GLatLng(50.67591,18.785291),
		new GLatLng(50.675258,18.80383),
		new GLatLng(50.677705,18.809),
		new GLatLng(50.677433,18.819966),
		new GLatLng(50.681839,18.827691),
		new GLatLng(50.685047,18.835673),
		new GLatLng(50.684259,18.839142),
		new GLatLng(50.684476,18.844249),
		new GLatLng(50.688256,18.851931),
		new GLatLng(50.682391,18.868761),
		new GLatLng(50.676789,18.867216),
		new GLatLng(50.67532,18.869019),
		new GLatLng(50.671948,18.869348),
		new GLatLng(50.665746,18.870206),
		new GLatLng(50.660414,18.859992),
		new GLatLng(50.656714,18.858032),
		new GLatLng(50.655245,18.865499),
		new GLatLng(50.652307,18.870206),
		new GLatLng(50.646211,18.893223),
		new GLatLng(50.646756,18.896484),
		new GLatLng(50.645123,18.898358),
		new GLatLng(50.643817,18.910031),
		new GLatLng(50.646157,18.917155),
		new GLatLng(50.640986,18.927455),
		new GLatLng(50.639625,18.941274),
		new GLatLng(50.614307,18.93827),
		new GLatLng(50.612945,18.944535),
		new GLatLng(50.610331,18.945479),
		new GLatLng(50.608261,18.944621),
		new GLatLng(50.606028,18.947024),
		new GLatLng(50.604394,18.945479),
		new GLatLng(50.604012,18.938441),
		new GLatLng(50.600254,18.936381),
		new GLatLng(50.596821,18.944879),
		new GLatLng(50.580201,18.943505),
		new GLatLng(50.574151,18.93621),
		new GLatLng(50.576767,18.916898),
		new GLatLng(50.575023,18.909001),
		new GLatLng(50.58031,18.905997),
		new GLatLng(50.579765,18.880162),
		new GLatLng(50.592789,18.871422),
		new GLatLng(50.601615,18.864112),
		new GLatLng(50.612183,18.85201),
		new GLatLng(50.616485,18.845844),
		new GLatLng(50.609514,18.843083),
		new GLatLng(50.602433,18.836646),
		new GLatLng(50.600417,18.832612),
		new GLatLng(50.593007,18.822484),
		new GLatLng(50.588594,18.819308),
		new GLatLng(50.586468,18.81536),
		new GLatLng(50.580255,18.829865),
		new GLatLng(50.572843,18.84789),
		new GLatLng(50.574314,18.859477),
		new GLatLng(50.566464,18.867459),
		new GLatLng(50.560958,18.864198),
		new GLatLng(50.556705,18.850722),
		new GLatLng(50.553433,18.85982),
		new GLatLng(50.539306,18.848319),
		new GLatLng(50.544107,18.836303),
		new GLatLng(50.55605,18.81742),
		new GLatLng(50.566464,18.802829),
		new GLatLng(50.566464,18.794074),
		new GLatLng(50.573115,18.786349),
		new GLatLng(50.58576,18.76987),
		new GLatLng(50.589084,18.762574),
		new GLatLng(50.592408,18.767724),
		new GLatLng(50.593171,18.77502),
		new GLatLng(50.592081,18.79313),
		new GLatLng(50.604557,18.787036),
		new GLatLng(50.614525,18.773818),
		new GLatLng(50.620624,18.761201),
		new GLatLng(50.615832,18.743949),
		new GLatLng(50.619807,18.737597),
		new GLatLng(50.633855,18.75751),
		new GLatLng(50.64017,18.752875),
		new GLatLng(50.641857,18.755708),
		new GLatLng(50.642456,18.754849),
		new GLatLng(50.642075,18.747725),
		new GLatLng(50.643871,18.745751),
		new GLatLng(50.645558,18.747382),
		new GLatLng(50.647463,18.746095),
		new GLatLng(50.647082,18.742833),
		new GLatLng(50.648606,18.74146),
		new GLatLng(50.650075,18.743691),
		new GLatLng(50.652143,18.740172),
		new GLatLng(50.658564,18.739142),
		new GLatLng(50.676696,18.713493)


		
    ], "blue", 4, 0.4));

	refreshMap();
	
	}





function refreshInfo()
{
	var code='<ul style="height: 426px; overflow: auto;">'
	var counter=0;
	for(i=0; i<markers.length; i++)
	{
		if(!markers[i].isHidden())
		{
			code+='<li><a href="#" onclick="markers['+i+'].showInfo(); return false;">'+markers[i].limit+'</a></li>';
			counter++;
		}
	}
	code+='</ul>';
	if(counter!=i)
		code='<h2>Nawigacja</h2>'+code;
	else
		code='<h2>Nawigacja</h2>'+code;
	document.getElementById('info').innerHTML=code;
}

function refreshMap(checkbox,category)
{			
	if(document.getElementById(checkbox).checked)
		hideCategory(category,true);
	else
		hideCategory(category,false);
	refreshInfo();
}

function windowContent(limit,local,zdjecie,links,web,pgk,wiki,nk,fb)
{
	m1='<div id=marker><div id="limit">'+limit+'</div><div id="local"><img src="'+zdjecie+'">'+local+'</div>';
	m2='<div id="links">';
	m3='<a href="http://'+web+'" target="_blank"><img src="jpg/firefox.jpg"></a>';
	m4='<a href="http://'+pgk+'" target="_blank"><img src="jpg/koszecin.jpg"></a>';
	m5='<a href="http://'+wiki+'" target="_blank"><img src="jpg/wiki.jpg"></a>';
	m6='<a href="http://'+nk+'" target="_blank"><img src="jpg/nk.jpg"></a>';
	m7='<a href="http://'+fb+'" target="_blank"><img src="jpg/fb.jpg"></a>';
	m8='</div></div>';
	
	razem= m1+m2+m3+m4+m5+m6+m7+m8;
	return razem ;
	}

function hideCategory(category,show)
{
	for(var i=0; i<markers.length; i++)
	{
		if(markers[i].category==category)
		{
			if(show==true)
				markers[i].show();
			else
				markers[i].hide();
		}
	}
}

GMarker.prototype.showInfo=function()
{
	this.openInfoWindowHtml(windowContent(this.limit,this.local,this.zdjecie,this.links,this.web,this.pgk,this.wiki,this.nk,this.fb));
};

function addMarker(lat,long,limit,local,zdjecie,links,category,iconfile,web,pgk,wiki,nk,fb)
{
	var punkt = new GLatLng(lat,long);
	var icon = new GIcon(base_icon);
	icon.image = iconfile;
	
	var marker = new GMarker(punkt,{icon: icon, title: limit});
	
	marker.category = category;
	marker.limit = limit;
	marker.local = local;
	marker.zdjecie = zdjecie;
	marker.links = links;
	marker.web = web;
	marker.pgk = pgk;
	marker.wiki = wiki;
	marker.nk = nk;
	marker.fb = fb;
	markers.push(marker);
	map.addOverlay(marker);
	GEvent.addListener(marker,'click',function()
	{
		marker.showInfo();
	});
}

function loadMap(xml_file)
{
	if(GBrowserIsCompatible())  
	{
	

		map = new GMap2(document.getElementById("map"));
		map.setCenter(new GLatLng(50.636007,18.841099),13);

		map.setMapType(mapaOSL1);
	
		
		

		GEvent.addListener(map,'mousemove',function(punkt)
				

{
	var lat = punkt.lat().toFixed(5);  // pobieramy współrzędne
	var lng = punkt.lng().toFixed(5);

	var zoom = map.getZoom(); // pobieramy zoom

	// formujemy napis
	
	var napis0 ='<b>Zoom mapy: '+zoom+'</b>';
	document.getElementById('POW').innerHTML=napis0;
	
	
	var napis1 = '<b>Współżędne GPS: B &nbsp; </b>= '+lat+',&nbsp; <b>L</b> = '+lng+'';
	document.getElementById('GPS').innerHTML=napis1;
});
				
	GEvent.addListener(map,'mousemove',function(punkt)
				{
					var lat = deg2dms(punkt.lat(),'lat');  // pobieramy współrzędne
					var lng = deg2dms(punkt.lng(),'lng');


					// formujemy napis
					var napis2 = '<b>Współżędne geograficzne:&nbsp; </b> '+lat+' ,&nbsp;  '+lng+'';
					document.getElementById('GEO').innerHTML=napis2;
				});
				
				
				

				
				
				
		
		
		
		GDownloadUrl(xml_file, function(data,response)
		{
			if(response==200)
			{
				var xml = GXml.parse(data);
				var markers = xml.documentElement.getElementsByTagName('marker');
				for(var i=0; i<markers.length; i++)
				{
					var lat = parseFloat(markers[i].getAttribute("lat"));
					var long = parseFloat(markers[i].getAttribute("long"));
					var limit = markers[i].getAttribute("limit");
					var local = markers[i].getAttribute("local");
					var zdjecie = markers[i].getAttribute("zdjecie");
					var links = markers[i].getAttribute("links");
					var category = markers[i].getAttribute("category");
					var iconfile = markers[i].getAttribute("iconfile");
					var web  = markers[i].getAttribute("web");
					var	pgk = markers[i].getAttribute("pgk");
					var	wiki = markers[i].getAttribute("wiki");
					var	nk = markers[i].getAttribute("nk");
					var	fb = markers[i].getAttribute("fb");
					
					
					
					

					var marker = addMarker(lat,long,limit,local,zdjecie,links,category,iconfile,web,pgk,wiki,nk,fb);
					
					
					refreshMap(category,category);
					refreshInfo();
				}
			}
			else
			{
				alert('Błąd odczytu danych');
			}
		});	

		GranicaGminy();


		
	}


	
	
	refreshInfo();

}



////////////////////////////////////////////







				var nota = new GCopyright(1,new GLatLngBounds(new GLatLng(-90, -180),new GLatLng(90, 180)),0,"<a href=\"http://www.openstreetmap.org/\">OpenStreetMap</a>");
				var prawaAutorskie = new GCopyrightCollection('map');
				prawaAutorskie.addCopyright(nota);
								
				var kawalki1 = [new GTileLayer(prawaAutorskie,1,17)];
				kawalki1[0].getTileUrl = function (p,z) {
					var y 	= p.y;
					var x	= p.x;
					return 'http://tile.openstreetmap.org/'+z+'/'+x+'/'+y+'.png';
				}
				var kawalki2 = [new GTileLayer(prawaAutorskie,1,17)];
				kawalki2[0].getTileUrl = function (p,z) {
					var y 	= p.y;
					var x	= p.x;
					return 'http://tah.openstreetmap.org/Tiles/tile/'+z+'/'+x+'/'+y+'.png';
				}

				var mapaOSL1 = new GMapType(kawalki1, G_NORMAL_MAP.getProjection(), "Mapa 2", {errorMessage:"Brak obrazka"});
				var mapaOSL2 = new GMapType(kawalki2, G_NORMAL_MAP.getProjection(), "Mapa 3", {errorMessage:"Brak obrazka"});
				map.addMapType(mapaOSL1);
				map.addMapType(mapaOSL2);	
				
				
				
				
				







////////////////////////////////////////////////////////////














