var map;
var mgr;
var cluster = new Array();
var hash = '';
var mapcenter = new GLatLng(-1.614776424905496, -79.134521484375000);
var mapzoom =  7;
var trip_name = 'Ecuador';
var trip_id = '69';
var departure_city = '';
var departure_array = departure_city.split("|");
var destination = 'Ecuador@38.822983000000000,-120.936768000000000';
departure_array.push(destination);
var zoomctrl = new GLargeMapControl();

function mapInit() {
  if (GBrowserIsCompatible()) {
    // Set up map
    map = new GMap2(document.getElementById("mapframe"), {backgroundColor:"#FFFFFF"});
    map.addControl(new GHierarchicalMapTypeControl());
    map.addControl(zoomctrl);
	
    map.enableScrollWheelZoom();
    map.addMapType(G_PHYSICAL_MAP);
    //Map Types: G_SATELLITE_MAP, G_HYBRID_MAP, G_PHYSICAL_MAP, G_NORMAL_MAP
    map.setCenter(mapcenter, mapzoom, G_PHYSICAL_MAP);
    mgr = new MarkerManager(map);
    if(window.gDirections) {
      window.gDirections.clear();
    }
		    
    window.gDirections = new GDirections(window.map);
    GEvent.addListener(window.gDirections, "addoverlay", onGDirectionsAddOverlay); // added to trigger marker swap
    GEvent.addDomListener(map, 'extinfowindowupdate',function(){
      var windowContent = document.getElementById("OARSwindowBlue_contents");
      var tabContainer = document.getElementById("tabs_header");
      if (tabContainer) {
        var tabs = tabContainer.getElementsByTagName("div");
        if( tabs.length > 0 ){
          var tabContentsArray = new Array(tabs.length);
            for( i=0; i < tabs.length; i++){
            tabContentsArray[i] = document.getElementById("tab"+i+"_content");
            if( i > 0){
              hide(tabContentsArray[i]);
            }

            tabs[i].setAttribute("name", i.toString());
            GEvent.addDomListener(tabs[i],"click",function(){
              var tabIndex = this.getAttribute("name");
              for(tabContentIndex=0; tabContentIndex < tabs.length; tabContentIndex++){
                if( tabContentIndex == tabIndex ){
                  show(tabContentsArray[tabContentIndex]);
                  document.getElementById("tab"+tabContentIndex).style.borderBottom = "2px solid black";
                }else{
		  hide(tabContentsArray[tabContentIndex]);
                  document.getElementById("tab"+tabContentIndex).style.borderBottom = "0px";
                }
              }
              map.getExtInfoWindow().resize();
            });
          }
        }
      }
      map.getExtInfoWindow().resize();
    });
		 
    function createMarker(lat, lng, icon, tooltiptext, windowtype, id, tabs, zoom, url) {
      window["marker"+id] = new GMarker(new GLatLng(lat,lng), icon);
      window["tooltip"+id] = new Tooltip(window["marker"+id],tooltiptext,10);
      window["marker"+id].tooltip = window["tooltip"+id];
      mgr.addMarker(window["marker"+id],zoom);
      if (tooltiptext != '')
      {
        map.addOverlay(window["tooltip"+id]);
        GEvent.addListener(window["marker"+id],'mouseover',function(){
          this.tooltip.show();
	});
	GEvent.addListener(window["marker"+id],'mouseout',function(){
	  this.tooltip.hide();
	});
      }
      if (tabs > 0) {
        GEvent.addListener(window["marker"+id], 'click', function(){
          window["marker"+id].openExtInfoWindow(map, windowtype,
            "Loading...",
            {
              ajaxUrl: "/maps/marker/"+id,
              beakOffset: 15
            }
          );
	 map.panTo(new GLatLng(lat,lng));
        });
      } else if (url != '') {
	GEvent.addListener(window["marker"+id], 'click', function(){
	  window.location=url;
	});
      }	
      return true;
    };
	
function createCluster(lat, lng, icon, tooltiptext, windowtype, id, tabs, minzoom, maxzoom, url) {
      window["cluster"+id] = new GMarker(new GLatLng(lat,lng), icon);
      window["tooltipc"+id] = new Tooltip(window["cluster"+id],tooltiptext,10);
      window["cluster"+id].tooltipc = window["tooltipc"+id];
    cluster["#"+tooltiptext] = id;
      mgr.addMarker(window["cluster"+id],minzoom, maxzoom);
      if (tooltiptext != '')
      {	
        map.addOverlay(window["tooltipc"+id]);
        GEvent.addListener(window["cluster"+id],'mouseover',function(){
          this.tooltipc.show();
        }	);
        GEvent.addListener(window["cluster"+id],'mouseout',function(){
          this.tooltipc.hide();
        }	);
      }	
      if (tabs > 0) {
        GEvent.addListener(window["cluster"+id], 'click', function(){
          window["cluster"+id].openExtInfoWindow(map, windowtype,
            "Loading...",
            {
              ajaxUrl:	 "/maps/cluster/"+id,
              beakOffset:	 15
            }	
          );
        });
      }	 else if (url != '') {
        GEvent.addListener(window["cluster"+id], 'click', function(){
          window.location=url;
        }	);
      }	else {
	GEvent.addListener(window["cluster"+id], 'click', function(){
          map.setCenter(new GLatLng(lat,lng),maxzoom+1);	
	  window.location.hash = tooltiptext;
	  hash = self.document.location.hash;
	});
      }	
      return true;
    }	;
					       
    function createPolyline(polyline, tooltiptext, color, id) {
      window["polyline"+id] = polyline;
      map.addOverlay(window["polyline"+id]);
      if (tooltiptext.length > 0) {
        window["polylinetooltip"+id] = new PolylineTooltip(window["polyline"+id],tooltiptext,10);
        window["polyline"+id].tooltip = window["polylinetooltip"+id];
        map.addOverlay(window["polylinetooltip"+id]);

        GEvent.addListener(window["polyline"+id],'mouseover',function(){
          window["polyline"+id].tooltip.show();
        });
        GEvent.addListener(window["polyline"+id],'mouseout',function(){
          window["polyline"+id].tooltip.hide();
        });
      }
    }
       
	


var NewCampingSpot = new GIcon();
NewCampingSpot.image = "/images/maps/elements/campingspot.png";
NewCampingSpot.shadow = "/images/maps/elements/dropshadow.png";
NewCampingSpot.iconSize = new GSize(20, 20);
NewCampingSpot.shadowSize = new GSize(22, 22);
NewCampingSpot.iconAnchor = new GPoint(10, 20);
NewCampingSpot.infoWindowAnchor = new GPoint(8, 0);

var NewCampingSpot2 = new GIcon();
NewCampingSpot2.image = "/images/maps/elements/campingspot2.png";
NewCampingSpot2.shadow = "/images/maps/elements/dropshadow.png";
NewCampingSpot2.iconSize = new GSize(20, 20);
NewCampingSpot2.shadowSize = new GSize(22, 22);
NewCampingSpot2.iconAnchor = new GPoint(10, 20);
NewCampingSpot2.infoWindowAnchor = new GPoint(8, 0);

var NewDestination = new GIcon();
NewDestination.image = "/images/maps/elements/destination.png";
NewDestination.shadow = "/images/maps/elements/dropshadow.png";
NewDestination.iconSize = new GSize(20, 20);
NewDestination.shadowSize = new GSize(22, 22);
NewDestination.iconAnchor = new GPoint(10, 20);
NewDestination.infoWindowAnchor = new GPoint(8, 0);

var NewDestination2 = new GIcon();
NewDestination2.image = "/images/maps/elements/destination2.png";
NewDestination2.shadow = "/images/maps/elements/dropshadow.png";
NewDestination2.iconSize = new GSize(20, 20);
NewDestination2.shadowSize = new GSize(22, 22);
NewDestination2.iconAnchor = new GPoint(10, 20);
NewDestination2.infoWindowAnchor = new GPoint(8, 0);

var NewMeeting = new GIcon();
NewMeeting.image = "/images/maps/elements/meeting.png";
NewMeeting.shadow = "/images/maps/elements/dropshadow.png";
NewMeeting.iconSize = new GSize(20, 20);
NewMeeting.shadowSize = new GSize(22, 22);
NewMeeting.iconAnchor = new GPoint(10, 20);
NewMeeting.infoWindowAnchor = new GPoint(8, 0);

var NewMeeting2 = new GIcon();
NewMeeting2.image = "/images/maps/elements/meeting2.png";
NewMeeting2.shadow = "/images/maps/elements/dropshadow.png";
NewMeeting2.iconSize = new GSize(20, 20);
NewMeeting2.shadowSize = new GSize(22, 22);
NewMeeting2.iconAnchor = new GPoint(10, 20);
NewMeeting2.infoWindowAnchor = new GPoint(8, 0);

var NewPointOfInterest = new GIcon();
NewPointOfInterest.image = "/images/maps/elements/pointofinterest.png";
NewPointOfInterest.shadow = "/images/maps/elements/dropshadow.png";
NewPointOfInterest.iconSize = new GSize(20, 20);
NewPointOfInterest.shadowSize = new GSize(22, 22);
NewPointOfInterest.iconAnchor = new GPoint(10, 20);
NewPointOfInterest.infoWindowAnchor = new GPoint(8, 0);

var NewPointOfInterest2 = new GIcon();
NewPointOfInterest2.image = "/images/maps/elements/pointofinterest2.png";
NewPointOfInterest2.shadow = "/images/maps/elements/dropshadow.png";
NewPointOfInterest2.iconSize = new GSize(20, 20);
NewPointOfInterest2.shadowSize = new GSize(22, 22);
NewPointOfInterest2.iconAnchor = new GPoint(10, 20);
NewPointOfInterest2.infoWindowAnchor = new GPoint(8, 0);

var NewPutIn = new GIcon();
NewPutIn.image = "/images/maps/elements/put-in.png";
NewPutIn.shadow = "/images/maps/elements/dropshadow.png";
NewPutIn.iconSize = new GSize(20, 20);
NewPutIn.shadowSize = new GSize(22, 22);
NewPutIn.iconAnchor = new GPoint(10, 20);
NewPutIn.infoWindowAnchor = new GPoint(8, 0);

var NewPutIn2 = new GIcon();
NewPutIn2.image = "/images/maps/elements/put-in2.png";
NewPutIn2.shadow = "/images/maps/elements/dropshadow.png";
NewPutIn2.iconSize = new GSize(20, 20);
NewPutIn2.shadowSize = new GSize(22, 22);
NewPutIn2.iconAnchor = new GPoint(10, 20);
NewPutIn2.infoWindowAnchor = new GPoint(8, 0);

var NewRapid = new GIcon();
NewRapid.image = "/images/maps/elements/rapid.png";
NewRapid.shadow = "/images/maps/elements/dropshadow.png";
NewRapid.iconSize = new GSize(20, 20);
NewRapid.shadowSize = new GSize(22, 22);
NewRapid.iconAnchor = new GPoint(10, 20);
NewRapid.infoWindowAnchor = new GPoint(8, 0);

var NewRapid2 = new GIcon();
NewRapid2.image = "/images/maps/elements/rapid2.png";
NewRapid2.shadow = "/images/maps/elements/dropshadow.png";
NewRapid2.iconSize = new GSize(20, 20);
NewRapid2.shadowSize = new GSize(22, 22);
NewRapid2.iconAnchor = new GPoint(10, 20);
NewRapid2.infoWindowAnchor = new GPoint(8, 0);

var NewTakeOut = new GIcon();
NewTakeOut.image = "/images/maps/elements/take-out.png";
NewTakeOut.shadow = "/images/maps/elements/dropshadow.png";
NewTakeOut.iconSize = new GSize(20, 20);
NewTakeOut.shadowSize = new GSize(22, 22);
NewTakeOut.iconAnchor = new GPoint(10, 20);
NewTakeOut.infoWindowAnchor = new GPoint(8, 0);

var NewTakeOut2 = new GIcon();
NewTakeOut2.image = "/images/maps/elements/take-out2.png";
NewTakeOut2.shadow = "/images/maps/elements/dropshadow.png";
NewTakeOut2.iconSize = new GSize(20, 20);
NewTakeOut2.shadowSize = new GSize(22, 22);
NewTakeOut2.iconAnchor = new GPoint(10, 20);
NewTakeOut2.infoWindowAnchor = new GPoint(8, 0);


	

  }
}
				 
