// JavaScript Document
(function($){$.fn.dispatchLogs=function(options){function prePad(input_string,pad_with,final_length){var ret_str="";for(var i=0;i<(final_length-input_string.length);i++){ret_str+=pad_with;}
ret_str+=input_string;return ret_str;}

var settings={'servicePath':'http://www.rrrdc.com/dispatchlogs/Service.asmx','showIncidentNumber':false,'showDuration':false,'venue':''};return this.each(function(){var $this=$(this);if(options){$.extend(settings,options);}
$this.empty();var content=$("<div/>");if(settings.venue==''){$this.append("<p/>").find("p:last").append("<p>Location: <select id=\"ddlVenue\"/></p>");var select=$this.find("#ddlVenue");select.prepend("<option value=\"\">-- All --</option>");$.ajax({type:"GET",url:settings.servicePath+"/GetVenueJSON",dataType:"jsonp",success:function(data){var list=data;if(list!=null){$(list.Venue).each(function(index,Venue){select.append("<option\>");select.find("option:last").attr("value",Venue.VenueName).text(Venue.VenueDescription);});}

select.css("width","150px");select.change(function(){loadTable($.datepicker.formatDate("mm/dd/yy",$("#divDate").datepicker("getDate")));});}});}
$this.append("<div id=\"divDate\" style=\display: inline;\"/><br/><div id=\"divLogTable\"/><p id=\"noresults\" style=\"display: none; font-weight: bold\">Your search yielded no results</p>");$this.find("#divDate").datepicker({maxDate:"0d",onSelect:function(dateText){loadTable(dateText);}});$(".ui-datepicker").css("display","block").css("visibility","hidden").css("height","0px");function loadTable(date){var venue=settings.venue;if(venue==''&&$("#ddlVenue").length>0){venue=$("#ddlVenue").val();}

if($("#tblLogTable").length==0){$("#divLogTable").append("<table id=\"tblLogTable\" cellpadding=\"0\" cellspacing=\"0\"/>");}
$("#tblLogTable").hide();
$("#noresults").hide();
$("#tblLogTable thead").remove();
if($("#tblLogTable thead").length==0){$("#tblLogTable").prepend("<thead/>");
$("#tblLogTable thead").append("<tr/>");$("#tblLogTable thead tr:last").append("<td/>")
.find("td:last").append("Date/Time");
$("#tblLogTable thead tr:last").append("<td/>")
.find("td:last").append("Address");
if(venue==''){$("#tblLogTable thead tr:last").append("<td/>")
.find("td:last").append("Location");}
$("#tblLogTable thead tr:last").append("<td/>")
.find("td:last").append("Call Type");
$("#tblLogTable thead tr:last").append("<td/>")
//.find("td:last").append("Description");
if(settings.showIncidentNumber){$("#tblLogTable thead tr:last").append("<td/>")
.find("td:last").append("Incident Number");}
if(settings.showDuration){$("#tblLogTable thead tr:last").append("<td/>")
.find("td:last").append("Duration");}}
if($("#tblLogTable tbody").length==0){$("#tblLogTable").append("<tbody/>");}
$("#tblLogTable tbody").empty();

$.ajax({type:"GET",data:"Venue="+venue+"&StartDate="+date+"&EndDate="+date,url:settings.servicePath+"/GetDispatchLogJSON",dataType:"jsonp",success:function(data){var list=data;
if(list!=null){$(list.DispatchLog).each(function(index,DispatchLog){var address="";
address+=DispatchLog.Block+" ";
if(DispatchLog.Block!=undefined&&DispatchLog.Block!=""){address+="BLK ";}
address+=DispatchLog.AdditionalInfo+" ";
address+=DispatchLog.StreetPrefix+" ";
address+=DispatchLog.StreetPreType+" ";
address+=DispatchLog.StreetName+" ";
address+=DispatchLog.StreetType+" ";
address+=DispatchLog.StreetSuffix;
address=address.replace(/  /gi," ");

$("#tblLogTable tbody").append("<tr/>");
$("#tblLogTable tbody tr:last").append("<td/>")
.find("td:last").append(DispatchLog.DateString);
$("#tblLogTable tbody tr:last").append("<td/>")
.find("td:last").append(address);
if(venue==''&&$("#ddlVenue").length>0){$("#tblLogTable tbody tr:last").append("<td/>")
.find("td:last").append(DispatchLog.VenueDescription);}
$("#tblLogTable tbody tr:last").append("<td/>")
.find("td:last").append(DispatchLog.CallType);
$("#tblLogTable tbody tr:last").append("<td/>")
//.find("td:last").append(DispatchLog.NatureOfCall);
if(settings.showIncidentNumber){$("#tblLogTable tbody tr:last").append("<td/>")
.find("td:last").append(DispatchLog.IncidentNumber);}
if(settings.showDuration){var duration="";var duration_seconds=parseInt(DispatchLog.Duration);
if(!isNaN(duration_seconds)){duration=prePad(Math.floor((duration_seconds/3600)).toString(),"0",2)+":";duration+=prePad(Math.floor(((duration_seconds%3600)/60)).toString(),"0",2)+":";duration+=prePad(Math.floor((duration_seconds%60)).toString(),"0",2);}
$("#tblLogTable tbody tr:last").append("<td/>")
.find("td:last").append(duration);}});}
$("#tblLogTable tbody tr:even").addClass("even");$("#tblLogTable tbody tr:odd").addClass("odd");if($("#tblLogTable tbody tr").length==0){$("#tblLogTable").hide();$("#noresults").show();}
else{$("#tblLogTable").show();$("#noresults").hide();}}});}
loadTable($.datepicker.formatDate("mm/dd/yy",$("#divDate").datepicker("getDate")));});};})(jQuery);
