//Functions used to create the various layers 
var graphic_format = "image/png";
var buffer_size = 0; 

//var use_alpha = OpenLayers.Util.alphaHack();
var use_alpha = true; 
function get_standard_layers(map_server, max_bounds)
{
	var layers = new Array();
	var requested_layers; 
	var loaded_layer_counter; 
	var requested_layer_counter; 
	var loaded_layer_name;
	var requested_layer_name;
	var result;
	
	//Check to see if alpha transparency is required 
	//use_alpha = useAlphaTransparency(); 
	
	//Set up the standard areas
	layers.push(create_os_base_layer(map_server, max_bounds));
	layers.push(create_aerial_photos_base_layer(map_server, max_bounds));
	layers.push(create_community_layer(map_server, max_bounds));	
	layers.push(create_offices_layer(map_server, max_bounds));	
	layers.push(create_democracy_layer(map_server, max_bounds));	
	layers.push(create_education_layer(map_server, max_bounds));		
	layers.push(create_environment_layer(map_server, max_bounds));			
	layers.push(create_health_layer(map_server, max_bounds));				
	layers.push(create_recreation_layer(map_server, max_bounds));					
	layers.push(create_waste_layer(map_server, max_bounds));						
	
	layers.push(create_areas_layer(map_server, max_bounds));
	layers.push(create_wards_layer(map_server, max_bounds));
	layers.push(create_medical_practices_layer(map_server, max_bounds));
	layers.push(create_aerial_photos_layer(map_server, max_bounds));
	//layers.push(create_confirm_lighting_layer(map_server, max_bounds));
				
	//Get the requested overlay layers
	requested_layers = get_requested_layers(); 
	
	//Loop through the standard layers to see 
	//which ones should be switched on 
	for(loaded_layer_counter in layers)
	{
		
		loaded_layer_name = unescape(layers[loaded_layer_counter].name);
		
		for(requested_layer_counter in requested_layers)
		{
			requested_layer_name = unescape(requested_layers[requested_layer_counter]);
			
			if(requested_layer_name == loaded_layer_name)
			{
				//switch on the layer 
				layers[loaded_layer_counter].setVisibility(true);
				
				//Found match so can break out of the loop
				break;
			}
		}

	}
	
	//Return the standard layers
	return layers;
}
function get_planning_layers(map_server, max_bounds)
{
	var layers = new Array();
	var requested_layers; 
	var loaded_layer_counter; 
	var requested_layer_counter; 
	var loaded_layer_name;
	var requested_layer_name;
	var result;

	//Check to see if alpha transparency is required 
	//use_alpha = useAlphaTransparency(); 

	//Set up the base layers
	layers.push(create_os_base_layer(map_server, max_bounds));
	layers.push(create_aerial_photos_base_layer(map_server, max_bounds));
	//layers.push(create_his_layer(map_server, max_bounds));
	
	//layers.push(create_confirm_lighting_layer(map_server, max_bounds));
	layers.push(create_plan_apps_layer(map_server, max_bounds));
	layers.push(create_housing_land_audit_live_layer(map_server, max_bounds));
	layers.push(create_housing_land_audit_draft_layer(map_server, max_bounds));
	layers.push(create_emp_land_audit_2008_layer(map_server, max_bounds));
	layers.push(create_housing_market_areas_layer(map_server, max_bounds));
	layers.push(create_alp_settlement_layer(map_server, max_bounds));

	layers.push(create_wards_layer(map_server, max_bounds));
	layers.push(create_aerial_photos_layer(map_server, max_bounds));

	//Get the requested overlay layers
	requested_layers = get_requested_layers(); 

	//Loop through the standard layers to see 
	//which ones should be switched on 
	for(loaded_layer_counter in layers)
	{
			
		loaded_layer_name = unescape(layers[loaded_layer_counter].name);
		
		for(requested_layer_counter in requested_layers)
		{

			requested_layer_name = unescape(requested_layers[requested_layer_counter]);
			if(requested_layer_name == loaded_layer_name)
			{
				//switch on the layer 
				layers[loaded_layer_counter].setVisibility(true);
			}
			
		}

	}
	
	//Return the standard layers
	return layers;
}
function get_hla_layers(map_server, max_bounds, stage)
{
	var layers = new Array();
	var requested_layers; 
	var loaded_layer_counter; 
	var requested_layer_counter; 
	var loaded_layer_name;
	var requested_layer_name;
	var result;

	//Check to see if alpha transparency is required 
	//use_alpha = useAlphaTransparency(); 

	//Set up the base layers
	layers.push(create_os_base_layer(map_server, max_bounds));
	layers.push(create_aerial_photos_base_layer(map_server, max_bounds));
	//layers.push(create_his_layer(map_server, max_bounds));
	
	//layers.push(create_confirm_lighting_layer(map_server, max_bounds));
	//layers.push(create_plan_apps_layer(map_server, max_bounds));
	layers.push(create_housing_land_audit_live_layer(map_server, max_bounds));
	layers.push(create_housing_land_audit_draft_layer(map_server, max_bounds));
	//layers.push(create_emp_land_audit_2008_layer(map_server, max_bounds));
	layers.push(create_housing_market_areas_layer(map_server, max_bounds));
	layers.push(create_alp_settlement_layer(map_server, max_bounds));

	layers.push(create_wards_layer(map_server, max_bounds));
	layers.push(create_aerial_photos_layer(map_server, max_bounds));

	//Get the requested overlay layers
	requested_layers = get_requested_layers(); 
	//Loop through the standard layers to see 
	//which ones should be switched on 
	for(loaded_layer_counter in layers)
	{
			
		loaded_layer_name = unescape(layers[loaded_layer_counter].name);
		//for(requested_layer_counter in requested_layers)
		//{

			//requested_layer_name = unescape(requested_layers[requested_layer_counter]);
			//if(requested_layer_name == loaded_layer_name)
			//{
				//switch on the layer 
				//layers[loaded_layer_counter].setVisibility(true);
			//}
			
		//}
		
		//Switch on the appropriate hla layer
		if(stage == "draft" && loaded_layer_name == "Draft Housing Land Audit")
		{
			layers[loaded_layer_counter].setVisibility(true);
		}
		if(stage == "live" && loaded_layer_name == "Housing Land Audit Live")
		{
			layers[loaded_layer_counter].setVisibility(true);
		}		
	}
	
	//Return the standard layers
	return layers;
}

