﻿var map;
var grar = [ ] ;
var maploaded = false;
var delay = 100;
var result1;

function Load() 
{
	if (GBrowserIsCompatible()) 
	{
		map = new GMap2(document.getElementById("map"));
		map.addControl(new GSmallMapControl());
		map.addControl(new GMapTypeControl());
		map.setCenter(new GLatLng(39.639538, -101.250000), 3);
		maploaded = true;
	}
}

function getHex()
    {
        colors = new Array(14)
        colors[0]="0"
        colors[1]="1"
        colors[2]="2"
        colors[3]="3"
        colors[4]="4"
        colors[5]="5"
        colors[5]="6"
        colors[6]="7"
        colors[7]="8"
        colors[8]="9"
        colors[9]="a"
        colors[10]="b"
        colors[11]="c"
        colors[12]="d"
        colors[13]="e"
        colors[14]="f"
        digit = new Array(5)
        color=""
        for (i=0;i<6;i++){
        digit[i]=colors[Math.round(Math.random()*14)]
        color = color+digit[i]
        }
        return color;
    }
    
     function getLabel(index, theshape, cssclassname)
    {
        var elabelsA = [];          
        var label = new ELabel(new GLatLng(theshape[index].Latitude,theshape[index].Longitude), theshape[index].Text, cssclassname);        
        elabelsA.push(label);
        return elabelsA;
    }

   function ShowColoredZips(geography_id)
    {    
        var sessionIdCtl = $get('ctl00_cphMain_sessionId');
	var txtInternalZipCtl = $get('ctl00_cphMain_txtInternalZip');
        if(sessionIdCtl != null)
        {
            var sessionId = sessionIdCtl.value;
            var txtInternalZip = txtInternalZipCtl.value;
            PageMethods.ShowColoredZips(sessionId, txtInternalZip, geography_id, ShowColoredZipsCallback);
            var prgBar = $get('divProgress');
            if(prgBar != null)
               prgBar.style.visibility = 'visible';
        }
    }

   // Show colored zips on the map
  function ShowColoredZipsCallback(result)
    {
	result1 = result
	if(!maploaded)
	{	
	  setTimeout("ShowColoredZipsCallback(result1)",delay);
	}
	else
	  ShowOverlays(result1);
    }

  function ShowOverlays(result)
    {
	
	map.clearOverlays();
	
	var zipcount = result.ZipImage.length;

        for(var i = 0; i < zipcount; i++)
        {
           var groundOverlay = addProjectedOverlay( result.ZipImage[i].image, new GLatLng(result.ZipImage[i].swLat,result.ZipImage[i].swLong), new GLatLng (result.ZipImage[i].neLat,result.ZipImage[i].neLong) );
   	   grar.push(groundOverlay) ;
   	   map.addOverlay(groundOverlay);
	}

	var bounds = new GLatLngBounds( new GLatLng( result.ZipImage[0].centerSWLat, result.ZipImage[0].centerSWLong ), new GLatLng( result.ZipImage[0].centerNELat,result.ZipImage[0].centerNELong ));
	var zoom = map.getBoundsZoomLevel(bounds) ;
	map.setCenter(bounds.getCenter(),10);
    }     

  

  function addProjectedOverlay( url, sw, ne )
  {
   var groundOverlay = new ProjectedOverlay ( url, new GLatLngBounds(sw,ne) );
   groundOverlay.percentOpacity = 50;
   return groundOverlay ;
  }

  function setOpacity(opacity)
  {
   for ( var i = 0; i < grar.length; i++ )
   {
    grar[i].setOpacity(opacity) ;
   }
  }
