var a,trafficmap;
function init_index(){jQuery(document).ready(function(){if(screen.width>480){trafficmap=new TrafficMap("map");trafficmap.load_live_cams("/cameras.xml");trafficmap.show_gtraffic();jQuery("span.traffic").click(function(){jQuery("input.traffic").click()});jQuery("input.traffic").click(function(){jQuery("input.traffic").attr("checked")?trafficmap.show_gtraffic():trafficmap.hide_gtraffic()});jQuery(".traffic").show();jQuery("span.live_cams").click(function(){jQuery("input.live_cams").click()});jQuery("input.live_cams").click(function(){jQuery("input.live_cams").attr("checked")?
trafficmap.show_live_cams():trafficmap.hide_live_cams()});jQuery(".live_cams").show();TrafficNews.show("#sactraffic_news","http://www.lectroid.net/category/sactrafficorg/feed/",7)}get_incidents(trafficmap)})}window.init_index=init_index;
function init_incident(b){jQuery(document).ready(function(){if(screen.width>480)trafficmap=new TrafficMap("map");jQuery.getJSON("/json/STCC-STCC.json",function(d){var g=TrafficList.show_incident(d,b);if(typeof trafficmap!="undefined"){trafficmap.gmap.zoomIn();trafficmap.show_incident(d,b)}g&&setInterval(function(){jQuery.getJSON("/json/STCC-STCC.json",function(c){TrafficList.show_incident(c,b)})},1E4)})})}window.init_incident=init_incident;
function get_incidents(b){jQuery.getJSON("/json/STCC-STCC.json",function(d){TrafficList.show_incidents(d);typeof b!="undefined"&&b.update(d);setTimeout(get_incidents,6E4,b)})}function tbxy2latlng(b){b=b.split(/:/);return{lat:b[1]*2.74E-6+33.172,lng:b[0]*3.5E-6-144.966}}
Date.prototype.getISO8601=function(){var b=this.getMonth()+1<10?"0"+this.getMonth()+1:this.getMonth()+1,d=this.getDate()<10?"0"+this.getDate():this.getDate(),g=this.getHours()<10?"0"+this.getHours():this.getHours(),c=this.getMinutes()<10?"0"+this.getMinutes():this.getMinutes(),e=this.getSeconds()<10?"0"+this.getSeconds():this.getSeconds(),f=this.getTimezoneOffset()/60;return this.getFullYear()+"-"+b+"-"+d+"T"+g+":"+c+":"+e+"-0"+f+"00"};var TrafficList=function(){function b(c){var e=new Date(c.LogTimeEpoch*1E3),f=c.TBXY&&c.TBXY!=""?tbxy2latlng(c.TBXY):null,h=c.LogDetails.details.length>1?"slow":"fast",i=c.LogDetails.details.length>0?"Click for incident details ("+c.LogDetails.details.length+")":"",j=d(c.LogDetails.details);e=jQuery("<li/>").attr("id",c.ID).attr("title",i).addClass("vevent").append(jQuery("<div/>").addClass("logtype summary").html(c.LogType)).append(jQuery("<div/>").addClass("location").html(c.Location+"<br/>"+c.Area).append(jQuery("<button/>").html("Show on map").click(function(){if(f){jQuery(this).parent().click();
location="http://maps.google.com/maps?q="+f.lat+","+f.lng}}))).append(jQuery("<div/>").addClass("logtime").html(c.LogTime).append(jQuery("<span/>").addClass("dtstart").html(e.getISO8601()))).append(j).hover(function(){typeof trafficmap!="undefined"&&trafficmap.center_on_id(c.ID)},function(){typeof trafficmap!="undefined"&&trafficmap.recenter()}).click(function(){if(c.LogDetails.details.length>0){j.toggle(h);g[c.ID]=g[c.ID]?false:true}});g[c.ID]&&j.css("display","block");if(f){i="/images/incident.png";
if(!/Traffic Hazard|Disabled Vehicle/.test(c.LogType))if(/Collision|Fatality|Hit \& Run/.test(c.LogType))i="/images/accident.png";jQuery("<img/>").attr("src",i).attr("height","18").attr("width","18").prependTo(e);jQuery("<div/>").addClass("geo").append(jQuery("<span/>").addClass("latitude").html(f.lat)).append(jQuery("<span/>").addClass("longitude").html(f.lng)).appendTo(e)}return e}function d(c){var e=jQuery("<ul/>").addClass("details");jQuery.each(c,function(f,h){jQuery("<li/>").append(jQuery("<span/>").addClass("detailtime").html(h.DetailTime)).append(jQuery("<span/>").addClass("incidentdetail").html(h.IncidentDetail.replace(/(\*.+?\*)/,
'<span class="alert">$1</span>'))).appendTo(e)});return e}var g={};return{show_incidents:function(c){jQuery(".incidents").empty();jQuery("#mediainfo").empty();jQuery.each(c,function(e,f){if(f.Status=="active")if(f.LogType=="Media Information"){if(f.LogDetails.details.length>0){d(f.LogDetails.details).appendTo("#mediainfo");jQuery("#mediainfo").show()}}else{e=e<4?"#incidents_above":"#incidents_below";b(f).appendTo(e)}});jQuery(".incidents li:last-child").addClass("last")},show_incident:function(c,
e){var f=false,h=false;jQuery("#incidents_above").empty();jQuery("#incidents_below").empty();jQuery.each(c,function(i,j){if(j.ID==e){b(j).appendTo("#incidents_above");jQuery("#incidents_above .details").show();f=true;h=j.Status=="active"?true:false;return false}});if(f)return h?true:false;else{jQuery("#incidents_above").append(jQuery("<li/>").addClass("noincident").html("The incident you requested does not exist or is no longer active."));return false}}}}();function TrafficMap(b){var d=this;this.center=new GLatLng(38.56,-121.4);this.live_cams=[];this.traffic_overlay=null;this.marker_list={};this.gmap=new GMap2(document.getElementById(b));this.gmap.setCenter(this.center,10);this.gmap.addControl(new GSmallMapControl);GEvent.addListener(this.gmap,"dragend",function(){d.center=d.gmap.getCenter()})}a=TrafficMap.prototype;
a.load_live_cams=function(b){var d=this;jQuery.ajax({url:b,dataType:"xml",success:function(g){function c(i){var j=i.getAttribute("id"),k=i.getAttribute("name"),l=i.getElementsByTagName("location")[0].getAttribute("lon"),m=i.getElementsByTagName("location")[0].getAttribute("lat"),n=i.getElementsByTagName("size")[0].getAttribute("height"),o=i.getElementsByTagName("size")[0].getAttribute("width");i=new GMarker(new GPoint(l,m),{title:k,icon:e});GEvent.addListener(i,"click",function(){var p=k.replace(/ /g,
"_").replace(/-/g,"_"),q=parseInt(o)+60,r=parseInt(n)+170;window.open("../showcamera.php?id="+j,p,"width="+q+",height="+r)});return i}var e=new GIcon;e.image="/images/camera_icon.gif";e.iconSize=new GSize(24,24);e.iconAnchor=new GPoint(12,12);e.infoWindowAnchor=new GPoint(12,1);for(var f=0;f<g.getElementsByTagName("camera").length;f++){var h=g.getElementsByTagName("camera")[f];h=c(h);d.live_cams.push(h)}d.show_live_cams()}})};
a.update=function(b){this.hide_incidents();this.marker_list={};for(var d=0;d<b.length;d++){var g=b[d];if(g.Status=="active"){var c=this.make_marker(g);if(c){this.marker_list[g.ID]=c;this.gmap.addOverlay(c)}}}};a.show_incident=function(b,d){for(var g=0;g<b.length;g++){var c=b[g];if(c.ID==d){if(b=this.make_marker(c)){this.gmap.addOverlay(b);this.center=b.getLatLng();this.gmap.setCenter(this.center,13)}break}}};a.show_live_cams=function(){for(var b=0;b<this.live_cams.length;b++)this.gmap.addOverlay(this.live_cams[b])};
a.hide_live_cams=function(){for(var b=0;b<this.live_cams.length;b++)this.gmap.removeOverlay(this.live_cams[b])};a.show_gtraffic=function(){this.traffic_overlay=new GTrafficOverlay({incidents:true});this.gmap.addOverlay(this.traffic_overlay)};a.hide_gtraffic=function(){this.traffic_overlay&&this.gmap.removeOverlay(this.traffic_overlay)};a.center_on_id=function(b){this.marker_list[b]&&this.gmap.panTo(this.marker_list[b].getLatLng())};a.recenter=function(){this.gmap.panTo(this.center)};
a.hide_incidents=function(){for(var b in this.marker_list)this.gmap.removeOverlay(this.marker_list[b])};
a.make_marker=function(b){if(b.TBXY&&b.TBXY!=""&&b.LogType!="Media Information"){var d=new GIcon(G_DEFAULT_ICON);d.image="/images/incident.png";d.shadow="/images/traffic_incident_shadow.png";d.iconSize=new GSize(18,18);d.shadowSize=new GSize(23,23);d.iconAnchor=new GPoint(9,9);d.infoWindowAnchor=new GPoint(8,3);var g=tbxy2latlng(b.TBXY),c=new GLatLng(g.lat,g.lng);if(!/Traffic Hazard|Disabled Vehicle/.test(b.LogType))if(/Collision|Fatality|Hit \& Run/.test(b.LogType))d.image="/images/accident.png";
var e=new GMarker(c,{icon:d,zIndexProcess:function(){return GOverlay.getZIndex(c.lat())*2}});GEvent.addListener(e,"click",function(){e.openInfoWindowHtml('<div class="marker"><div class="logtype">'+b.LogType+'</div><div class="location">'+b.Location+'</div><div class="logtime">'+b.LogTime+"</div></div>")});return e}};var TrafficNews=function(){return{show:function(b,d,g){(new google.feeds.Feed(d)).load(function(c){if(!c.error){var e=false;jQuery(b).empty();jQuery.each(c.feed.entries,function(f,h){if(g){f=new Date((new Date).getTime()-86400*g*1E3);if(new Date(h.publishedDate)<f)return false}e=true;jQuery(b).append(jQuery("<li/>").append(jQuery("<a/>").attr("href",h.link).html(h.title)).append("<br/>").append(jQuery("<span/>").html(h.contentSnippet)))});e&&jQuery("#sitenews").show("slow")}})}}}();