var map;
var _Longitude=0;
var _Latitude=0;
var _PointTitle='';
var _BranchNumber=0;
var _GoogleInfoWindowHtml='';
var _SmallMap=true;

var mgr;
var icons = {};
var geocoder = null;
var layerMain=null;

var iconData = {
  "us":{width:24,height:14},
  "ca":{width:24,height:14},
  "flag-shadow":{width:40,height:30},
  "house": {width:32,height:32},
  "RR_icon":{width:32,height:32},
  "ARS_icon":{width:32,height:32},
  "CBTL_icon":{width:32,height:32},
  "RR_iconsm":{width:30,height:27},
  "ARS_iconsm":{width:30,height:27},
  "CBTL_iconsm":{width:30,height:27},
  "house-shadow":{width:59,height:32},
  "headquarters":{width:32,height:32},
  "headquarters-shadow":{width:59,height:32}
}

function load() {
  if (GBrowserIsCompatible()) {
    map = new GMap2(document.getElementById("map"));
    if(_SmallMap)
        map.addControl(new GSmallMapControl()); //GLargeMapControl
    else
        map.addControl(new GLargeMapControl());
    map.addControl(new GOverviewMapControl());
    map.addControl(new GMapTypeControl());
    map.setCenter(new GLatLng(40, -98), 4);
    map.enableDoubleClickZoom();
    map.enableContinuousZoom();
    mgr = new GMarkerManager(map,{trackMarkers:false});
    window.setTimeout(setupOfficeMarkers, 0);
    geocoder = new GClientGeocoder();
	setupOverLayForBranch(_BranchNumber)
  }
}
function showAddress(address) {
  if (geocoder) {
    geocoder.getLatLng(
      address,
	    function(point) {
	    if (!point) {
		    alert(address + " not found");
	    } else {
		    map.setCenter(point, 13);
		    var marker = new GMarker(point);
		    map.addOverlay(marker);
		    marker.openInfoWindowHtml(address);
	    }
      }
    );
  }
}
function getIcon(images) {
    var icon = null;
    if (images) {
	    if (icons[images[0]]) {
		    icon = icons[images[0]];
	    } else {
		    icon = new GIcon();
		    icon.image = "http://www.pgdg.com/ars/imap/dev/" + images[0] + ".png";
		    var size = iconData[images[0]];
		    icon.iconSize = new GSize(size.width, size.height);
		    icon.iconAnchor = new GPoint(size.width >> 1, size.height >> 1);
		    icon.infoWindowAnchor = new GPoint(size.width >> 1, size.height >> 1);
		    icon.shadow = "http://www.pgdg.com/ars/imap/dev/" + images[1] + ".png";
		    size = iconData[images[1]];
		    icon.shadowSize = new GSize(size.width, size.height);
		    icons[images[0]] = icon;
	    }
    }
    return icon;
}
function setupOfficeMarkers() {	
    for (var i in officeLayer) {
	    var layer = officeLayer[i];
	    var markers = [];
	    var bAdd=false;
	    for (var j in layer["places"]) {
		    var place = layer["places"][j];
		    var icon = getIcon(place["icon"]);
		    if(icon!=null || icon!=undefined){
                bAdd=true;
		        var posn = new GLatLng(place["posn"][0], place["posn"][1]);
		        var lat = place["posn"][0];
		        var long = place["posn"][1];
		        var theTitle = place["name"];
		        var theAddress = place["address"];
		        var theCityStateZip = place["citystatezip"];
		        var thePhone = place["phone"];
		        var theServices = place["theServices"];
		        var theNumber = place["theNumber"];
		        var theZipCodes = place["theZipCodes"];
		        var marker = createMarker(posn, icon, theTitle, theAddress, theCityStateZip, thePhone, theServices, theZipCodes, theNumber, lat, long);
		        markers.push(marker);
		    }
	    }
	    if(bAdd)
	        mgr.addMarkers(markers, layer["zoom"][0], layer["zoom"][1]);
    }
    /*for (var i in officeLayer) {
	    var layer = officeLayer[i];
	    var markers = [];
	    for (var j in layer["places"]) {
		    var place = layer["places"][j];
		    var icon = getIcon(place["icon"]);
		    var posn = new GLatLng(place["posn"][0], place["posn"][1]);
		    var lat = place["posn"][0];
		    var long = place["posn"][1];
		    var theTitle = place["name"];
		    var theAddress = place["address"];
		    var theCity = place["citystatezip"];
		    var thePhone = place["phone"];
		    var theServices = place["theServices"];
		    var theNumber = place["theNumber"];
		    var theZipCodes = place["theZipCodes"];
		    var marker = createMarker(posn, icon, theTitle, theAddress, theCity, theState, theZip, thePhone, theServices, theZipCodes, theNumber, lat, long);
		    markers.push(marker);    			
	    }
	    mgr.addMarkers(markers, layer["zoom"][0], layer["zoom"][1]);
    }*/
    mgr.refresh();
}
function createMarker(posn, icon, theTitle, theAddress, theCityStateZip, thePhone, theServices, theZipCodes, theNumber, lat, long) {
    var marker = new GMarker(posn, {title: theTitle, icon: icon, draggable:false });
    var html = "";
    var FontStart="<font style=\"font-family:Arial,Helvetica,sans-serif;font-size:12px;color:#4d4d4d;\">";
    var NameFontStart="<font style=\"font-family:Arial,Helvetica,sans-serif;font-size:14px;font-weight:bold;color:#ec1c24;text-decoration:underline;\">";    
    var ServicesFontStart="<font style=\"font-family:Arial,Helvetica,sans-serif;font-size:12px;color:blue;\">";
    var FontEnd="</font>";
    
    html = NameFontStart + theTitle + FontEnd;
    html = html + "<br>"+ FontStart + theAddress+ FontEnd + "<BR>" + FontStart + theCityStateZip + FontEnd + "<BR>" + FontStart + "Phone:  "+ thePhone + FontEnd;
    //html = html + "<br><BR><a href=\"javascript:testOverlay('" + theNumber +"','" + theZipCodes + "', '" + lat + "', '" + long + "');void(0);\">Click here</a> to see coverage area.  ";
    html = html + "<span id='waitingMessage" + theNumber + "'></span>";
    html = html + "<BR><BR><B>Services</B>:  "+ ServicesFontStart + theServices + FontEnd;
	
    GEvent.addListener(marker, "click", function() {marker.openInfoWindowHtml(html) } ); 
    return marker;
}
function testOverlay(theBranch, theZips, lat, long)
{    	
    if (theZips) {
	    var bgcolor = "#1569be"
	    var colormain ="#001f60"
	    var lines = LinesList.split('|');
	    var pointsListArray = PointsList.split('~');
	    var iLoopIndex=0;
	    // read each line
	    for (var a = 0; a < lines.length; a++) {
	        var width  = parseFloat(lines[a]);
	        var pointsItems = pointsListArray[iLoopIndex].split('|');
	        var pts = [];
            if(pointsItems!=null || pointsItems!=undefined){
	            for(var j=0;j<pointsItems.length;j++){
                    var LatLangs=pointsItems[j].split(',');
                    if(LatLangs.length>0){
                        pts[j] = new GLatLng(parseFloat(LatLangs[0]),parseFloat(LatLangs[1]));
                    }
                }
	            map.addOverlay(new GPolygon(pts,colormain,width,0.5,bgcolor, 0.25));
	        }
	        iLoopIndex++;
	    }
	    map.setCenter(new GLatLng(lat, long), 8);
        //request.send(null);
    }
}
function setupOverLayForBranch(theBranch) {
    var needOverLay = "no";
    for (var i in officeLayer) {
	    var layer = officeLayer[i];
	    layerMain=layer;
	    var markers = [];
	    for (var j in layer["places"]) {
		    var place2 = layer["places"][j];
		    if (theBranch == place2["theNumber"]) {
			    var icon2 = getIcon(place2["icon"]);
			    var posn2 = new GLatLng(place2["posn"][0], place2["posn"][1]);
			    var lat2 = place2["posn"][0];
			    var long2 = place2["posn"][1];
			    var theTitle2 = place2["name"];
			    var theAddress2 = place2["address"];
			    var theCityStateZip2 = place2["citystatezip"];
			    var thePhone2 = place2["phone"];
			    var theServices2 = place2["theServices"];
			    var theNumber2 = place2["theNumber"];
			    var theZipCodes2 = place2["theZipCodes"];
			    needOverLay = "yes";
		    }
	    }		
    }
    if (needOverLay == "yes") {
	    testOverlay(theNumber2,theZipCodes2,lat2,long2)
    }    	
}
function openBigMap(url)
{
    window.open (url, "mywindow","location=0,status=1,resizable=1,scrollbars=1,width=800,height=800");
}