/*
 * 
 */



/*
 * Query Map
 * 
 * displays a map with a marker and stores current position and zoom
 * in hidden fields for cforms
 * 
 */

function LovoQueryMap(divId, lonStoreId, latStoreId, zoomStoreId) {
	this.divId=divId;
    this.lonStoreId=lonStoreId;
    this.latStoreId=latStoreId;
    this.zoomStoreId=zoomStoreId;
}

LovoQueryMap.prototype.init = function (lat,lon,zoom,displayMarker) {
		this.map =  new GMap2(document.getElementById(this.divId),[G_HYBRID_MAP]);
		this.map.addControl(new GLargeMapControl());
    	this.map.addControl(new GScaleControl());
    	this.map.addControl(new GMapTypeControl());
    	this.map.addControl(new GOverviewMapControl());
    	this.map.setCenter(new GLatLng(lat, lon), zoom);
    	if (displayMarker) {
      		icon = new GIcon(G_DEFAULT_ICON, "../../skin-resources/img/marker.png");
      		var marker = new GMarker(this.map.getCenter(),icon);
      		this.map.addOverlay(marker);       
      		var map = this.map;
      		var lonStoreId = this.lonStoreId;
      		var latStoreId = this.latStoreId;
      		var zoomStoreId = this.zoomStoreId;
      		GEvent.addListener(this.map, "drag", function() {
        		marker.setPoint(map.getCenter());
            });
            GEvent.addListener(this.map, "dragend", function() {
                document.getElementById(lonStoreId).value = map.getCenter().x;
                document.getElementById(latStoreId).value = map.getCenter().y;
            });
            GEvent.addListener(this.map, "zoomend", function(oldLevel,newLevel) {
                document.getElementById(zoomStoreId).value = map.getZoom();
        	});
        } else {
            this.map.disableDragging();
        }
}



/*
 * Result Map
 *
 *
 */

function LovoResultMapMarker(lon,lat,roleId,letter) {
	this.lon=lon;
	this.lat=lat;
	this.roleId=roleId;
	
	var point = new GPoint(lon,lat);
	var icon = new GIcon(G_DEFAULT_ICON, "../../skin-resources/img/marker" + letter + ".png");
  	this.gmarker = new GMarker(point, icon);
  	
}

LovoResultMapMarker.prototype.getGMarker =  function() {
	return this.gmarker;
}

LovoResultMapMarker.prototype.toString =  function() {
	return "["+this.gmarker + ","+this.roleId+"]";
}
 

function LovoResultMap(divId) {
	this.divId=divId;
	this.gmarkers = new Array();
	this.feratelManager=null;
} 
 
LovoResultMap.prototype.init =  function(lat,lon,zoom) {
	this.map =  new GMap2(document.getElementById(this.divId),{mapTypes:[G_NORMAL_MAP,G_SATELLITE_MAP,G_HYBRID_MAP]});
	this.feratelManager = new LovoFeratelManager(this.map);
    this.map.addControl(new GLargeMapControl());
    this.map.addControl(new GScaleControl());
    this.map.addControl(new GMapTypeControl());
    this.map.addControl(new GOverviewMapControl());
    this.map.setCenter(new GLatLng(lat, lon), zoom);
    var icon = new GIcon(G_DEFAULT_ICON, "../../skin-resources/img/marker.png");
    var marker = new GMarker(this.map.getCenter(),icon);
    this.map.addOverlay(marker);
    
    this.feratelManager.init(zoom);
	
	var map = this.map;
	var feratelManager = this.feratelManager;

    GEvent.addListener(map, "dragstart", function() {
      feratelManager.oldBounds = map.getBounds();
    });
    GEvent.addListener(map, "zoomstart", function() {
      feratelManager.oldBounds = map.getBounds();
    });
    GEvent.addListener(map, "dragend", function() {
      if (map.getZoom() > 8) {
        feratelManager.showFeratelMarkers(true);
      } else {
        feratelManager.removeFeratelMarkers();
      }
    });
    GEvent.addListener(map, "zoomend", function(oldLevel,newLevel) {
      document.getElementById("zoom").innerHTML = newLevel + "";
      feratelManager.init(newLevel);
    });
}

LovoResultMap.prototype.showFeratel =  function() {
	this.feratelManager.loadIndex();
}

LovoResultMap.prototype.hideFeratel =  function() {
	this.feratelManager.clearFeratelMarkers();	
}

LovoResultMap.prototype.addTipMarker =  function(lat,lon,roleId,tipManager,title,highlightId,highlightIndex,highlightLength,sessionId) {
	
    var letter = String.fromCharCode("A".charCodeAt(0) + this.gmarkers.length);
  	var marker = new LovoResultMapMarker(lon,lat,roleId,letter);
  	this.gmarkers.push(marker);
  	this.map.addOverlay(marker.getGMarker());
  
	GEvent.addListener(marker.getGMarker(), 'click', function() {
	   tipManager.initQuery(roleId,title,highlightId,highlightIndex,highlightLength,sessionId);
	});    
}

LovoResultMap.prototype.zoomToAllVisible = function() {
  	var bounds = new GLatLngBounds();
  	bounds.extend(this.map.getCenter());
  	for (var i=0; i<this.gmarkers.length; i++) {
    	bounds.extend(this.gmarkers[i].getGMarker().getPoint());
  	}
  	var center_lat = (bounds.getSouthWest().lat() + bounds.getNorthEast().lat()) / 2;
  	var center_lon = (bounds.getSouthWest().lng() + bounds.getNorthEast().lng()) / 2;
  	this.map.setZoom(this.map.getBoundsZoomLevel(bounds));
  	this.map.setCenter(new GLatLng(center_lat, center_lon));
  	this.map.savePosition();
}

LovoResultMap.prototype.centerMarker = function(roleId,zoom) {
   	for (var i=0; i<this.gmarkers.length; i++) {
    	
    	if (this.gmarkers[i].roleId == roleId) {
	   		this.map.setZoom(zoom);	
	   		this.map.setCenter(this.gmarkers[i].getGMarker().getPoint());
		  	this.map.savePosition();
  		}
  	}
   	
   
}

 
