var mapstorage = new Object;

/*var side_bar_html = "";
var gmarkers = [];
var htmls = [];
var i = 0;
var gpoint = [];
var glat = [];
var glng = [];
var map;
var maxmarker = 0;
var baseIcon = 0;
var lasttoplevelcnt = "";
*/

/*----------------------------------------------------------------------------*/

function MapObj()
{
	this.side_bar_html = "";
	this.gmarkers = [];
	this.htmls = [];
	this.gpoint = [];
	this.glat = [];
	this.glng = [];
	this.map;
	this.maxmarker = 0;
	this.baseIcon = 0;
	this.lasttoplevelcnt = "";
	this.i = 0;
	this.toplevelcnt = '';
	this.loaded = 0;
	
	this.baseIcon = new GIcon();
	this.baseIcon.iconSize=new GSize(32,32);
	this.baseIcon.shadowSize=new GSize(56,32);
	this.baseIcon.iconAnchor=new GPoint(16,32);
	this.baseIcon.infoWindowAnchor=new GPoint(16,0);
}

/*----------------------------------------------------------------------------*/

MapObj.prototype.constructor = MapObj;

/*----------------------------------------------------------------------------*/

MapObj.prototype.createMarker = function(point,name,html,icon) {
	var marker = new GMarker(point);
	
	if (String(icon).length>2)
	{
		var parametry = icon.split('|');
		var ikonka = new GIcon(this.baseIcon
								, "http://maps.google.com/mapfiles/kml/pal"+parametry[0]+"/icon"+parametry[1]+".png"
								, null
								, "http://maps.google.com/mapfiles/kml/pal"+parametry[0]+"/icon"+parametry[1]+"s.png"
								);
		marker = new GMarker(point, ikonka);
	}
	
	GEvent.addListener(marker, "click", function() {
		marker.openInfoWindowHtml(html);
	});

	// save the info we need to use later for the side_bar
	this.gmarkers[this.i] = marker;
	this.htmls[this.i] = html;
	
	// add a line to the side_bar html
	this.side_bar_html += '<div class="line"><a href="javascript:mapclick(' + this.i + ',\''+this.toplevelcnt+'\')"><img src="'+"http://maps.google.com/mapfiles/kml/pal"+parametry[0]+"/icon"+parametry[1]+".png"+'" width="32" height="32" /></a> <a href="javascript:mapclick(' + this.i + ',\''+this.toplevelcnt+'\')">' + name + '</a></div>';
	this.i++;
	return marker;
};

/*----------------------------------------------------------------------------*/
// This function picks up the click and opens the corresponding info window

function mapclick(clickno, storagename)
{
	var thisstorage = mapstorage[storagename];
	var mybounds = new GLatLngBounds();

	if (clickno==0)
	{
		var deltalat =  thisstorage.glat[0] - thisstorage.glat[1];
		var deltalng =  thisstorage.glng[0] - thisstorage.glng[1];
		var pseudopoint1 = new GLatLng(thisstorage.glat[0]+deltalat,thisstorage.glng[0]-deltalng);
		mybounds.extend(pseudopoint1);
	};

	if (clickno==thisstorage.maxmarker)
	{
		var deltalat =  thisstorage.glat[thisstorage.maxmarker-1] - thisstorage.glat[thisstorage.maxmarker];
		var deltalng =  thisstorage.glng[thisstorage.maxmarker-1] - thisstorage.glng[thisstorage.maxmarker];
		var pseudopoint2 = new GLatLng(thisstorage.glat[thisstorage.maxmarker]+deltalat,thisstorage.glng[thisstorage.maxmarker]-deltalng);
		mybounds.extend(pseudopoint2);
	};

	for (var j=Math.max(clickno-1,0);j<Math.min(clickno+2,thisstorage.maxmarker);j++)
	{
		mybounds.extend(thisstorage.gpoint[j]);
	};
 
	thisstorage.map.setZoom(Math.min(thisstorage.map.getBoundsZoomLevel(mybounds),18));
	thisstorage.map.setCenter(mybounds.getCenter());
	
	thisstorage.gmarkers[clickno].openInfoWindowHtml(thisstorage.htmls[clickno]);
};

/*----------------------------------------------------------------------------*/