function get_ela_layers(map_server, max_bounds)
{
	var layers = new Array();
	var emp_layer;

	//Check to see if alpha transparency is required 
	//use_alpha = useAlphaTransparency(); 

	//Set up the base layers
	layers.push(create_os_base_layer(map_server, max_bounds));
	layers.push(create_aerial_photos_base_layer(map_server, max_bounds));
	//layers.push(create_his_layer(map_server, max_bounds));
	
	//layers.push(create_confirm_lighting_layer(map_server, max_bounds));
	//layers.push(create_plan_apps_layer(map_server, max_bounds));
	layers.push(create_housing_land_audit_live_layer(map_server, max_bounds));
	//layers.push(create_housing_land_audit_draft_layer(map_server, max_bounds));
	
	//Ensure the employment land audit layer is swwitched on by default
	emp_layer = create_emp_land_audit_2008_layer(map_server, max_bounds);
	emp_layer.setVisibility(true);
	layers.push(emp_layer);
	
	layers.push(create_housing_market_areas_layer(map_server, max_bounds));
	layers.push(create_alp_settlement_layer(map_server, max_bounds));

	layers.push(create_wards_layer(map_server, max_bounds));
	layers.push(create_aerial_photos_layer(map_server, max_bounds));

	//Return the standard layers
	return layers;
}

function get_fault_reporting_layers(map_server, max_bounds)
{
	var layers = new Array();
	var requested_layers; 
	var loaded_layer_counter; 
	var requested_layer_counter; 
	var loaded_layer_name;
	var requested_layer_name;
	var result;

	//Check to see if alpha transparency is required 
	//use_alpha = useAlphaTransparency(); 

	//Set up the standard layers
	layers.push(create_os_base_layer(map_server, max_bounds));
	layers.push(create_aerial_photos_base_layer(map_server, max_bounds));
	layers.push(create_aerial_photos_layer(map_server, max_bounds));

	//Set up the layers that users can report faults on
	layers.push(create_confirm_lighting_layer(map_server, max_bounds));
	layers.push(create_road_unit_layer(map_server, max_bounds));

	
	//Get the requested overlay layers
	requested_layers = get_requested_layers(); 
		
	//Loop through the standard layers to see 
	//which ones should be switched on 
	for(loaded_layer_counter in layers)
	{
		
		loaded_layer_name = unescape(layers[loaded_layer_counter].name);

		for(requested_layer_counter in requested_layers)
		{

			requested_layer_name = unescape(requested_layers[requested_layer_counter]);
			
			if(requested_layer_name == loaded_layer_name)
			{
				//switch on the layer 
				layers[loaded_layer_counter].setVisibility(true);
			}
		}

	}
	
	//Return the standard layers
	return layers;
}

function get_requested_layers()
{	
	var params = new Array();
	var requested_layers = new Array();
	var all_layers = new Array();
	var layers_param; 
	
	
	//Get the url parameters 
	params = get_url_params();

	//Extract the requested layers
	layers_param = params['overlays'];
	if(layers_param)
	{
		requested_layers = layers_param.split(',');
	}
	
	if(requested_layers)
	{
		//all_layers.concat(requested_layers);
		//all_layers.push(requested_layers);
		var current_layer;
		var temp_array;
		
		for(current_layer in requested_layers)
		{	
			var layer_name = unescape(requested_layers[current_layer]);		
			switch(layer_name)
			{
				case "Results Base Layer":
					break;
				case "Results": 
					break;
				default:
					//Normal layer so add it					
					temp_array = new Array();
	
					temp_array.push(requested_layers[current_layer]);
					all_layers.push(temp_array);
					break;		
			} 

		}
		
	}

	return all_layers;
			
}

