document.write('<script src="http://www.google.com/jsapi?key='+googlekey+'" type="text/javascript"><\/script>' );

var gmarkers2 		= [];
var gmarkers_code2 	= [];
var gmarkers_color2 = [];
var gmarkers_longtext = [];

var gmarkers 		= [];
var gmarkers_code 	= [];
var gmarkers_color 	= [];
var lats 	= [];
var longs 	= [];
var geocoder 		= null;
var targetaddress 	= null;
var actmarker 		= null;
var ewindow;
var point_style;
var citycenter;

// Call this function when the page has been loaded
function initialize()
{
	map = new GMap2(document.getElementById("map"));
	map.setCenter(new google.maps.LatLng(lat,long), 11);
	map.addControl(new GSmallZoomControl());
	map.addControl(new GMapTypeControl());
	geocoder = new GClientGeocoder();

	// 2 BE FINISHED!!!
	citycenter = new GMarker(new GLatLng(lat, long));
	map.addOverlay(citycenter);

	createIcon();

	//var point_style = new EStyle("/design/images/pointer.png", new GSize(24,24), "googlepointer", new GPoint(-10,18));
	var point_style = new EStyle("/design/images/onepix.gif", new GSize(1,1), "googlepointer", new GPoint(15,-25));
	ewindow = new EWindow(map, point_style);
}
function initializedetails(lata, longb, resultid)
{
	map_d = new GMap2($("map_details"+resultid));

	map_d.setCenter(new google.maps.LatLng(lata,longb), 15);
	map_d.addControl(new GSmallZoomControl());
	map_d.addControl(new GMapTypeControl());
	map_d.addControl(new GOverviewMapControl());

	var marker_details = new GMarker(new GLatLng(lata, longb));
    map_d.addOverlay(marker_details);
}
function initializedetails_car(pickUpLat, pickUpLong, dropOffLat, dropOffLong, sameDropOff, resultid )
{
	map_d = new GMap2($("map_pickUp"+resultid));
	map_d.setCenter(new google.maps.LatLng(pickUpLat,pickUpLong), 12);
	map_d.addControl(new GSmallZoomControl());
	map_d.addControl(new GMapTypeControl());
	map_d.addControl(new GOverviewMapControl());

	var marker_pickUp = new GMarker(new GLatLng(pickUpLat, pickUpLong));
    map_d.addOverlay(marker_pickUp);

    if (sameDropOff == false)
    {
    	map_d = new GMap2($("map_dropOff"+resultid));
		map_d.setCenter(new google.maps.LatLng(dropOffLat,dropOffLong), 12);
		map_d.addControl(new GSmallZoomControl());
		map_d.addControl(new GMapTypeControl());
		map_d.addControl(new GOverviewMapControl());

		var marker_dropOff = new GMarker(new GLatLng(dropOffLat, dropOffLong));
	    map_d.addOverlay(marker_dropOff);
    }
}
function initializePreview()
{
	map_det = new GMap2(document.getElementById("map_preview"));
	map_det.setCenter(new google.maps.LatLng(lat,longg), 10);
	var marker = new GMarker(new GLatLng(lat, longg));
    map_det.addOverlay(marker);
}
function initializeCityMap()
{
	map = new GMap2(document.getElementById("map"));
	map.setCenter(new google.maps.LatLng(lat,long), 3);
	map.addControl(new GSmallZoomControl());

	citycenter = new GMarker(new GLatLng(lat, long));
	map.addOverlay(citycenter);
}
function initializeAirportMap()
{
	map = new GMap2(document.getElementById("map"));
	map.setCenter(new google.maps.LatLng(lat,long), 9);
	map.addControl(new GSmallZoomControl());

	citycenter = new GMarker(new GLatLng(lat, long));
	map.addOverlay(citycenter);
}

// Create a base icon for all of our markers that specifies the shadow, icon dimensions, etc.
function createIcon()
{
	baseIcon = new GIcon();
	baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
	//baseIcon.iconSize = new GSize(20, 34);
	baseIcon.iconSize = new GSize(36, 48);
	//baseIcon.shadowSize = new GSize(37, 34);
	baseIcon.shadowSize = new GSize(36, 48);
	//baseIcon.iconAnchor = new GPoint(20, 34);
	baseIcon.iconAnchor = new GPoint(10, 8);

	//baseIcon.infoWindowAnchor = new GPoint(15, 20);
	baseIcon.infoWindowAnchor = new GPoint(0, 5);
	baseIcon.infoShadowAnchor = new GPoint(18, 25);
}

