/*
** Use of this software constitutes your agreement to the following terms of use:
** This application is protected under the same restrictions and terms of use and
** conditions as included in the product license. Consistent with these terms,
** you may not: 1) sublicense, resell, rent, lease, redistribute, assign or 
** otherwise transfer user rights to the Software in any manner, and 2) use the 
** Software for timesharing, service bureau or other such purposes, or otherwise
** host the Software for commercial gain and/or for the benefit of a third party.
** Use of the Software may be subject to applicable fees, and any use of the 
** Software prior to paying any such applicable fees is strictly prohibited.
** IMPORTANT: Milsoft’s warranty, support and maintenance for this application is
** limited solely to the original code as provided. Any changes, modifications or
** customization to the code performed by the user or any party other than
** Milsoft will not be supported or maintained, and such changes may be lost to
** the user at the time of application upgrade, update or patch installation.
** Milsoft expressly disclaims any / all liability for such losses. You do not
** have the right to remove Milsoft Utility Solutions, Inc. copyrights from the
** source code or user interface. All copies of the Covered Code must include the
** Milsoft Utility Solutions, Inc. copyright notice in the same form as it appears
** in this distribution. 
** 
** Copyright (C) 2010 Milsoft Utility Solutions, Inc.; All Rights Reserved.
*/
function detail_view(a){var b="";if($("#view-historical").is(":checked"))b="&historical=1";if(a[0]!="'")a="'"+a+"'";$("#details-popup-bg").show();$("#details-popup").load("detailview.php?popup=1&id="+a+b).show()}function viewport_info(a){var b=bounding_box(a);var c=parseInt(menu_options.width)+parseInt($("#viewer-menu").css("padding-left"))+parseInt($("#viewer-menu").css("padding-right"));var d=$(window).height()-12,e=$(window).width()-c-12;var f=40033580/360;var g=f*(b.east-b.west);var h=f*(b.north-b.south);h/=Math.cos(b.center[0]/180*Math.PI);var i=Math.max(g/e,h/d);var j=Math.floor(Math.log(156543.04/i)/Math.log(2));i=156543.04/Math.pow(2,j);b.center[1]+=i*c/f/2;return[b.center,j]}function bounding_box(a){var b=-180,c=180,d=-90,e=90;for(var f=0;f<a.length;f++){d=Math.max(d,a[f][0]);e=Math.min(e,a[f][0]);b=Math.max(b,a[f][1]);c=Math.min(c,a[f][1])}var g=[(d+e)/2,(b+c)/2];return{north:d,east:b,south:e,west:c,center:g}}function get_timestamp(){return(new Date).getTime()}function delete_shapes(a){if(a!==undefined){for(var b=viewer_data.outages.length-1;b>=0;b--){var c=viewer_data.outages[b].shape_index;if(c!==undefined){if(Date.parse(viewer_data.outages[b].start_time)<=a&&Date.parse(viewer_data.outages[b].end_time)>=a)continue;if(shape_ids[c].icon!==undefined)map.removePopup(shape_ids[c].icon);if(shape_ids[c].polygon!==undefined)vector_layer.removeFeatures([shape_ids[c].polygon]);for(var d=c;d<shape_ids.length-1;d++)shape_ids[d]=shape_ids[d+1];shape_ids.length--;for(var d=0;d<viewer_data.outages.length;d++)if(viewer_data.outages[d].shape_index!==undefined&&viewer_data.outages[d].shape_index>c)viewer_data.outages[d].shape_index--;delete viewer_data.outages[b].shape_index}}}else{$("css3-container").remove();vector_layer.removeAllFeatures();for(var b=0;b<shape_ids.length;b++)if(shape_ids[b].icon!==undefined)map.removePopup(shape_ids[b].icon);for(var b=0;b<viewer_data.outages.length;b++)if(viewer_data.outages[b].shape_index!==undefined)delete viewer_data.outages[b].shape_index;shape_ids.length=0}}function expand_icon(a){a="#"+a.replace(".","\\.");$(a).parent().parent().css({width:Math.max(map_options.outage_box_width,$(a).width()+6)+"px",height:Math.max(map_options.outage_box_width,$(a).height()+6)+"px"})}function add_polygon(a,b,c,d,e,f,g){var h=bounding_box(a);var i=[];for(var j=0;j<a.length;j++){var k=new OpenLayers.Geometry.Point(parseFloat(a[j][1]),parseFloat(a[j][0]));k.transform(new OpenLayers.Projection("EPSG:4326"),map.getProjectionObject());i.push(k)}shape_ids.push(new Object);if(a.length>2){var l=new OpenLayers.Geometry.Polygon([new OpenLayers.Geometry.LinearRing(i)]);shape_ids[shape_ids.length-1].polygon=new OpenLayers.Feature.Vector(l,null,{fillColor:"#"+f,fillOpacity:g,strokeWidth:3,strokeColor:"#"+d,strokeOpacity:e,strokeDashstyle:"solid"});vector_layer.addFeatures(shape_ids[shape_ids.length-1].polygon)}if(c!==false){var m=(new OpenLayers.LonLat(h.center[1],h.center[0])).transform(new OpenLayers.Projection("EPSG:4326"),map.getProjectionObject());var n=new OpenLayers.Size(parseInt(map_options.outage_box_width),parseInt(map_options.outage_box_width));var o=new OpenLayers.Popup(null,m,n,"",false);map.addPopup(o);shape_ids[shape_ids.length-1].icon=o;$(o.div).css({"background-color":"transparent",width:"auto",height:"auto","margin-left":-map_options.outage_box_width/2+"px","margin-top":-map_options.outage_box_width/2+"px"});var p="";var q="";if(typeof c=="string"){var r=Math.ceil(map_options.outage_box_width/2)+"px";if(c.indexOf("verified")!=-1)q="border-radius:"+r+";-webkit-border-radius:"+r+";-moz-border-radius:"+r+";behavior:url(externals/PIE.htc);";if(c.indexOf("crew_assigned")!=-1){var s=$(o.div).attr("id")+"b";p='<img id="'+s+'" onload="expand_icon(\''+s+"')\" "+'src="'+map_options.crew_assigned_icon+'" style="margin:6px 0px 0px 6px;" />'}}var t=$(o.div).attr("id")+"a";$(o.div).html('<div style="background:#'+f+";width:"+map_options.outage_box_width+"px;"+"height:"+map_options.outage_box_width+"px;"+q+'" id="'+t+'">'+p+"</div>");add_hover_popup(b,m,t)}}function add_outage_polygon(a,b,c,d,e,f){var g=0;for(;g<menu_options.outage_levels.length-1;g++){if(c>=menu_options.outage_levels[g].min&&c<=menu_options.outage_levels[g].max)break}var h=menu_options.outage_levels[g].line_color;var i=menu_options.outage_levels[g].line_opacity;var j=menu_options.outage_levels[g].fill_color;var k=menu_options.outage_levels[g].fill_opacity;if(f&&!$("#view-historical").is(":checked"))h=j="008E22";var l="";if(d)l+="verified";if(map_options.crew_assigned_icon&&e)l+="crew_assigned";try{if(!map_options.show_outage_polygons)a.length=1;add_polygon(a,b,l,h,i,j,k)}catch(m){}}function initialize_outages(a,b){for(var c=0;c<a.length;c++){var d=a[c];if(b!==undefined){if(d.shape_index!==undefined)continue;if(Date.parse(d.start_time)>b||Date.parse(d.end_time)<b)continue}var e=d.fieldlabel[0]+": "+d.fieldvalue[0];var f="<ul>";for(var g=0;g<d.fieldlabel.length;g++){if(d.fieldvalue[g]!=null)f+="<li>"+d.fieldlabel[g]+": <b>"+d.fieldvalue[g]+"</b></li>"}if(map_options.outage_details=="internal")f+="<br /><a href='#' onClick='detail_view(\""+d.rec_id+"\")'>Details for this outage</a>";else if(map_options.outage_details=="streets")f+="<br /><a href='#' onClick='detail_view(\""+d.rec_id+"\")'>Streets affected by this outage</a>";f+="</ul>";var h=d.customers_out_now;if(!map_options.update_restored_marks||$("#view-historical").is(":checked"))h+=d.customers_restored;add_outage_polygon(d.boundary,f,h,d.verified,d.crew_assigned,d.customers_out_now==0);if(b!==undefined)d.shape_index=shape_ids.length-1}}function initialize_model(a){if(!map_options.show_service_area)return;add_polygon(a.boundary,"",false,map_options.model_polygon.line_color,map_options.model_polygon.line_opacity,map_options.model_polygon.fill_color,map_options.model_polygon.fill_opacity)}function resizeDocument(){var a=$(window).height();var b=$(window).width();var c=null;var d=null;var e=null;var f=null;var g=null;if(menu_options.show){c=a;d=b;e=a;f=menu_options.width;g=b-menu_options.width-parseInt($("#viewer-menu").css("padding-left"))-parseInt($("#viewer-menu").css("padding-right"))}else{c=a;d=b;e=0;f=0;g=0}$("#viewer-menu").height(e);$("#viewer-menu").width(f);$("#viewer-menu").css({top:0,left:g});$("#viewer-map").height(c);$("#viewer-map").width(d);$("#viewer-map").css({top:0,left:0})}function initialize_outage_by_area(a){if(menu_options.show_outage_by_area){$("#outage-by-area-div").show();$("#outage-by-area-title").html(a);for(var b=0;b<viewer_data.areas.length;b++){var c=$.trim(viewer_data.areas[b].name);var d=viewer_data.areas[b].count;if(c==""){c="Other"}$("#outage-by-area-list").append("<li><strong>"+c+"</strong> - "+d+"</li>")}}else{$("#outage-by-area-div").hide()}}function initialize_key(){$("#map-key-title").html(menu_options.map_key_title);for(var a=0;a<menu_options.outage_levels.length;a++){var b=menu_options.outage_levels[a].min;var c=menu_options.outage_levels[a].max;var d="#"+menu_options.outage_levels[a].fill_color;if(menu_options.outage_levels.length==a+1){$("#key-list").append('<li><div class="key-box" id="key-box-'+(a+1)+'"></div> '+b+"+ "+menu_options.legend_label+"</li>")}else{$("#key-list").append('<li><div class="key-box" id="key-box-'+(a+1)+'"></div> '+b+"-"+c+" "+menu_options.legend_label+"</li>")}$("#key-box-"+(a+1)).css({background:d})}if(!map_options.when_to_hide_outages){$("#key-list").append('<li><div class="key-box" id="key-box-restored"></div> Restored Outage</li>');$("#key-box-restored").css("background","#008E22")}if(map_options.show_predicted_outages){var e="";if(map_options.crew_assigned_icon)e='<img class="key-type-crew" src="'+map_options.crew_assigned_icon+'" border=0 /> Crew Assigned';$("#key-list").append('<li><div class="key-box key-type-predicted"></div> Predicted'+'<div class="key-box key-type-verified"></div> Verified'+e+"</li>")}if(map_options.show_service_area){$("#key-list").prepend('<li><div class="key-box" id="key-box-0"></div> Service Area</li>');$("#key-box-0").css({background:"#"+map_options.model_polygon.fill_color})}}function initialize_menu(){$("#viewer-menu").css({color:"#"+menu_options.colors.text_color,background:"#"+menu_options.colors.background_color,opacity:menu_options.colors.background_opacity});$("#viewer-menu a").css("color","#"+menu_options.colors.text_color);if(menu_options.show_logo){$("#viewer-menu").prepend('<a id="logo-link"><img id="logo" src="'+menu_options.logo_location+'" /></a>');$("#logo-link").css({"text-align":menu_options.logo_alignment,display:"block"});if($("#view-historical").is(":checked"))$("#logo-link").hide();if(menu_options.logo_link)$("#logo-link").attr("href",menu_options.logo_link)}if(!$("#view-historical").is(":checked"))$("#menu-title").html(menu_options.menu_title.replace(/\\'/g,"'"));$("#menu-description").html(menu_options.menu_description.replace(/\\'/g,"'"));var a=viewer_data.outages.length;var b=0;var c=0;for(var d=0;d<viewer_data.outages.length;d++){c+=viewer_data.outages[d].customers_restored;b+=viewer_data.outages[d].customers_out_now}var e=c+b;var f=parseInt(c/e*100);$("#viewer-totals").append("<li>Total Outages: <strong>"+a+"</strong></li>");$("#viewer-totals").append("<li> </li>");$("#viewer-totals").append("<li>"+menu_options.customer_name+" Affected: <strong>"+e+"</strong></li>");if(!map_options.when_to_hide_outages)$("#viewer-totals").append("<li>"+menu_options.customer_name+" Restored: <strong>"+c+"</strong></li>");$("#viewer-totals").append("<li>"+menu_options.customer_name+" Out Now: <strong>"+b+"</strong></li>");if(menu_options.show_call_summary){$("#viewer-totals").append("<li>"+menu_options.customer_name+" Called: <strong>"+viewer_data.call_summary.customer_calls+"</strong></li>");$("#viewer-totals").append("<li>Calls Received Last Hour: <strong>"+viewer_data.call_summary.calls_last_hour+"</strong></li>")}initialize_key();initialize_outage_by_area(menu_options.outage_by_area_title);$("#viewer-menu p,#viewer-menu li").css("font-size",menu_options.font_size+"px")}function destruct_menu(){$("#logo").remove();$("#viewer-totals").children().remove();$("#outage-by-area-list").children().remove();$("#key-list").children().remove()}function initialize_map(){var a=[map_options.initial_latitude,map_options.initial_longitude];var b=map_options.zoom_level;if(map_options.use_fixed_viewport!="on"&&viewer_data.model.boundary.length>1){var c=viewport_info(viewer_data.model.boundary);a=c[0];b=c[1]}if(b>map_options.max_zoom)b=map_options.max_zoom;map=new OpenLayers.Map("viewer-map");var d=new OpenLayers.Layer.OSM("OSM Layer",null,{transitionEffect:"resize",numZoomLevels:parseInt(map_options.max_zoom)+1});map.addLayer(d);vector_layer=new OpenLayers.Layer.Vector("polygon layer");map.addLayer(vector_layer);map.setCenter((new OpenLayers.LonLat(a[1],a[0])).transform(new OpenLayers.Projection("EPSG:4326"),map.getProjectionObject()),b);if(map_options.map_fixed){var e=map.getControlsByClass("OpenLayers.Control.Navigation");if(e.length>0)e[0].deactivate();$(".olControlPanZoom").hide()}}function get_historical_data(){var a=null;var b="";if($("#filter-cause").is(":checked"))b=$(".cause-choice").map(function(){return this.value}).get().join(",");$.ajax({url:"index.php",dataType:"json",data:{action:"get_historical_outages",start:$("#historical-start").val(),end:$("#historical-end").val(),causes:b,duration:$("#filter-duration").is(":checked")?$("#duration-value").val():"0",cr:$("#filter-cust-responsible").is(":checked")?1:0,timestamp:get_timestamp()},async:false,success:function(b){a=b;for(var c=0;c<a.outages.length;c++){a.outages[c].start_time=a.outages[c].start_time.replace(/PM/g," PM").replace(/AM/g," AM");a.outages[c].end_time=a.outages[c].end_time.replace(/PM/g," PM").replace(/AM/g," AM")}}});return a}function get_viewer_data(){var a=null;$.ajax({url:"index.php",dataType:"json",data:{action:"get_viewer_data",timestamp:get_timestamp()},async:false,success:function(b){a=b}});return a}function get_map_options(){var a=null;$.ajax({url:"index.php",dataType:"json",data:{action:"get_map_options",timestamp:get_timestamp()},async:false,success:function(b){a=b}});return a}function update_map(){$("#update-message").show(function(){if($("#view-historical").is(":checked"))viewer_data=get_historical_data();else viewer_data=get_viewer_data();if(viewer_data!=null){try{destruct_menu();delete_shapes();initialize_menu();initialize_model(viewer_data.model);initialize_trucks();initialize_outages(viewer_data.outages)}catch(a){}}$("#update-message").hide()})}function update_historical_pos(){var a=new Date(parseInt($("#historical-pos-hidden").val()));if(isNaN(a.getTime()))return;$("#historical-pos-val").html(zpad(a.getMonth()+1)+"/"+zpad(a.getDate())+"/"+a.getFullYear()+" "+zpad(a.getHours())+":"+zpad(a.getMinutes())+":"+zpad(a.getSeconds()));if(!$("#historical-stop").attr("disabled")){delete_shapes(a.getTime());initialize_outages(viewer_data.outages,a.getTime())}}function zpad(a){if(a<10)return"0"+a;return a}var map=null;var vector_layer=null;var shape_ids=[];$(document).ready(function(){initialize_map();if(menu_options.show)initialize_menu();resizeDocument();initialize_model(viewer_data.model);initialize_trucks();initialize_outages(viewer_data.outages);$("#update-message").hide();var a=parseInt(map_options.refresh_time)*60*1e3;if(parseInt(map_options.refresh_time)>1){$("#update-time").html("This page will update automatically every "+map_options.refresh_time+" minutes.")}else if(parseInt(map_options.refresh_time)==1){$("#update-time").html("This page will update automatically every minute.")}setInterval(function(){if(!$("#view-historical").is(":checked"))update_map()},a);$("#view-historical").click(function(){if($(this).is(":checked")){delete_shapes();initialize_model(viewer_data.model);$("#historical-play").attr("disabled","disabled");$("#menu-title").html("Viewing Historical Outages")}else{$("#menu-title").html(menu_options.menu_title.replace(/\\'/g,"'"));$("#historical-stop").click();update_map()}$("#logo-link").slideToggle(200);$("#menu-description").slideToggle(200);$("#update-time").slideToggle(200);$("#historical-input").slideToggle(200)});$("#enable-playback").click(function(){$(this).hide();$("#playback-input").show()});$("#historical-start").datepicker();$("#historical-end").datepicker();$("#historical-update").click(function(){var a=Date.parse($("#historical-start").val());if(isNaN(a)||isNaN(Date.parse($("#historical-end").val())))return;viewer_data.outages=[];update_map();$("#historical-pos-hidden").val(a);update_historical_pos();$("#historical-pos").slider("value",1);$("#historical-play").removeAttr("disabled")});$("#historical-speed").slider({min:0,max:14,value:0}).bind("slide",function(a,b){$("#historical-speed-val").html(Math.pow(2,b.value))});$("#historical-pos").slider({min:0,max:1e4,value:0}).bind("slide",function(a,b){var c=Date.parse($("#historical-start").val());var d=Date.parse($("#historical-end").val())+1e3*60*60*24-1e3;$("#historical-pos-hidden").val(c+(d-c)*(b.value/1e4));update_historical_pos()});$("#historical-play").click(function(){if($("#historical-play").val()=="Play"){$("#historical-start").attr("disabled","disabled");$("#historical-end").attr("disabled","disabled");$("#historical-update").attr("disabled","disabled");$("#historical-play").val("Pause");if($("#historical-stop").attr("disabled")){$("#historical-stop").removeAttr("disabled");delete_shapes();initialize_model(viewer_data.model)}}else $(this).val("Play")});$("#historical-stop").click(function(){$("#historical-start").removeAttr("disabled");$("#historical-end").removeAttr("disabled");$("#historical-update").removeAttr("disabled");$("#historical-play").val("Play");$("#historical-stop").attr("disabled","disabled");delete_shapes();initialize_model(viewer_data.model);initialize_outages(viewer_data.outages)});$("#filter-cause").click(function(){if($(this).is(":checked")&&$("#cause-input").html()==""){$("#cause-input").html("Select some or all cause codes and click Update:");for(var a in viewer_data.cause_codes){var b='<br /><select class="cause-choice" style="width:100%;" id="cause-'+a.replace(/[^0-9a-zA-Z_]/g,"-")+'"><option value="NOP">-- '+a+" --</option>";for(var c in viewer_data.cause_codes[a])b+='<option value="'+c+'">'+viewer_data.cause_codes[a][c]+"</option>";b+="</select>";$("#cause-input").append(b)}}else if(!$(this).is(":checked"))$("#historical-update").click();$("#cause-input").slideToggle(200)});$("#filter-duration").click(function(){if(!$(this).is(":checked"))$("#historical-update").click();$("#duration-input").slideToggle(200)});$("#view-all-details").click(function(){var a="";for(var b=0;b<viewer_data.outages.length;b++){if($("#historical-stop").is(":disabled")||viewer_data.outages[b].shape_index!==undefined){if(a.length>0)a+=",";a+="'"+viewer_data.outages[b].rec_id+"'"}}if(a.length==0)alert("No outages currently visible.");else detail_view(a)});setInterval(function(){if($("#view-historical").is(":checked")&&$("#historical-play").val()=="Pause"){var a=parseInt($("#historical-pos-hidden").val());var b=Date.parse($("#historical-start").val());var c=Date.parse($("#historical-end").val())+1e3*60*60*24-1e3;a+=parseInt($("#historical-speed-val").html())*125;if(a>=c){a=c;$("#historical-play").trigger("click")}$("#historical-pos-hidden").val(a);update_historical_pos();$("#historical-pos").slider("value",(a-b)/(c-b)*1e4)}},125)});$(window).resize(function(){resizeDocument()})