function get_requested_base_layer_name()
{	
	var params = new Array();
	var base_layers = new Array();
	var base_layer_param; 
    var base_layer = "";	
	
	//Get the url parameters 
	params = get_url_params();
	
	//Extract the requested base layers (should be only one)
	base_layer_param = params['base'];
	if(base_layer_param)
	{
		base_layers = base_layer_param.split(',');
		
		//Get the first base layer requested
		base_layer = base_layers.shift();

	}

	return base_layer;
		
}

function get_requested_base_layer()
{
	var base_layer_name = ""
	var found_layers;
	var base_layer;
	var current_layer_id;	
	var current_layer;
	
	//Get the requested base layer
	base_layer_name = unescape(get_requested_base_layer_name()); 	
	
	if(base_layer_name != "") 
	{
		
		//Get the requested layer
		found_layers = map.layers;
		if(found_layers)
		{
			for(current_layer_id in found_layers)
			{
				//Get the current layer 
				current_layer = found_layers[parseInt(current_layer_id)];
				if(current_layer)
				{
					
					if(current_layer.name == base_layer_name)
					{
						base_layer = current_layer;
					}
					
				}
				
			}
					
		}
	}

	return base_layer;
}


function get_url_params()
{	
	var supplied_values = new Array();

	var query_string = window.location.search.substring(1);
	var temp; 
	
	//Extract the sections from the query string 
	var params = query_string.split('&'); 
	for(var i=0; i < params.length; i++)
	{
		var pos = params[i].indexOf('=');
		if(pos > 0)
		{
			var key = params[i].substring(0, pos);
			temp = params[i].substring(pos + 1);
				
			temp = replaceSubstring(temp, "+", " ");
			val = replaceSubstring(temp, "%2C", ",");
			supplied_values[key] = val;
		}
	}
	
	//Ensure valid values were obtained 
	return supplied_values;
	
}

function create_os_base_layer(map_server, max_bounds)
{
	//Set up the Ordnance survey base layer 
	var base_layer = new OpenLayers.Layer.WMS(
		"OS Basemap",
		map_server,
   		{
			version : "1.1.0",
			exceptions : "application/vnd.ogc.se_xml",
			format : graphic_format
        },
    	{
			gutter : 15,
			buffer : buffer_size,						
			visibility : true,
			isBaseLayer : true,		
			transitionEffect: 'resize',
			maxExtent : max_bounds			
    	}
		
    );

	//The base map shouldn't be transparent 
	
	//Return the OS base layer
	return base_layer;	
}




function create_aerial_photos_base_layer(map_server, max_bounds)
{
	//Set up the Aerial Photos as a base layer 
	var base_layer = new OpenLayers.Layer.WMS(
		"Aerial Base Map",
		map_server,
   		{
			version : "1.1.0",
			format : graphic_format,
			exceptions : "application/vnd.ogc.se_xml",
			//layers : 'swrefVcollectionVdatasetZaerialVcollectionZmb_mastermap_ortho_image',
			layers : 'swrefVcollectionVdatasetZaerial_ecwVcollectionZA$0020tiled$0020data$0020product$002e',
			transparent : false

        },
    	{
			//scales : [100000, 25000, 10000, 4000, 2500, 1000],			
			scales : [25000, 10000, 4000, 2500, 1000],			
			gutter : 15,
			buffer : buffer_size,						
			visibility : false,
			transitionEffect: 'resize',
			isBaseLayer : true,				
			maxExtent : max_bounds			
    	}
		
    );

	//The base map shouldn't be transparent 
	
	//Return the OS base layer
	return base_layer;	
}

function create_areas_layer(map_server, max_bounds)
{
	//Set up the Aberdeenshire Areas layer
	var areas = new OpenLayers.Layer.WMS(
		"Aberdeenshire Areas",
		map_server,
   		{
			layers : 'swrefVcollectionVdatasetZgisVcollectionZaberdeenshire_area',
			version : "1.1.0",
			exceptions : "application/vnd.ogc.se_xml",
			transparent : true,				
			format : graphic_format
        },
    	{
			gutter : 15,			
			buffer : buffer_size,						
			visibility : false,
			isBaseLayer : false,				
			maxExtent : max_bounds			
    	}
		
    );

	//If the current browser is IE 5.X or 6.x then set alpha transparency on
	if(true == use_alpha)
	{
		areas.alpha = true;
	}

	//Return the aberdeenshire areas layer
	return areas;
	
}


