/*
	Initalize VARs
*/
var t0 = performance.now();
var t1;

powka2 = [0,0,0,0.0001,0.0002,0.0003,0.0005,0.0007,0.0009,0.0012,0.0015,0.0018,0.0022,0.0025,0.003,0.0034,0.0039,0.0044,0.0049,0.0055,0.0061,0.0067,0.0074,0.0081,0.0088,0.0096,0.0103,0.0112,0.012,0.0129,0.0138,0.0147,0.0157,0.0167,0.0177,0.0188,0.0199,0.021,0.0222,0.0233,0.0246,0.0258,0.0271,0.0284,0.0297,0.0311,0.0325,0.0339,0.0354,0.0369,0.0384,0.04,0.0415,0.0431,0.0448,0.0465,0.0482,0.0499,0.0517,0.0535,0.0553,0.0572,0.0591,0.061,0.0629,0.0649,0.0669,0.069,0.0711,0.0732,0.0753,0.0775,0.0797,0.0819,0.0842,0.0865,0.0888,0.0911,0.0935,0.0959,0.0984,0.1008,0.1034,0.1059,0.1085,0.1111,0.1137,0.1164,0.119,0.1218,0.1245,0.1273,0.1301,0.133,0.1358,0.1387,0.1417,0.1446,0.1476,0.1507,0.1537,0.1568,0.16,0.1631,0.1663,0.1695,0.1727,0.176,0.1793,0.1827,0.186,0.1894,0.1929,0.1963,0.1998,0.2033,0.2069,0.2105,0.2141,0.2177,0.2214,0.2251,0.2288,0.2326,0.2364,0.2402,0.2441,0.248,0.2519,0.2559,0.2599,0.2639,0.2679,0.272,0.2761,0.2802,0.2844,0.2886,0.2928,0.2971,0.3014,0.3057,0.31,0.3144,0.3188,0.3233,0.3278,0.3323,0.3368,0.3414,0.346,0.3506,0.3553,0.36,0.3647,0.3694,0.3742,0.379,0.3839,0.3887,0.3936,0.3986,0.4035,0.4085,0.4136,0.4186,0.4237,0.4288,0.434,0.4392,0.4444,0.4496,0.4549,0.4602,0.4656,0.4709,0.4763,0.4817,0.4872,0.4927,0.4982,0.5038,0.5094,0.515,0.5206,0.5263,0.532,0.5377,0.5435,0.5493,0.5551,0.561,0.5669,0.5728,0.5787,0.5847,0.5907,0.5968,0.6029,0.609,0.6151,0.6213,0.6275,0.6337,0.64,0.6462,0.6526,0.6589,0.6653,0.6717,0.6782,0.6846,0.6911,0.6977,0.7042,0.7108,0.7175,0.7241,0.7308,0.7375,0.7443,0.7511,0.7579,0.7647,0.7716,0.7785,0.7854,0.7924,0.7994,0.8064,0.8135,0.8206,0.8277,0.8348,0.842,0.8492,0.8565,0.8638,0.8711,0.8784,0.8858,0.8932,0.9006,0.908,0.9155,0.9231,0.9306,0.9382,0.9458,0.9534,0.9611,0.9688,0.9766,0.9843,0.9921,1]
powka3 = [0,0,0,0,0,0,0,0,0,0,0,0,0.0001,0.0001,0.0001,0.0002,0.0002,0.0002,0.0003,0.0004,0.0004,0.0005,0.0006,0.0007,0.0008,0.0009,0.001,0.0011,0.0013,0.0014,0.0016,0.0017,0.0019,0.0021,0.0023,0.0025,0.0028,0.003,0.0033,0.0035,0.0038,0.0041,0.0044,0.0047,0.0051,0.0054,0.0058,0.0062,0.0066,0.007,0.0075,0.008,0.0084,0.0089,0.0094,0.01,0.0105,0.0111,0.0117,0.0123,0.013,0.0136,0.0143,0.015,0.0158,0.0165,0.0173,0.0181,0.0189,0.0198,0.0206,0.0215,0.0225,0.0234,0.0244,0.0254,0.0264,0.0275,0.0286,0.0297,0.0308,0.032,0.0332,0.0344,0.0357,0.037,0.0383,0.0397,0.041,0.0425,0.0439,0.0454,0.0469,0.0485,0.05,0.0517,0.0533,0.055,0.0567,0.0585,0.0603,0.0621,0.064,0.0659,0.0678,0.0698,0.0718,0.0738,0.0759,0.0781,0.0802,0.0824,0.0847,0.087,0.0893,0.0917,0.0941,0.0965,0.099,0.1016,0.1042,0.1068,0.1095,0.1122,0.1149,0.1177,0.1206,0.1235,0.1264,0.1294,0.1324,0.1355,0.1387,0.1418,0.1451,0.1483,0.1517,0.155,0.1584,0.1619,0.1654,0.169,0.1726,0.1763,0.18,0.1838,0.1876,0.1915,0.1955,0.1994,0.2035,0.2076,0.2117,0.216,0.2202,0.2245,0.2289,0.2333,0.2378,0.2424,0.247,0.2516,0.2564,0.2611,0.266,0.2709,0.2758,0.2808,0.2859,0.291,0.2962,0.3015,0.3068,0.3122,0.3177,0.3232,0.3287,0.3344,0.3401,0.3458,0.3517,0.3576,0.3635,0.3696,0.3756,0.3818,0.388,0.3943,0.4007,0.4071,0.4136,0.4202,0.4268,0.4335,0.4403,0.4471,0.454,0.461,0.4681,0.4752,0.4824,0.4897,0.497,0.5045,0.512,0.5195,0.5272,0.5349,0.5427,0.5505,0.5585,0.5665,0.5746,0.5827,0.591,0.5993,0.6077,0.6162,0.6248,0.6334,0.6421,0.6509,0.6598,0.6687,0.6778,0.6869,0.6961,0.7054,0.7147,0.7242,0.7337,0.7433,0.753,0.7628,0.7727,0.7826,0.7927,0.8028,0.813,0.8233,0.8337,0.8441,0.8547,0.8653,0.876,0.8869,0.8978,0.9088,0.9198,0.931,0.9423,0.9536,0.9651,0.9766,0.9882,1]
powka4 = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0.0001,0.0001,0.0001,0.0001,0.0001,0.0002,0.0002,0.0002,0.0003,0.0003,0.0003,0.0004,0.0004,0.0005,0.0006,0.0006,0.0007,0.0008,0.0008,0.0009,0.001,0.0011,0.0012,0.0013,0.0014,0.0016,0.0017,0.0018,0.002,0.0021,0.0023,0.0024,0.0026,0.0028,0.003,0.0032,0.0034,0.0037,0.0039,0.0042,0.0044,0.0047,0.005,0.0053,0.0056,0.006,0.0063,0.0067,0.007,0.0074,0.0078,0.0083,0.0087,0.0092,0.0096,0.0101,0.0106,0.0112,0.0117,0.0123,0.0129,0.0135,0.0141,0.0148,0.0155,0.0162,0.0169,0.0176,0.0184,0.0192,0.02,0.0209,0.0218,0.0227,0.0236,0.0246,0.0256,0.0266,0.0276,0.0287,0.0298,0.031,0.0321,0.0333,0.0346,0.0359,0.0372,0.0385,0.0399,0.0413,0.0428,0.0443,0.0458,0.0474,0.049,0.0506,0.0523,0.0541,0.0559,0.0577,0.0596,0.0615,0.0634,0.0654,0.0675,0.0696,0.0718,0.074,0.0762,0.0785,0.0809,0.0833,0.0857,0.0882,0.0908,0.0934,0.0961,0.0988,0.1016,0.1045,0.1074,0.1104,0.1134,0.1165,0.1197,0.1229,0.1262,0.1296,0.133,0.1365,0.14,0.1436,0.1473,0.1511,0.1549,0.1589,0.1628,0.1669,0.171,0.1752,0.1795,0.1839,0.1883,0.1929,0.1975,0.2022,0.2069,0.2118,0.2167,0.2218,0.2269,0.2321,0.2374,0.2428,0.2482,0.2538,0.2594,0.2652,0.271,0.277,0.283,0.2892,0.2954,0.3017,0.3082,0.3147,0.3213,0.3281,0.335,0.3419,0.349,0.3562,0.3634,0.3708,0.3784,0.386,0.3937,0.4016,0.4096,0.4176,0.4259,0.4342,0.4426,0.4512,0.4599,0.4687,0.4777,0.4868,0.496,0.5053,0.5148,0.5244,0.5341,0.544,0.554,0.5641,0.5744,0.5848,0.5954,0.6061,0.6169,0.6279,0.6391,0.6504,0.6618,0.6734,0.6851,0.697,0.709,0.7212,0.7336,0.7461,0.7588,0.7716,0.7846,0.7978,0.8111,0.8246,0.8382,0.8521,0.8661,0.8802,0.8946,0.9091,0.9238,0.9387,0.9537,0.9689,0.9844,1]

