$(document).ready(function(){bindGarbageFunctions()});function bindGarbageFunctions(){$("#garbageArea").change(function(){getRegion($(this).val());if(typeof $("#garbageSearch")!=="undefined"){$("#garbageSearch").val("")}});$("#garbageSearch").autocomplete({delay:100,minLength:0,appendTo:"#garbageSearchWrapper",source:function(req,response){let request=new Request;let data="phrase="+$("#garbageSearch").val();request.postPlugin("garbage_collection","SEARCH",data,function(data){response(data.regions)})},select:function(event,ui){event.preventDefault();getRegion(ui.item.id);$(this).val(ui.item.phrase);$("#garbageArea").val(ui.item.id)},create:function(){$(".ui-helper-hidden-accessible").hide();$(this).data("ui-autocomplete")._renderItem=function(ul,item){return $("
").append(""+item.phrase+"
").appendTo(ul)}}});$("#getGarbagePdf").click(function(){$("#getGarbagePdf").attr("disabled",true);let request=new Request;let region=$("#garbageArea").val();if(region!==""){let data="region="+region;request.postPlugin("garbage_collection","PDF",data,function(response){if(response.pdf==null||!response.pdf){$("#garbageAlerts").html('Nie można pobrać pliku pdf
');$("html, body").animate({scrollTop:$("body").offset().top},500)}else{window.open(response.pdf,"_blank")}$("#getGarbagePdf").attr("disabled",false)})}else{$("#garbageAlerts").html('Nie wybrano obszaru
');$("#getGarbagePdf").attr("disabled",false)}})}function getRegion(region){$("#getGarbagePdf").attr("disabled",true);$("#garbageAlerts").html("");let request=new Request;let data="region="+region;request.postPlugin("garbage_collection","REGION",data,function(response){if(response.region==null){$("#garbageAlerts").html('Nie znaleziono obszaru
');$("#garbageContent").html("")}else{let region=response.region;let today=new Date;let dd=today.getDate();let mm=today.getMonth();let yyyy=today.getFullYear();if(dd<10){dd="0"+dd}if(mm<10){mm="0"+mm}let todayTime=new Date(yyyy,mm,dd).getTime();let html="";if(region.description.length>0){html+='';html+='
'+region.description+"
";html+="
"}if(typeof region.disposals!=="undefined"&®ion.disposals.length>0){$.each(region.disposals,function(){html+='';html+='
'+this.name+"
";if(this.current_date!==null){html+='
Data wywozu: '+getDateText(todayTime,this.current_date)+"
";if(this.next_date!==null){html+='
Następny wywóz: '+getDateText(todayTime,this.next_date)+"
"}html+="
"}html+="
"});$("#getGarbagePdf").attr("disabled",false);$("#garbageContent").html(html)}else{$("#garbageAlerts").html('Ten obszar nie ma jeszcze przypisanych informacji o wywozie odpadów.
');$("#garbageContent").html("")}}})}function getDateText(today,date){let nextDate=date.split("-");let nextDate2=new Date(nextDate[0],nextDate[1]-1,nextDate[2]).getTime();let html=nextDate[2]+"-"+nextDate[1];let days=Math.round((nextDate2-today)/864e5);if(days>=0){switch(days){case 0:html+=" (dzisiaj)";break;case 1:html+=" (jutro)";break;default:html+=" (za "+days+" dni)";break}}return html}