﻿
function NetworkMap(map,minZoomLevel,maxZoomLevel,locked){this._vemap=new VEMap(map);this._mapjq=$("#"+map);this._minZoomLevel=minZoomLevel;this._maxZoomLevel=maxZoomLevel;this._locked=locked;this._data=null;this._shapeLayer=null;this._inClickEvent=false;}
NetworkMap.prototype={init:function(){this._vemap.onLoadMap=createRef(this,this._changeBGColour);var opt=new VEMapOptions();opt.LoadBaseTiles=false;this._vemap.LoadMap(new VELatLong(0,0),this._minZoomLevel,VEMapStyle.Road,this._locked,VEMapMode.Mode2D,false,0,opt);this._vemap.HideDashboard();var tileSourceSpec=new VETileSourceSpecification("tiles","tiles/%4.png");tileSourceSpec.NumServers=1;tileSourceSpec.MaxZoomLevel=8;tileSourceSpec.ZIndex=100;this._vemap.AddTileLayer(tileSourceSpec,true);if(!this._locked){this._vemap.AttachEvent("onclick",createRef(this,this._onClick));}
this._vemap.AttachEvent("onmouseover",createRef(this,this._onMouseOver));this._vemap.AttachEvent("onendzoom",createRef(this,this._onEndZoom));this._vemap.AttachEvent("onstartzoom",createRef(this,this._changeBGColour));this._vemap.AttachEvent("onendzoom",createRef(this,this._changeBGColour));this._vemap.AttachEvent("onstartpan",createRef(this,this._changeBGColour));this._vemap.AttachEvent("onendpan",createRef(this,this._changeBGColour));this._vemap.AttachEvent("onchangeview",createRef(this,this._changeBGColour));this._vemap.AttachEvent("onmousewheel",createRef(this,this._changeBGColour));this._vemap.AttachEvent("onkeypress",createRef(this,this._changeBGColour));this._mapjq.watch("backgroundColor",createRef(this,this._changeBGColour),100,"_mapBGColour");$.ajax({url:"stationdata.ashx",dataType:"json",success:createRef(this,this._dataLoaded)});},_changeBGColour:function(e){if(this._mapjq.css("backgroundColor")!="#FFFFFF")
this._mapjq.css("backgroundColor","#FFFFFF");},_pageLoad:function(hash){if(!this._inClickEvent&&hash){this._setPoint(hash,true);}},_dataLoaded:function(data){this._data=data;this._shapeLayer=new VEShapeLayer();for(var index in data.Items){var ll=new VELatLong(data.Items[index].nm_lat,data.Items[index].nm_lon);var circle=new VEShape(VEShapeType.Pushpin,ll);circle.data=data.Items[index];circle.selected=false;circle.SetTitle(data.Items[index].nearest_station_name);var description;if(this.OnCreatingPopup!=null){description=this.OnCreatingPopup(data.Items[index]);}
else{description="<div class=popuplinks>"+"<a href=\""+data.Items[index].station_information_url+"\">Station Information</a><br/>"+"<a href=\""+data.Items[index].station_map_download_url+"\">Download Station Map</a><br/>"+"<a href=\""+data.Items[index].station_map_view_url+"\">View this station on a road map</a><br/><br/>"+"<span style='color:black'>"+data.Items[index].street+", "+data.Items[index].pc+"</span>"+"</div>";}
circle.SetDescription(description);var circleIconHTML=this._getIconMarkup(this._vemap.GetZoomLevel(),"Clear");circle.SetCustomIcon(circleIconHTML);this._shapeLayer.AddShape(circle);data.Items[index].Shape=circle;}
this._vemap.AddShapeLayer(this._shapeLayer);this._shapeLayer.Show();if(this.OnDataLoad!=null)
this.OnDataLoad(data);$.history.init(createRef(this,this._pageLoad));var crsCode=querySt("stn");if(crsCode!=null){this.SelectPoint(crsCode);}
else{var hash=window.location.hash.substring(1);this.SelectPoint(hash);}},OnDataLoad:null,OnPointSelected:null,OnCreatingPopup:null,SelectPoint:function(id){$.history.load(id);},_setPoint:function(id,doCallback){var data=this._data;for(var index in data.Items){if(data.Items[index].nearest_station_crs==id){this._selectShape(data.Items[index].Shape,true,true,doCallback);}}},GetMap:function(){return this._vemap;},_onClick:function(e){if(e.elementID){this._inClickEvent=true;var clickedShape=this._vemap.GetShapeByID(e.elementID);$.history.load(clickedShape.data.nearest_station_crs);this._selectShape(clickedShape,false,false,true,true);this._inClickEvent=false;}},_selectShape:function(selectedShape,setCenter,showInfo,doCallback){if(selectedShape){var wh=2*Math.pow(2,this._vemap.GetZoomLevel());for(var shapeIdx=0;shapeIdx<this._shapeLayer.GetShapeCount();++shapeIdx){var shape=this._shapeLayer.GetShapeByIndex(shapeIdx);if(shape.GetID()==selectedShape.GetID()){selectedShape.SetCustomIcon(this._getIconMarkup(this._vemap.GetZoomLevel(),"Icon"));selectedShape.selected=true;if(doCallback&&this.OnPointSelected!=null){this.OnPointSelected(selectedShape.data);}
if(showInfo){setTimeout(createRef(this,function(){try{this._vemap.ShowInfoBox(selectedShape,selectedShape.GetPoints()[0]);}catch(e){}}),1000);}
if(setCenter){this._vemap.SetCenterAndZoom(selectedShape.GetPoints()[0],4);}}
else{shape.SetCustomIcon(this._getIconMarkup(this._vemap.GetZoomLevel(),"Clear"));this._vemap.HideInfoBox();shape.selected=false;}}}},_onMouseOver:function(e){if(e.elementID){var shape=this._vemap.GetShapeByID(e.elementID);if(shape){return shape.selected!==true;}}},_onEndZoom:function(e){if(e.zoomLevel<this._minZoomLevel){this._vemap.SetZoomLevel(this._minZoomLevel);return true;}
else if(e.zoomLevel>this._maxZoomLevel){this._vemap.SetZoomLevel(this._maxZoomLevel);return true;}
for(var shapeIdx=0;shapeIdx<this._shapeLayer.GetShapeCount();++shapeIdx){var shape=this._shapeLayer.GetShapeByIndex(shapeIdx);var wh=2*Math.pow(2,this._vemap.GetZoomLevel());if(shape.selected){shape.SetCustomIcon(this._getIconMarkup(this._vemap.GetZoomLevel(),"Icon"));shape.Show();}
else{shape.SetCustomIcon(this._getIconMarkup(this._vemap.GetZoomLevel(),"Clear"));shape.Show();}}},_getIconMarkup:function(zl,image){var wh=2*Math.pow(2,zl);var tl=12-(wh/2);var iconMarkup="<div style=\"position:relative;top:"+tl+"px;left:"+tl+"px;\">"+"<span style=\"display:inline-block;filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='Images/"+image+"_"+wh+".png');\">"+"<img style=\"filter:progid:DXImageTransform.Microsoft.Alpha(opacity=0);\" src=\"Images/"+image+"_"+wh+".png\" border=\"0\" alt=\"\">"+"</span>"+"</div>";return iconMarkup;}};
