var selected_marker;
var selected_icon;
var current_lable;
var hotelmarker;
var map;

function addIcon(x, y, name, message, image)
{
	var baseIcon = new GIcon();
	baseIcon.iconSize = new GSize(16, 16);		        
	baseIcon.shadow = "";				
	baseIcon.iconAnchor = new GPoint(8, 8);
	baseIcon.infoWindowAnchor = new GPoint(0, 0);
	baseIcon.infoShadowAnchor = new GPoint(0, 0);
	var point = new GLatLng(x, y);
	var hotelIcon = new GIcon(baseIcon);
	hotelIcon.image = image;
	markerOptions = { icon:hotelIcon };
	hotelmarker = createMarker(map, point, markerOptions, name, message);
	map.addOverlay(hotelmarker);
}

function clearovl()
{
	map.clearOverlays();
}

function VLabel(point, html, classname, pixelOffset, percentOpacity, overlap) {
        // Mandatory parameters
        this.point = point;
        this.html = html;
        
        // Optional parameters
        this.classname = classname||"";
        this.pixelOffset = pixelOffset||new GSize(0,0);
        if (percentOpacity) {
          if(percentOpacity<0){percentOpacity=0;}
          if(percentOpacity>100){percentOpacity=100;}
        }        
        this.percentOpacity = percentOpacity;
        this.overlap=overlap||false;
        this.hidden = false;
      } 
      
      VLabel.prototype = new GOverlay();

      VLabel.prototype.initialize = function(map) {
        var div = document.createElement("div");
        div.style.position = "absolute";
        div.innerHTML = '<div class="' + this.classname + '">' + this.html + '</div>' ;
        map.getPane(G_MAP_FLOAT_SHADOW_PANE).appendChild(div);
        this.map_ = map;
        this.div_ = div;
        if (this.percentOpacity) {        
          if(typeof(div.style.filter)=='string'){div.style.filter='alpha(opacity:'+this.percentOpacity+')';}
          if(typeof(div.style.KHTMLOpacity)=='string'){div.style.KHTMLOpacity=this.percentOpacity/100;}
          if(typeof(div.style.MozOpacity)=='string'){div.style.MozOpacity=this.percentOpacity/100;}
          if(typeof(div.style.opacity)=='string'){div.style.opacity=this.percentOpacity/100;}
        }
        if (this.overlap) {
          var z = GOverlay.getZIndex(this.point.lat());
          this.div_.style.zIndex = z;
        }
        if (this.hidden) {
          this.hide();
        }
      }

      VLabel.prototype.remove = function() {
        this.div_.parentNode.removeChild(this.div_);
      }

      VLabel.prototype.copy = function() {
        return new VLabel(this.point, this.html, this.classname, this.pixelOffset, this.percentOpacity, this.overlap);
      }

      VLabel.prototype.redraw = function(force) {
        var p = this.map_.fromLatLngToDivPixel(this.point);
        var h = parseInt(this.div_.clientHeight);
        this.div_.style.left = (p.x + this.pixelOffset.width) + "px";
        this.div_.style.top = (p.y +this.pixelOffset.height - h) + "px";
      }

      VLabel.prototype.show = function() {
        if (this.div_) {
          this.div_.style.display="";
          this.redraw();
        }
        this.hidden = false;
      }
      
      VLabel.prototype.hide = function() {
        if (this.div_) {
          this.div_.style.display="none";
        }
        this.hidden = true;
      }
      
      VLabel.prototype.isHidden = function() {
        return this.hidden;
      }
      
      VLabel.prototype.supportsHide = function() {
        return true;
      }

      VLabel.prototype.setContents = function(html) {
        this.html = html;
        this.div_.innerHTML = '<div class="' + this.classname + '">' + this.html + '</div>' ;
        this.redraw(true);
      }
      
      VLabel.prototype.setPoint = function(point) {
        this.point = point;
        if (this.overlap) {
          var z = GOverlay.getZIndex(this.point.lat());
          this.div_.style.zIndex = z;
        }
        this.redraw(true);
      }
      
      VLabel.prototype.setOpacity = function(percentOpacity) {
        if (percentOpacity) {
          if(percentOpacity<0){percentOpacity=0;}
          if(percentOpacity>100){percentOpacity=100;}
        }        
        this.percentOpacity = percentOpacity;
        if (this.percentOpacity) {        
          if(typeof(this.div_.style.filter)=='string'){this.div_.style.filter='alpha(opacity:'+this.percentOpacity+')';}
          if(typeof(this.div_.style.KHTMLOpacity)=='string'){this.div_.style.KHTMLOpacity=this.percentOpacity/100;}
          if(typeof(this.div_.style.MozOpacity)=='string'){this.div_.style.MozOpacity=this.percentOpacity/100;}
          if(typeof(this.div_.style.opacity)=='string'){this.div_.style.opacity=this.percentOpacity/100;}
        }
      }

      VLabel.prototype.getPoint = function() {
        return this.point;
      }
      VLabel.prototype.U = function() {
        return this.point;
      }
      VLabel.prototype.V = function() {
        return this.point;
      }
      VLabel.prototype.W = function() {
        return this.point;
      }
      VLabel.prototype.X = function() {
        return this.point;
      }
      VLabel.prototype.Y = function() {
        return this.point;
      }
      VLabel.prototype.Z = function() {
        return this.point;
      }
		
function g_load()
{
	if (GBrowserIsCompatible()) 
	{
		var div = document.getElementById("allmap");

		div.style.width = 412 + "px"; 
		div.style.height = 250 + "px";
		map = new GMap2(div, G_NORMAL_MAP);
								
		map.addControl(new GSmallMapControl());
		map.addControl(new GMapTypeControl());

		if (window.location.hash == "#allmap")
		{
			map.setCenter(new GLatLng(59.942245, 30.325356), 13);
		}
		else
		{
			map.setCenter(new GLatLng(59.942245, 30.325356), 12);
		}
		map.removeMapType(G_SATELLITE_MAP);
		map.removeMapType(G_HYBRID_MAP);
	}
}

function fnShowProps(obj, objName)
{
	var result = "";
	for (var i in obj) 
	{
        	result += objName + "." + i + " = " + obj[i] + "<br />\n";
	}
	alert(result);
}

function selectMarker(marker, map)
{
	DeSelectMarker(map);
			
	selected_marker = marker;
	icon = marker.getIcon();
			
	selected_icon = icon.image;
	//marker.setImage("/i/map-hot.png");
	//fnShowProps(marker, "marker");
	current_lable = new VLabel(marker.getLatLng(), "<div class=marker>" + marker.value + "</div>", "VLabel", new GSize(0, 30), false, true);
	map.addOverlay(current_lable);
}
		
function DeSelectMarker(map)
{
	if(typeof(selected_marker) == 'undefined') 
	{
		return;
	}
			
	selected_marker.setImage(selected_icon);
			
	selected_marker = undefined;
			
	current_lable.hide();
}
		
function createMarker(map, point, options, name, description) 
{
	var marker = new GMarker(point, options);
	marker.value = name;
	GEvent.addListener(marker,"click", function() {
		var myHtml = "<b>" + name + "</b>" + "<br>" + description;
		map.openInfoWindowHtml(point, myHtml);
		});
			
	GEvent.addListener(marker,"mouseover", function() {
		selectMarker(this, map);
		});

	GEvent.addListener(marker,"mouseout", function() {
		DeSelectMarker(map);
		});

	return marker;
}
