/*
** 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=-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=new VELatLong((d+e)/2,(b+c)/2);var h=parseInt(menu_options.width)+parseInt($("#viewer-menu").css("padding-left"))+parseInt($("#viewer-menu").css("padding-right"));var i=$(window).height()-12,j=$(window).width()-h-12;var k=40033580/360;var l=k*(b-c);var m=k*(d-e);m/=Math.cos(g.Latitude/180*Math.PI);var n=Math.max(l/j,m/i);var o=Math.floor(Math.log(156543.04/n)/Math.log(2));n=156543.04/Math.pow(2,o);g.Longitude+=n*h/k/2;return[g,o]}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--){if(viewer_data.outages[b].shape_index!==undefined){if(Date.parse(viewer_data.outages[b].start_time)<=a&&Date.parse(viewer_data.outages[b].end_time)>=a)continue;var c=viewer_data.outages[b].shape_index;map.DeleteShape(map.GetShapeByID(shape_ids[c]));for(var d=c;d<shape_ids.length-1;d++)shape_ids[d]=shape_ids[d+1];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--;shape_ids.length--;delete viewer_data.outages[b].shape_index}}}else{for(var b=0;b<shape_ids.length;b++){var e=map.GetShapeByID(shape_ids[b]);map.DeleteShape(e)}shape_ids=[];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}}function hex2decColor(a){var b=a.substring(0,2);var c=a.substring(2,4);var d=a.substring(4,6);b=parseInt(b,16);c=parseInt(c,16);d=parseInt(d,16);return{red:b,blue:d,green:c}}function dec2hexColor(a,b,c){var d=a.toString(16);var e=b.toString(16);var f=c.toString(16);if(d.length==1)d="0"+d;if(e.length==1)e="0"+e;if(f.length==1)f="0"+f;return d+e+f}function add_polygon(a,b,c,d,e,f,g,h){var i;if(a.length>2)i=new VEShape(VEShapeType.Polygon,a);else i=new VEShape(VEShapeType.Pushpin,a[0]);if(d===false)i.HideIcon();var j=hex2decColor(e);var k=hex2decColor(g);var l=Math.ceil(map_options.outage_box_width/2);var m="";var n="";if(typeof d=="string"){if(d.indexOf("verified")!=-1)m="border-radius:"+l+"px;-webkit-border-radius:"+l+"px;-moz-border-radius:"+l+"px;behavior:url(externals/PIE.htc);";if(d.indexOf("crew_assigned")!=-1)n="<img src='"+map_options.crew_assigned_icon+"' style='margin:6px 0px 0px 6px;' />"}i.SetCustomIcon('<div style="background:#'+g+";border:1px solid #"+e+";width:"+map_options.outage_box_width+"px;height:"+map_options.outage_box_width+"px;"+m+'">'+n+"</div>");i.SetZIndex(1001);i.SetTitle(b);i.SetDescription(c);i.SetLineColor(new VEColor(j.red,j.green,j.blue,f));i.SetFillColor(new VEColor(k.red,k.green,k.blue,h));map.AddShape(i);shape_ids[shape_ids.length]=i.GetID()}function add_outage_polygon(a,b,c,d,e,f,g){var h=0;for(;h<menu_options.outage_levels.length-1;h++){if(d>=menu_options.outage_levels[h].min&&d<=menu_options.outage_levels[h].max)break}var i=menu_options.outage_levels[h].line_color;var j=menu_options.outage_levels[h].line_opacity;var k=menu_options.outage_levels[h].fill_color;var l=menu_options.outage_levels[h].fill_opacity;if(g&&!$("#view-historical").is(":checked"))i=k="008E22";var m="";if(e)m+="verified";if(map_options.crew_assigned_icon&&f)m+="crew_assigned";try{if(!map_options.show_outage_polygons)a.length=1;add_polygon(a,b,c,m,i,j,k,l)}catch(n){}}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=[];for(var f=0;f<d.boundary.length;f++)e[f]=new VELatLong(d.boundary[f][0],d.boundary[f][1]);var g=d.fieldlabel[0]+": "+d.fieldvalue[0];var h="";for(var f=1;f<d.fieldlabel.length;f++){if(d.fieldvalue[f]!=null)h+='<div style="padding-top: 4px;">'+d.fieldlabel[f]+": <strong>"+d.fieldvalue[f]+"</strong></div>"}if(map_options.outage_details=="internal")h+="<br /><a href='#' onClick='detail_view(\""+d.rec_id+"\")'>Details for this outage</a>";else if(map_options.outage_details=="streets")h+="<br /><a href='#' onClick='detail_view(\""+d.rec_id+"\")'>Streets affected by this outage</a>";var i=d.customers_out_now;if(!map_options.update_restored_marks||$("#view-historical").is(":checked"))i+=d.customers_restored;add_outage_polygon(e,g,h,i,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;var b=[];for(var c=0;c<a.boundary.length;c++){b[c]=new VELatLong(parseFloat(a.boundary[c][0]),parseFloat(a.boundary[c][1]))}add_polygon(b,"","",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});map.Resize(d,c)}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 zoom_handler(a){if(a.zoomLevel>map_options.max_zoom)map.SetZoomLevel(map_options.max_zoom);return true}function initialize_map(){var a=new VELatLong(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 VEMap("viewer-map");map.LoadMap(a,b,VEMapStyle[map_options.map_style],map_options.map_fixed);map.AttachEvent("onendzoom",zoom_handler)}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_outages(viewer_data.outages);initialize_trucks()}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 shape_ids=[];$(document).ready(function(){initialize_map();if(menu_options.show){initialize_menu()}resizeDocument();initialize_model(viewer_data.model);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(){detail_view(viewer_data.outages_sql_list)});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()})
