/*
	Function: wales.Map
	Configure and write a map instance into a document. 
	
	
	Usage:
	> <script type="text/javascript" src="/wales/flash/remapping/js/map.js"></script>
	> <script type="text/javascript" src="/wales/flash/remapping/js/mediaselector.js"></script>
	> <script type="text/javascript">
	> // <![CDATA[
	> var map = new wales.Map();
	> map.setSize(430, 298 );
	> map.setConfig('/wales/nature/customobjects/map/config.xml');
	> map.setZoom(6.604);
	> map.setOSCentre(270000,207400);
	> map.setLowVersion('To see the interactive map on this page, you need Flash 8. We have a <a href="/webwise/askbruce/articles/download/howdoidownloadflashplayer_1.shtml">step by step guide on installing Flash here</a>. The <a href="nonflashindex.shtml">non flash version is available here</a>.');
	> map.embed();
	> // ]]>
	> </script>
	
*/

if(typeof wales == "undefined") var wales = new Object();
wales.Map = function(width, height){
	if (!document.getElementById) { return; }
	

	this.dataLayers = new Array();
	this.dataLayerColors = new Array();
	this.dataLayerIds = new Array();
	this.dataLayerIconsrc = new Array();
	this.dataLayerIcons = new Array();
	this.dataLayerZoomTo = new Array();
	this.dataLayerClustering = new Array();
	this.dataLayerTurnOff = new Array();
	
	this.attributes = new Array();
	
	var width = width ? width : 400;
	var height = height ? height: 400;
	this.centreSet = false;
	this.setSize(width,height);

	this.setSwf("map19.swf");	

}