// Creates a marker whose info window displays the letter corresponding to the given index.
function createMarker(point, index, text, baseIcon, latt, longg, color)
{
	// Create a lettered icon for this point using our icon class
	var letteredIcon = new GIcon(baseIcon);
	//letteredIcon.image = "/design/images/markers/" + color + "/marker" + index + ".png";
	//letteredIcon.image = "/design/images/markers/" + color + index + ".gif";
	letteredIcon.image = "/design/images/markers/grey" + index + ".gif";
	markerOptions = { icon:letteredIcon };
	var marker = new GMarker(point, markerOptions);

	//GEvent.addListener(marker, 'click'	  , function() { marker.openInfoWindowHtml(text); });
	if (lastrun != 'false')
	{
		GEvent.addListener(marker, 'mouseover', function() { ewindow.openOnMarker(marker,text);markerin(index); });
		GEvent.addListener(marker, 'mouseout' , function() { ewindow.hide(marker,text);markerout(index); });
		GEvent.addListener(marker, 'infowindowclose' , function() { marker.closeInfoWindow();markerout(index);map.setCenter(new google.maps.LatLng(lat,long), 12);LegendMarkerOut(index); });
		GEvent.addListener(marker, 'click'	  , function() { markerclick(index); });
	}

	gmarkers[index] = marker;
	gmarkers_code[index] = text;
	gmarkers_color[index] = color;
	lats[index] = latt;
	longs[index] = longg;
	return marker;
}

function createMarker2(point, index, text, baseIconx, latt, longg, color)
{
	// Create a lettered icon for this point using our icon class
	var letteredIcon2 = new GIcon(baseIcon);
	//letteredIcon2.image = "/design/images/markers/red/marker" + index + ".png";
	letteredIcon2.image = "/design/images/markers/blue" + index + ".gif";

	markerOptions2 = { icon:letteredIcon2 };
	var marker2 = new GMarker(point, markerOptions2);

	if (lastrun != 'false')
	{
		GEvent.addListener(marker2, 'mouseover', function() { ewindow.openOnMarker(marker2,text);markerin(index); });
		GEvent.addListener(marker2, 'mouseout' , function() { ewindow.hide(marker2,text);markerout(index); });
		GEvent.addListener(marker2, 'infowindowclose' , function() { marker2.closeInfoWindow();markerout(index);map.setCenter(new google.maps.LatLng(lat,long), 12);LegendMarkerOut(index); });
		GEvent.addListener(marker2, 'click'	  , function() { markerclick(index); });
	}
	gmarkers2[index] = marker2;
	gmarkers_code2[index] = text;
	gmarkers_color2[index] = color;

	return marker2;
}

function markerclose(i)
{
	gmarkers2[i].closeInfoWindow();
	gmarkers[i].closeInfoWindow();
	LegendMarkerOut(i);
	map.setCenter(new google.maps.LatLng(latCC,longCC), 12);
}
function markerclick(i)
{
	document.location.href = '#leg_'+i;
	map.setCenter(new google.maps.LatLng(lats[i],longs[i]), 15);
	gmarkers[i].openInfoWindowHtml(gmarkers_longtext[i]);
	gmarkers[i].hide();gmarkers2[i].show();
	LegendMarkerIn(i);
//map.panBy(new GSize(-300, 60));
}
function markerin(i)
{
	ewindow.openOnMarker(gmarkers[i],gmarkers_code[i]);
	gmarkers[i].hide();
	gmarkers2[i].show();
}
function markerout(i)
{
	ewindow.hide(gmarkers[i],gmarkers_code[i]);
	gmarkers[i].show();
	gmarkers2[i].hide();
}
function showAddress(address)
{
	if (geocoder)
	{
		geocoder.getLatLng( address,
			function(point) {
            if (!point) { alert(address + " not found"); }
            else
            {
              if (actmarker) actmarker.remove();
              var marker = new GMarker(point);
              actmarker = marker;

              map.addOverlay(marker);
              targetaddress = marker.getLatLng();
              map.setCenter(point);
              //colorActSort('sortdistance');
              //getData2('distance');
              getData2(actsort);
              if (citycenter) citycenter.hide();
              //marker.openInfoWindowHtml(address);
            }
          }
        );
	}
}

