/* main map initialization + data loading */
var map;
var campsites;
var rentals;
var geocoder;
var zoomMgr;
var contextMenu;
var mgr;var rent_mgr;
var directions;

/* bc bounds */
var bounds = new GLatLngBounds(new GLatLng(48, -139),new GLatLng(60, -112));

var ZOOM_FACTOR = 4;
var ZOOM_START;
var MIN_ZOOM = 9;

var loadingMessage;
function initialize() {
  if (GBrowserIsCompatible()) {
    
    map = new GMap2(document.getElementById("map_canvas"));
    GEvent.addListener(map, "load", addCampgrounds);

    map.setUIToDefault();
    map.setMapType(G_NORMAL_MAP); // changed from G_HYBRID_MAP dues to perf issues 
    map.disableScrollWheelZoom();
    
    zoomToBC();

    loadingMessage=document.createElement("div");
    loadingMessage.innerHTML = "Map loading..."; 
    loadingMessage.id = "map_canvas_load";
    
    map.getContainer().appendChild(loadingMessage);
    map.enableContinuousZoom() ;
    var pos = new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(235,215));  
    pos.apply(loadingMessage);

    var clusterIcon = new GIcon();
    clusterIcon.image = 'http://econym.org.uk/gmap/blue_large.png';
    clusterIcon.shadow = "";
    clusterIcon.iconSize = new GSize( 30, 51 );
    clusterIcon.shadowSize = null;
    clusterIcon.iconAnchor = new GPoint( 13, 34 );
    clusterIcon.infoWindowAnchor = new GPoint( 13, 3 );
    clusterIcon.infoShadowAnchor = new GPoint( 27, 37 );

    mgr = new MarkerManager(map);
    campsites = new Clusterer(map,mgr);
    campsites.icon = clusterIcon;
    campsites.maxVisibleMarkers = 25;
    campsites.gridSize = 25;
    campsites.minMarkersPerCluster = 5;
    campsites.maxLinesPerInfoBox = 10;
    campsites.minZoom = MIN_ZOOM;
    campsites.more = "Click Here to Zoom In <img width=\"16px\" height=\"16px\" onclick=\"cZoom(campsites);\" src=\"/images/maps/mActionZoomIn.png\">";
    
    
    var rentalsIcon = new GIcon();
    rentalsIcon.image = "/images/maps/BC_RV_CLUSTER.gif";
    rentalsIcon.shadow = "";
    rentalsIcon.iconSize = new GSize( 20, 20 );
    rentalsIcon.shadowSize = null;
    rentalsIcon.iconAnchor = new GPoint( 10, 10 );
    rentalsIcon.infoWindowAnchor = new GPoint( 13, 3 );
    
    rent_mgr = new MarkerManager(map);
    rentals = new Clusterer(map,rent_mgr);
    rentals.icon = rentalsIcon;
    // this config essentially disables the clusterer
    rentals.maxVisibleMarkers = 100;
    rentals.gridSize = 50;
    rentals.minMarkersPerCluster = 10;
    rentals.maxLinesPerInfoBox = 10;
    rentals.more = "Click Here to Zoom In <img width=\"16px\" height=\"16px\" onclick=\"cZoom(rentals);\" src=\"/images/maps/mActionZoomIn.png\">";
        
    
    geocoder = new GClientGeocoder();
    geocoder.setBaseCountryCode("ca");
    geocoder.setViewport(bounds);
        
    contextMenu = new ContextMenu(map);
    directions = new GDirections(map);
    GEvent.addListener(directions, "error", handleErrors);
    GEvent.addListener(directions, "addoverlay", clearDirectionMarkers);
    
    $("#find_place").autocomplete(cities);
    
    $("#find_camp").autocomplete(null);
    $("#find_camp").setOptions({matchContains:true,scrollHeight:220 });
    
    $(function() {
	    $('#left_container').tabs({ fxFade: true, fxSpeed: 'fast' });    
    });
    
    zoomHandler_H = GEvent.addListener(map,"zoomend",zoomHandler);
    
  }
}

var CAMPING = "CAMPING";
var RENTAL = "RENTAL";
var FISHING = "FISHING";
   
function zoomToBC(){
    ZOOM_START = map.getBoundsZoomLevel(bounds);
    map.setCenter(bounds.getCenter(), ZOOM_START);
}

// this is event driven to force the images to complete loading first
function addCampgrounds(){

  // wait for the init to be completed
  if(!campsites){
    setTimeout("addCampgrounds()",200);
    return;
  }

  for(var i=0;i<layers.length;i=i+1){
    if(CAMPING == layers[i][1]){
      addCampground(layers[i][2],"/camping/sites.php?layer="+layers[i][0],layers[i][4],layers[i][3],layers[i][5]=='Y');
    }else if(FISHING == layers[i][1]){
      addFishing(layers[i][2],"/camping/sites.php?layer="+layers[i][0],layers[i][4],layers[i][3],layers[i][5]=='Y');
    }else if(RENTAL == layers[i][1]){
      addRentals(layers[i][2],"/camping/sites.php?layer="+layers[i][0],layers[i][4],layers[i][3],layers[i][5]=='Y');
    }
  }

  map.getContainer().removeChild(loadingMessage);
  
  
  loadingMessage=document.createElement("div");
  loadingMessage.innerHTML = "Zoom in to see Markers"; 
  loadingMessage.id = "map_canvas_zoomin";
      
  map.getContainer().appendChild(loadingMessage);
  var pos = new GControlPosition(G_ANCHOR_BOTTOM_LEFT, new GSize(220,6));  
  pos.apply(loadingMessage);
}

var zoomHandler_H = null;
function zoomHandler(oldLevel,newLevel){
  if(newLevel>=MIN_ZOOM && loadingMessage){
    loadingMessage.style.visibility="hidden";
  }else{
    if(loadingMessage)
      loadingMessage.style.visibility="visible";
  }
}

function handleErrors(){
  directions.clear();
    
  alert("Directions could not be found to one or more of your Destinations.");
}

function keyPressEvent_Handler(event){
  if(event.keyCode == 27){
    // esc key 
    clearMarkerPlacement();
  }
}