const electron        = require('electron');
const win             = electron.remote.getCurrentWindow();
const fs              = require('fs');
const jsmediatags     = require("jsmediatags");
var version           = "FrozeT v.0.6a";

var xddd;             //Timeout variable for menu
trackrn = 0;          //Track right now
barAmount = 62;       //Amount of visualizer bars (52)
play = 1;             //Is music on/off
duration = 0;         //current time of play
total = 0;            //total duration of play
smof = 0              //Is smoothing on
fullmenu = 0          //Is full menu mode on?
CANW = 7              //Canvas width *10%
typeOfVis = 1;        //Type of visualization

var randlist   = [];
var tims       = [];
var amudiot    = [];
var coverslink = [];

inputTag  = document.getElementsByTagName('input')[0];
folderTag = document.getElementById('foldername')
visTag    = document.getElementById("vistag");

ctx = visTag.getContext("2d");

optionss = { 'ffa': 4, 'smo': 0, 'dra': 2, 'sty': 0 , 'typ': 0 };

var audioCtx = new (window.AudioContext || window.webkitAudioContext)();
var audioElement = document.getElementById('xd');
	audioElement.volume  = 0.3;
var audioSrc = audioCtx.createMediaElementSource(audioElement);
var analyser = audioCtx.createAnalyser();
	analyser.fftSize     = 4096
	analyser.minDecibels = -100;
	analyser.maxDecibels = -20;
	audioSrc.connect(analyser);
	audioSrc.connect(audioCtx.destination);
