var mfzmap = null;
var mfzmgr = null;
var mfzCurrLines = new Array();
var mfzCurrDestMarkers = new Array();
var mfzCurrLineMarkers = new Array();
var mfzland = null;
var mfzoffers = null;
var mark = null;

function gmload() {
	if(document.getElementById("mfzmap")) {
		if (GBrowserIsCompatible()) {
		   mfzmap = new GMap2(document.getElementById("mfzmap"));
		   mfzmap.setCenter(new GLatLng(50, 10), 5);
		   mfzmap.addControl(new GSmallMapControl());
		   mfzmap.enableScrollWheelZoom()
           $.getJSON("/js/getMapResults.php",{type:'land'}, function(json){
		      mfzland = json.mfzland;
   		   }
       );
		 $.getJSON('/js/getMapResults.php',{type:'offer',count:showStartCount}, function(json) {
		     mfzoffers = json.mfzoffers;
   	         window.setTimeout(setupMFZMarkers,0);
	     });
	   GEvent.addListener(mfzmap, "click", function(marker,point) {
         if(!marker) {
           $.getJSON('/js/getMapResults.php',{type:'cityByAmbit',lati:point.y,longi:point.x}, function(json) {
             mfzcities = json.mfzcities;
             if ( mfzcities.length > 0) {
               $('#mfz_map_result').hide('slow');             
               $('#mfz_map_result_content').empty();               
               mfzmap.setCenter(point,8);
               mfzmap.clearOverlays();
               var table = '<table width="100%" border="0" cellpadding="0" cellspacing="0">';
               for (var i=0; i<mfzcities.length; i++) {
               	 string = startOfferResultTable.replace(/##land##/g,mfzcities[i].land.toUpperCase());
            	 string = string.replace(/##city##/g,mfzcities[i].place);
				 string = string.replace(/##ocount##/,mfzcities[i].ocount);
			     string = string.replace(/##rcount##/,mfzcities[i].rcount); 
                 table += string;           
                 mfzmap.addOverlay(addCityMarker(mfzcities[i]));
               }
               $('#mfz_map_result_content').append(table+'</table>');
               $('#mfz_map_result').show('slow');
             }
           });
         }
       });
		} else {
		 	document.getElementById("mfzmap").style.display='none';
		}
	}
}

/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/

function getLandIcon(l) {
  var icon = new GIcon();
  icon.image = "/images/"+l+".gif";
  icon.iconAnchor = new GPoint(16, 16);
  icon.infoWindowAnchor = new GPoint(16, 0);
  icon.iconSize = new GSize(20, 16);
  return icon;
}

function addLandMarker(land) {
	var marker = new GMarker(new GLatLng(land.lati,land.longi), { title: land.land, icon: getLandIcon(land.land) });
	GEvent.addListener(marker, "click", function() {
	  var string = landOfferRequestString.replace(/##count##/,land.anzahl);
	  string = string.replace(/##land##/g,land.land.toUpperCase());
 	  string = string.replace(/##ocount##/,land.anzahlo);
 	  string = string.replace(/##rcount##/,land.anzahlr); 	  
		marker.openInfoWindowHtml(string);
	});
  return marker;
}

function getLandMarkers() {
  var batch = new Array();
  for (var i = 0; i < mfzland.length; i++) {
    batch.push(addLandMarker(mfzland[i]));
  }
  return batch;
}

function getOfferIcon(type) {
  var icon = new GIcon();
  if(type == 2) 
    icon.image = "/images/gmap_icon_auto.png";
  else if(type == 1) 
    icon.image = "/images/gmap_icon_mitfahrer.png";
  else if(type == 3) 
    icon.image = "/images/gmap_icon_flagge.png";
  else if(type == 4) 
    icon.image = "/images/gmap_icon_info.png";
  icon.iconAnchor = new GPoint(16, 16);
  icon.infoWindowAnchor = new GPoint(16, 0);
  icon.iconSize = new GSize(20, 16);
  return icon;
}

function addOfferMarker(offer) {
	var marker = new GMarker(new GLatLng(offer.lati,offer.longi), { title: offer.abort, icon: getOfferIcon(2) });
	GEvent.addListener(marker, "click", function() {
	  var string = startOfferRequestString.replace(/##count##/,offer.anzahl);
	  string = string.replace(/##land##/g,offer.abland.toUpperCase());
	  string = string.replace(/##city##/g,offer.abort);
 	  string = string.replace(/##ocount##/,offer.anzahlo);
 	  string = string.replace(/##rcount##/,offer.anzahlr);
      marker.openInfoWindowHtml(string);
	  var url = '/js/getOfferResults.php?abort=' + encodeURIComponent(offer.abort) + '&abland=' + encodeURIComponent(offer.abland);		
	  $.getJSON(url, function(json) {
        addOfferPolylines(marker, json.mfzdest);
	  });
	});
  return marker;
}

function addCityMarker(offer) {
	var marker = new GMarker(new GLatLng(offer.lati,offer.longi), { title: offer.place, icon: getOfferIcon(2) });
  return marker;
}

function addOfferPolylines(marker, mfzd) {

	if(mfzCurrLines && mfzCurrLines.length > 0) {
		for( var i=0; i< mfzCurrLines.length; i++) {
			mfzmap.removeOverlay(mfzCurrLines[i]);
		}
		mfzCurrLines = null;
		mfzCurrLines = new Array();
	}
	
	if(mfzCurrLineMarkers && mfzCurrLineMarkers.length > 0) {
		for(var i=0; i< mfzCurrLineMarkers.length; i++) {
			mfzmap.removeOverlay(mfzCurrLineMarkers[i]);
		}
		mfzCurrLineMarkers = null;
		mfzCurrLineMarkers = new Array();
	}
	
	if(mfzCurrDestMarkers && mfzCurrDestMarkers.length > 0) {
		for(var j = 0; j < mfzCurrDestMarkers.length; j++) {
			mfzmap.removeOverlay(mfzCurrDestMarkers[j]);
		}
		mfzCurrDestMarkers = null;
		mfzCurrDestMarkers = new Array();
	}

  for (var i=0; i<mfzd.length; i++) { 
    var line = getLine(marker.getPoint(),new GLatLng(mfzd[i].lati,mfzd[i].longi),mfzd[i]);
    mfzCurrLines.push(line);
  	mfzmap.addOverlay(line);
  	var dmarker = new GMarker(new GLatLng(mfzd[i].lati,mfzd[i].longi),{title: mfzd[i].anort, icon:getOfferIcon(1)});
    mfzCurrDestMarkers.push(dmarker);
  	mfzmap.addOverlay(dmarker); 
  }

}

function getLine(x,y,data) {
  var points = new Array(x,y);
  var line = new GPolyline(points,"#cc0000",2,0.95);
  var icon = new GIcon();
  icon.image = "/images/gmap_icon_info.png";
  icon.iconAnchor = new GPoint(14, 13);
  icon.infoWindowAnchor = new GPoint(14, 0);
  icon.iconSize = new GSize(14, 13);	
  var marker = new GMarker(new GLatLng(((parseFloat(x.lat()) + parseFloat(y.lat()))/2.0), ((parseFloat(x.lng()) + parseFloat(y.lng()))/2.0)),{icon: icon});
  mfzCurrLineMarkers.push(marker);
  mfzmap.addOverlay(marker);
	GEvent.addListener(marker, "click", function() {
	  var string = destOfferRequestString.replace(/##count##/,data.anzahl);
	  string = string.replace(/##land##/g,data.abland.toUpperCase());
	  string = string.replace(/##start##/g,data.abort);
 	  string = string.replace(/##dest##/g,data.anort);
	  string = string.replace(/##ocount##/,data.anzahlo);
	  string = string.replace(/##rcount##/,data.anzahlr);
		marker.openInfoWindowHtml(string);
	});
	return line;
}

function getOfferMarkers() {
  var batch = new Array();
  for (var i = 0; i<mfzoffers.length;i++) {
    batch.push(addOfferMarker(mfzoffers[i]));
  }
  return batch;
}

function setupMFZMarkers() {
  mfzmgr = new GMarkerManager(mfzmap);
  mfzmgr.addMarkers(getLandMarkers(), 3, 5);
  mfzmgr.addMarkers(getOfferMarkers(), 4, 10);
  mfzmgr.refresh();
}