wales.Map.prototype = {
	setAttribute: function(name, value){
		this.attributes[name] = value;
	},
	getAttribute: function(name){
		return this.attributes[name];
	},
	
	
/*
	Function: wales.Map.setSwf
	Override the default swf file used. 
*/
	setSwf: function(swfName){
		this.attributes['swf'] = swfName;
	},
/*
	Function: wales.Map.setMercator
	Use a different projection system.
	Temporary measure - would rather do this in the config than pick up a different swf.
*/
	setMercator: function(){
		this.attributes['swf'] = 'mercator_map1.swf'
	},
	
/*
	Function: wales.Map.setLowVersion
	Define the string written into the document if the version of Flash on the client system is too low.
*/
	setLowVersion: function(low){
		this.attributes['lowversion'] = low;
	},
	
/*
	Function: wales.Map.setZoom
	
	How zoomed in would you like the map to be? 1 is the whole of the National Grid.
	
	Parameters: 
	
	z - amount of zoom.
	
*/
	setZoom: function(z){
		this.attributes['zoom'] = z;
	},
/*
	Function: wales.Map.setZoomLayer
	
	What layer would you like to zoom in to?
	
	Parameters: 
	
	layer: string id of layer
	
*/
	setZoomLayer: function(id){
		this.attributes['zoomlayer'] = id;
	},	
	/*
		Function: wales.Map.setLatLng
		Parameters:

		lat - Mercator latitude
		lng - Mercator longitude

	*/	
	setLatLng: function(lat,lng){
		this.attributes['lat'] = lat;
		this.attributes['lng'] = lng;
	},
/*
	Function: wales.Map.setOSCentre
	
	Where would you like the map centred?
	
	Parameters:
	
	x - horizontal position on Ordnance Survey National Grid in metres
	y - vertical position in metres
	
*/
	setOSCentre: function(x,y){
		this.attributes['x'] = x;
		this.attributes['y'] = y;
		this.centreSet = true;
	},	
/* 
	Function: wales.Map.setLandrangerCentre
	
	Use landranger OS reference instead, e.g. SN123456
	
*/
	setLandrangerCentre: function(ref){
		this.attributes['landranger'] = ref;
		this.centreSet = true;
	},
	
/*
	Function: wales.Map.setBgcolor
	
	Parameters: 
	
	col - hexadecimal colour e.g. 'ffffff'

*/	
	setBgcolor: function(col){
		this.attributes['bgcolor'] = col;
	},

/*
	Function: wales.Map.setDataLayer
	
	Parameters:
	
	datalayer 	- path to xml data file defining points
	id 			- string providing unique id for layer. Default is 'data'+n.
	color 		- hexadecimal e.g. '0000FF'. Default is 'FF0000'
	
	Can be called multiple times to add an arbitrary quantity of layers.
	
*/
	setDataLayer: function(datalayer, id, color, iconsrc, icon, zoomto, clustering, turnoff){
		this.dataLayers.push(datalayer);
		this.dataLayerIds.push(id);
		if(color) {
			this.dataLayerColors.push(color);
		}else { 
			this.dataLayerColors.push("");
		}if(iconsrc) {
			this.dataLayerIconsrc.push(iconsrc);
		}else { 
			this.dataLayerIconsrc.push("");
		}
		if(icon) {
			this.dataLayerIcons.push(icon);
		}else { 
			this.dataLayerIcons.push("");
		}
		if(zoomto) {
			this.dataLayerZoomTo.push(zoomto);
		}else {
			this.dataLayerZoomTo.push("");
		}
		if(clustering) {
			this.dataLayerClustering.push(clustering);
		}else {
			this.dataLayerClustering.push("");
		}		
		if(turnoff) {
			this.dataLayerTurnOff.push(turnoff);
		}else {
			this.dataLayerTurnOff.push("");
		}		
	},
/*
	Function: wales.Map.setSize
	
	How big, in pixels, would you like your map?
	
	Parameters:
	width - integer width of map
	height - integer height of map
	
*/
	setSize: function(width, height){
		this.attributes['width'] = width;
		this.attributes['height'] = height;
	},
/*
	Function: wales.Map.setConfig
	
	Allocate an xml config file to the map.
	
	Parameters:
	
	config - string defining path.
	
*/
	setConfig: function(configxml){
		this.attributes['config'] = configxml;
	},
/*
	Function: wales,Map.isValidFlashVersion
	
	Detects if users flash version is OK to use
*/
	
	flashVersion: function(){
		var testFlashMovie = new bbcjs.plugins.FlashMovie("/wales/flash/remapping/" + this.getAttribute('swf'));
		return (testFlashMovie.detected);
	},
	
/*
	Function: setHighlight
	
	Determine which point to activate highlight on.
	
	Parameters:
	
	highlight - string id
*/
	setHighlight: function(id) {
	
		this.attributes['highlight'] = id;
	},
/*
	Function: setFlagged
	
	Determine which point to activate flag on.
	
	Parameters:
	
	flag - string id
*/
	setFlagged: function(id) {
	
		this.attributes['flagged'] = id;
	},	
	
/*
	Function: wales.Map.write
	
	Use swfobject to write an instance of the map into the page (not for use on live).

*/
	write: function(elementId){
		var so = new SWFObject("/wales/flash/remapping/" + this.getAttribute('swf'), "WalesMap", this.getAttribute('width'), this.getAttribute('height'), "8", "#CEEFF8");
		so.addVariable("htmlWidth", this.getAttribute('width')); 
		so.addVariable("htmlHeight", this.getAttribute('height'));
		if(this.centreSet){
			so.addVariable("xVal", this.getAttribute('x'));
			so.addVariable("yVal", this.getAttribute('y'));
		}
		if( this.getAttribute('datalayer1') ) {
			so.addVariable("datalayer1", this.getAttribute('datalayer1'));
		}
		
		so.write(elementId);
		return false;
	},
/*
	Function: wales.Map.embed
	
	Use IFL Flash embed script to write an instance of the map into the page.
*/	
	embed: function(){
	
		var dataString='';
		for(var i=1; i <= this.dataLayers.length; i++){
			dataString = dataString + '&datalayer'+i + "=" + escape(this.dataLayers[i-1]);
			dataString = dataString + '&datalayercolor'+i + '=' + this.dataLayerColors[i-1];
			dataString = dataString + '&datalayerid'+i + '=' + this.dataLayerIds[i-1];
			dataString = dataString + '&datalayericonsrc'+i + '=' + this.dataLayerIconsrc[i-1];
			dataString = dataString + '&datalayericon'+i + '=' + this.dataLayerIcons[i-1];
			dataString = dataString + '&datalayerzoomto'+i + '=' + this.dataLayerZoomTo[i-1];
			dataString = dataString + "&datalayerclustering"+i + '=' + 
			this.dataLayerClustering[i-1];
			dataString = dataString + "&datalayerturnoff"+i + '=' + 
			this.dataLayerTurnOff[i-1];			
			}
		var myFlashMovie = new bbcjs.plugins.FlashMovie("/wales/flash/remapping/" + this.getAttribute('swf'));
		myFlashMovie.version = 8;
		myFlashMovie.width = this.getAttribute('width');  
		myFlashMovie.height = this.getAttribute('height'); 
		myFlashMovie.loop = true;
		myFlashMovie.lowversion = this.getAttribute('lowversion') ? this.getAttribute('lowversion') : 'You need Flash 8 to see this map';
		myFlashMovie.flashvars = 'configxml=' + this.getAttribute('config') + '&htmlWidth=' + this.getAttribute('width') + '&htmlHeight=' + this.getAttribute('height') + '&xVal=' + this.getAttribute('x') + '&yVal=' + this.getAttribute('y') + '&lat=' + this.getAttribute('lat') + '&lng=' + this.getAttribute('lng') +'&landranger=' + this.getAttribute('landranger') + '&highlight=' + this.getAttribute('highlight') + '&flagged=' + this.getAttribute('flagged') +  '&zoom=' + this.getAttribute('zoom') + '&zoomLayer=' + this.getAttribute('zoomlayer') +  dataString;
		myFlashMovie.id = "WalesMap";
		myFlashMovie.quality = "high";

		myFlashMovie.bgcolor = this.getAttribute('bgcolor') ? this.getAttribute('bgcolor') : 'ffffff';
		myFlashMovie.noPluginMsg = myFlashMovie.lowversion;	
		myFlashMovie.embed();	
	
		return false;
	}
}
/* add Array.push if needed (ie5) */
if (Array.prototype.push == null) { Array.prototype.push = function(item) { this[this.length] = item; return this.length; }}


var Map = wales.Map;