var frequencyData = new Uint8Array(barAmount);


/*
	MISC functions
*/
function hexToBase64(str) {
    return btoa(String.fromCharCode.apply(null, str.replace(/\r|\n/g, "").replace(/([\da-fA-F]{2}) ?/g, "0x$1 ").replace(/ +$/, "").split(" ")));
}
function toDoubleInt(x){
	if(x.toString().split("").length > 1){
		return x;
	} else { 
		return "0" + x; 
	}
}
function cutLastSlash(url) {
    var lastSlashIndex = url.lastIndexOf("/");
    if (lastSlashIndex > url.indexOf("/") + 1) { // if not in http://
        return url.substr(0, lastSlashIndex); // cut it off
    } else {
        return url;
    }
}
function shuffle(a) {
    for (let i = a.length; i; i--) {
        let j = Math.floor(Math.random() * i);
        [a[i - 1], a[j]] = [a[j], a[i - 1]];
    }
}
function getJSON(url, callback) {
    var xhr = new XMLHttpRequest();
    xhr.open('GET', url, true);
    xhr.responseType = 'json';
    xhr.onload = function() {
        var status = xhr.status;
        if (status === 200) {
            callback(null, xhr.response);
        } else {
            callback(status, xhr.response);
        }
    };
    xhr.send();
};