function EStyle(stemImage, stemSize, boxClass, boxOffset)
{
	this.stemImage = stemImage;
	this.stemSize = stemSize;
	this.boxClass = boxClass;
	this.boxOffset = boxOffset;
	//this.border = border;

	// Known fudge factors are:
	// Firefox (1.0.6 and 1.5)    5, -1
	// IE 6.0                     0, -1
	// Opera 8.54                 3, -1
	// Opera 9 prev               4, -1
	// Netscape (7.2, 8.0)        5, -1
	// Safari                     5, -1

	var agent = navigator.userAgent.toLowerCase();
	var fudge = 5;  // assume Netscape if no match found

    if (agent.indexOf("opera") > -1) fudge = 3;
    if (agent.indexOf("firefox") > -1) fudge = 5;
    if (agent.indexOf("safari") > -1) fudge = 5;
	if ((agent.indexOf("msie") > -1) && (agent.indexOf("opera") < 1)) fudge = 5;
	this.fudge = fudge;
}
function EWindow(map,estyle)
{
	// parameters
	this.map=map;
	this.estyle=estyle;
	// internal variables
	this.visible = false;
	// browser - specific variables
	this.ie = false;
	var agent = navigator.userAgent.toLowerCase();
	if ((agent.indexOf("msie") > -1) && (agent.indexOf("opera") < 1)){ this.ie = true} else {this.ie = false}
}
EWindow.prototype.initialize = function(map)
{
	var div1 = document.createElement("div");
	div1.style.position = "absolute";
	map.getPane(G_MAP_FLOAT_SHADOW_PANE).appendChild(div1);
	var div2 = document.createElement("div");
	div2.style.position = "absolute";
	div2.style.width = this.estyle.stemSize.width+"px";
	map.getPane(G_MAP_FLOAT_SHADOW_PANE).appendChild(div2);
	this.div1 = div1;
	this.div2 = div2;
}
EWindow.prototype.openOnMap = function(point, html, offset)
{
	this.offset = offset||new GPoint(0,0);
	this.point = point;
	this.div1.innerHTML = '<div class="' + this.estyle.boxClass + '"><nobr>' + html + '</nobr></div>';
	if (this.ie && this.estyle.stemImage.toLowerCase().indexOf(".png")>-1)
	{
		var loader = "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+this.estyle.stemImage+"', sizingMethod='scale');";
		this.div2.innerHTML = '<div style="height:' +this.estyle.stemSize.height+ 'px; width:'+this.estyle.stemSize.width+'px; ' +loader+ '" ></div>';
	}
	else this.div2.innerHTML = '<img src="'+this.estyle.stemImage+'" width="'+this.estyle.stemSize.width+'" height="'+this.estyle.stemSize.height+'">';
	var z = GOverlay.getZIndex(this.point.lat());
	this.div1.style.zIndex = z;
	this.div2.style.zIndex = z+1;
	this.visible = true;
	this.show();
	this.redraw(true);
}
EWindow.prototype.openOnMarker = function(marker,html)
{
	var vx = marker.getIcon().iconAnchor.x - marker.getIcon().infoWindowAnchor.x;
	var vy = marker.getIcon().iconAnchor.y - marker.getIcon().infoWindowAnchor.y;
	this.openOnMap(marker.getPoint(), html, new GPoint(vx,vy));
}
EWindow.prototype.redraw = function(force)
  {
	if (!this.visible) {return;}
	var p = this.map.fromLatLngToDivPixel(this.point);
	this.div2.style.left   = (p.x + this.offset.x) + "px";
	this.div2.style.bottom = (-p.y + this.offset.y -this.estyle.fudge) + "px";
	this.div1.style.left   = (p.x + this.offset.x + this.estyle.boxOffset.x) + "px";
	this.div1.style.bottom = (-p.y + this.offset.y + this.estyle.boxOffset.y) + "px";
}
EWindow.prototype.remove = function()
{
	this.div1.parentNode.removeChild(this.div1);
	this.div2.parentNode.removeChild(this.div2);
	this.visible = false;
}
EWindow.prototype.show = function()
{
	this.div1.style.display="";
	this.div2.style.display="";
	this.visible = true;
}
EWindow.prototype.hide = function()
{
	this.div1.style.display="none";
	this.div2.style.display="none";
	this.visible = false;
}
EWindow.prototype.zindex = function(zin)
{
	var z = GOverlay.getZIndex(this.point.lat());
	this.div1.style.zIndex = z+zin;
	this.div2.style.zIndex = z+1+zin;
}
