///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////  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;
	var center_latLon;
	
	var currentLocation = window.location.pathname;
	
	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, _usercnts, _usercntsFormatted, iconLoc)
	{
		this.name = name;
		this.latitude = lat;
		this.longitude = log;
		this.usercounts = _usercnts;
		this.usercntsFormatted = _usercntsFormatted;
		this.pinstyle = iconLoc;
	}
	
	//
	// The function to add the pin to the location
	//
	function AddPinToLayer( pinloc )
	{
		try
		{	
			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\"><span class=\"title\">" + 
				pinloc.name + ' Region' +
				"</span><br/><span class=\"description\">" +
				pinloc.usercntsFormatted + ' customers without power' +
				"</span></div>";
			
			shape.SetDescription ( strDescVal  );
				
			shape.SetCustomIcon("<div class='" + pinloc.pinstyle + "'></div>");
			
			g_layer.AddShape(shape); 
		}
		catch ( err )
		{
			alert ( "AddPinToLayer - name: " + err.name + " \nmessage:" + err.message + " \nDescription:" + err.description + " \nLine:" + err.lineNumber   );
		}		    
			
	}
	
	//
	// Define the array of the AORs
	//
	function AddPins()
	{
		try
		{	
			// Iterate through the array and create the pins
			for (var i = 0; i < g_PinLocationsArr.length ; i += 1)
			{
				AddPinToLayer(g_PinLocationsArr[i]);
			}
		}
		catch ( err )
		{
			alert ( "AddPins - name: " + err.name + " \nmessage:" + err.message + " \nLine:" + err.lineNumber   );
		}		    
			
	}
	
	function GetMap()
	{
	    try
		{
		    var lat1 = 40.756693; 
		    var lon1 = -87.315750;
		    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();  		
		    //Resize Map
		    Resize();
    				

		    // Center location assumtion
		    //var center_lat = 41.94723447797775;
		    //var center_log = -88.59375000000001;
		    //var center_latLon = new VELatLong( center_lat  , center_log  );

		    //g_map.SetZoomLevel(7);
	            g_map.SetZoomLevel(9);

		    	AddRegionsLayer();
			//g_map.SetCenterAndZoom( center_latLon , 9);
			
			//Resize My Account - System Status (OutageMap)	    
			if (currentLocation.indexOf('systemstatus.aspx') != -1)
			{
				//alert('status');
				center_lat = 41.64723447797775;
				center_log = -88.39375000000001;
		
		        	//Test MSIE Version
				if (/MSIE (\d+\.\d+);/.test(navigator.userAgent))
				{
					var ieVersion = new Number(RegExp.$1);
					if (ieVersion >= 8)
					{
						center_log = -88.39375000000001;  
			            		//alert('ie8');	
		                  	
					}
					else if (ieVersion <= 7)
					{
						//alert('ie7');
					    	center_lat = 41.756693;
			            		center_log = -88.315750;

									

					}					
 		
					center_latLon = new VELatLong( center_lat  , center_log  );
			
					g_map.SetCenter(center_latLon);
					g_map.SetZoomLevel(7);	
					g_map.ZoomIn();
					//alert('ZoomIn');
					
				}
				else
				{
					//alert('fireSafari');
					
					center_latLon = new VELatLong( center_lat  , center_log  ); 
			
					g_map.SetCenter(center_latLon);
					g_map.SetZoomLevel(7);	
					g_map.ZoomIn();

				}

			}
				
			// Hide the dash board
		    	g_map.HideDashboard();	

		}
		catch ( err )
		{
			alert ( "GetMap - name: " + err.name + " \nmessage:" + err.message + " \nDescription:" + err.description + " \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 ( "AddRectangle - name: " + err.name + " \nmessage:" + err.message + " \nDescription:" + err.description + " \nLine:" + err.lineNumber   );
		}
		
	}
	
	function AddRegionsLayer()
	{
		try
		{
			var geoRssLayer1 = new VEShapeLayer();
			
			if (currentLocation.indexOf('systemstatus.aspx') != -1) 
			{
				//alert ('myaccount')
				g_GeoRssURLLocation = "https://myaccount.comed.com/_layouts/reduced_regions_area_boundaries.xml";
			}
			//alert(g_GeoRssURLLocation);
			var geoRssLayerSpec1 = new VEShapeSourceSpecification(VEDataType.GeoRSS , 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 ( "AddRegionsLayer - name: " + err.name + " \nmessage:" + err.message + " \nDescription:" + err.description + " \nLine:" + err.lineNumber   );
		}
	}
	
	function onFeedLoad(feed)
	{
		try
		{	
		    //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 ( 120, 120, 120 , 1.0) );
					    shape.SetFillColor( new VEColor ( 255, 255,  64 , 0.1) );
					    shape.SetLineWidth ( 2 ) ;
				    }
			    }
    			
			    //Resize My Account - System Status (OutageMap)	    
			    if (currentLocation.indexOf('systemstatus.aspx') == -1)
				{
				    //alert(currentLocation.indexOf('systemstatus.aspx'));        		
				    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.');
		}
		catch ( err )
		{
			alert ( "onFeedLoad - name: " + err.name + " \nmessage:" + err.message + " \nDescription:" + err.description + " \nLine:" + err.lineNumber   );
		}		    
	}
	
	//
	// 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 );
	}
	
	//
	// Resize
	//
	function Resize()
	{
		//Resize My Account - System Status (OutageMap)	    
		if (currentLocation.indexOf('systemstatus.aspx') != -1)
		{
			g_map.Resize(565,500);
		}	
	}
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////  End of the script    /////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