/*
	Options
*/
function chsmo(x,y = 0){
	smof = x;
	for(i=0;i<5;i++){
		document.getElementById("smo" + i).style.borderLeftColor = "#333"
		document.getElementById("smo" + i).style.backgroundImage = "linear-gradient(to right, rgba(123,123,123,0.2), transparent)";
	}
	document.getElementById("smo" + x).style.borderLeftColor = "#8BC34A"
	document.getElementById("smo" + x).style.backgroundImage = "linear-gradient(to right, rgba(139,195,74,0.5), transparent)";
	optionss.smo = x
	if(y == 0){saveOpt()}
}
function chdra(x,y = 0){
	analyser.smoothingTimeConstant = 0.6 + 0.05 * x;
	for(i=0;i<8;i++){
		document.getElementById("dra" + i).style.borderLeftColor = "#333"
		document.getElementById("dra" + i).style.backgroundImage = "linear-gradient(to right, rgba(123,123,123,0.2), transparent)";
	}
	document.getElementById("dra" + x).style.borderLeftColor = "#8BC34A"
	document.getElementById("dra" + x).style.backgroundImage = "linear-gradient(to right, rgba(139,195,74,0.5), transparent)";
	optionss.dra = x
	if(y == 0){saveOpt()}
}
function chffa(x,y = 0){
	analyser.fftSize = Math.pow(2,8+x);
	for(i=0;i<7;i++){
		document.getElementById("ffa" + i).style.borderLeftColor = "#333"
		document.getElementById("ffa" + i).style.backgroundImage = "linear-gradient(to right, rgba(123,123,123,0.2), transparent)";
	}
	document.getElementById("ffa" + x).style.borderLeftColor = "#8BC34A"
	document.getElementById("ffa" + x).style.backgroundImage = "linear-gradient(to right, rgba(139,195,74,0.5), transparent)";
	optionss.ffa = x
	if(y == 0){saveOpt()}
}
function chsty(x,y = 0){
	typeOfVis = x;
	for(i=0;i<4;i++){
		document.getElementById("sty" + i).style.borderLeftColor = "#333"
		document.getElementById("sty" + i).style.backgroundImage = "linear-gradient(to right, rgba(123,123,123,0.2), transparent)";
	}
	document.getElementById("sty" + x).style.borderLeftColor = "#8BC34A"
	document.getElementById("sty" + x).style.backgroundImage = "linear-gradient(to right, rgba(139,195,74,0.5), transparent)";
	optionss.sty = x
	if(y == 0){saveOpt()}
}
function chtyp(x,y = 0){
	switch(x){
        case 0:
            document.getElementsByClassName('back')[0].style.filter = "brightness(50%)";
            break;
        case 1:
            document.getElementsByClassName('back')[0].style.filter = "blur(10px) brightness(80%)";
            break;
        default:
            break;
    }
	for(i=0;i<2;i++){
		document.getElementById("typ" + i).style.borderLeftColor = "#333"
		document.getElementById("typ" + i).style.backgroundImage = "linear-gradient(to right, rgba(123,123,123,0.2), transparent)";
	}
	document.getElementById("typ" + x).style.borderLeftColor = "#8BC34A"
	document.getElementById("typ" + x).style.backgroundImage = "linear-gradient(to right, rgba(139,195,74,0.5), transparent)";
	optionss.typ = x
	if(y == 0){saveOpt()}
}

/*
	Load/Save settings
*/
function saveOpt(){
	fs.writeFile(__dirname + '/optionki.txt', JSON.stringify(optionss), function (err) {
	    if (err){
			console.log(err)
		};
	    console.log('Options saved! ' + __dirname);
	});
}
function readOpt(){
	fs.access(__dirname + '/optionki.txt', (err) => {
		if(err){
			console.log(err)
		} else {
			fs.readFile(__dirname + '/optionki.txt', function(err, data) {
				if (err){
					console.log(err);
				};
				optionss = JSON.parse(data);
				chsmo(optionss.smo,1);
				chdra(optionss.dra,1);
				chffa(optionss.ffa,1);
				chsty(optionss.sty,1);
                chtyp(optionss.typ,1);
				folderOpen("C:|Users|User|Desktop");
			})
		}
	})
}

/*
	Default App Setting: Autorun selected song
*/
if(require('electron').remote.process.argv.length > 1){
	if(require('electron').remote.process.argv[1] != "C:\Users\User\Desktop\ELECTRON\hi\index.js"){
		her(require('electron').remote.process.argv[1]);
	}
}
document.getElementById("debug").innerHTML = require('electron').remote.process.argv.join("<br>")

/*
	Load album covers of tracks
*/
function appCovers(x = 0){
	var img = new Image();
    img.onload = function() {
		document.getElementById("tracko"+x).style.backgroundImage = albumcovers[x];
		if(x+1 < tracks.length){
			appCovers(x+1);
		} else {
            t1 = performance.now();
            console.log(t1-t0)
        }
	};
    img.src = albumcovers[x].substring(5, albumcovers[x].length-2);   
}
function findTime(x){
    new jsmediatags.Reader(tracks[x]).setTagsToRead(["picture"]).read({
		onSuccess: function(tag) {
            if(typeof tag.tags.picture !== 'undefined'){
                k = "";
                for(let g of tag.tags.picture.data){
                    k += String.fromCharCode(g)
                }
                var img = new Image();
                var can = document.createElement('canvas');
                var ctx = can.getContext("2d");
                can.width = 16 //168
                can.height = 16//168
                img.onload = function(){
                    //ctx.drawImage(img,0,0,168,168);
                    ctx.drawImage(img,0,0,16,16);
                    document.getElementById("tracko"+x).style.backgroundImage = "url(" + can.toDataURL(tag.tags.picture.format, 1) + ")";
                    if(x+1 < tracks.length){
                        findTime(x+1);
                    } else {
                        t1 = performance.now();
                        console.log(t1-t0)
                    }
                }
                img.src= "data:" + tag.tags.picture.format + ";base64," + window.btoa(k)
           }
		},
		onError: function(error) {
	       console.log(':(', error.type, error.info);
            if(x+1 < tracks.length){
                findTime(x+1);
            } else {
                t1 = performance.now();
                console.log(t1-t0)
            }
		}}
    );
}

