///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////  Start of the script    ///////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////


	onerror=handleErr;
	var txt="";
	var g_PinLocationsArr = new Array();
    var g_GeoRssURLLocation;
	// The token string is assigned in the page's code behind
    var g_token;
    var points = new Array();
	// varible to hold the map
    var g_map = null; 
	var g_layer;
	var shape;
	
	function ShapeHandler(e)      
	{         
               /*
            if(e.elementID != null)
            {
               shape = g_map.GetShapeByID(e.elementID);
               var info = "";
               info += "ID (event object): " + e.elementID + "\n";
               info += "ID (GetID method): " + shape.GetID() + "\n";
               info += "Type: " + shape.GetType() + "\n";
               info += "Title: " + shape.GetTitle() + "\n";
               info += "Description: " + shape.GetDescription() + "\n";
               info += "More Info URL: " + shape.GetMoreInfoURL() + "\n";
               info += "Photo URL: " + shape.GetPhotoURL() + "\n";
             
               fillColor = shape.GetFillColor();
               info += "Fill Color: R: " + fillColor.R +
                       " | G: " + fillColor.G +
                       " | B: " + fillColor.B +
                       " | A: " + fillColor.A +
                       "\n";
          
               lineColor = shape.GetLineColor();  
               info += "Line Color: R: " + lineColor.R +
                       " | G: " + lineColor.G +
                       " | B: " + lineColor.B +
                       " | A: " + lineColor.A + "\n"; 
             
               info += "Line Width: " + shape.GetLineWidth() + "\n";
               alert(info);
            }
            else
            {
               //alert("No shape information.");
            }
            */
		/*
		if (e.elementID != null)
		{   
			document.getElementById('resultDiv').innerHTML = e.eventName + " event occurred on shape (id=" + e.elementID + typeof(e) + ").";         

			var s = typeof e;
			
			document.getElementById('resultDiv').innerHTML = s;
			if ( s == 'object' )
			{
			}
			if ( s == 'shape' )
			{
				s = 'VEShape';
				alert ( s);
				s.Hide();
			}			
			
			var info = "";
            //info += "ID (event object): " + e.elementID + "\n";
            //info += "ID (GetID method): " + shape.GetID() + "\n";
            //info += "Type: " + shape.GetType() + "\n";
            //info += "Title: " + shape.GetTitle() + "\n";
            //info += "Description: " + shape.GetDescription() + "\n";
            //info += "More Info URL: " + shape.GetMoreInfoURL() + "\n";
            //info += "Photo URL: " + shape.GetPhotoURL() + "\n";
            //var fillColor = e.GetFillColor();
            //e.Hide();
        
            var lineColor = shape.GetLineColor();  
            info += "Line Color: R: " + lineColor.R +
                    " | G: " + lineColor.G +
                    " | B: " + lineColor.B +
                    " | A: " + lineColor.A + "\n"; 
            
            info += "Line Width: " + shape.GetLineWidth() + "\n";
		}
		else            
			document.getElementById('resultDiv').innerHTML = e.eventName + " event occurred on g_map. "  + typeof(e);      
			*/
	}

	function handleErr(msg,url,l)
	{
		txt="There was an error on this page.\n\n";
		txt+="Error: " + msg + "\n";
		txt+="URL: " + url + "\n";
		txt+="Line: " + l + "\n\n";
		txt+="Click OK here to continue.\n\n";
		alert(txt);
		return true;
	}

    //
    // The event handler function which can be extended in the future
    //
    function MyHandleTokenError()
    {
	    alert("Token is invalid.");
    }
    //
    // The event handler for the future enhancement.
    //

    function MyHandleTokenExpire()
    {
	    alert("Token has expired.");
    
    }

    function MyHandleMouseWheel()
	{
		//alert('In function MyHandleMouseWheel');
		return true;
	}

    function MyHandleMouseDown()
	{
		//alert('In function MyHandleMouseDown');
		return true;
	}
	function MyHandleOnKeyUp()
	{
		return true;
	}
	function MyHandleOnKeyDown()
	{
		return true;
	}
	function MyHandleOnKeyPress()
	{
		return true;
	}
	function MyHandleOnDBlClick()
	{
		return true;
	}

	function MyHandleOnStartZoom()
	{
		return true;
	}
	
	function MyHandleOnStartPan()
	{
		return true;
	}
	
	function MyHandleOnClick( e )
	{
		var x = e.mapX;
		var y = e.mapY;
		var pixel = new VEPixel( x, y );
		alert ( pixel.x + " " + pixel.y );
		if ( g_map != null )
		{
			var LatLong123 = g_g_map.PixelToLatLong ( pixel );
			// NOTE: make sure there is info span
			info.innerHTML = "Lat: " + LatLong123.Latitude +  " Log: " + LatLong123.Longitude ;
		}
		//val LL = g_g_map.PixelToLatLong ( pixel );
		//alert ( "Lat Log : " + LL );
		//return true;
	}
	// 
	// The pin location creat function.
	//
	function pinlocation(name, lat, log, cust_tot, _usercnts, _usercntsFormatted, iconLoc)
	{
		this.name = name;
		this.latitude = lat;
		this.longitude = log;
		this.totalcustomers = cust_tot;
		this.usercounts = _usercnts;
		this.usercntsFormatted = _usercntsFormatted;
		this.pinstyle = iconLoc;
	}
	//
	// The function to add the pin to the location
	//
	function AddPinToLayer( pinloc )
	{
		var latLon = new VELatLong( pinloc.latitude, pinloc.longitude); 
		//Add a Aurora pushpin to the new layer
		
		shape = new VEShape(VEShapeType.Pushpin, latLon );
		//shape.SetTitle(pinloc.name );
		//shape.SetDescription(pinloc.usercntsFormatted + ' customers without power');
		
		
		var strDescVal = "<div class=\"" + pinloc.pinstyle + "Popup\">" + 
			"<strong>County: " + pinloc.name +
			"<br/>" +
			"Customers Affected: " + pinloc.usercntsFormatted +
			"<br/>" +
			"Customers Served: " + addCommas(pinloc.totalcustomers) + "</strong>" +
			"<br/>" +
			"<br/>" +
			"<A HREF=\"https://ecustomer.ceco.com/ECS/PecoSelfService/loginSecure.do\" target=\"_blank\">Report Problem / Check Status</A>" +
			"</div>";
		
		shape.SetDescription ( strDescVal  );
		

		
		
        shape.SetCustomIcon("<div class='" + pinloc.pinstyle + "'></div>");
        
		g_layer.AddShape(shape); 
	}
	
    function addCommas(nStr)
    {
	    nStr += '';
	    x = nStr.split('.');
	    x1 = x[0];
	    x2 = x.length > 1 ? '.' + x[1] : '';
	    var rgx = /(\d+)(\d{3})/;
	    while (rgx.test(x1)) {
		    x1 = x1.replace(rgx, '$1' + ',' + '$2');
	    }
	    return x1 + x2;
    }

	//
	// Define the array of the AORs
	//
	function AddPins()
	{
		// Iterate through the array and create the pins
		for (var i = 0; i < g_PinLocationsArr.length ; i += 1)
		{
			AddPinToLayer(g_PinLocationsArr[i]);
		}
	}
    function GetMap()
    {
        iframeResizePipe();
        
		var lat1 = 40.212166; 
		var lon1 = 75.668500;
		var latLon = new VELatLong( lat1 , lon1 ); 
		//
		// The ID must match what is in the aspx page.
		//
        g_map = new VEMap('OutageMapApplication');
        // Attache an event handler
        g_map.AttachEvent('onmousewheel', MyHandleMouseWheel); 
        // Attache an event handler
        g_map.AttachEvent('onmousedown', MyHandleMouseDown); 
        
        //g_map.AttachEvent('onclick', MyHandleOnClick); 

		g_map.AttachEvent("onmouseover",ShapeHandler);
		g_map.AttachEvent("onmouseout",ShapeHandler);
		g_map.AttachEvent("onkeyup",MyHandleOnKeyUp);
		g_map.AttachEvent("onkeydown",MyHandleOnKeyDown);
		g_map.AttachEvent("onkeypress",MyHandleOnKeyPress);
		g_map.AttachEvent("ondoubleclick",MyHandleOnDBlClick);
		g_map.AttachEvent("onstartzoom", MyHandleOnStartZoom);
		g_map.AttachEvent("onstartpan", MyHandleOnStartPan);
        
        g_map.SetClientToken(g_token); 
        
        g_map.LoadMap();
        
        // Hide the dash board
        g_map.HideDashboard();
        // Center location assumtion
        var center_lat = 41.21216666666667;
        var center_log = 75.66850000000000;
		var center_latLon = new VELatLong( center_lat  , center_log  ); 
        //g_map.SetCenterAndZoom( center_latLon , 7);
        
        
	    try
	    {
			AddRegionsLayer();
	        //g_map.SetCenterAndZoom( center_latLon , 9);
		}
		catch ( err )
		{
			alert ( "name: " + err.name + " \nmessage:" + err.message + " \nLine:" + err.lineNumber   );
		}


    }
    
	function AddRectangle()
    {
	    try
	    {
			var view = g_map.GetMapView();
			//Create a pair of VELatLong arrays in the shape of an octagon
			var points01 = new Array();
			points01 [0] = view.TopLeftLatLong		;
			points01 [1] = new VELatLong( view.BottomRightLatLong.Latitude, view.TopLeftLatLong.Longitude);
			points01 [2] = view.BottomRightLatLong ;
			points01 [3] = new VELatLong( view.TopLeftLatLong.Latitude, view.BottomRightLatLong.Longitude);
			points01 [4] = view.TopLeftLatLong		;
	        
	        //alert (   points01 );  

			//Create VEShape objects, set parameters, and add to the map         
			var shape01 = new VEShape(VEShapeType.Polygon, points01);            
			//shape01.SetLineWidth(3);
			shape01.SetLineColor(new VEColor(120,120,120,0.5));
			shape01.SetFillColor(new VEColor(255,255,255,0.4));
			shape01.SetTitle("Title for shape01");
			shape01.SetDescription("This is the description of shape01.");
			shape01.SetPoints(points01);
			shape01.HideIcon();
			g_map.AddShape(shape01);
			
			

		}
		catch ( err )
		{
			alert ( "name: " + err.name + " \nmessage:" + err.message + " \nLine:" + err.lineNumber   );
		}
		
    }
    
	function AddRegionsLayer()
    {
		try
		{
			var geoRssLayer1 = new VEShapeLayer();
			//alert(g_GeoRssURLLocation);
			var geoRssLayerSpec1 = new VEShapeSourceSpecification(VEDataType.ImportXML , g_GeoRssURLLocation , geoRssLayer1);
			//alert("VEShapeSourceSpecification created");
			g_map.ImportShapeLayerData(geoRssLayerSpec1, onFeedLoad, 1);
			//alert(" Called g_map.ImportShapeLayerData");
			/*
			alert(geoRssLayer1.GetShapeCount())
			for ( var i = 0; i < geoRssLayer1.GetShapeCount() ; i++ )
			{
				var veShape = geoRssLayer1.GetShapeByIndex(i);
				//veShape.Hide();
				//alert(veShape.GetType());
			}
			*/
		}
		catch ( err )
		{
			alert ( "name: " + err.name + " \nmessage:" + err.message + " \nLine:" + err.lineNumber   );
		}
    }
	function onFeedLoad(feed)
    {
		//alert('RSS or Collection loaded. There are ' + feed.GetShapeCount() + ' items in this list.');
		if ( feed != null )
		{
			for ( var cnt = 0; cnt < feed.GetShapeCount() ; cnt ++ )
			{
				var shape = feed.GetShapeByIndex ( cnt );
				points[ cnt ] = shape.GetIconAnchor();
				if ( shape != null )
				{
					shape.HideIcon();
					shape.SetLineColor( new VEColor ( 0, 51, 153, .7) );
					shape.SetFillColor( new VEColor ( 192, 192,  192, .2) );
					shape.SetLineWidth ( 2 ) ;
				}
			}
			g_map.SetMapView ( points );
			// Create a sample hex layer
			CreateLayer();
			// Create and add the push pins
			AddPins();
			//AddRectangle();
		}
		//alert('RSS or Collection loaded. There are ' + feed.GetShapeCount() + ' items in this list.');
    }
    
    //
    // The is a helper function to find a center for the map
    //
	function GetCenter()
    {  
		var center = g_map.GetCenter();
		alert(center);
    }   
    //
    // Create layer
    //
    function CreateLayer()
    {
		g_layer = new VEShapeLayer();
		g_map.AddShapeLayer( g_layer );
	}
	
	function iframeResizePipe() 
    { 
        // What's the page height? 
        var height = document.body.scrollHeight; 
 
        // Going to 'pipe' the data to the parent through the helpframe.. 
        var pipe = document.getElementById('helpframe'); 
 
        // Cachebuster a precaution here to stop browser caching interfering 
        pipe.src = 'http://www.peco.com/helper.html?height='+height+'&cacheb='+Math.random();  
  } 
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////  End of the script    /////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////