function create_community_layer(map_server, max_bounds)
{
	//Declare local varaibles 
	var selected_layers = 'swrefVcollectionVdatasetZgisVcollectionZpost_office';
	selected_layers += ',swrefVcollectionVdatasetZgisVcollectionZcom_facility';
	selected_layers += ',swrefVcollectionVdatasetZgisVcollectionZcommunity_centre';
	selected_layers += ',swrefVcollectionVdatasetZgisVcollectionZpark';
	selected_layers += ',swrefVcollectionVdatasetZgisVcollectionZpolice_station';
	
	//Set up the Electoral Wards layer
	var community_layer = new OpenLayers.Layer.WMS(
		"Community",
		map_server,
  		{
			layers : selected_layers,
			version : "1.1.0",
			exceptions : "application/vnd.ogc.se_xml",
			transparent : true,
			format : graphic_format
			
       	},
		{
			gutter : 15,			
			buffer : buffer_size,						
			visibility : false,			
			isBaseLayer : false,		
			maxExtent : max_bounds			
		}

    );

	//If the current browser is IE 5.X or 6.x then set alpha transparency on
	if(true == use_alpha)
	{
		community_layer.alpha = true;
	}

	//Return the community layer
	return community_layer;
	
}

function create_offices_layer(map_server, max_bounds)
{
	//Declare local varaibles 
	var selected_layers = 'swrefVcollectionVdatasetZgisVcollectionZcouncil_office';
	
	//Set up the Council Offices layer
	var offices_layer = new OpenLayers.Layer.WMS(
		"Council Offices",
		map_server,
  		{
			layers : selected_layers,
			version : "1.1.0",
			exceptions : "application/vnd.ogc.se_xml",
			transparent : true,
			format : graphic_format
			
       	},
		{
			gutter : 15,			
			buffer : buffer_size,						
			visibility : false,			
			isBaseLayer : false,		
			maxExtent : max_bounds			
		}

    );

	//If the current browser is IE 5.X or 6.x then set alpha transparency on
	if(true == use_alpha)
	{
		offices_layer.alpha = true;
	}

	//Return the council offices layer
	return offices_layer;
	
}

function create_democracy_layer(map_server, max_bounds)
{
	//Declare local varaibles 
	var selected_layers = 'swrefVcollectionVdatasetZgisVcollectionZpolling_district';

	//Set up the Democracy Layers
	var democracy_layer = new OpenLayers.Layer.WMS(
		"Democracy",
		map_server,
  		{
			layers : selected_layers,
			version : "1.1.0",
			exceptions : "application/vnd.ogc.se_xml",
			transparent : true,
			format : graphic_format
			
       	},
		{
			gutter : 15,			
			buffer : buffer_size,						
			visibility : false,			
			isBaseLayer : false,		
			maxExtent : max_bounds			
		}

    );

	//If the current browser is IE 5.X or 6.x then set alpha transparency on
	if(true == use_alpha)
	{
		democracy_layer.alpha = true;
	}

	//Return the democracy layer
	return democracy_layer;
	
}

function create_education_layer(map_server, max_bounds)
{
	//Declare local varaibles 
	var selected_layers = 'swrefVcollectionVdatasetZgisVcollectionZprim_school';
	selected_layers += ',swrefVcollectionVdatasetZgisVcollectionZsecon_school';
	
	//Set up the Education Wards layer
	var education_layer = new OpenLayers.Layer.WMS(
		"Education",
		map_server,
  		{
			layers : selected_layers,
			version : "1.1.0",
			exceptions : "application/vnd.ogc.se_xml",
			transparent : true,
			format : graphic_format
			
       	},
		{
			gutter : 15,			
			buffer : buffer_size,						
			visibility : false,			
			isBaseLayer : false,		
			maxExtent : max_bounds			
		}

    );

	//If the current browser is IE 5.X or 6.x then set alpha transparency on
	if(true == use_alpha)
	{
		education_layer.alpha = true;
	}

	//Return the education layer
	return education_layer;
	
}

function create_environment_layer(map_server, max_bounds)
{
	//Declare local varaibles 
	var selected_layers = 'swrefVcollectionVdatasetZgisVcollectionZnational_park';
	selected_layers += ',swrefVcollectionVdatasetZgisVcollectionZconservation_area';
	selected_layers += ',swrefVcollectionVdatasetZgisVcollectionZsite_special_scientific_interest';
	

	//Set up the Environment layer
	var environment_layer = new OpenLayers.Layer.WMS(
		"Environment",
		map_server,
  		{
			layers : selected_layers,
			version : "1.1.0",
			exceptions : "application/vnd.ogc.se_xml",
			transparent : true,
			format : graphic_format
			
       	},
		{
			gutter : 15,			
			buffer : buffer_size,						
			visibility : false,			
			isBaseLayer : false,		
			maxExtent : max_bounds			
		}

    );

	//If the current browser is IE 5.X or 6.x then set alpha transparency on
	if(true == use_alpha)
	{
		environment_layer.alpha = true;
	}

	//Return the environment layer
	return environment_layer;
	
}

