var gmarkers = [];
//var j=0;
var mcnt =0;
var standardIcon = sDir + "/images/trackpoint2.png";
var startIcon = sDir + "/images/startpoint.gif";
var stopIcon = sDir + "/images/endpoint.gif";
var currentIcon = sDir + "/images/ship2.png";

function highlightMarker(j) {
	
	var seitenlimit = limit;
	
	//alert(j);
	if (page>1) {j = j+1;}
	if (lastpage==1) { seitenlimit = seitenlimit + 1;} 
	
	var m = j;
	var j = j + ((page-1)*seitenlimit);
	//alert(j);
	
    for (var i = 0; i < gmarkers.length; i++) {
		 if (i == 0){
			 gmarkers[i].setImage(startIcon);
     	}
     	else if (i == (mcnt - 1)) {
     		gmarkers[i].setImage(stopIcon);
     	}
     	else {
     		gmarkers[i].setImage(standardIcon);
     	}	    	
    }
	//alert(gmarkers[0].setImage);
    //alert(j+' '+gmarkers.length);

    
    tmpbounds=null;
    clearTimeout(timer);
    
    if (j >=0) {
		 if (j < gmarkers.length) {
		    	
			 gmarkers[j].setImage(currentIcon);
			    	zoomClose(4,j);
			    
			    	//alert(gmarkers[j].C.x);
			    	//gmarkers[j].hide();    	if (page == 1 && j == 0) { centerAndZoomOnBounds2(bounds);
				}
	

	    else {
	    	//alert('ah');
	    	centerAndZoomOnBounds2(bounds);
	    }
    }
    else {
    	//alert('uh'); 
    	centerAndZoomOnBounds(bounds);
    	
    }

//alert(j);
    if (j!= -1) {dynamicRefresh(m);}
    //alert(j);
	   //gmap.setCenter(new GLatLng(gmarkers[j].C.y,gmarkers[j].C.x), 15);

//	j++;
/*obj = gmarkers[0];
		   if(typeof obj == "object") {
		      return "Type: "+typeof(obj)+((obj.constructor) ? "\nConstructor: "+obj.constructor : "")+"\nValue: " + obj;
		   } else {
		      return "Type: "+typeof(obj)+"\nValue: "+obj;
		   
		   }
		   */
}
var map;
var gmap;
var bounds;
var tmpbounds;
var timer; 