/**
* Helper function to hide the given DOM element
* @param {Object} element The DOM element that should be hidden
*/
function hide(element){
  element.style.display = "none";
  //     element.style.position = "absolute";
}

/**
* Helper function to show the given DOM element
* @param {Object} element The DOM element that should be displayed
*/
function show(element){
  element.style.display = "block";
  element.style.position = "relative";
}
	   
/**
* Show Driving Directions
*/
function showDirections()
{
  if (document.getElementById("from").value != '')
    var from = document.getElementById("from").value;
  if(window.gDirections) {
    window.gDirections.clear();
  }
    
  document.getElementById("directionsPanel").innerHTML = '';
  $("#directionsPanel").css("background-image", "url(/images/maps/elements/loading.gif)");
  
  $("#directionsPanel").html("<div id='cleardirections'>Clear Directions</div>");
	
  window.gDirections = new GDirections(window.map, document.getElementById("directionsPanel"));
  GEvent.addListener(window.gDirections, "load", onGDirectionsLoad); // Clear loading graphic when directions are loaded
  window.gDirections.load("from: "+from+" to: "+destination);
  window.map.closeExtInfoWindow();
}

// clear driving directions 
$("#cleardirections").live('click', function() {
if(window.gDirections) {
  window.gDirections.clear();
}
  map.setCenter(mapcenter, mapzoom, G_PHYSICAL_MAP);
  $("#directionsPanel").load("/maps/directions/"+trip_id);
});
	