function create_health_layer(map_server, max_bounds)
{
	//Declare local varaibles 
	var selected_layers = 'swrefVcollectionVdatasetZgisVcollectionZmedical_practice';
	selected_layers += ',swrefVcollectionVdatasetZgisVcollectionZdental_practice';
	selected_layers += ',swrefVcollectionVdatasetZgisVcollectionZhospital';
	selected_layers += ',swrefVcollectionVdatasetZgisVcollectionZpharmacy';
	selected_layers += ',swrefVcollectionVdatasetZgisVcollectionZoptician';
	
	//Set up the Health layer
	var health_layer = new OpenLayers.Layer.WMS(
		"Health",
		map_server,
  		{
			layers : selected_layers,
			version : "1.1.0",
			exceptions : "application/vnd.ogc.se_xml",
			transparent : true,
			format : graphic_format
			
       	},
		{
			gutter : 15,			
			buffer : buffer_size,						
			visibility : false,			
			isBaseLayer : false,		
			maxExtent : max_bounds			
		}

    );

	//If the current browser is IE 5.X or 6.x then set alpha transparency on
	if(true == use_alpha)
	{
		health_layer.alpha = true;
	}

	//Return the Health layer
	return health_layer;
	
}


function create_recreation_layer(map_server, max_bounds)
{
	//Declare local varaibles 
	var selected_layers = 'swrefVcollectionVdatasetZgisVcollectionZmuseum';
	selected_layers += ',swrefVcollectionVdatasetZgisVcollectionZgardens_and_designed_landscapes';
	selected_layers += ',swrefVcollectionVdatasetZgisVcollectionZcaravan_park';
	selected_layers += ',swrefVcollectionVdatasetZgisVcollectionZleisure_centre';
	selected_layers += ',swrefVcollectionVdatasetZgisVcollectionZswimming_pool';
	
	
	//Set up the Recreation layer
	var recreation_layer = new OpenLayers.Layer.WMS(
		"Recreation",
		map_server,
  		{
			layers : selected_layers,
			version : "1.1.0",
			exceptions : "application/vnd.ogc.se_xml",
			transparent : true,
			format : graphic_format
			
       	},
		{
			gutter : 15,			
			buffer : buffer_size,						
			visibility : false,			
			isBaseLayer : false,		
			maxExtent : max_bounds			
		}

    );

	//If the current browser is IE 5.X or 6.x then set alpha transparency on
	if(true == use_alpha)
	{
		recreation_layer.alpha = true;
	}

	//Return the Recreation layer
	return recreation_layer;
	
}

function create_waste_layer(map_server, max_bounds)
{
	//Declare local varaibles 
	var selected_layers = 'swrefVcollectionVdatasetZgisVcollectionZrecycling_location';
	
	//Set up the Waste layer
	var waste_layer = new OpenLayers.Layer.WMS(
		"Waste",
		map_server,
  		{
			layers : selected_layers,
			version : "1.1.0",
			exceptions : "application/vnd.ogc.se_xml",
			transparent : true,
			format : graphic_format
			
       	},
		{
			gutter : 15,			
			buffer : buffer_size,						
			visibility : false,			
			isBaseLayer : false,		
			maxExtent : max_bounds			
		}

    );

	//If the current browser is IE 5.X or 6.x then set alpha transparency on
	if(true == use_alpha)
	{
		waste_layer.alpha = true;
	}

	//Return the Waste layer
	return waste_layer;
	
}


function create_wards_layer(map_server, max_bounds)
{
	//Set up the Electoral Wards layer
	var wards = new OpenLayers.Layer.WMS(
		"Electoral Wards",
		map_server,
  		{
			layers : 'swrefVcollectionVdatasetZgisVcollectionZelectoral_ward',
			version : "1.1.0",
			exceptions : "application/vnd.ogc.se_xml",
			transparent : true,
			format : graphic_format
			
       	},
		{
			gutter : 15,			
			buffer : buffer_size,						
			visibility : false,			
			isBaseLayer : false,		
			maxExtent : max_bounds			
		}

    );

	//If the current browser is IE 5.X or 6.x then set alpha transparency on
	if(true == use_alpha)
	{
		wards.alpha = true;
	}

	//Return the electoral wards layer
	return wards;
	
}

function create_medical_practices_layer(map_server, max_bounds)
{
	//Set up the Medical Practices layer	
	var practices = new OpenLayers.Layer.WMS(
		"Medical Practices",
		map_server,
   		{
			layers : 'swrefVcollectionVdatasetZgisVcollectionZmedical_practice',
			version : "1.1.0",
			exceptions : "application/vnd.ogc.se_xml",
			transparent : true,
			format : graphic_format				
        },
    	{
			scales : [25000.0, 10000.0, 5000.0, 2000.0, 1000.0],
			gutter : 15,			
			buffer : buffer_size,						
			visibility : false,				
			isBaseLayer : false,		
			maxExtent : max_bounds			
    	}
    );

	//If the current browser is IE 5.X or 6.x then set alpha transparency on
	if(true == use_alpha)
	{
		practices.alpha = true;
	}

	//Return the medical practices layer
	return practices;
	
}

