
function GoogleMapsApp(divId, latitude, longitude, zoom, mapType) {
  this.map = null;
  this.init = false;
  this.mapType = mapType;
  this.divId = divId;
  this.latitude = latitude;
  this.longitude = longitude;
  this.zoom = zoom;
  this.maxZoom = 13;
  this.markerImage = "/images/base/marker.png";
  this.markers = new Array();
  this.markerOptions = null;
  this.products = new Array();
  this.tooltip = null;
  this.timerID = null;
  this.timerRunning = false;
  this.delay = 3000;
  
  this.textTranslations = new Array();
 
  this.setMapType = function(mapType) {
	this.mapType = mapType;  
  }
  
  this.isInitialize = function() {
    return this.init;
  }
  
  this.setDivId = function(divId) {
	this.divId = divId;
  }
  
  this.setLatitude = function(latitude) {
    this.latitude = latitude;
  }
  
  this.setLongitude = function(longitude) {
    this.longitude = longitude; 
  }
	  
  this.setZoom = function(zoom) {
    this.zoom = zoom;
  }
		  
  this.setMaxZoom = function(maxZoom) {
    this.maxZoom = maxZoom;
  }
  
  this.setMarkerImage = function(markerImage) {
    this.markerImage = markerImage;
  }
  
  this.setDelay = function(delay) {
    this.delay = delay;
  }
  
  this.addMarker = function(latitude, longitude) {
	//todo  initialize
	var lmnIcon = new GIcon(G_DEFAULT_ICON);
	lmnIcon.image = this.markerImage;
	this.markerOptions = {icon:lmnIcon};  
	  
	var point = new GLatLng(latitude, longitude);
    var marker = new GMarker(point, this.markerOptions);
    this.markers.push(marker);
  }
  
  this.clearMarkers = function() {
	this.markers = new Array()  
  }
  
  this.setProducts = function(products) {
	this.products = products;
  }
  
  this.addProduct = function(product) {
	this.products.push(product);
  }
  
  this.clearProducts = function() {
	this.products = new Array();  
  }
  
  this.addTextTranslation = function(key, value) {
	  this.textTranslations[key] = value;
  }
  
  this.initialize = function() {
	if (GBrowserIsCompatible()) {
	  var lmnIcon = new GIcon(G_DEFAULT_ICON);
	  lmnIcon.image = this.markerImage;
	  this.markerOptions = {icon:lmnIcon};
	  this.map = new GMap2(document.getElementById(this.divId));
	  this.map.addControl(new GSmallZoomControl());
	  this.map.addControl(new GMapTypeControl());
	  this.map.setMapType(this.mapType);
	  if (this.latitude != null && this.longitude != null) {
		var basePoint = new GLatLng(this.latitude, this.longitude);
	  	this.map.setCenter(basePoint, this.zoom);
	  	var marker = new GMarker(basePoint, this.markerOptions);
	  	var self = this;
    	GEvent.addListener(marker, "click", function(latlng) {
    		self.markerClicked(latlng)
	    });
	    GEvent.addListener(marker, "mouseover", function(latlng) {
	    	self.showTooltip(latlng)
	    });
	    GEvent.addListener(marker, "mouseout", function(latlng) {
	    	self.hideTooltip(latlng)
	    });
	    this.markers.push(marker);
	  	this.map.addOverlay(marker);
	  } else {
		  var basePoint = new GLatLng(48.140, 11.558);
		  this.map.setCenter(basePoint, this.zoom)
	  }
	  this.tooltip = document.createElement("div");
	  this.map.getPane(G_MAP_FLOAT_PANE).appendChild(this.tooltip);
	  this.tooltip.style.visibility = "hidden";
	  this.init = true;
	}
  }

  this.redrawMap = function() {
	  this.map.clearOverlays();
	  this.map.checkResize();
	  var bounds = new GLatLngBounds();
	  var centerPoint = this.map.getCenter();
	  if (this.markers.length > 0) {
	    for(var i=0; i<this.markers.length; i++) {
	    	var marker = this.markers[i];
	    	bounds.extend(marker.getPoint());
	    	var self = this;
	    	GEvent.addListener(marker, "click", function(latlng) {
	    		self.markerClicked(latlng)
    	    });
    	    GEvent.addListener(marker, "mouseover", function(latlng) {
    	    	self.showTooltip(latlng)
    	    });
    	    GEvent.addListener(marker, "mouseout", function(latlng) {
    	    	self.hideTooltip(latlng)
    	    });
	    	this.map.addOverlay(marker);
	    }
	    centerPoint = bounds.getCenter();
	    this.zoom = this.map.getBoundsZoomLevel(bounds);
	    if (this.zoom > this.maxZoom) {
	    	this.zoom = this.maxZoom;
	    }
	  }
	  this.map.setCenter(centerPoint, this.zoom);
  }
  
  this.getMarkerIndexByLatLng = function (latlng) {
	  for(var i=0; i<this.markers.length; i++) {
		  if (this.markers[i].getLatLng().equals(latlng)) {
			  return i;
		  }
	  }
	  return null;
  }
  
  this.hideTooltip = function (latlng) {
	  this.tooltip.style.visibility = "hidden";
	  //can change marker image
  }
  
  this.showTooltip = function (latlng) {
	  var index = this.getMarkerIndexByLatLng(latlng);
	  if (index == null) {
		  return null;
	  }
	  var marker = this.markers[index];
	  
	  var tipText = "<div class='GoogleMapsTooltip'>";
	  tipText += this.products[index].name;
	  tipText += "</div>";
	  this.tooltip.innerHTML = tipText;
	  
	  var point = this.map.getCurrentMapType().getProjection().fromLatLngToPixel(
			this.map.fromDivPixelToLatLng(new GPoint(0, 0), true), this.map.getZoom());
	  var offset = this.map.getCurrentMapType().getProjection().fromLatLngToPixel(
			marker.getPoint(), this.map.getZoom());
	  var anchor = marker.getIcon().iconAnchor;
	  var width = marker.getIcon().iconSize.width;
	  var height = this.tooltip.clientHeight;
	  var pos = new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(
			offset.x - point.x - anchor.x + width, offset.y - point.y - anchor.y - height));
	  pos.apply(this.tooltip);
	  
	  this.tooltip.style.visibility = "visible";
	  //can change marker image
	  //marker.setImage(marker.getIcon().image.replace("iconb", "icong"))
  }
  
  this.markerClicked = function(latlng) {
	  if (GBrowserIsCompatible()) {
		try {
			var index = this.getMarkerIndexByLatLng(latlng);
		    if (index == null) {
			  return null;
		    }
		    var marker = this.markers[index];
			marker.openInfoWindowHtml(this.getBubbleHtml(index));
		} catch (ignored) {
		}
	  }
  }
  
  this.getBubbleHtml = function (index, resultnum) {
	  var product = this.products[index];
	  var html = "<div class='GoogleMapsDescription'>"
		  + "<h2><a href='" + product.url + "' title='' onclick='saveCookie(); return true;'>"
		  + product.name + "</a></h2>"
		  + "<p style='width:280px;'>" + product.description + "</p>"
		  + "<ul class='Linklist'>"
		  + "<li><a href='" + product.url + "' title='" + this.textTranslations["linkToDetailsTitle"] + "'>" + this.textTranslations["details"] + "</a></li>"
		  + "<li><a href='" + product.url + "/" + this.textTranslations["linkToMap"] + ".do' title='" + this.textTranslations["linkToMapTitle"] + "'>" + this.textTranslations["map"] + "</a></li>"
		  +  "</ul>";
		  + "</div>";
	  return html;
  }
  
  this.initializeTimer = function() {
      this.stopTimer();
      this.startTimer();
  }

  this.stopTimer = function() {
    if(this.timerRunning) {
      clearTimeout(this.timerID)
    }
    this.timerRunning = false;
  }

  this.startTimer = function() {
    if (this.zoom < this.maxZoom) {
      this.timerRunning = true;
      var self = this;
      this.timerID = setTimeout(function() { self.startTimer() }, this.delay);
      this.map.zoomIn();
      this.zoom++;
    } else {
      this.stopTimer();
    }
  }
}

