angular.module("mystrom.scheduler.directives",[]).directive("scheduler",["$window","$timeout","$translate","$ionicScrollDelegate",function(t,r,a,e){return{restrict:"E",scope:{data:"=",redraw:"=",onText:"=",offText:"=",onColor:"=",offColor:"="},link:function(a,n){function o(t,r){var o=n[0].parentElement.clientWidth,i=o,l=o>=340?35:30,d=50,c=o/2-d-l,s=o/2-d;t=t||"grey",r=r||"black";var f,h=d3.layout.pie().sort(null),p=d3.svg.arc().innerRadius(c).outerRadius(s),u=d3.svg.arc().innerRadius(c-30).outerRadius(s+50),g=d3.select(n[0]).append("svg").attr("width",o).attr("height",i).append("g").attr("transform","translate("+o/2+","+i/2+")"),m=function(r){a.data[r]=f==t?0:1;var e=d3.select(g.select("g").selectAll("path")[0][r]);e.attr("fill",f)},v=48,x=function(t){for(var r=[],a=0;t>a;a++)r.push(1);return r},w=x(v);g.append("g").datum(w).selectAll("path").data(h).enter().append("path").attr("fill",function(e,n){return 1==a.data[n]?r:t}).attr("stroke","#fff").attr("stroke-width",1).attr("idx",function(t,r){return r}).attr("d",p);for(var y=0;v>y;y+=2)g.append("line").attr("x1",0).attr("y1",s).attr("x2",0).attr("y2",s+2).attr("stroke","#666666").attr("stroke-width",2).attr("stroke-linecap","round").attr("transform","rotate("+7.5*y+")"),g.append("line").attr("x1",0).attr("y1",c).attr("x2",0).attr("y2",s).attr("stroke","white").attr("stroke-width",2).attr("transform","rotate("+7.5*y+")"),g.append("line").attr("x1",Math.sin(Math.PI/24)*c).attr("y1",Math.cos(Math.PI/24)*c).attr("x2",Math.sin(Math.PI/24)*s).attr("y2",Math.cos(Math.PI/24)*s).attr("stroke","#e6e6e6").attr("stroke-width",1).attr("transform","rotate("+7.5*y+")");for(var y=0;8>y;y++){var k=[0,-45,-90,-135,-180,135,90,45],$=[0,0,0,0,0,0,0,0],C=[0,5,5,5,10,5,5,5],M=3*y+":00";3>y&&(M="0"+M);var b="start";0===y||4==y?b="middle":y>4&&(b="end"),g.append("g").attr("transform","rotate("+45*y+")").append("text").attr("x",0).attr("y",-(s+10)).attr("fill","#333333").attr("text-anchor",b).attr("transform","rotate("+k[y]+",0,"+-(s+10)+") translate("+$[y]+","+C[y]+")").text(M)}var T=ionic.Platform.isAndroid()?20:15;g.append("image").attr("x",0).attr("y",c-o/(T+5)).attr("width",o/T).attr("height",i/T).attr("xlink:href","img/noon.svg").attr("transform","translate("+-(o/T)/2+","+-(i/T)/2+")"),g.append("image").attr("x",0).attr("y",-(c-o/(T+5))).attr("width",o/T).attr("height",i/T).attr("xlink:href","img/midnight.svg").attr("transform","translate("+-(o/T)/2+","+-(i/T)/2+")"),g.append("image").attr("x",c-o/(T+5)).attr("y",0).attr("width",o/T).attr("height",i/T).attr("xlink:href","img/6oclock.svg").attr("transform","translate("+-(o/T)/2+","+-(i/T)/2+")"),g.append("image").attr("x",-(c-o/(T+5))).attr("y",0).attr("width",o/T).attr("height",i/T).attr("xlink:href","img/6oclock.svg").attr("transform","translate("+-(o/T)/2+","+-(i/T)/2+")");var P=g.append("g").attr("transform","translate(-37,0)");P.append("rect").attr("x",-s).attr("y",c+20).attr("width",8).attr("height",8).attr("fill",r),P.append("text").attr("x",-s+15).attr("y",c+28).attr("fill","#333333").text(a.onText),P.append("rect").attr("x",-s).attr("y",c+40).attr("width",8).attr("height",8).attr("fill",t),P.append("text").attr("x",-s+15).attr("y",c+48).attr("fill","#333333").text(a.offText),g.append("g").datum(w).selectAll("path").data(h).enter().append("path").attr("opacity",0).attr("idx",function(t,r){return r}).attr("d",u).on("touchstart",function(a,n){e.getScrollView().options.scrollingY=!1,f=d3.select(g.select("g").selectAll("path")[0][n]).attr("fill"),f=f==r?t:r,m(n)}).on("touchmove",function(){var t=d3.event.changedTouches[0],r=d3.select(document.elementFromPoint(t.clientX,t.clientY)).attr("idx");null!==r&&m(r)}).on("touchend",function(){e.getScrollView().options.scrollingY=!0,a.$$phase||a.$apply()})}function i(){$(n).select("svg").children().remove(),o(a.onColor,a.offColor)}var l=function(){var t=0;return function(r,a){clearTimeout(t),t=setTimeout(r,a)}}();angular.element(t).bind("resize",function(){l(function(){i()},500)}),a.$watch("redraw",function(){r(function(){i()},100)}),a.$watch("data",function(){i()}),o(a.onColor,a.offColor)}}}]).directive("removeHeaderLine",function(){return{restrict:"E",link:function(t){t.$on("$ionicView.enter",function(){$('[nav-bar="entering"] ion-header-bar').addClass("no-line")}),t.$on("$ionicView.beforeLeave",function(){$('[nav-bar="active"] ion-header-bar').removeClass("no-line")})}}});