/*
	Open Folder and create tracks inside track list
*/
function folderOpen(x = ""){
	var path, file;
	if(x == ""){
		path = inputTag.files[0].path;
	} else {
		path = x.split("|").join("\\");
	}
	fs.readdir(path, (err, files) => {
		file = files;
		tracks = [];
		albumcovers.length = 0;
		tims.length = 0;
		amudiot.length = 0;
		for(let xd of file){
			if(xd.substring(xd.length-4) == ".mp3"){
				tracks.push(path+"\\"+xd)
				var hh = new Audio(path+"\\"+xd)
				amudiot.push(hh)
			}
		}
		folderTag.innerHTML = tracks.length + " tracks found in: " + path;
		var p = "";
		for(var i=0; i<tracks.length; i++){
            p += "<div class='track-alb' id='tracko"+i+"' onclick='her(tracks["+i+"],"+i+")'><div class='track-tit'>"+tracks[i].replace(/^.*[\\\/]/, '').slice(0, -4).split(" - ")[0]+"</div><div class='track-sun'>"+tracks[i].replace(/^.*[\\\/]/, '').slice(0, -4).split(" - ")[1]+"</div></div>"
		}
		document.getElementsByClassName('trackshow')[0].innerHTML = p;
        findTime(0);     //Find album covers
	});
}

/*
	When song ended
*/
function songisend(){
	if(repets == false){
		if(randomx == true){
			if(trackrn+1 >= tracks.length){
				trackrn=-1;
			}
			her(tracks[randlist[trackrn+1]],randlist[trackrn+1])
		} else {
			if(trackrn+1 >= tracks.length){
				trackrn=-1;
			}
			her(tracks[trackrn+1],trackrn+1)
		}
	} else {
		her(tracks[trackrn],trackrn)
	}
}

/*
	Repeat song
*/
repets = false;
function repet(){
	repets = !repets;
	if(repets == true){
		document.getElementById("repetbut").style.color = "white"
	} else {
		document.getElementById("repetbut").style.color = "#888"
	}
}

/*
	Next/Prev Song
*/
function changesng(x){
	if(x == 0){
		trackrn--
		if(trackrn < 0){
			trackrn = tracks.length-1
		}
		her(tracks[trackrn],trackrn)
	} else {
		trackrn++
		if(trackrn >= tracks.length){
			trackrn = 0
		}
		her(tracks[trackrn],trackrn)
	}
}

/*
	Randomize songs
*/
randomx = false;
function randomize(){
	randomx = !randomx;
	if(randomx == true){
		document.getElementById("randbut").style.color = "white"
		randlist.length = 0;
		for(var i=0; i < tracks.length; i++){
			randlist[i] = i;
		}
		shuffle(randlist)
	} else {
		document.getElementById("randbut").style.color = "#888"
	}
}