function create_aerial_photos_layer(map_server, max_bounds)
{
	
	//Set up the aerial photos layer
	var aerial = new OpenLayers.Layer.WMS(
		"Aerial Photos",
		map_server,
   		{
			//layers : 'swrefVcollectionVdatasetZaerialVcollectionZmb_mastermap_ortho_image',
			layers : 'swrefVcollectionVdatasetZaerial_ecwVcollectionZA$0020tiled$0020data$0020product$002e',
			version : "1.1.0",
			exceptions : "application/vnd.ogc.se_xml",
			transparent : true,
			format : graphic_format							
       	},
    	{
			//scales : [25000.0, 10000.0, 5000.0, 2000.0, 1000.0],			
			scales : [25000.0, 10000.0, 5000.0, 2000.0, 1000.0],			
			visibility : false,		
			opacity : 0.5,		
			isBaseLayer : false,				
			gutter : 15,
			buffer : buffer_size,						
			maxExtent : max_bounds			
    	}
   	);

	//No need to use transparency work-around for aerial photos
	
	//Return the aerial photos layer
	return aerial;
	
}

function create_plan_apps_layer(map_server, max_bounds)
{
	//Set up the Planning applications layer	
	var plan_apps = new OpenLayers.Layer.WMS(
		"Planning Applications",
		map_server,
   		{
			layers : 'swrefVcollectionVdatasetZplan_appsVcollectionZplan_app_geom',
			version : "1.1.0",
			exceptions : "application/vnd.ogc.se_xml",
			transparent : true,
			format : graphic_format				
        },
    	{
			scales : [25000.0, 10000.0, 5000.0, 2000.0, 1000.0],
			gutter : 15,			
			buffer : buffer_size,						
			visibility : false,				
			isBaseLayer : false,		
			maxExtent : max_bounds			
    	}
    );

	//If the current browser is IE 5.X or 6.x then set alpha transparency on
	if(true == use_alpha)
	{
		plan_apps.alpha = true;
	}

	//Return the planning applications layer
	return plan_apps;
	
}

function create_housing_land_audit_live_layer(map_server, max_bounds)
{
	//Set up the Housing and Industrial Schedules layer	
	var hi_schedules = new OpenLayers.Layer.WMS(
		"Housing Land Audit Live",
		map_server,
   		{
			layers : 'swrefVcollectionVdatasetZhousing_land_audit_liveVcollectionZhousing_audit_live',
			version : "1.1.0",
			exceptions : "application/vnd.ogc.se_xml",
			transparent : true,
			format : graphic_format				
        },
    	{
			//scales : [25000.0, 10000.0, 5000.0, 2000.0, 1000.0],
			gutter : 15,			
			buffer : buffer_size,						
			visibility : false,				
			isBaseLayer : false,		
			maxExtent : max_bounds			
    	}
    );

	//If the current browser is IE 5.X or 6.x then set alpha transparency on
	if(true == use_alpha)
	{
		hi_schedules.alpha = true;
	}
	
	//Return the Housing and Industrial Schedules layer	
	return hi_schedules;
	
}

function create_confirm_lighting_layer(map_server, max_bounds)
{
	//Set up the Confirm Lighting layer	
	var lighting = new OpenLayers.Layer.WMS(
		"Confirm Lights",
		map_server,
   		{
			layers : 'swrefVcollectionVdatasetZgisVcollectionZconfirm_lighting_unit',
			version : "1.1.0",
			exceptions : "application/vnd.ogc.se_xml",
			transparent : true,
			format : graphic_format				
        },
    	{
			scales : [4000.0, 2500.0, 1000.0],
			gutter : 15,			
			buffer : buffer_size,						
			visibility : false,				
			isBaseLayer : false,		
			maxExtent : max_bounds			
    	}
    );

	//If the current browser is IE 5.X or 6.x then set alpha transparency on
	if(true == use_alpha)
	{
		lighting.alpha = true;
	}

	//Return the confirm lighting layer
	return lighting;
	
}

function create_road_unit_layer(map_server, max_bounds)
{
	//Set up the Road Units layer	
	var roads = new OpenLayers.Layer.WMS(
		"Roads",
		map_server,
   		{
			layers : 'swrefVcollectionVdatasetZgisVcollectionZroad_unit',
			version : "1.1.0",
			exceptions : "application/vnd.ogc.se_xml",
			transparent : true,
			format : graphic_format				
        },
    	{
			scales : [4000.0, 2500.0, 1000.0],
			gutter : 15,			
			buffer : buffer_size,						
			visibility : false,				
			isBaseLayer : false,		
			maxExtent : max_bounds			
    	}
    );

	//If the current browser is IE 5.X or 6.x then set alpha transparency on
	if(true == use_alpha)
	{
		roads.alpha = true;
	}

	//Return the roads layer
	return roads;
	
}