function loadmap (mapid) {

    	var map = document.getElementById("map"+mapid);
    	var linesArray= [];
    	bounds = new GLatLngBounds();
    	
    	if (GBrowserIsCompatible()) {
    		gmap = new GMap2(map);
    		 var customUI = gmap.getDefaultUI();
    	        // Remove MapType.G_HYBRID_MAP
    	        customUI.maptypes.hybrid = false;
    	        gmap.setUI(customUI);

    		//gmap.addControl(new GSmallMapControl());
    		
    		gmap.disableDoubleClickZoom();
			gmap.disableScrollWheelZoom();
    		gmap.setCenter ( new GLatLng(0,0), 0 );
    		gmap.setMapType(G_SATELLITE_MAP);
    		function makeIcon (image) {
    			var icon = new GIcon();
    			var msize;
    			var mheight;
    			msize=32;
    			mheight=40;
    			if ((image == (sDir + "/images/startpoint.gif")) || (image == (sDir + "/images/endpoint.gif"))) {
    				
    			}
    			if (image == (sDir + "/images/normal.png")) {
    				msize= 24;
    			}
    			icon.image = image;
    			icon.iconSize = new GSize(msize, mheight);
    			icon.iconAnchor = new GPoint(16, 35);
    			icon.infoWindowAnchor = new GPoint(8, 1);	
    			return icon;
    		}


    		
    		function createMarker(input,icon,k) {
    			var marker = new GMarker(new GLatLng(input.poiLat,input.poiLon), icon );
	   			GEvent.addListener(marker, "click", function() {
	   				//marker.openInfoWindowHtml("ba"+k);  
	   				var d;
	   				d = k;
	   				d = (k-((limit*(page-1))))+1;

	   				if (page == 1) {d = k+2;}
	   				mapClick(d);
					jQuery('ul.pagination a[rel="'+(d)+'"]').click();
					//highlightMarker(k);
    			});
	   			alert(marker);
	   			mcnt++;
     			gmarkers.push(marker);
    			return marker;
    		}
    	    function createLines(input) {
    			var lines = new GLatLng(input.poiLat,input.poiLon);
    			linesArray.push(lines);
    			bounds.extend(lines);
    			return lines;
    		}

    	    function parseJson (doc) {
    			
    	    	//var stopIcon = sDir + "/images/stop.png";
    	    	//var standardIcon = sDir + "/images/icon2-black.png";
    			var jsonData = eval("(" + doc + ")");
    	        for (var i = 0; i < jsonData.length; i++) {
    	        	if (i == 0){
    	        		var marker = createMarker(jsonData[i],makeIcon(startIcon),i);
    	        	}
    	        	else if (i == (jsonData.length - 1)) {
    	        		var marker = createMarker(jsonData[i],makeIcon(stopIcon),i);
    	        	}
    	        	else {
    	        		var marker = createMarker(jsonData[i],makeIcon(standardIcon),i);
    	        	}
    				gmap.addOverlay(marker);
    				createLines(jsonData[i]);
    			}
    	    	var lines = new GPolyline(linesArray, 'red', 1, 1);
    			gmap.addOverlay(lines);
    			centerAndZoomOnBounds(bounds);
   		}     	
       		
    		GDownloadUrl(sDir+"/ajax/getturngm/cruiseId/"+mapid+"/sort/asc", function(data, responseCode) { 
    			parseJson(data);
    			//alert(activeMarker);
    			
    			if (!(page == 1 && activeMarker == 1)) {
    				//if (activeMarker==null) { activeMarker=1;}
    				//if (page)
    				if (activeMarker==-1) {
    					highlightMarker(activeMarker);
    				}
    				else {
    					//alert(activeMarker);
    					highlightMarker(activeMarker-2);
    				}
    			}
    		});

    	} else {
    		alert("Sorry, your browser cannot handle the true power of Google Maps");
    	}
    }


