/* Color changer by PRUSA3D  // Licence: CC BY-NC-ND ( https://creativecommons.org/licenses/by-nc-nd/2.0/ ) // Prusa Research s.r.o. // prusa3d.com */
var filepath;var LineNumber=1;var slic3r=5;var data;var text;var Layers=new Array();var LayersH=new Array();var M600s=new Array();var num=0;var ChangeFlag=1;var ObjectHeight="";var FilamentChanges=new Array();var FilamentChange="M600";var fs=require("fs");function init(){FilamentChanges=new Array();Layers=[];M600s=[];LineNumber=1;num=0;ChangeFlag=1;ObjectHeight="";$(".buttonky").each(function(){$(this).remove()})}function readthatshit(){var a=require("fs");data=a.readFileSync(filepath).toString().split("\n");analyze();for(var b=0;b<M600s.length;b++){addChange();$("#spinner"+b).val(M600s[b][1]);$("#slider"+b).slider("value",LayersH.indexOf(M600s[b][1]))}}function analyze(){Layers=[];LayersH=[];M600s=[];ChangeFlag=1;slic3r=5;for(var a=0;a<10;a++){if(data[a].indexOf("Slic3r")>=0){slic3r=1}else{if(data[a].indexOf("Simplify")>=0){slic3r=2}}}if(slic3r==5){alert("WARNING! Unrecognized slicer engine, application may not work as described!")}var a=4;LineNumber=1;data.forEach(function(b){if(b.indexOf("G1 Z")>=0){ObjectHeight="";for(a=4;b[a]!=" ";a++){ObjectHeight+=b[a]}if(slic3r==1){if(Layers.length==0){Layers.push([(LineNumber+1),ObjectHeight])}else{if(Layers[Layers.length-1][1]!=ObjectHeight){Layers.push([(LineNumber+1),ObjectHeight])}}}else{if(Layers.length==0){Layers.push([(LineNumber),ObjectHeight])}else{if(Layers[Layers.length-1][1]!=ObjectHeight){Layers.push([(LineNumber),ObjectHeight])}}}LayersH.push(ObjectHeight)}if(b.indexOf("M600")>=0){M600s.push([LineNumber,ObjectHeight])}LineNumber++})}function sortArray(){var c=[0,0];for(var b=FilamentChanges.length-2;b>=0;b--){for(var a=0;a<=b;a++){if(FilamentChanges[a][0]>FilamentChanges[a+1][0]){c[0]=FilamentChanges[a][0];c[1]=FilamentChanges[a][1];FilamentChanges[a][1]=FilamentChanges[a+1][1];FilamentChanges[a][0]=FilamentChanges[a+1][0];FilamentChanges[a+1][1]=c[1];FilamentChanges[a+1][0]=c[0]}}}}function WriteM600(){var a=require("fs");deleteM600s();analyze();FilamentChanges=[];$(".sliders").each(function(){for(var c=0;c<Layers.length;c++){if((LayersH[$(this).slider("value")]==Layers[c][1])){FilamentChanges.push([Layers[c][0],Layers[c][1]])}}});sortArray();for(var b=FilamentChanges.length-1;b>=0;b--){data.splice(FilamentChanges[b][0],0,FilamentChange);text=data.join("\n")}a.writeFile(filepath,text,function(c){if(c){alert(c)}});ChangeFlag=0;$("#dev").html("DONE!");setTimeout(function(){$("#dev").html("");init();readthatshit()},2000)}function deleteM600s(){for(var a=M600s.length-1;a>=0;a--){data.splice(M600s[a][0]-1,1);var b=data.join("\n")}M600s=[]}function iterateSlider(b,a){if(a==1){if($("#slider"+b).slider("value")==LayersH.length-1){}else{$("#slider"+b).slider("value",$("#slider"+b).slider("value")+1)}}else{if($("#slider"+b).slider("value")==0){}else{$("#slider"+b).slider("value",$("#slider"+b).slider("value")-1)}}}function check(e){var c=5;var a=2;var d=parseFloat($("#spinner"+e).val());if(isNaN(d)){d=LayersH[0]}else{if(d>=LayersH[LayersH.length-1]){d=LayersH.length-1}else{if(d<LayersH[0]){d=LayersH[0]}else{var b=0;while((a<c)&&(b<LayersH.length-1)){c=Math.abs(LayersH[b]-d);a=Math.abs(LayersH[b+1]-d);b++}}}}$("#spinner"+e).val(LayersH[b]);$("#slider"+e).slider("value",b)}function addChange(){$("#changeDrop").after('<div id="change'+num+'" class="buttonky"><br>                          <button onclick="$(\'#change'+num+'\').remove();"><span class="glyphicon glyphicon-remove" aria-hidden="true"></span></button>                          <input class="spinners" id="spinner'+num+'" name="spinner'+num+'" onchange="check('+num+')" >                          <div class="sliders slider" id="slider'+num+'"></div>                          <button onclick="iterateSlider('+num+', 1);"><span class="glyphicon glyphicon-plus" aria-hidden="true"></span></button>                          <button onclick="iterateSlider('+num+', 0);"><span class="glyphicon glyphicon-minus" aria-hidden="true"></span></button>                    </div>');$("#slider"+num).slider({min:0,max:LayersH.length-1,value:LayersH[0]}).slider("pips",{first:"pip",last:"pip"}).on("slidechange",function(d,c){for(var b=0;b<=num;b++){$("#spinner"+b).val(LayersH[$("#slider"+b).slider("value")])}});for(var a=0;a<=num;a++){$("#spinner"+a).val(LayersH[$("#slider"+a).slider("value")])}num=num+1};