/*
	Redraw canvas
*/
function redraw(){
	visTagw = visTag.width = (window.innerWidth/10)*CANW;
	visTagh = visTag.height = ((window.innerHeight-40)/2.5);
}
window.onresize = function(){
  redraw();
}
redraw()
function setIt(x){
    document.getElementsByClassName("back")[0].style.backgroundImage = x
}
function getAlbum(x){
    if(tracks[x] === undefined){
        return ""
    }
    new jsmediatags.Reader(tracks[x]).setTagsToRead(["picture"]).read({
		onSuccess: function(tag) {
            if(typeof tag.tags.picture !== 'undefined'){
                k = "";
                for(let g of tag.tags.picture.data){
                    k += String.fromCharCode(g)
                }
                var img = new Image();
                var can = document.createElement('canvas');
                var ctx = can.getContext("2d");
                can.width = 1920
                can.height = 1080
                img.onload = function(){
                    can.width = img.width;
                    can.height = img.height;
                    ctx.drawImage(img,0,0,img.width,img.height);
                    let meme = "url(" + can.toDataURL(tag.tags.picture.format, 1) + ")";
                    setIt(meme);
                }
                img.src= "data:" + tag.tags.picture.format + ";base64," + window.btoa(k)
           }
		},
		onError: function(error) {
	       console.log(':(', error.type, error.info);
		}}
    );
}
function retu(x){
    if(!x){
        return "documen";
    }
    return x.style.backgroundImage;
}
function her(x,y){
	document.getElementById("xd").src = x;
	document.getElementById("title").innerHTML = x.replace(/^.*[\\\/]/, '').slice(0, -4).split(" - ")[0];
	document.getElementById("subtitle").innerHTML = x.replace(/^.*[\\\/]/, '').slice(0, -4).split(" - ")[1]
	siz = 60;
	trackrn = y;
	document.getElementById("title").style.fontSize = siz + "px"
	document.getElementById("htmltit").innerHTML = x.replace(/^.*[\\\/]/, '').slice(0, -4).split(" - ")[0] + " - " + x.replace(/^.*[\\\/]/, '').slice(0, -4).split(" - ")[1] +  " | FrozeT"
	while((document.getElementById("title").getBoundingClientRect().height + document.getElementById("subtitle").getBoundingClientRect().height) > 128){
		siz--
		document.getElementById("title").style.fontSize = siz + "px"
	}
	if(siz >= 30){
		document.getElementById("subtitle").style.fontSize = Math.floor(siz/2) + "px"
	} else {
		document.getElementById("subtitle").style.fontSize = "22px"
	}
	document.getElementById("album").style.backgroundImage = retu(document.getElementById("tracko"+trackrn))
    getAlbum(trackrn)
	//document.getElementsByClassName("back")[0].style.backgroundImage = retu(document.getElementById("tracko"+trackrn))
	play = 1;
	duration = document.getElementById("xd").currentTime
	playit() 
	total = document.getElementById('xd').duration
}


/*
	Append onLoadedMetadata event to player
*/
document.getElementById('xd').onloadedmetadata = function() {
	total = document.getElementById('xd').duration;
};


