
function PointRenderer(){this.name="PointRenderer";var theObject=this;this._imagePath;this._width=20;this._height=20;this._infoboxName="";this._stationLayer=new Microsoft.Maps.EntityCollection();this._layerName="";this._categoryName="";this._oDataEntityName="";this._tileLayerLayer=null;this._tileLayer=null;this._ratingTargetId=-1;this._centered="false";this.setImagePath=function(value){this._imagePath=value;}
this.getImagePath=function(){return this._imagePath;}
this.setWidth=function(value){this._width=parseInt(value);}
this.getWidth=function(){return this._width;}
this.setHeight=function(value){this._height=parseInt(value);}
this.getHeight=function(){return this._height;}
this.setCentered=function(value){this._centered=value;}
this.getCentered=function(){return this._centered;}
this.setInfoboxName=function(value){this._infoboxName=value;}
this.getInfoboxName=function(){return this._infoboxName;}
this.setLayerName=function(value){this._layerName=value;}
this.getLayerName=function(){return this._layerName;}
this.setCategoryName=function(value){this._categoryName=value;}
this.getCategoryName=function(){return this._categoryName;}
this.setODataEntityName=function(value){this._oDataEntityName=value;}
this.getODataEntityName=function(){return this._oDataEntityName;}
this.setTileLayerLayer=function(value){this._tileLayerLayer=value;}
this.getTileLayerLayer=function(){return this._tileLayerLayer;}
this.setTileLayer=function(value){this._tileLayer=value;}
this.getTileLayer=function(){return this._tileLayer;}
this.setRatingTargetId=function(value){this._ratingTargetId=parseInt(value);}
this.getRatingTargetId=function(){return this._ratingTargetId;}
this.Render=function(oDataUri){var bounds=map.getBounds();var lat1=bounds.getSoutheast().latitude;var lat2=bounds.getNorthwest().latitude;var minLatitude=0;var maxLatitude=0;if(lat1>lat2){minLatitude=lat2;maxLatitude=lat1;}
else{minLatitude=lat1;maxLatitude=lat2;}
var long1=bounds.getSoutheast().longitude;var long2=bounds.getNorthwest().longitude;var minLongitude=0;var maxLongitude=0;if(long1>long2){minLongitude=long2;maxLongitude=long1;}
else{minLongitude=long1;maxLongitude=long2;}
var request=oDataUri+this.getODataEntityName()+"?$filter="+escape("Latitude gt "+minLatitude+" and Latitude lt "+maxLatitude+" and Longitude gt "+minLongitude+" and Longitude lt "+maxLongitude);if(this.getRatingTargetId()!=-1){request+=escape(" and RatingTargetId eq "+this.getRatingTargetId());}
OData.read(request,theObject.GetStationsSuccessHandler,theObject.GetStationsErrorHandler);}
this.Clear=function(){this._stationLayer.clear();if(map.entities.indexOf(this._stationLayer)!=-1){map.entities.remove(this._stationLayer);}}
this.GetStationsSuccessHandler=function(data,request){if(theObject.getTileLayerLayer()!=null&&theObject.getTileLayer()!=null){if(theObject.getTileLayerLayer().indexOf(theObject.getTileLayer())!=-1){theObject.getTileLayerLayer().remove(theObject.getTileLayer());}}
theObject.Clear();for(var i=0;i<data.results.length;i++){theObject.AddStationToStationLayer(data.results[i]);}
theObject.CleanUpStationLayer();if(map.entities.indexOf(theObject._stationLayer)==-1){map.entities.push(theObject._stationLayer);}}
this.GetStationsErrorHandler=function(error){var test=error;}
this.AddStationToStationLayer=function(station){var contained=false;var containedIndex=-1;var typename="";var infoboxSuffix="Infobox";var infoboxTypeName=theObject.getInfoboxName().substring(0,theObject.getInfoboxName().length-infoboxSuffix.length);if(station.StationId!=undefined){typename=""+station.StationId+","+infoboxTypeName;}
else if(station.NoiseId!=undefined){typename=""+station.NoiseId+","+infoboxTypeName;}
var pushpinOptions;if(!contained){if(this._imagePath!=null&&this._imagePath!=""){var filename=this._imagePath;if(station.QualityIndex!=null){filename=theObject.FillInQualityIndexInFilename(filename,station.QualityIndex);}
if(station.Rating!=null){filename=theObject.FillInQualityIndexInFilename(filename,station.Rating);}
if((this._centered==true)&&(this._width>1)&&(this._height>1)){pushpinOptions={typeName:typename,icon:filename,width:this._width,height:this._height,anchor:new Microsoft.Maps.Point(this._width/2,this._height/2)};}
else{pushpinOptions={typeName:typename,icon:filename,width:this._width,height:this._height,};}}
else{pushpinOptions={typeName:typename};}
var newStation=new Microsoft.Maps.Pushpin(new Microsoft.Maps.Location(station.Latitude,station.Longitude),pushpinOptions);Microsoft.Maps.Events.addHandler(newStation,"mouseover",theObject.MouseOverHandler);Microsoft.Maps.Events.addHandler(newStation,"mouseout",theObject.MouseOutHandler);Microsoft.Maps.Events.addHandler(newStation,"mousedown",theObject.MouseDownHandler);this._stationLayer.push(newStation);}}
this.MouseOverHandler=function(e){if(theObject.getRatingTargetId()!=-1){return;}
var filename=e.target.getIcon();var index=filename.indexOf("noEffect.png");if(index==-1){return;}
var newFilename=filename.substring(0,index);newFilename=newFilename.concat("hover.png");e.target.setOptions({icon:newFilename});}
this.MouseOutHandler=function(e){if(theObject.getRatingTargetId()!=-1){return;}
var filename=e.target.getIcon();var index=filename.indexOf("hover");if(index==-1){return;}
var newFilename=filename.substring(0,index);newFilename=newFilename.concat("noEffect.png");e.target.setOptions({icon:newFilename});}
this.MouseDownHandler=function(e){if((e.target!=clickedPushpin)&&(clickedPushpin!=null)){var oldTypeName=clickedPushpin.getTypeName().split(",");var newTypeName=e.target.getTypeName().split(",");if(oldTypeName[1]!=newTypeName[1]){hideInfobox(""+oldTypeName[1]+"Infobox");}}
hideDisabled=true;clickedPushpin=e.target;var location=e.target.getLocation();var typeName=e.target.getTypeName().split(",");var shareString=""+location.latitude+"|"+location.longitude+"|"+map.getZoom()+"|"+theObject.getCategoryName()+"|"+theObject.getLayerName();shareString=escape(shareString);$("#"+theObject.getInfoboxName()+"ShareString").text("http://"+currentHostname+"/?share="+shareString);$("#"+theObject.getInfoboxName()+"Latitude").text(""+location.latitude);$("#"+theObject.getInfoboxName()+"Longitude").text(""+location.longitude);$("#"+theObject.getInfoboxName()+"PushpinID").text(""+typeName[0]);theObject.ShowInfobox(e);}
this.ShowInfobox=function(e){if(theObject.getInfoboxName()==null||theObject.getInfoboxName()==""){return;}
if(e.targetType=="pushpin"){var offsetTop=0;var offsetLeft=20;var pixel=map.tryLocationToPixel(e.target.getLocation(),Microsoft.Maps.PixelReference.control);var infobox=document.getElementById(theObject.getInfoboxName());if(infobox==undefined||infobox==null){return;}
var mapdivWidth=parseInt(document.getElementById('myMap').style.width);var mapdivHeight=parseInt(document.getElementById('myMap').style.height);var infoboxWidth=$("#"+theObject.getInfoboxName()).width();var infoboxHeight=$("#"+theObject.getInfoboxName()).height();if((pixel.x+infoboxWidth)>mapdivWidth){infobox.style.left=(pixel.x-infoboxWidth)+"px";}
else{infobox.style.left=(pixel.x+offsetLeft)+"px";}
if((pixel.y+infoboxHeight)>mapdivHeight){infobox.style.top=(pixel.y-infoboxHeight)+"px";}
else{infobox.style.top=(pixel.y+offsetTop)+"px";}
var functionname=theObject.getInfoboxName()+"LoadData";var typeName=e.target.getTypeName().split(",");window[functionname](typeName[0]);infobox.style.visibility="visible";$('#'+theObject.getInfoboxName()).fadeIn('slow');}}
this.HideInfobox=function(e){if(theObject.getInfoboxName()==null||theObject.getInfoboxName()==""){return;}
$('#'+theObject.getInfoboxName()).fadeOut('slow');}
this.CleanUpStationLayer=function(){var viewBounds=map.getBounds();for(var i=0;i<this._stationLayer.getLength();i++){if(!viewBounds.contains(this._stationLayer.get(i).getLocation())){this._stationLayer.remove(this._stationLayer.get(i));}}}
this.MouseOverInfobox=function(e){if(theObject.getInfoboxName()==null||theObject.getInfoboxName()==""){return;}}
this.MouseOutInfobox=function(e){if(!hideDisabled){}}
this.RegisterInfoboxHandler=function(){if(theObject.getInfoboxName()==null||theObject.getInfoboxName()==""){return;}
var infobox=document.getElementById(theObject.getInfoboxName());infobox.onmouseover=theObject.MouseOverInfobox;infobox.onmouseout=theObject.MouseOutInfobox;}
this.RemoveInfoboxHandler=function(){var infobox=document.getElementById(theObject.getInfoboxName());infobox.onmouseover=null;infobox.onmouseout=null;}
this.FillInQualityIndexInFilename=function(filename,qualityIndex){var dotPosition=-1;var returnFilename=filename;for(var i=returnFilename.length;i>=0;i--){if(returnFilename.charAt(i)=='_'){dotPosition=i;break;}}
if(dotPosition!=-1){var name=filename.substring(0,dotPosition);var ending=filename.substring(dotPosition,filename.length);returnFilename=name+qualityIndex+ending;}
return returnFilename;}}
