﻿var hotelmarker;
var hotelmarkerHover;
var airportMarker;
var airportMarkerHover;
var landmarker;
var landmarkerHover;
var imgStars;

var lat;
var lng;
var Map;
var infoTemplate;
var infoTemplateEW;
var airplaneInfoTemplate;
var airplaneInfoTemplateEW;
var useEW;
//var lasdZ;

function createLandmarker(latlng, title) 
{
  var icon = new GIcon();
			icon.image = landmarker;			
			icon.iconAnchor = new GPoint(10, 20);
			icon.infoWindowAnchor = new GPoint(115, 20);
			icon.iconSize = new GSize(20, 20);
			opt ={icon:icon};
			
      var marker = new GMarker(latlng,icon);                 
            
      GEvent.addListener(marker,"mouseover",function(e){      
      var myHtml;
      if(useEW)
				myHtml = airplaneInfoTemplateEW.replace("{0}",title);
			else
				myHtml = airplaneInfoTemplate.replace("{0}",title);

      marker.setImage(landmarkerHover);      
      if (marker.lq)
      {
				lasdZ = marker.lq.style.zIndex;
				marker.lq.style.zIndex=1600;
      }

      if(useEW)
				ew.openOnMarkerEx(marker,myHtml);
			else
				Map.openInfoWindowHtml(latlng, myHtml, {pixelOffset:new GSize(0,-30)});      
      e.cancelBubble=true;});
      
      GEvent.addListener(marker,"mouseout", function(e){      
      marker.setImage(landmarker);      
      if (marker.lq)      
				marker.lq.style.zIndex = lasdZ;           
      if(useEW)
				ew.hide();
			else
				Map.closeInfoWindow();
      e.cancelBubble=true;});      
      return marker;
}

function createHotelMarker(latlng, title, descr,stars,picUrl,url,price) 
    {    
			var icon = new GIcon();
			icon.image = hotelmarker;
			icon.iconAnchor = new GPoint(8, 9);
			icon.infoWindowAnchor = new GPoint(115, 20);
			icon.iconSize = new GSize(16, 18);
			opt ={icon:icon};
			
      var marker = new GMarker(latlng,icon);                 
            
      GEvent.addListener(marker,"mouseover",function(e){      
      var myHtml;
      if(useEW)
				myHtml = infoTemplateEW.replace("{0}",title).replace("{1}",stars).replace("{2}",picUrl).replace("{3}",descr).replace("{4}",price);
			else
				myHtml = infoTemplate.replace("{0}",title).replace("{1}",stars).replace("{2}",picUrl).replace("{3}",descr).replace("{4}",price);

      marker.setImage(hotelmarkerHover);      
      if (marker.lq)
      {
				lasdZ = marker.lq.style.zIndex;
				marker.lq.style.zIndex=1600;
      }

      if(useEW)
				ew.openOnMarkerEx(marker,myHtml);
			else
				Map.openInfoWindowHtml(latlng, myHtml, {pixelOffset:new GSize(0,-30)});      
      e.cancelBubble=true;});
      
      GEvent.addListener(marker,"mouseout", function(e){      
      marker.setImage(hotelmarker);      
      if (marker.lq)      
				marker.lq.style.zIndex = lasdZ;           
      if(useEW)
				ew.hide();
			else
				Map.closeInfoWindow();
      e.cancelBubble=true;});
      
      GEvent.addListener(marker,"click", function(e){document.location=url;});
      
      return marker;
	  }
	  
	  function createAirportMarker(airplane)
		{
			var icon = new GIcon();
			icon.image = airportMarker;
			icon.iconAnchor = new GPoint(10, 10);		
			icon.infoWindowAnchor = new GPoint(115, 20);	
			icon.iconSize = new GSize(20, 20);			
			
			var latlng = new GLatLng(airplane.Lat,airplane.Lng)
			
      var marker = new GMarker(latlng,icon);                 
      
      GEvent.addListener(marker,"mouseover",function(e){      
      var myHtml;       
      if(useEW)
				myHtml = airplaneInfoTemplateEW.replace("{0}",airplane.Name);
			else
				myHtml = airplaneInfoTemplate.replace("{0}",airplane.Name);

      marker.setImage(airportMarkerHover);
      if (marker.lq)
      {
				lasdZ = marker.lq.style.zIndex;
				marker.lq.style.zIndex=1600;
      } 
			marker.setImage(airportMarkerHover);      
			if(useEW)
				ew.openOnMarkerEx(marker,myHtml);
			else
				Map.openInfoWindowHtml(latlng, myHtml, {pixelOffset:new GSize(0,-30)});  
			e.cancelBubble=true;});
      
			GEvent.addListener(marker,"mouseout", function(e){            
      if (marker.lq)      
				marker.lq.style.zIndex = lasdZ;           
			marker.setImage(airportMarker);
      if(useEW)
				ew.hide();
			else
				Map.closeInfoWindow();
      e.cancelBubble=true;});

      return marker;
  }

function loadMap() {
  if (GBrowserIsCompatible())
  { 
    var map =  new GMap2(document.getElementById("map"));        
    Map=map;    
    map.setCenter(new GLatLng(lat, lng), 9);    
    map.addControl(new GLargeMapControl(), new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(5,5)));        
    
    // Create an EWindow
      ew = new EWindow(map, E_STYLE_7);      
      map.addOverlay(ew);
      
    //map.addControl(new GMapTypeControl());
    // bind a search control to the map, suppress result list
    //map.addControl(new google.maps.LocalSearch(), new GControlPosition(G_ANCHOR_BOTTOM_RIGHT, new GSize(10,20)));        
    var bounds = map.getBounds();
    var southWest = bounds.getSouthWest();
    var northEast = bounds.getNorthEast();    
    Hotel.Web.Airports.GetAirports(southWest.lng(),northEast.lat(),northEast.lng(),southWest.lat(),onAirportsLoaded);
  }  
  
  function onAirportsLoaded(result)  
  {
		for (i = 0;i<result.length;i++)
		{
			var latlng = new GLatLng(result[i].Lat,result[i].Lng);
			var marker = createAirportMarker(result[i]);
			Map.addOverlay(marker);			
		}		
  }
}