function showDefaultDirections()
{
  if (document.getElementById('legend').style.display=="block") {
    $("#legend").hide("slide", {direction: "left"}, 1000);
    $(".logo_overlay").fadeTo(1000, .5);
  }
  window.map.closeExtInfoWindow();
  $("#directionsPanel").load("/maps/directions/"+trip_id);

  if (document.getElementById('directions').style.display!="block")
    $("#directions").show("slide", {direction:"left"}, 1000);
}
      
function showDirectionLine(departure_city) {
  var departure_array = departure_city.split("|");
  departure_array.push(destination);
  
  if(window.gDirections) {
    window.gDirections.clear();
  }
  window.gDirections = new GDirections(window.map);
	      
  window.gDirections.loadFromWaypoints(departure_array,{preserveViewport:true});
}

var newMarkers = [];
var latLngs = [];
var icons = [];

function onGDirectionsLoad(){
   $("#directionsPanel").css("height", "auto");
   $("#directionsPanel").css("background-image", "none");
}	
	
function onGDirectionsAddOverlay(){
  // Remove the draggable markers from previous function call.
  for (var i=0; i<newMarkers.length; i++){
    map.removeOverlay(newMarkers[i]);
  }
      
  // Loop through the markers and create draggable copies
  for (var i=0; i<=window.gDirections.getNumRoutes(); i++){
    var originalMarker = window.gDirections.getMarker(i);
    map.removeOverlay(originalMarker);
  }
}

function copyClick(newMarker,oldMarker){
  GEvent.addListener(newMarker, 'click', function(){
    GEvent.trigger(oldMarker,'click');
  });
}
		     