MapObj.prototype.runmap = function() {
	if (GBrowserIsCompatible())
	{
		var rootel = this;
		
		$.getJSON("mapy/"+this.toplevelcnt+".map",function(data){
		
		if (rootel.loaded == 1)
			return;

		// Display the map, with some controls and set the initial location 
		
		rootel.map = new GMap2(document.getElementById(rootel.toplevelcnt));
		rootel.map.addControl(new GLargeMapControl());
		rootel.map.addControl(new GMapTypeControl());
		rootel.map.setCenter(new GLatLng(0,0),0);
		var bounds = new GLatLngBounds();

		//var request = GXmlHttp.create();
		//request.open("GET", "mapy/"+toplevelcnt+".xml", true);

		//request.onreadystatechange = function() {
			//if (request.readyState == 4)
			//{
				//alert('done');
			
				//var xmlDoc = GXml.parse(request.responseText);
				// obtain the array of markers and loop through it
				var markers =  data.markers;//xmlDoc.documentElement.getElementsByTagName("marker");
    
				rootel.maxmarker = markers.length;
  
				for (var i = 0; i < markers.length; i++)
				{
					// obtain the attribues of each marker
					//var lat = parseFloat(markers[i].getAttribute("lat"));
					//var lng = parseFloat(markers[i].getAttribute("lng"));
					var lat = parseFloat(markers[i].lat);
					var lng = parseFloat(markers[i].lng);
					var point = new GLatLng(lat,lng);
					rootel.gpoint[i] = point;
					rootel.glat[i] = lat;
					rootel.glng[i] = lng;
					/*
					var html = markers[i].getAttribute("html");
					var label = markers[i].getAttribute("label");
					var iconsrc = markers[i].getAttribute("gicon");
					*/
					
					var html = markers[i].html;
					var label = markers[i].label;
					var iconsrc = markers[i].gicon;
					// create the marker
					var marker = rootel.createMarker(point,label,html,iconsrc);
					rootel.map.addOverlay(marker);
					bounds.extend(point);
				}
				
				
				// put the assembled side_bar_html contents into the side_bar div
				document.getElementById(rootel.toplevelcnt+'_side_bar').innerHTML = rootel.side_bar_html;
    
				
				// ========= Now process the polylines ===========
				//var lines = xmlDoc.documentElement.getElementsByTagName("line");
				
				var lines =  data.lines;
				
				// read each line
				for (var a = 0; a < lines.length; a++) 
				{
					// get any line attributes
					var colour = lines[a].colour; //lines[a].getAttribute("colour");
					var width  = lines[a].width; //parseFloat(lines[a].getAttribute("width"));
					// read each point on that line
					var points = lines[a].points;//lines[a].getElementsByTagName("point");
					var pts = [];
					
					for (var i = 0; i < points.length; i++)
					{
						pts[i] = new GLatLng(	parseFloat(points[i].lat), //points[i].getAttribute("lat")
												parseFloat(points[i].lng)  // points[i].getAttribute("lng")
											);
					}
					
					rootel.map.addOverlay(new GPolyline(pts,colour,width));
				}
				
				// ================================================           
  				
				rootel.map.setZoom(rootel.map.getBoundsZoomLevel(bounds));
				rootel.map.setCenter(bounds.getCenter());
				
			//}
			//else
			//{
			//	alert(request.readyState);
			//}
		//   };

		//request.send(null);
		
		
		});
	} else
	{
		alert("Niestety, w tej przeglądarce Google Maps API nie działa :(");
	}
};

/*----------------------------------------------------------------------------*/

function CreateMap(toplevelcnt_p)
{
	if (typeof mapstorage[toplevelcnt_p] == 'undefined')
	{
		mapstorage[toplevelcnt_p] = new MapObj;
		mapstorage[toplevelcnt_p].toplevelcnt = toplevelcnt_p;
	}
	return mapstorage[toplevelcnt_p];
};

/*----------------------------------------------------------------------------*/

function LoadMap(toplevelcnt_p)
{
	if (typeof mapstorage[toplevelcnt_p] == 'undefined')
	{
		CreateMap(toplevelcnt_p);
		mapstorage[toplevelcnt_p].runmap();
	}
};

/*----------------------------------------------------------------------------*/