function get_cycle_paths_layers(map_server, max_bounds)
{
	var layers = new Array();
	var requested_layers; 
	var loaded_layer_counter; 
	var requested_layer_counter; 
	var loaded_layer_name;
	var requested_layer_name;
	var result;

	//Check to see if alpha transparency is required 
	//use_alpha = useAlphaTransparency(); 

	//Set up the standard layers
	layers.push(create_os_base_layer(map_server, max_bounds));
	layers.push(create_aerial_photos_base_layer(map_server, max_bounds));
	layers.push(create_aerial_photos_layer(map_server, max_bounds));

	//Set up the cycle path layers
	//layers.push(create_cycle_paths_layer(map_server, max_bounds));
	
	//Get the requested overlay layers
	requested_layers = get_requested_layers(); 
		
	//Loop through the standard layers to see 
	//which ones should be switched on 
	for(loaded_layer_counter in layers)
	{
		
		loaded_layer_name = unescape(layers[loaded_layer_counter].name);

		for(requested_layer_counter in requested_layers)
		{

			requested_layer_name = unescape(requested_layers[requested_layer_counter]);
			
			if(requested_layer_name == loaded_layer_name)
			{
				//switch on the layer 
				layers[loaded_layer_counter].setVisibility(true);
			}
		}

	}
	
	//Return the standard layers
	return layers;
}


function create_cycle_paths_layer(map_server, max_bounds)
{
	//Set up the Road Units layer	
	var cycle_paths = new OpenLayers.Layer.WMS(
		"Cycle Paths",
		map_server,
   		{
			layers : 'swrefVcollectionVdatasetZgisVcollectionZspr_route, swrefVcollectionVdatasetZgisVcollectionZspr_section',
			version : "1.1.0",
			exceptions : "application/vnd.ogc.se_xml",
			transparent : true,
			format : graphic_format				
        },
    	{
			//scales : [4000.0, 2500.0, 1000.0],
			gutter : 15,			
			buffer : buffer_size,						
			visibility : false,				
			isBaseLayer : false,		
			maxExtent : max_bounds			
    	}
    );

	//If the current browser is IE 5.X or 6.x then set alpha transparency on
	if(true == use_alpha)
	{
		cycle_paths.alpha = true;
	}

	//Return the cycle_paths layer
	return cycle_paths;
	
}

function create_housing_land_audit_draft_layer(map_server, max_bounds)
{
	//Set up the housing land audit layer	
	var housing_audit = new OpenLayers.Layer.WMS(
		"Draft Housing Land Audit",
		map_server,
   		{
			layers : 'swrefVcollectionVdatasetZhousing_land_audit_draftVcollectionZhousing_audit_draft',
			version : "1.1.0",
			exceptions : "application/vnd.ogc.se_xml",
			transparent : true,
			format : graphic_format				
        },
    	{
			//scales : [25000.0, 10000.0, 5000.0, 2000.0, 1000.0],
			gutter : 15,			
			buffer : buffer_size,						
			visibility : false,				
			isBaseLayer : false,		
			maxExtent : max_bounds			
    	}
    );

	//If the current browser is IE 5.X or 6.x then set alpha transparency on
	if(true == use_alpha)
	{
		housing_audit.alpha = true;
	}

	//Return the housing land audit layer
	return housing_audit;
	
}

function create_emp_land_audit_2008_layer(map_server, max_bounds)
{
	//Set up the employment land audit layer	
	var employment_audit = new OpenLayers.Layer.WMS(
		"Employment Land Audit 2008",
		map_server,
   		{
			layers : 'swrefVcollectionVdatasetZemp_land_audit_2008VcollectionZemp_audit_2008',
			version : "1.1.0",
			exceptions : "application/vnd.ogc.se_xml",
			transparent : true,
			format : graphic_format				
        },
    	{
			//scales : [25000.0, 10000.0, 5000.0, 2000.0, 1000.0],
			gutter : 15,			
			buffer : buffer_size,						
			visibility : false,				
			isBaseLayer : false,		
			maxExtent : max_bounds			
    	}
    );

	//If the current browser is IE 5.X or 6.x then set alpha transparency on
	if(true == use_alpha)
	{
		employment_audit.alpha = true;
	}

	//Return the employment land audit layer
	return employment_audit;
	
}