/*
	Step even of visualizer
*/
function hh(){
	analyser.getByteFrequencyData(frequencyData);
	kup = (visTagw-(barAmount-1)*5)/barAmount
	ctx.beginPath();
	ctx.clearRect(0,0,visTagw,visTagh)
	ctx.fill();
	ctx.closePath();
	if(smof == 1){
		frequencyData[0] = Math.floor(frequencyData[0] + frequencyData[1])/2
		for(j=1;j<frequencyData.length-1;j++){
			frequencyData[j] = Math.floor(frequencyData[j-1] + frequencyData[j+1])/2
		} //SMOOTHING
		frequencyData[frequencyData.length-1] = Math.floor(frequencyData[frequencyData.length-1] + frequencyData[frequencyData.length-2])/2
	} else 
	if(smof == 2){
		frequencyData[0] = Math.floor(frequencyData[0] + frequencyData[1] + frequencyData[2])/3
		frequencyData[1] = Math.floor(frequencyData[0] + frequencyData[1] + frequencyData[2] + frequencyData[3])/4
		for(j=2;j<frequencyData.length-2;j++){
			frequencyData[j] = Math.floor(frequencyData[j-1] + frequencyData[j+1] + frequencyData[j-2] + frequencyData[j+2])/4
		} //SMOOTHING
		frequencyData[frequencyData.length-2] = Math.floor(frequencyData[frequencyData.length-1] + frequencyData[frequencyData.length-2] + frequencyData[frequencyData.length-3] + frequencyData[frequencyData.length-4])/4
		frequencyData[frequencyData.length-1] = Math.floor(frequencyData[frequencyData.length-1] + frequencyData[frequencyData.length-2] + frequencyData[frequencyData.length-3])/3
	}
	if(smof == 3){
		frequencyData[0] = Math.floor(frequencyData[0] + frequencyData[1] + frequencyData[2] + frequencyData[3])/4
		frequencyData[1] = Math.floor(frequencyData[0] + frequencyData[1] + frequencyData[2] + frequencyData[3] + frequencyData[4])/5
		frequencyData[2] = Math.floor(frequencyData[0] + frequencyData[1] + frequencyData[2] + frequencyData[3] + frequencyData[4] + frequencyData[5])/6
		for(j=3;j<frequencyData.length-3;j++){
			frequencyData[j] = Math.floor(frequencyData[j-1] + frequencyData[j+1] + frequencyData[j-2] + frequencyData[j+2] + frequencyData[j-3] + frequencyData[j+3])/6
		} //SMOOTHING
		frequencyData[frequencyData.length-3] = Math.floor(frequencyData[frequencyData.length-1] + frequencyData[frequencyData.length-2] + frequencyData[frequencyData.length-3] + frequencyData[frequencyData.length-4] + frequencyData[frequencyData.length-5] + frequencyData[frequencyData.length-6])/6
		frequencyData[frequencyData.length-2] = Math.floor(frequencyData[frequencyData.length-1] + frequencyData[frequencyData.length-2] + frequencyData[frequencyData.length-3] + frequencyData[frequencyData.length-4] + frequencyData[frequencyData.length-5])/5
		frequencyData[frequencyData.length-1] = Math.floor(frequencyData[frequencyData.length-1] + frequencyData[frequencyData.length-2] + frequencyData[frequencyData.length-3] + frequencyData[frequencyData.length-4])/4
	}
	if(smof == 4){
		frequencyData[0] = Math.floor(frequencyData[0] + frequencyData[1] + frequencyData[2] + frequencyData[3] + frequencyData[4])/5
		frequencyData[1] = Math.floor(frequencyData[0] + frequencyData[1] + frequencyData[2] + frequencyData[3] + frequencyData[4] + frequencyData[5])/6
		frequencyData[2] = Math.floor(frequencyData[0] + frequencyData[1] + frequencyData[2] + frequencyData[3] + frequencyData[4] + frequencyData[5] + frequencyData[6])/7
		frequencyData[3] = Math.floor(frequencyData[0] + frequencyData[1] + frequencyData[2] + frequencyData[3] + frequencyData[4] + frequencyData[5] + frequencyData[6] + frequencyData[7])/8
		for(j=4;j<frequencyData.length-4;j++){
			frequencyData[j] = Math.floor(frequencyData[j-1] + frequencyData[j+1] + frequencyData[j-2] + frequencyData[j+2] + frequencyData[j-3] + frequencyData[j+3] + frequencyData[j-4] + frequencyData[j+4])/8
		} //SMOOTHING
		frequencyData[frequencyData.length-4] = Math.floor(frequencyData[frequencyData.length-1] + frequencyData[frequencyData.length-2] + frequencyData[frequencyData.length-3] + frequencyData[frequencyData.length-4] + frequencyData[frequencyData.length-5] + frequencyData[frequencyData.length-6] + frequencyData[frequencyData.length-7] + frequencyData[frequencyData.length-8])/8
		frequencyData[frequencyData.length-3] = Math.floor(frequencyData[frequencyData.length-1] + frequencyData[frequencyData.length-2] + frequencyData[frequencyData.length-3] + frequencyData[frequencyData.length-4] + frequencyData[frequencyData.length-5] + frequencyData[frequencyData.length-6] + frequencyData[frequencyData.length-7])/7
		frequencyData[frequencyData.length-2] = Math.floor(frequencyData[frequencyData.length-1] + frequencyData[frequencyData.length-2] + frequencyData[frequencyData.length-3] + frequencyData[frequencyData.length-4] + frequencyData[frequencyData.length-5] + frequencyData[frequencyData.length-6])/6
		frequencyData[frequencyData.length-1] = Math.floor(frequencyData[frequencyData.length-1] + frequencyData[frequencyData.length-2] + frequencyData[frequencyData.length-3] + frequencyData[frequencyData.length-4] + frequencyData[frequencyData.length-5])/5
	}
	for(i=0;i<frequencyData.length;i++){
		ctx.beginPath();
		ctx.fillStyle = "white"
		if(typeOfVis == 0){	
			ctx.fillRect(kup*i+(i*5), visTagh-1-(frequencyData[i]/255)*visTagh, kup, (frequencyData[i]/255)*visTagh+1)
		} else if(typeOfVis == 1){
			ctx.fillRect(kup*i+(i*5), visTagh-1-powka2[Math.floor(frequencyData[i])]*visTagh, kup, powka2[Math.floor(frequencyData[i])]*visTagh+1)
            //ctx.fillRect(kup*i+(i*5), visTagh-1-(Math.pow(frequencyData[i],2)/65025)*visTagh, kup, (Math.pow(frequencyData[i],2)/65025)*visTagh+1)
		} else if(typeOfVis == 2){
            ctx.fillRect(kup*i+(i*5), visTagh-1-powka3[Math.floor(frequencyData[i])]*visTagh, kup, powka3[Math.floor(frequencyData[i])]*visTagh+1)
            //ctx.fillRect(kup*i+(i*5), visTagh-1-(Math.pow(frequencyData[i],3)/16581375)*visTagh, kup, (Math.pow(frequencyData[i],3)/16581375)*visTagh+1)
        } else if(typeOfVis == 3){
            ctx.fillRect(kup*i+(i*5), visTagh-1-powka4[Math.floor(frequencyData[i])]*visTagh, kup, powka4[Math.floor(frequencyData[i])]*visTagh+1)
            //ctx.fillRect(kup*i+(i*5), visTagh-1-(Math.pow(frequencyData[i],3)/16581375)*visTagh, kup, (Math.pow(frequencyData[i],3)/16581375)*visTagh+1)
        }
		ctx.closePath();
	}
	window.requestAnimationFrame(hh);
}
window.requestAnimationFrame(hh);

