var map,debug="n",centre_point,loaded=[],polygons=[],ignore_scroll="y",resize_timeout;if(!l)var l="e";var last_id=0,embed="n",levels=[],layers=[],current_width=0,marker_cats=[],markers_loaded="n",crimelevels={};crimelevels[1]={high:new Array(213,105,76,0.7),above_average:new Array(255,168,75,0.7),average:new Array(247,237,77,0.7),below_average:new Array(147,214,241,0.7),low:new Array(130,136,230,0.7)};
crimelevels[2]={high:new Array(18,34,70,0.7),above_average:new Array(74,86,113,0.7),average:new Array(130,138,156,0.7),below_average:new Array(186,190,199,0.7),low:new Array(227,234,245,0.7)};var comparison_total=0,ignore_zoom="n",current_crimetype,colour_scheme;
function link_page(){var a=map.GetCenter();$("#link_page input").val("http://"+window.location.host+"/view/?ll="+a.Latitude+","+a.Longitude+"&z="+map.GetZoomLevel());$("#nav a#feedback_link").attr("href","http://"+window.location.host+"/feedback/?ll="+a.Latitude+","+a.Longitude+"&z="+map.GetZoomLevel());$("#languages ul li.english a").attr("href","http://"+window.location.host+"/view/?l=e&ll="+a.Latitude+","+a.Longitude+"&z="+map.GetZoomLevel());$("#languages ul li.cymraeg a").attr("href","http://"+
window.location.host+"/view/?l=c&ll="+a.Latitude+","+a.Longitude+"&z="+map.GetZoomLevel())}function Area(){this.colours={};this.shape={};this.fill={}}Area.prototype.addColour=function(a,c){a=parseInt(a);this.colours[a]=crimelevels[colour_scheme][c]};Area.prototype.getColour=function(a){a=parseInt(a);return this.colours[a]};Area.prototype.addShape=function(a){this.shape=a};Area.prototype.getShape=function(){return this.shape};Area.prototype.setFill=function(a,c){parseInt(a);this.fill=c};
Area.prototype.getFill=function(){return this.fill};
$(function(){$("#footer").hide();$("#link_page").hide();$("#link_page_link").click(function(){$("#link_page").show();$("#link_page input").select();return false});$("#link_page a").click(function(){$("#link_page").hide();return false});$("#link_page input").click(function(){$(this).select()});$("#nav a.fancybox").fancybox({width:800,height:520,padding:20,hideOnContentClick:false,type:"ajax",titleShow:false,autoDimensions:false});$("#map").fadeTo(1,0);$.browser.msie==true||$.browser.webkit==true?$(window).wresize(resize_alternative):
$(window).resize(function(){resize_map(ignore_scroll)});$("#poi_icon").click(function(){$("#poi").toggle();$("#poi").is(":visible")==true?$(this).addClass("here"):$(this).removeClass("here");return false});$("#controls ul li a.zoom_in").click(function(){map.GetZoomLevel()<17&&map.ZoomIn();return false});$("#controls ul li a.zoom_out").click(function(){map.GetZoomLevel()>6&&map.ZoomOut();return false});$("#poi ul li input").click(function(){id=$(this).attr("class");$(this).hide();$(this).parent().prepend('<img src="images/loading_markers.gif" />');
markers_loaded=="y"?markers_show(id):markers(id)});$("#controls ul li.zoom a").click(function(){if(ignore_zoom=="n"){$("#controls ul li.zoom a").removeClass("here");$(this).addClass("here");var a=parseInt($(this).attr("rel"));map.SetZoomLevel(a)}return false});$(window).unload(function(){map&&map.Dispose()})});
function markers_show(a){if(layers[a])$("#poi ul li input[class="+a+"]").is(":checked")==true?layers[a].Show():layers[a].Hide();$("#poi ul li img").remove();$("#poi ul li input[class="+a+"]").show()}
function resize_map(){if(embed=="y"){width=parseInt($(window).width());height=370}else if(embed=="nhp"){width=parseInt($(window).width())-($("#info").width()+7);height=parseInt($(window).height())}else{width=parseInt($(window).width())-$("#info").width();height=parseInt($(window).height())-150}if(width<300)width=301;map.Resize(width,height)}
function resize_alternative(){if(parseInt($(window).width())!=current_width){if($.browser.webkit==true)if(embed=="y"){width=parseInt($(window).width());height=370}else{width=parseInt($(window).width())-($("#info").width()+2);height=parseInt($(window).height())-150}else if(embed=="y"){width=parseInt($(window).width());height=370}else{if($.browser.version<7){$("#map_key").css("right",10);width=parseInt($(window).width())-($("#info").width()+5)}else width=parseInt($(window).width())-($("#info").width()+
2);if(width<300)width=301;height=embed=="nhp"?parseInt($(window).height()):parseInt($(window).height())-$("#header").height()}clearTimeout(resize_timeout);resize_timeout=setTimeout("map.Resize(width, height);",600);current_width=parseInt($(window).width())}}
function info(a,c,d){$("#cdrp_list").hide();var b=c.indexOf("type_")!=-1?c.split("_")[1]:default_crimetype;if(b){current_crimetype=b;$(loaded).each(function(g,h){colour_in(h,b)})}$("#info div.panel").html('<img src="images/loading.gif" alt="Loading, please wait" class="loading" />');var f=map.GetCenter();url="request/details/"+a+"/"+c+"/?ll="+f.Latitude+","+f.Longitude+"&c="+colour_scheme;try{_gat._getTracker(analytics_code)._trackPageview("/"+d)}catch(e){}$("#info").load(url,"",function(){$("#info div.panel a.compare").fancybox({width:800,
height:420,padding:20,hideOnContentClick:false,type:"iframe",titleShow:false,autoDimensions:false});var g=$("#info div.confidence_panel a.confidence_link");if(g.attr("href")!=null){$("#cdrp_list ul li a").fancybox({width:700,height:400,padding:20,hideOnContentClick:false,type:"iframe",scrolling:"no",titleShow:false,onComplete:function(){$("#cdrp_list").hide()}});$(g).attr("href").indexOf("#")==-1?$(g).fancybox({width:700,height:400,padding:20,hideOnContentClick:false,type:"iframe",scrolling:"no",
titleShow:false}):$(g).click(function(){var h=$(this).position(),i=$(this).width();$("#cdrp_list").css({left:h.left+i+20,top:h.top-5,zIndex:1E4}).show();return false})}$("#info div.panel a.compare_change").fancybox({width:500,height:200,padding:20,hideOnContentClick:false,type:"iframe",titleShow:false,autoDimensions:false});$("#info a.fancybox").fancybox({width:800,height:520,padding:20,hideOnContentClick:false,type:"ajax",titleShow:false,autoDimensions:false});$("#breadcrumb a").click(function(){if(ignore_zoom==
"n"){var h=parseInt($(this).attr("rel"));map.SetZoomLevel(h)}return false})});$("#search_fail").css("z-index","10000")}
function info_switch(a,c,d){$("#cdrp_list").hide();try{_gat._getTracker(analytics_code)._trackPageview("/"+d)}catch(b){}$("#info div.typebox").hide();$("#info #"+a+"_types_nav li").removeClass("here");$("#info #"+a+"_"+c).show();$("#info #"+a+"_types_nav #link_"+c).addClass("here");if(a=="crime"){current_crimetype=c;$(loaded).each(function(f,e){colour_in(e,c)})}$.ajax({type:"GET",url:"/ajax/?"+a+"_type="+c})}
function init(a,c,d){map=new VEMap("map");map.HideDashboard();map.EnableShapeDisplayThreshold(false);map.SetClientToken(t);map.LoadMap();$("#map").append('<div id="crosshair"></div><iframe id="iframe_loading" marginheight="0" marginwidth="0" frameborder="0" src="/loading.html" allowtransparency="true"></iframe>');$("#map iframe").fadeOut(1);layers.shapes=new VEShapeLayer;map.AddShapeLayer(layers.shapes);map.AttachEvent("onmousewheel",function(){return true});map.AttachEvent("onkeypress",function(){return true});
map.AttachEvent("onkeydown",function(){return true});map.AttachEvent("onkeyup",function(){return true});map.AttachEvent("onendzoom",function(b){link_page();if(ignore_scroll=="n"){loading("start");layers.shapes.DeleteAllShapes();request()}$("#controls ul li.zoom a").each(function(){if($(this).attr("rel")==map.GetZoomLevel()){$("#controls ul li.zoom a").removeClass("here");$(this).addClass("here")}});map.GetZoomLevel()<=8?$("#map_key").hide():$("#map_key").show();$("#controls ul li a.zoom_in, #controls ul li a.zoom_out").parent().removeClass("disabled");
b=map.GetZoomLevel();if(b==6)$("#controls ul li a.zoom_out").parent().addClass("disabled");else b==17&&$("#controls ul li a.zoom_in").parent().addClass("disabled")});map.AttachEvent("onstartzoom",function(b){loading("start")});map.AttachEvent("onendpan",function(b){link_page();if(ignore_scroll=="n"){loading("start");request()}});map.SetCenterAndZoom(new VELatLong(a,c),d);d==6&&$("#controls ul li a.zoom_out").parent().addClass("disabled");$.browser.msie==true||$.browser.webkit==true?resize_alternative():
resize_map(ignore_scroll);ignore_scroll="n";after_loading()}
function markers(a){$.ajax({type:"GET",dataType:"json",url:"request/markers/",success:function(c){if(c.total>0){$.each(c.groups,function(d,b){layers[b.id]=new VEShapeLayer;map.AddShapeLayer(layers[b.id]);layers[b.id].Hide();marker_cats[b.id]={};marker_cats[b.id].image=b.image;d=new VEClusteringOptions;var f=new VECustomIconSpecification;f.Image="/images/poi/"+b.cluster;d.Icon=f;layers[b.id].SetClusteringConfiguration(VEClusteringType.Grid,d)});$.each(c.markers,function(d,b){d=new VEShape(VEShapeType.Pushpin,
new VELatLong(parseFloat(b.lat),parseFloat(b.lng)));d.SetCustomIcon("/images/poi/"+marker_cats[b.group].image);d.SetTitle(b.name);layers[b.group].AddShape(d)})}markers_loaded="y";markers_show(a)}})}
function request(a){$("#cdrp_list").hide();if(a=="y"){last_id=0;loaded=[];levels=[];layers.shapes.DeleteAllShapes()}else a="n";var c=map.GetCenter(),d=map.GetMapView();a="request/?tl_lat="+d.TopLeftLatLong.Latitude+"&tl_lon="+d.TopLeftLatLong.Longitude+"&br_lat="+d.BottomRightLatLong.Latitude+"&br_lon="+d.BottomRightLatLong.Longitude+"&lon="+c.Longitude+"&lat="+c.Latitude+"&zoom="+map.GetZoomLevel()+"&clear="+a+"&x="+Math.floor(Math.random()*1E4);trace("URL: "+a);$.ajax({type:"GET",dataType:"json",
url:a,success:function(b){document.title=b.info.pagetitle;b.force.id!=current_client&&change_client(b.force);b.polygons.total>0&&$.each(b.polygons.items,function(f,e){itemid=parseInt(e.id);if(!levels[itemid]){levels[itemid]=new Area;$(e.crimelevels).each(function(g,h){bits=h.split("|");levels[itemid].addColour(bits[0],bits[1])});levels[itemid].setFill(e.id,e.fill)}outlines_add(e.id,e.points,e.fill)});b.polygons.clear.length>0&&$.each(b.polygons.clear,function(f,e){loaded=$.grep(loaded,function(g){return parseInt(e)!=
parseInt(g)})});if(parseInt(b.info.id)!=0){info(b.info.id,b.info.type);last_id=parseInt(b.info.id)}else{last_id=0;b=l=="e"?"no_details":"no_details_cymraeg";$("#info").html('<div id="nodetails"><img src="images/'+b+'.png" id="no_details" alt="No data" style="margin: 20px 0 0 25px;" /></div>')}loading("end")},beforeSend:function(){loading("start")}})}
function outlines_add(a,c,d){loaded.push(parseInt(a));var b=[];$(c).each(function(f,e){f=e.split(",");f=new VELatLong(parseFloat(f[0]),parseFloat(f[1]));b.push(f)});c=new VEShape(VEShapeType.Polygon,b);levels[parseInt(a)].addShape(c);d=="y"?c.SetFillColor(get_colour(a,current_crimetype)):c.SetFillColor(new VEColor(255,255,102,0));c.SetLineWidth(2);c.SetLineColor(new VEColor(0,0,0,1));layers.shapes.AddShape(c);c.HideIcon()}
function colour_in(a,c){c=get_colour(parseInt(a),c);levels[parseInt(a)].getShape().SetFillColor(c)}function get_colour(a,c){a=levels[a].getFill()=="y"?c==""?levels[a].getColour(default_crimetype):levels[a].getColour(c):Array(255,255,102,0);return new VEColor(parseInt(a[0]),parseInt(a[1]),parseInt(a[2]),parseFloat(a[3]))}
function change_client(a){if(compare_level!="uk"){last_id=0;loaded=[];levels=[];layers.shapes.DeleteAllShapes()}if(embed=="y"&&colour_scheme!=a.colour_scheme){colour_scheme=a.colour_scheme;$("#map_key ul li img").each(function(){$(this).attr("src",$(this).attr("src").replace(/\d\//,a.colour_scheme+"/"))})}$("#force_logo").attr("src","/resources/"+a.logo);current_client=a.id}
function loading(a){if(a=="start"){$("#map iframe").show();$("#crosshair, #controls, #poi_icon, #poi").hide();ignore_zoom="y"}else{$("#crosshair, #controls, #poi_icon").show();$("#poi_icon").hasClass("here")&&$("#poi").show();$("#map iframe").hide();$("#map").fadeTo(1,1);$("#footer").show();$("#preloader").hide();ignore_zoom="n"}}
function map_find(a){var c=map.GetCenter(),d=map.GetZoomLevel();if(a!=default_search_term){a=decodeURIComponent(a.replace(/\+/g," "))+", uk";map.Find(null,a,null,null,null,null,null,null,false,true,function(b,f,e){if(e!=null){b=e[0].LatLong;if(b.Latitude!="54.07100021839142"&&b.Longitude!="-2.783999517560005")$.ajax({type:"GET",dataType:"json",url:"/request/uk/?lat="+b.Latitude+"&lng="+b.Longitude,success:function(g){if(g.result=="n"){map.SetCenterAndZoom(c,d);$("#search_fail").show()}else{$("#search_fail").hide();
request()}}});else{map.SetCenterAndZoom(c,d);$("#search_fail").show()}}else{map.SetCenterAndZoom(c,d);$("#search_fail").show()}return false})}}function trace(a){$("#test").append("<br/>"+a)};
