var map;
var gdir;
var geocoder = null;
var addressMarker;
var setDirectionsExtraInfo = '';
var geEnabled = false;
var ge;

var icon = new GIcon();
    icon.image = "../themes/triton3/images/tritontools_icon.gif";
    icon.iconSize = new GSize(10,10); 	  
    icon.iconAnchor = new GPoint(8, 9);
    icon.infoWindowAnchor = new GPoint(6, 6);



function gotoPoint(point, details, zoom, silent) {
	if (G_SATELLITE_3D_MAP == map.getCurrentMapType()) {
	} else {
		map.setMapType(G_NORMAL_MAP);
	}
	map.setCenter(point, zoom);

	if (!silent) {
		var marker = new GMarker(point);
		map.addOverlay(marker);
		marker.openInfoWindowHtml(details);
	}
}



function markDealer(dealer) {
	
	var point = new GLatLng(dealers[dealer].pnt[0], dealers[dealer].pnt[1]);
	/*var details = '<div style="width:330px; padding-top:15px;"><div style="width:180px;float:right;"><img style="padding-left:15px;" src="'+dealers[dealer].img+'" /><br /><br /><p style="text-align:center;"><a href="http://'+dealers[dealer].website+'">'+dealers[dealer].website+'</a></p> <br /><div onclick="gotoPoint(new GLatLng('+dealers[dealer].pnt[0]+', '+dealers[dealer].pnt[1]+'), \'\', 16, true);"><p style="text-align:center;">[Click to zoom in]</p></div></div><div style="width:150px; float:left;">'+dealers[dealer].name+'<br />'+dealers[dealer].address+'</div></div>';*/ 
    var details = '<div style="width:280px; padding-top:15px;">';
    details += '<div style="float:right;" class="dealerG">';
    details +=  '<img src="'+dealers[dealer].img+'" style="padding-left:5px;" />';
    details += ' <p style="text-align:center;"><a href="http://'+dealers[dealer].website+'">'+dealers[dealer].website+'</a></p>';
    details +=  '<div onclick="gotoPoint(new GLatLng('+dealers[dealer].pnt[0]+', '+dealers[dealer].pnt[1]+'), \'\', 16, true);"><p style="text-align:center;text-decoration:underline;"><span style="cursor:hand;">[Click to zoom in]</span></p></div></div><span>'+dealers[dealer].name+'</span><div>'+dealers[dealer].address+'<br />';
    details += '</div>'; 
    
    if(dealers[dealer].phone != "") {
      details += '<br />Tel: '+dealers[dealer].phone+'<br />';
    }
    if(dealers[dealer].fax != "") {
      details += '<br />Fax: '+dealers[dealer].fax+'<br />';
    }
    if(dealers[dealer].email != "") {
      details += '<br />Email: '+dealers[dealer].email+'<br />';
    }  
    
    details += '</div>';    
    
	dealers[dealer].marker = new GMarker(point,icon);
	GEvent.addListener(dealers[dealer].marker, "click", function() {
		dealers[dealer].marker.openInfoWindowHtml(details);
	});
	map.addOverlay(dealers[dealer].marker);
}


function addDealers() {

 for(dealer in dealers) {  // print out the bands with descriptions
   
  markDealer(dealer);
  
   //dealers[address]['point'] = new GLatLng(dealers[address]['dealer_google_lat'], dealers[address]['dealer_google_lng']);
	
	
	//var details = shops[shopDetails].add;
	//if (shops[shopDetails].sta !== 1) {
		//var iconToUse = iconSoon;
	//} else {
		//var iconToUse = icon;
	//}
	//shops[shopDetails].marker = new GMarker(point, iconToUse);
	//GEvent.addListener(shops[shopDetails].marker, "click", function() {
		//shops[shopDetails].marker.openInfoWindowHtml(details);
	//});
	
	 //// Create our "tiny" marker icon
       //var blueIcon = new GIcon(G_DEFAULT_ICON);
       //blueIcon.image = "images/arrows/silverlinetools_icon.gif";
      
       // Set up our GMarkerOptions object
       //markerOptions = { icon:blueIcon };
       
       // marker = new GMarker(point);
       //map.addOverlay(new GMarker(point, markerOptions));
       
       //  dealers[address]['marker'] = new GMarker(dealers[address]['point']);
         //     map.addOverlay(dealers[address]['marker']);
             
       
       //GEvent.addListener(dealers[address]['marker'], "click", function() {
		// dealers[address]['marker'].openInfoWindowHtml(dealers[address]['dealer_address1']);
	  //});
       
        //map.addOverlay(marker);
        //marker.openInfoWindowHtml(place.address + '<br>' +
          //'<b>Country code:</b> ' + place.AddressDetails.Country.CountryNameCode + '<br>' + place.Point.coordinates[1] + '<br>' + place.Point.coordinates[0]);
   
	
	
	//map.addOverlay(shops[shopDetails].marker);
  

}
}

function load() {
	
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("map"));
		map.addControl(new GLargeMapControl());
		map.addControl(new GMenuMapTypeControl());
		map.addMapType(G_PHYSICAL_MAP);
		
		
		/*
		geocoder = new GClientGeocoder();

		gdir = new GDirections(map, document.getElementById("directions_info"));
		GEvent.addListener(gdir, "load", onGDirectionsLoad);
		GEvent.addListener(gdir, "error", handleErrors);
        */
		

		map.setCenter(new GLatLng(main_pos[0], main_pos[1]), main_zoom);
		map.setMapType(G_PHYSICAL_MAP);

		addDealers();
	}
}
window.onload=load;
window.onunload=GUnload;