function loadmap2 (mapid, sprache) {

    	var map = document.getElementById("map"+mapid);
    	var linesArray= [];
    	bounds = new GLatLngBounds();
    	
    	if (GBrowserIsCompatible()) {
    		gmap = new GMap2(map);
    		


    		 var customUI = gmap.getDefaultUI();
    	        // Remove MapType.G_HYBRID_MAP
    	        customUI.maptypes.hybrid = false;
    	        gmap.setUI(customUI);

    		//gmap.addControl(new GSmallMapControl());
    		
    		gmap.disableDoubleClickZoom();
			gmap.disableScrollWheelZoom();
    		gmap.setCenter ( new GLatLng(0,0), 0 );
    		gmap.setMapType(G_SATELLITE_MAP);
    		function makeIcon (image) {
    			var icon = new GIcon();
    			var msize;
    			var mheight;
    			msize=32;
    			mheight=40;
    			if ((image == (sDir + "/images/startpoint.gif")) || (image == (sDir + "/images/endpoint.gif"))) {
    				
    			}
    			if (image == (sDir + "/images/normal.png")) {
    				msize= 24;
    			}
    			icon.image = image;
    			icon.iconSize = new GSize(msize, mheight);
    			icon.iconAnchor = new GPoint(16, 35);
    			icon.infoWindowAnchor = new GPoint(8, 1);	
    			return icon;
    		}
    		
    		function createMarker(input,icon,k) {
    			var marker = new GMarker(new GLatLng(input.poiLat,input.poiLon), icon );
	   			GEvent.addListener(marker, "click", function() {
	   				//marker.openInfoWindowHtml("ba"+k);  
	   				var d;
	   				d = k;
	   				d = (k-((limit*(page-1))))+1;

	   				if (page == 1) {d = k+2;}
	   				mapClick(d);
					jQuery('ul.pagination a[rel="'+(d)+'"]').click();
					//highlightMarker(k);
    			});
	   			mcnt++;
     			gmarkers.push(marker);
    			return marker;
    		}
    		
    	    function createLines(input) {
    			var lines = new GLatLng(input.poiLat,input.poiLon);
    			linesArray.push(lines);
    			bounds.extend(lines);
    			return lines;
    		}

    	    function parseJson (doc) {
    	    	//var stopIcon = sDir + "/images/stop.png";
    	    	//var standardIcon = sDir + "/images/icon2-black.png";
    			var jsonData = eval("(" + doc + ")");
    	        for (var i = 0; i < jsonData.length; i++) {
    	        	if (i == 0){
    	        		var marker = createMarker(jsonData[i],makeIcon(startIcon),i);
    	        	}
    	        	else if (i == (jsonData.length - 1)) {
    	        		var marker = createMarker(jsonData[i],makeIcon(stopIcon),i);
    	        	}
    	        	else {
    	        		var marker = createMarker(jsonData[i],makeIcon(standardIcon),i);
    	        	}
    				gmap.addOverlay(marker);
    				createLines(jsonData[i]);
    			}
    	    	var lines = new GPolyline(linesArray, 'red', 1, 1);
    			gmap.addOverlay(lines);
    			centerAndZoomOnBounds(bounds);
    	    }     	
       		
    		GDownloadUrl(sDir+"/ajax/getturngm/cruiseId/"+mapid+"/sort/asc", function(data, responseCode) { 
    			parseJson(data);
    			//alert(activeMarker);
    			
    			if (!(page == 1 && activeMarker == 1)) {
    				//if (activeMarker==null) { activeMarker=1;}
    				//if (page)
    				if (activeMarker==-1) {
    					highlightMarker(activeMarker);
    				}
    				else {
    					//alert(activeMarker);
    					highlightMarker(activeMarker-2);
    				}
    			}
    			
    		});
			
			// We define the function first
			function WikipediaOverlay() {
			}

			// To "subclass" the GControl, we set the prototype object to
			// an instance of the GControl object
			WikipediaOverlay.prototype = new GControl();

			WikipediaOverlay.prototype.initialize = function(map) {
				var container = document.createElement("div");
				var wikiLayer = new GLayer("org.wikipedia."+sprache);
				
				var wikiOnDiv = document.createElement("div");
				this.setButtonStyle_(wikiOnDiv);
				container.appendChild(wikiOnDiv);
				wikiOnDiv.appendChild(document.createTextNode("Wikipedia"));

				var wikiOffDiv = document.createElement("div");
				this.setButtonStyle_(wikiOffDiv);
				wikiOffDiv.appendChild(document.createTextNode("Wikipedia"));

				GEvent.addDomListener(wikiOnDiv, "click", function() {
					map.addOverlay(wikiLayer);
					container.removeChild(wikiOnDiv);
					container.appendChild(wikiOffDiv);
				});

				GEvent.addDomListener(wikiOffDiv, "click", function() {
					map.removeOverlay(wikiLayer);
					container.removeChild(wikiOffDiv);
					container.appendChild(wikiOnDiv);
				});
				
				map.getContainer().appendChild(container);
				return container;
			};

			// By default, the control will appear in the top left corner of the
			// map with 7 pixels of padding.
			WikipediaOverlay.prototype.getDefaultPosition = function() {
			  return new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(7, 30));
			};

			// Sets the proper CSS for the given button element.
			WikipediaOverlay.prototype.setButtonStyle_ = function(button) {
			  button.style.textDecoration = "none";
			  button.style.color = "#000000";
			  button.style.backgroundColor = "white";
			  button.style.font = "small Arial";
			  button.style.border = "1px solid black";
			  button.style.padding = "2px";
			  button.style.marginBottom = "3px";
			  button.style.textAlign = "center";
			  button.style.width = "68px";
			  button.style.cursor = "pointer";
			};

			// We define the function first
			function PanoramioOverlay() {
			};

			// To "subclass" the GControl, we set the prototype object to
			// an instance of the GControl object
			PanoramioOverlay.prototype = new GControl();

			PanoramioOverlay.prototype.initialize = function(map) {
				var container = document.createElement("div");
				var panoramioLayer = new GLayer("com.panoramio.all");
				
				var imageOnDiv = document.createElement("div");
				this.setButtonStyle_(imageOnDiv);
				container.appendChild(imageOnDiv);
				imageOnDiv.appendChild(document.createTextNode("Panoramio"));

				var imageOffDiv = document.createElement("div");
				this.setButtonStyle_(imageOffDiv);
				imageOffDiv.appendChild(document.createTextNode("Panoramio"));

				GEvent.addDomListener(imageOnDiv, "click", function() {
					map.addOverlay(panoramioLayer);
					container.removeChild(imageOnDiv);
					container.appendChild(imageOffDiv);
				});

				GEvent.addDomListener(imageOffDiv, "click", function() {
					map.removeOverlay(panoramioLayer);
					container.removeChild(imageOffDiv);
					container.appendChild(imageOnDiv);
				});
				
				map.getContainer().appendChild(container);
				return container;
			};

			// By default, the control will appear in the top left corner of the
			// map with 7 pixels of padding.
			PanoramioOverlay.prototype.getDefaultPosition = function() {
			  return new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(80, 30));
			};

			// Sets the proper CSS for the given button element.
			PanoramioOverlay.prototype.setButtonStyle_ = function(button) {
			  button.style.textDecoration = "none";
			  button.style.color = "#000000";
			  button.style.backgroundColor = "white";
			  button.style.font = "small Arial";
			  button.style.border = "1px solid black";
			  button.style.padding = "2px";
			  button.style.marginBottom = "3px";
			  button.style.textAlign = "center";
			  button.style.width = "70px";
			  button.style.cursor = "pointer";
			};

			gmap.addControl(new WikipediaOverlay());
			gmap.addControl(new PanoramioOverlay());
			
    	} else {
    		alert("Sorry, your browser cannot handle the true power of Google Maps");
    	}
    }
   

   
   
   function showmap(poimap) {
	   load_poimap + poimap();
   }
   
   function centerAndZoomOnBounds(bounds) {
	   var center_lat = (bounds.getNorthEast().lat() + bounds.getSouthWest().lat()) / 2.0;
	   var center_lng = (bounds.getNorthEast().lng() + bounds.getSouthWest().lng()) / 2.0;
	   var center = new GLatLng(center_lat,center_lng);
	   gmap.setCenter(center, gmap.getBoundsZoomLevel(bounds));
   }
   
   function centerAndZoomOnBounds2(bounds) {
	   var center_lat = (bounds.getNorthEast().lat() + bounds.getSouthWest().lat()) / 2.0;
	   var center_lng = (bounds.getNorthEast().lng() + bounds.getSouthWest().lng()) / 2.0;
	   var center = new GLatLng(center_lat,center_lng);
	   gmap.panTo(center, gmap.getBoundsZoomLevel(bounds));
   }
   
function zoomClose(cnt,j) {
	tmpbounds = new GLatLngBounds();
	
	//alert(gmarkers.toSource());
	
	if (gmarkers.length-cnt >= j) {
		
		for (i=0; i<cnt;i++) {
			// war !!! FEHLER !!!    
			tmpbounds.extend(new GLatLng(gmarkers[j+i].Aa.y,gmarkers[j+i].Aa.x));
		}
		// war Kein Fehler - aber nur einkommentieren, wenn alles wieder geht:  
		timer = setTimeout("zoomStuff()",1500);
	}
	// war !!! FEHLER !!!    
	 gmap.panTo(new GLatLng(gmarkers[j].Aa.y,gmarkers[j].Aa.x));
	   
	// altes Zeugs, war bereits auskommentiert
	// gmap.panTo(new GLatLng(gmarkers[j].C.y,gmarkers[j].C.x),13);
}

function zoomStuff () {
	gmap.setZoom(gmap.getBoundsZoomLevel(tmpbounds)-1);
}


function stoper() {
	clearTimeout(timer);
}

function zoomMap() {
	
}

