/**    
 * Java-script function for the cycle page. This display a GOOGLE map 
 * at two zoom levels 9 and 15. At level 9 an image is overlaid to indicate
 * the area. At level 15 we dynamically draw poly-lines.
 *
 * The functions in the file rely on GOOGLE Maps, the ProgressControl and the
 * AreaSelectorControl
 */    
 	// progress message
    var progressMessage;
    var zoomLevel;
    var refreshNeeded;
    
    // GLOBALS
    var MINZOOM = 11;
    var MIDZOOM = 15;
    var MAXZOOM = 17;
    
    //zoom levels to be removed
    var MINZOOMREMOVE = 12;
    var MAXZOOMREMOVE = 14;
    var TOWNZOOM = 13;
  
    /**
     * Initialises the cycle page
     * @param id  the identifier for map where we draw the cycle route
     */
    function initializeCyclePage(id) 
    {
        try
        {
            // Can we display the maps?
            if (GBrowserIsCompatible()) 
            {
            	var mapTypes = _map.getMapTypes();
				// Overwrite the getMinimumResolution() and getMaximumResolution() methods
				for (var i=0; i < mapTypes.length; i++) 
				{
					mapTypes[i].getMinimumResolution = function() {return MINZOOM;}
					mapTypes[i].getMaximumResolution = function() {return MAXZOOM;}
				}
				
            	// add our custom controls
			    progressMessage = new ProgressControl();
			    _map.addControl(progressMessage);
			    
                // Move end, redraw the cycle routes
                GEvent.addListener(_map, "moveend", redraw);                              
            }
        }
        catch (e)
        {}
    }
    
    /**
     * Called to set the map zoom level as zoomed in to speed up cycle paths display.
     */
    function setZoomLevelCycle()
    {
    	if (zoomLevel == MINZOOMREMOVE || zoomLevel == TOWNZOOM)
		{
			_map.setZoom(MIDZOOM);			
		} 
		else if (zoomLevel == MAXZOOMREMOVE)
		{
			_map.setZoom(MINZOOM);
		}
    }
    
    /**
     * Called to redraw the map. If at the MAXZOOM then we will load
     * the cycle routes as poly-lines otherwise we will load the overview images
     */
    function redraw()
    {
    	//get zoom
		zoomLevel = _map.getZoom();
	   	if (cyclepaths.valueOf() == 'on')
	    {
	    	_map.clearOverlays();	    	
	    	refreshNeeded = 1;

	    	if (zoomLevel == MAXZOOMREMOVE || zoomLevel == MINZOOMREMOVE || zoomLevel == TOWNZOOM)
	    	{
	    		setZoomLevelCycle();
	    	}
	    	else
	    	{
			    // If we are at the min zoom add the ground overlay
				if (zoomLevel < MINZOOMREMOVE)
				{
				   	progressMessage.setText("<p>Loading Cycle Routes...</p>");
				   	//do not load cycle paths load GIF image over map
				    var boundaries = new GLatLngBounds(new GLatLng(53.72265,-1.718351),new GLatLng(53.88142,-1.413));
					var boundaries1 = new GLatLngBounds(new GLatLng(53.72345,-1.718351),new GLatLng(53.88202,-1.413));
	
				   	if(_map.getCurrentMapType() == G_NORMAL_MAP)
				   	{			    				        			        
				       	var overviewMap = new GGroundOverlay("../images/cyclingKey/cyclingoverview_image.gif",boundaries);			        	
				    }
				    else if(_map.getCurrentMapType() == G_SATELLITE_MAP)
				    {
				      	var overviewMap = new GGroundOverlay("../images/cyclingKey/cyclingoverview_imageSatellite.gif",boundaries);			        	
				    }
				   	else if (_map.getCurrentMapType() == G_HYBRID_MAP)
				   	{
				   		var overviewMap = new GGroundOverlay("../images/cyclingKey/cyclingoverview_imageHybrid.gif",boundaries1);			        	
				    }
				      	
				   	_map.addOverlay(overviewMap);
				   	 progressMessage.setText("");
				    refreshPage();
			    }
			    else if (zoomLevel > MAXZOOMREMOVE)
			    {
				   	progressMessage.setText("<p>Loading Cycle Routes...</p>");  
					// Down load the java-script that draws the poly-lines
				    var url = "../dynamicjs/cycleRouteSection.js?x1=" + _map.getBounds().getNorthEast().lat() 
				                    + "&y1=" + _map.getBounds().getNorthEast().lng()  
				                    + "&x2=" + _map.getBounds().getSouthWest().lat() 
				                    + "&y2=" + _map.getBounds().getSouthWest().lng();
				    GDownloadUrl(url,  drawLines);
				}			
			}
		}
		else if(refreshNeeded == 1)
		{
			//refresh page
			_map.clearOverlays();
			refreshPage();
			refreshNeeded = 0;
		}
    } 

    /**
     * Draws the poly-lines representing the cycle routes 
     */
    function drawLines(data, httpCode)
    {
        if (httpCode==200)
        {   
            // Call the java-script to draw the poly-lines
            eval(data);
            progressMessage.setText("");
            refreshPage();
        }
        else
        {
        	progressMessage.setText("<p>Failed to load Cycle Routes.</p>");
        }
    }
