// Mostra ou oculta as listas em segundo-nível //
$(".accordion-lista .glyphicon").on("click", function (e) {
    if ($(this).next().next().is(":visible")) {
        $(".lista-accordion").slideUp();
        $(".glyphicon").removeClass("glyphicon-minus").addClass("glyphicon-plus");
    } else {
        $(".lista-accordion").slideUp();
        $(this).next().next().slideDown();
        $(".glyphicon").removeClass("glyphicon-minus").addClass("glyphicon-plus");
        $(this).removeClass("glyphicon-plus").addClass("glyphicon-minus");
    }
});
/////////////////////////////////////////////////

/* Muda as abas da grid */
$(".tabs-grid-footer li").on("click", function() {
    var idx = $(this).index() + 1;

    $(".tabs-grid-footer li").removeClass("active-tab");
    $(this).addClass("active-tab");
    $("[class^='tab-grid-footer-']").hide();
    $(".tab-grid-footer-" + idx).show();
});
//////////////////////////

// jQuery ColorPicker //
$(".btn-color").on("click", function () {
    var btn = $(this);
    var cvId = $("span canvas", $(this)).attr("id");
    var bCanPreview = true; // can preview

    if ($(".colorpicker", btn).is(":hidden")) {
        $(".colorpicker").fadeOut(100, "linear");
        $(".colorpicker", btn).fadeIn(100, "linear");
    } else {
        $(".colorpicker").fadeOut(100, "linear");
    }

    bCanPreview = true;

    // create canvas and context objects
    var canvas = document.getElementById(cvId);
    var ctx = canvas.getContext("2d");

    var hex;

    // drawing active image
    var image = new Image();

    image.onload = function () {
        ctx.drawImage(image, 0, 0, 150, 150); // draw the image on the canvas
    }

    // select desired colorwheel
    image.src = "../Veiculos/img/formas/colorwheel.png";

    $("#" + cvId).mousemove(function (e) { // mouse move handler
        if (bCanPreview) {
            // get coordinates of current position
            var canvasOffset = $(canvas).offset();
            var canvasX = Math.floor(e.pageX - canvasOffset.left);
            var canvasY = Math.floor(e.pageY - canvasOffset.top);

            // get current pixel
            var imageData = ctx.getImageData(canvasX, canvasY, 1, 1);
            var pixel = imageData.data;

            // update preview color
            var pixelColor = "rgb(" + pixel[0] + ", " + pixel[1] + ", " + pixel[2] + ")";

            $(btn).css("backgroundColor", pixelColor);

            var dColor = pixel[2] + 256 * pixel[1] + 65536 * pixel[0];
            hex = "#" + ("0000" + dColor.toString(16)).substr(-6);
        }
    });

    $("#" + cvId).click(function (e) { // click event handler
        bCanPreview = !bCanPreview;
    });
});
////////////////////////