//Global variables

var markersCluster = {};
var map2;
var icons = {};
var gLocalSearch;
var currentLocalSearchStr;

//Functions

function propertyCreateMarker(searchResult, name) {
  var latlng = new GLatLng(searchResult.lat, searchResult.lng);
  var markerOptions = { icon:icons[name] };
    
  var marker = new GMarker(latlng,markerOptions);
  marker.resultHTML=searchResult.html.innerHTML;
  GEvent.addListener(marker, "click", function() {
    marker.openInfoWindowHtml(marker.resultHTML);
  });
  return marker;
}


//OnLocalSearch = function (gLocalSearch,name) {  
OnLocalSearch = function (gLocalSearch) {  
  if (!gLocalSearch.results) return;
  var name = currentLocalSearchStr;

  //load new markers 
  for (var i = 0; i < gLocalSearch.results.length; i++) {
    var marker = propertyCreateMarker(gLocalSearch.results[i],name);
    map2.addOverlay(marker);
    markersCluster[name].push(marker);
  }
}



/**
 * Entry point
*/
InitMap2 = function(){
	if (GBrowserIsCompatible()){
  	var geo1 = $("#geoOne").html();
  	var geo2 = $("#geoTwo").html();    
    if (geo1 == null) {
      $('#map').html('Map for this property cannot be displayed');
      $('#mapTypeControl').hide();
      $('#mapTools').hide();
      return "";
    }


		map2 = new GMap2(document.getElementById("map2"));
		var latLong = new GLatLng(geo1, geo2);
    map2.setCenter(latLong, 13);
   	map2.addControl(new GLargeMapControl3D());
		
		var icon = new GIcon();
		icon.image = '/modules/property/icons/home.png';
		icon.iconSize = new GSize(32,37);
		
    icon.shadow = '/modules/property/images/icon-shadow.png';
    icon.shadowSize = new GSize(42, 35);


		icon.iconAnchor = new GPoint(16,37);
		
		var marker = new GMarker(latLong,icon);
		
		map2.addOverlay(marker);

		//streetview
		var Map2streetviewClient = new GStreetviewClient();
    Map2streetviewClient.getNearestPanoramaLatLng(latLong, function (newLatLong) {
      if (null == newLatLong) {
        $("#mapTypeControl a.streetviewlink").hide();
        $('#streetview2').hide();
      }
    });
      
		$('#streetview2').width($('#map2').width());
		$('#streetview2').height($('#map2').height());
		$('#streetview2').hide();
		
		var myPano = new GStreetviewPanorama(document.getElementById("streetview2"));
		var fenwayPOV = {yaw:360,pitch:0};
		
    myPano.setLocationAndPOV(latLong, fenwayPOV);
    
    // Initialize the local searcher
    gLocalSearch = new GlocalSearch();
    
    gLocalSearch.setSearchCompleteCallback(this, function(){ OnLocalSearch(gLocalSearch); });
    gLocalSearch.setCenterPoint(map2);
        
    //Map Control part
    $("#mapTypeControl a").click(function(){
      $("#mapTypeControl a").each(function(){
        $(this).removeClass('selected');
        $('#streetview2').hide();
        $('#map2').show();
      });
      
      
      //switch map type
      if($(this).attr('name') == 'terrain'){
        map2.setMapType(G_PHYSICAL_MAP);
      }
      if($(this).attr('name') == 'satellite'){
        map2.setMapType(G_SATELLITE_MAP);
      }
      if($(this).attr('name') == 'maplink'){
        map2.setMapType(G_NORMAL_MAP);
      }

      if($(this).attr('name') == 'streetviewlink'){
        $('#map2').hide();
        $('#streetview2').show();
      }
      
      $(this).addClass('selected');
      return false;
    });
    
    //map Services part
    //init claster
    $("#mapTools a").each(function(){
      var name = $(this).attr('name');
      var filename = '';
      markersCluster[name]=[];
            
      switch(name){
          case 'school':
              filename = 'school.png';
            break;
          case 'grocery store':
              filename = 'grocery.png';
            break;
          case 'bank':
              filename = 'bank.png';
            break;
          case 'coffee':
              filename = 'coffee.png';
            break;
          case 'park':
              filename = 'park.png';
            break;
          case 'restaurant':
              filename = 'restaurant.png';
            break;
          case 'gas':
              filename = 'gazstation.png';
            break;
          case 'day care':
              filename = 'daycare.png';
            break;
          case 'hospital':
              filename = 'hospital.png';
            break;
          case 'shopping centre':
              filename = 'shoppingmall.png';
            break;
          case 'attraction':
              filename = 'themepark.png';
            break;

          case 'bus':
              filename = 'bus.png';
            break;
          default:
              filename = 'cabin.png';
      }

      var icon = new GIcon();
      icon.image = '/modules/property/icons/' + filename ;
      icon.iconSize = new GSize(32, 37);
      
      icon.shadow = '/modules/property/images/icon-shadow.png';
		  icon.shadowSize = new GSize(42, 35);
		
      icon.iconAnchor = new GPoint(16, 37);
      icon.infoWindowAnchor = new GPoint(16, 18);
      icons[name] = icon;
    
    });
    
    //hide it back
    $("#mapGoBackControl").click(function(){
      $('#newPropertyMap').hide('slow');
      $('#oldmap').show('slow');
    });
    
    
    //attach clicks
    $("#mapTools a").click(function(){
      if('streetviewlink' == $("#mapTypeControl a.selected").attr('name')){
        $('#map2').show();
        $('#streetview2').hide();
      }
      var thisObj = $(this);
      var name=thisObj.attr('name');
      
      if(thisObj.hasClass('selected')){
        thisObj.removeClass('selected');

        //remove markers
        for (var i = 0; i < markersCluster[name].length; i++) {
          map2.removeOverlay(markersCluster[name][i]);
        }
        markersCluster[name]=[];

      }else{
        currentLocalSearchStr = name;
        gLocalSearch.execute(name);

        thisObj.addClass('selected');
      }
      return false;
    });
    
   
	}
};