/*
    Check for updates
*/
getJSON('http://holo.frozent.pl/info.json',
function(err, data) {
	if(err){
		document.getElementById("foo").innerHTML = version;
		return;
	}
    if(data.ver != version){
		version = "<span style='color:green;font-size:10px;'>Update found: v." + data.ver + "</span> " + version
		document.getElementById("foo").innerHTML = version;
    };
});

/*
	Change state of play button
*/
function playit(){
	if(play == 1){
		play = 0;
		document.getElementById("plaz").className = "fa fa-pause button-play"
		document.getElementById("xd").play();
	} else { 
		play = 1;
		document.getElementById("plaz").className = "fa fa-play button-play"
		document.getElementById("xd").pause();
	}
}


/*
	Show bottom menu
*/
function showmenu(x = 1){
	if(fullmenu == 0){
		if(x == 1){
			document.getElementsByClassName("bottombar")[0].style.transform = "translateY(-100px)"
		} else
		if(x == 2){
			document.getElementsByClassName("bottombar")[0].style.transform = "translateY(-100vh)"
			document.getElementById("menus").className = "fa fa-angle-double-down"
			fullmenu = 1;
		} else {
			document.getElementsByClassName("bottombar")[0].style.transform = "translateY(0)"
		}
		clearTimeout(xddd);
		xddd = setTimeout(function(){showmenu(0)},2000)
	} else {
		if(x == 2){
			document.getElementsByClassName("bottombar")[0].style.transform = "translateY(0)"
			document.getElementById("menus").className = "fa fa-angle-double-up"
			clearTimeout(xddd);
			fullmenu = 0
		} else {
			clearTimeout(xddd);
			xddd = setTimeout(function(){showmenu(0)},2000)
		}
	}
}


/*
	Display time of song
*/
function tim(){
	duration = document.getElementById("xd").currentTime
	if(isNaN(total)){
		total = 0
	}
	document.getElementsByClassName("durat")[0].innerHTML = Math.floor(duration/60) + ":" + toDoubleInt(Math.floor(duration%60)) + " / " + Math.floor(total/60) + ":" + toDoubleInt(Math.floor(total%60))
	document.getElementById("timelino").style.width = (duration/total)*100 + "%"
	//win.setProgressBar(Math.floor((duration/total)*100)/100,{'mode':'normal'});
}

/*
	Show tabs
*/
function showtab(x){
	elemt = document.getElementsByClassName("butos");
	for(var i=0;i<elemt.length;i++){
		elemt[i].style.borderLeftColor = "#333"
		elemt[i].style.color = "#333"
	}
	elemt[x].style.borderLeftColor = "white"
	elemt[x].style.color = "white"
	if(x == 0){
		document.getElementById("fb1").style.display = "block"
		document.getElementById("fb2").style.display = "none"
	} else 
	if(x == 1){ 
		document.getElementById("fb1").style.display = "none"
		document.getElementById("fb2").style.display = "block"
	}
}



window.onload = readOpt;
//TYLKO DO TESTOW TO CO NA DOLE
var stats = new Stats();
    stats.showPanel(0);
document.body.appendChild(stats.dom);
function animate() {
	stats.begin();
	stats.end();
	requestAnimationFrame(animate);
}
requestAnimationFrame(animate);

fullmenu = 0
showmenu(2)