var g_realtime=null;$().ready(function(){$(".panel").fadeTo(0,0.85);$(".panel").draggable({opacity:0.6});$(".panel").each(function(){var contents=$(".contents",this);$(".show",this).click(function(){contents.slideToggle();$(this).toggleClass("expand");});});$(".panel").click(function(){return false;});var link_id_to_type={'toggle_users':'user','toggle_accidents':'ACCIDENT','toggle_policemen':'POLICEMAN','toggle_user_messages':'OTHER','toggle_traffic_jam':'JAM'};$(".panel .contents a").click(function(e){var type=link_id_to_type[this.id];toggleType(type,this.id);return false;});var view_map=new ViewMap();if(document.URL.indexOf('rt=true')==-1){$('#loginBox').hide();g_realtime.start();return;}
$("#toggle_users").show().addClass("checked");g_realtime.toggleDisplayFilter("user");g_realtime.georss_client.server_url="/rtserver/management/ExtendedGeoRSS";$('#user_id_field').focus();if(document.URL.indexOf('showcase=true')>-1){g_realtime.georss_client.showcase=true;g_realtime.georss_client._getExtent=function(){return this.map.getMaxExtent();};g_realtime.renderer.type_filter_map={};$("#alertsPanel").hide();}
if(document.URL.indexOf('sf=true')>-1)
g_realtime.georss_client.base_params["sf"]="true";g_realtime.start();if(document.URL.indexOf('showcase=true')>-1)
g_realtime.startShowcase();});var toggleType=function(type,toggle_link_id){g_realtime.toggleDisplayFilter(type);$("#"+toggle_link_id).toggleClass('checked');}
function ViewMap(){var objRef=this;OpenLayers.IMAGE_RELOAD_ATTEMPTS=3;this.map=new OpenLayers.Map('map',{controls:[new OpenLayers.Control.Navigation(),new OpenLayers.Control.PanZoomBar(),new OpenLayers.Control.Scale(),new OpenLayers.Control.Permalink(),new OpenLayers.Control.MousePosition(),new OpenLayers.Control.LayerSwitcher()],maxResolution:.010986328125,maxExtent:new OpenLayers.Bounds(33.75,28.125,36.5625,33.75),numZoomLevels:11});var layer=new OpenLayers.Layer.WMS("שכבת בסיס",["http://t5.maps.freemap.co.il/wms-c/","http://t6.maps.freemap.co.il/wms-c/","http://t7.maps.freemap.co.il/wms-c/","http://t8.maps.freemap.co.il/wms-c/"],{layers:'base',format:'image/png'},{transitionEffect:"resize",buffer:1});this.map.addLayer(layer);var wms=new OpenLayers.Layer.WMS("OpenLayers WMS","http://labs.metacarta.com/wms/vmap0",{layers:'basic'});var options={layers:[wms],minRatio:8,maxRatio:256};this.addSpeedCams();this.trafficLayer=new OpenLayers.Layer.TrafficLayer("traffic");this.map.addLayer(this.trafficLayer);g_realtime=new Realtime(this.map);this.trafficControl=new OpenLayers.Control.TrafficControl(this.trafficLayer);this.map.addControl(this.trafficControl);if(document.URL.indexOf('debug=true')>-1)
this.trafficControl.deactivate();else
this.trafficControl.activate();this.map.events.register("click",this.map,this.handleClick.bind(this));if(!this.map.getCenter())this.map.zoomToMaxExtent();this.searchCallback=function(message,extent,point){this.map.zoomToExtent(extent);popup=new OpenLayers.Popup.FramedCloud("result",point,null,'<p>'+message+'<br /></p>'+'<p> <a class="updateLink" href="#">מצאת טעות?</a></p>',null,true);popup.setOpacity(0.95);popup.autoSize=false;popup.setSize(new OpenLayers.Size(150,120));this.map.addPopup(popup,true);$("a.updateLink").click(this.updateReport.bind(this,point));}
this.address_search=new SearchAddress(function(){objRef.searchCallback.apply(objRef,arguments);});this.address_search.setFocus();this.updateReport=function(lonlat){$('#update_form').html("<p>אנא המתן...</p>");$('#updatePopup').jqm({overlay:0}).jqmShow();if(window.UpdateForm==undefined){$.getScript("/js/update_form.js",function(){new UpdateForm($('#update'),lonlat);});return false;}
new UpdateForm($('#update'),lonlat);return false;}
params=OpenLayers.Util.getParameters();var mistake=params.mistake?params.mistake:"false";if(mistake=="true"){oldHref=this.map.getControlsByClass("OpenLayers.Control.Permalink")[0].element.href;newHref=oldHref.replace(/mistake=true&{0,1}/,"");this.map.getControlsByClass("OpenLayers.Control.Permalink")[0].element.href=newHref;var popupWidth=200;var popupHeight=150;var pixel=this.map.getPixelFromLonLat(this.map.getCenter());pixel.x-=popupWidth;pixel.y-=popupHeight;var popupLocation=(this.map.getLonLatFromPixel(pixel));var message="אנא עדכן אותנו!"+"<br/>"+"לחץ על הנקודה במפה אותה יש לתקן ומלא את פרטי הבעיה.";mistakePopup=new OpenLayers.Popup("mistake",popupLocation,new OpenLayers.Size(popupWidth,popupHeight),"<div class='hebrew' style='font-family:arial'><h2>מצאת טעות?</h2><h3>"+message+"</h3></div>",true);mistakePopup.setBackgroundColor("#c9d7ec");mistakePopup.setOpacity(0.95);this.map.addPopup(mistakePopup,false);}}
ViewMap.prototype.handleClick=function(e){var lonlat=this.map.getLonLatFromViewPortPx(e.xy);if(e.ctrlKey){window.open("/rtserver/management/QueryStreet?lon="+lonlat.lon+"&lat="+lonlat.lat);return;}
var service='/wms/israel_wfs2?';var bounds,minXY,maxXY;var tolerance=5;this.queryPoint=this.map.getLonLatFromPixel(new OpenLayers.Pixel(e.xy.x,e.xy.y));minXY=this.map.getLonLatFromPixel(new OpenLayers.Pixel(e.xy.x-tolerance,e.xy.y+tolerance));maxXY=this.map.getLonLatFromPixel(new OpenLayers.Pixel(e.xy.x+tolerance,e.xy.y-tolerance));bounds=new OpenLayers.Bounds(minXY.lon,minXY.lat,maxXY.lon,maxXY.lat);var url=service+OpenLayers.Util.getParameterString({SERVICE:"WFS",VERSION:"1.0.0",REQUEST:"GetFeature",TYPENAME:'speed_cams,roads',MAXFEATURES:1,BBOX:bounds.toBBOX()});if(!this.queryInd){var img=new Image();img.src='/images/common/indicator2.gif';this.queryInd=$(img);this.queryInd.css("display","none");this.queryInd.css("position","absolute");this.queryInd.css("zIndex",5000);$(this.map.viewPortDiv).append(this.queryInd);}
this.queryInd.css("left",(e.xy.x-5)+'px');this.queryInd.css("top",(e.xy.y-5)+'px');this.queryInd.show('fast');OpenLayers.loadURL(url,null,this,this.lookupSuccess,this.lookupFailure);}
ViewMap.prototype.lookupSuccess=function(request){var doc=request.responseXML;if(!doc||request.fileType!="XML"){doc=request.responseText;}
var gml=new OpenLayers.Format.GML();var features=gml.read(doc);if(!this.vectorLayer){var style=new Object();style.strokeColor="#0000FF";style.strokeOpacity=0.8;style.strokeWidth=3;this.vectorLayer=new OpenLayers.Layer.Vector("סימון",{style:style});this.map.addLayer(this.vectorLayer);this.map.setLayerIndex(this.vectorLayer,this.map.layers.length-2);}else{this.vectorLayer.removeFeatures(this.selectedFeatures);}
this.selectedFeatures=features;var info='';if(features.length==0){info='לא נמצא רחוב בנקודה זו.';}
else{var feature=features[0];if(feature.fid.startsWith("roads.")){if(feature.attributes['street_name'])
info+=feature.attributes['street_name'];else info+='רחוב ללא שם';if(feature.attributes['city_name'])
info+=', '+feature.attributes['city_name']+'<br>';else info+='<br />';if(feature.attributes['road_speed'])
info+='מהירות ממוצעת: '+
Math.round(parseInt(feature.attributes['road_length'])/parseInt(feature.attributes['from_cross_time'])*3.6)+' קמ"ש<br />';if(feature.attributes['direction']==1)
info+='כיוון נסיעה: דו כיווני<br />';else info+='כיוון נסיעה: חד כיווני<br />';this.vectorLayer.addFeatures([feature]);}
else{if(feature.fid.startsWith("speed_cam")){if(feature.attributes['cam_type']){if(feature.attributes['cam_type']==2){info+='<img id="speed_cam" src="/img/alert_02.gif" alt=""><br />';info+='<b>מצלמת מהירות</b><br />'}
else
info+='<b>מצלמת דמה</b><br />'}
else info+='';if(feature.attributes['cam_street_name'])
info+=feature.attributes['cam_street_name'];if(feature.attributes['cam_city_name'])
info+=", "+feature.attributes['cam_city_name']+'<br>';else info+='<br />';if(feature.attributes['cam_speed'])
info+='מהירות מותרת: '+feature.attributes['cam_speed']+'<br>';else info+='<br />';info+="<strong>";info+="<br /> אחראי:  ";info+="</strong>";info+="<span class=\"text\">";info+='('+feature.attributes['user_rank']+') <a href="/forum/profile.php?mode=viewprofile&u='+feature.attributes['user_name']+'" target="_blank">';var name=feature.attributes['user_name'];if(name.substr(0,5)=='anon_')name='אנונימי';if(name.length>20)name=name.substr(1,20);info+=name+'</a>';info+="</span>";info+="<br />";}}}
popup=new OpenLayers.Popup.FramedCloud("query",this.queryPoint,null,'<p>'+info+'</p>'+'<p> <a class="updateLink" href="#">מצאת טעות?</a></p>',null,true);popup.panMapIfOutOfView=true;popup.isAlphaImage=true;this.map.addPopup(popup,true);popup.autoSize=false;var adjustedHeight=20;if(feature&&feature.fid.startsWith("speed_cam")){adjustedHeight=$("#query_contentDiv").height();}
popup.setSize(new OpenLayers.Size(150,adjustedHeight+100));$("a.updateLink").click(this.updateReport.bind(this,this.queryPoint));this.queryInd.hide('fast');}
ViewMap.prototype.lookupFailure=function(request){this.queryInd.hide('fast');}
ViewMap.prototype.addSpeedCams=function(){var speedCamLayer=new OpenLayers.Layer.WMS("מצלמות מהירות",["http://t5.maps.freemap.co.il/wms-c/","http://t6.maps.freemap.co.il/wms-c/","http://t7.maps.freemap.co.il/wms-c/","http://t8.maps.freemap.co.il/wms-c/"],{layers:'speed_cams',format:'image/png',transparent:"true"},{alpha:true,numZoomLevels:10,buffer:0});
//this.map.addLayer(speedCamLayer);
}