var GMapsAPIKey = "";
var isMapSmall = false;

function positionMap(hideMapDivId, showMapDivId, mapSize, mappApp) {
	var hideMapDiv = document.getElementById(hideMapDivId) || null;
	if (hideMapDiv) {
	  hideMapDiv.style.display = "none";
	}
	var showMapDiv = document.getElementById(showMapDivId) || null;
	if (showMapDiv) {
		showMapDiv.style.display = "block";
	}
	
	if (mapSize == "big") {
	  isMapSmall = false;
	  redrawMap(mapApp);
	  var showBigSpan = document.getElementById(hideMapDivId + "ShowBigMap");
	  if (showBigSpan) {
		  showBigSpan.style.display = "none";
	  }
	  var hideBigSpan = document.getElementById(hideMapDivId + "HideBigMap");
	  if (hideBigSpan) {
		  hideBigSpan.style.display = "block";
	  }
	} else {
	  isMapSmall = true;
	  redrawStaticMap(showMapDivId + "Img");
	  var showBigSpan = document.getElementById(showMapDivId + "ShowBigMap");
	  if (showBigSpan) {
		  showBigSpan.style.display = "block";
	  }
	  var hideBigSpan = document.getElementById(showMapDivId + "HideBigMap");
	  if (hideBigSpan) {
		  hideBigSpan.style.display = "none";
	  }
	}
}

function redrawMap(mapApp) {
	mapApp.clearProducts();
	mapApp.clearMarkers();
	if (!mapApp.isInitialize()) {
	  mapApp.initialize();
	}
	var items = Paging.getFilteredItems();
	for (var i = 0; i < items.length; i++) {
	  if (items[i].latitude != null && items[i].longitude != null) {
	    mapApp.addMarker(items[i].latitude, items[i].longitude);
	    mapApp.addProduct(items[i]);
	  }
	}
	mapApp.redrawMap();
}

function redrawStaticMap(staticMapImgId) {
	var markersString = "";
	var items = Paging.getFilteredItems();
	for (var i = 0; (i < items.length && i < 57);  i++) {
	  if (items[i].latitude != null && items[i].longitude != null) {
	    if (i != 0) {
	      markersString += "|";
	    }
	    markersString += items[i].latitude + "," + items[i].longitude + "," + "tinyred";
	  }
	}
	var img = document.getElementById(staticMapImgId) || null;
	if (img) {
	  img.src = "http://maps.google.com/staticmap?markers=" + markersString + "&size=170x130&key=" + GMapsAPIKey;
    }
}