function create_housing_market_areas_layer(map_server, max_bounds)
{
	//Set up the hosuing market area layer	
	var housing_market = new OpenLayers.Layer.WMS(
		"Housing Markets",
		map_server,
   		{
			layers : 'swrefVcollectionVdatasetZhousing_market_areasVcollectionZhousing_market_areas',
			version : "1.1.0",
			exceptions : "application/vnd.ogc.se_xml",
			transparent : true,
			format : graphic_format				
        },
    	{
			//scales : [25000.0, 10000.0, 5000.0, 2000.0, 1000.0],
			gutter : 15,
			buffer : buffer_size,						
			visibility : false,				
			isBaseLayer : false,		
			maxExtent : max_bounds			
    	}
    );

	//If the current browser is IE 5.X or 6.x then set alpha transparency on
	if(true == use_alpha)
	{
		housing_market.alpha = true;
	}

	//Return the housing market layer
	return housing_market;
	
}

function create_alp_settlement_layer(map_server, max_bounds)
{
	//Set up the alp settlement layer	
	var alp_settlement = new OpenLayers.Layer.WMS(
		"ALP Settlements",
		map_server,
   		{
			layers : 'swrefVcollectionVdatasetZalp_settlementVcollectionZalp_settlement_geom',
			version : "1.1.0",
			exceptions : "application/vnd.ogc.se_xml",
			transparent : true,
			format : graphic_format				
        },
    	{
			//scales : [25000.0, 10000.0, 5000.0, 2000.0, 1000.0],
			gutter : 15,			
			buffer : buffer_size,						
			visibility : false,				
			isBaseLayer : false,		
			maxExtent : max_bounds			
    	}
    );

	//If the current browser is IE 5.X or 6.x then set alpha transparency on
	if(true == use_alpha)
	{
		alp_settlement.alpha = true;
	}

	//Return the alp settlement layer
	return alp_settlement;
	
}

function create_lookup_results_layer(results_xml)
{

	//Set up the lookup results layer	
	var results = new OpenLayers.Layer.LookupLayer(
		"Results",
		results_xml, 
   		{
       	},
    	{
			visibility : true
    	}
    );

	
	//Return the lookup results layers
	return results;
	
}

function create_results_base_layer(map_server, max_bounds)
{
	//Set up the Ordnance survey base layer 
	var base_layer = new OpenLayers.Layer.WMS(
		"Results Base Layer",
		map_server,
   		{
			version : "1.1.0",
			exceptions : "application/vnd.ogc.se_xml",
			transparent : true,
			format : graphic_format
        },
    	{
			gutter : 15,
			buffer : buffer_size,			
			visibility : true,
			isBaseLayer : false,				
			displayInLayerSwitcher : false,						
			maxExtent : max_bounds			
    	}
		
    );

	//If the current browser is IE 5.X or 6.x then set alpha transparency on
	if(true == use_alpha)
	{
		base_layer.alpha = true;
	}
	
	//Return the OS base layer
	return base_layer;	
}

function replaceSubstring(inputString, fromString, toString) {
   // Goes through the inputString and replaces every occurrence of fromString with toString
   var temp = inputString;
   
   if(inputString)
   {
	   if (fromString == "") {
		  return inputString;
	   }
	   if (toString.indexOf(fromString) == -1) { // If the string being replaced is not a part of the replacement string (normal situation)
		  while (temp.indexOf(fromString) != -1) {
			 var toTheLeft = temp.substring(0, temp.indexOf(fromString));
			 var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
			 temp = toTheLeft + toString + toTheRight;
		  }
	   } else { // String being replaced is part of replacement string (like "+" being replaced with "++") - prevent an infinite loop
		  var midStrings = new Array("~", "`", "_", "^", "#");
		  var midStringLen = 1;
		  var midString = "";
		  // Find a string that doesn't exist in the inputString to be used
		  // as an "inbetween" string
		  while (midString == "") {
			 for (var i=0; i < midStrings.length; i++) {
				var tempMidString = "";
				for (var j=0; j < midStringLen; j++) { tempMidString += midStrings[i]; }
				if (fromString.indexOf(tempMidString) == -1) {
				   midString = tempMidString;
				   i = midStrings.length + 1;
				}
			 }
		  } // Keep on going until we build an "inbetween" string that doesn't exist
		  // Now go through and do two replaces - first, replace the "fromString" with the "inbetween" string
		  while (temp.indexOf(fromString) != -1) {
			 var toTheLeft = temp.substring(0, temp.indexOf(fromString));
			 var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
			 temp = toTheLeft + midString + toTheRight;
		  }
		  // Next, replace the "inbetween" string with the "toString"
		  while (temp.indexOf(midString) != -1) {
			 var toTheLeft = temp.substring(0, temp.indexOf(midString));
			 var toTheRight = temp.substring(temp.indexOf(midString)+midString.length, temp.length);
			 temp = toTheLeft + toString + toTheRight;
		  }
	   } // Ends the check to see if the string being replaced is part of the replacement string or not
	   
	   
   }
   
   return temp; // Send the updated string back to the user
} // Ends the "replaceSubstring" function

