/* FUNCTIONS */

function $_GET(obj = null){
	query = location.search.slice(1);
	partes = query.split('&');
	data = {};
	partes.forEach(function(parte){
		chaveValor = parte.split('=');
		chave = chaveValor[0];
		valor = chaveValor[1];
		data[chave] = valor;
	});
	
	if (obj == null) {
		return data;
	}
	else{
		return data[obj];
	}
}

function imagePreview(input, img){
	if (input.prop("files") && input.prop("files")[0]){
		var r = new FileReader();
		r.onload = function(e){
			img.attr("src", e.target.result);
		}

		r.readAsDataURL(input.prop("files")[0]);
	}
}

function resizeImage(object, dir_img){
	var image = new Image();
	image.addEventListener('load', function(){
		var divs = document.querySelectorAll(object), 
		oWidth = this.width,
		oHeight = this.height;

		for (var i = 0; i < divs.length; i++) {
			var canvas = document.createElement('canvas'),
			ctx = canvas.getContext('2d'),

			sx = 0,
			sy = 0,
			sWidth = oWidth,
			sHeight = oHeight,
	
			dWidth = divs[i].offsetWidth,
			dHeight = divs[i].offsetHeight,

			iWidth = Math.round(sHeight / dHeight * dWidth),
			iHeight = Math.round(sWidth / dWidth * dHeight);

			if (sWidth > iWidth) { // cortar na largura
				sx = parseInt((sWidth - iWidth) / 2);
				sWidth = iWidth;
			} else if (sHeight > iHeight) { // cortar na altura
				sy = parseInt((sHeight - iHeight) / 2);
				sHeight = iHeight;
			}
			
			canvas.width = dWidth;
			canvas.height = dHeight;
			ctx.drawImage(this, sx, sy, sWidth, sHeight, 0, 0, dWidth, dHeight);
			divs[i].appendChild(canvas);
		}
	}, false);
	image.src = dir_img;
}

function convertDate(date){
	var time = "";

	if (date.indexOf(" ") != -1 && date.indexOf(":") != -1) {
		var aux = date.split(" ");
		date = aux[0];
		time = " "+aux[1];
	}

	if (date.indexOf("-") != -1) {
		var aux = date.split("-");

		return aux[2]+"/"+aux[1]+"/"+aux[0]+time;
	}
	else if (date.indexOf("/") != -1) {
		var aux = date.split("/");

		return aux[2]+"-"+aux[1]+"-"+aux[0]+time;
	}
	else{
		return date;
	}
}


function copyText(text, success = null){
	var inputTemp = document.createElement("input");
	inputTemp.value = text;

	document.body.appendChild(inputTemp);

	inputTemp.select();
	document.execCommand("copy");

	document.body.removeChild(inputTemp);

	if (typeof(success) == "function") {
		success.call();
	}
}