$(function() {
    Handlebars.registerHelper('if_eq', function(a, b, opts) {
        if (a == b) {
            return opts.fn(this);
        } else {
            return opts.inverse(this);
        }
    });

    $( "#download" ).click(function(e) {
        console.log("clicked download");
        var did = $(this).attr("data-name").trim();
        e.preventDefault();  //stop the browser from following
        window.location.href = 'download?did=' + did;
    });

    $('#pagination-demo').twbsPagination({
        totalPages: 6,
        visiblePages: 6,
        onPageClick: function (event, page) {
            console.log('Page ' + page);
            var target_url = "getpage?page="+page;
            $.ajax({
                url: target_url,
                type: 'GET',
                success: function (response) {
                    console.log("Success.", response);
                    $("#header-name").text( response["data"][0][0] );
                    $("#front-number").text( response["data"][0][5]);
                    $("#top-number").text( response["data"][0][4]);
                    $("#left-number").text( response["data"][0][3]);
                  
                    for (var item in response["data"]) {
                        console.log(response["data"][item]);
                    }
                    /* 
                    var sidebar_hbs; 
                    $.ajax({
                        url: 'static/dist/template/sidebar.hbs',
                        success: function(data) {
                            sidebar_hbs = data;
                            var template = Handlebars.compile(data);

                            var tmp = "";
                            var html = "";
                            for (var item in response["data"]) {
                                console.log(response["data"][item]);
                                var context = {
                                    title: response["data"][item][0],
                                    desc: response["data"][item][1],
                                    size: response["data"][item][2],
                                    left: response["data"][item][3],
                                    top: response["data"][item][4],
                                    front: response["data"][item][5]
                                };
                                tmp = template(context);
                                html += tmp;
                            }
                            
                            $("#sidebar-list").empty().html(html);
                            $('#sidebar-list').collapse({toggle: false});
                        }
                    });
                    */
                    
                },
                error: function () {
                    console.log("Error.")
                }
            }); 
        }
    });

    $(".data-pil").click(function() {
        $("#header-name").text($(this).text().trim() );
        var dimension = JSON.parse($(this).attr("data-dim"));
        $("#front-number").text(dimension[2]);
        $("#top-number").text(dimension[1]);
        $("#left-number").text(dimension[0]);
    });
   

});
