﻿var GUID_EMPTY = "00000000-0000-0000-0000-000000000000";

$(function () {

    $("#nav ul li.logoff a").click(function (e) {
        if (confirm("Are you sure you want to log off?") === false) {
            e.preventDefault();
        }
    });

    $("#home .jCarouselLite").jCarouselLite({
        btnNext: ".next",
        btnPrev: ".prev",
        circular: false,
        mouseWheel: true,
        scroll: 1,
        start: 0,
        vertical: false,
        visible: 1
    });

    // The home carousel is not circular so disable the previous button
    $("#home .carousel .prev").addClass("disabled");

    // and if there is only one item disable the next button
    if ($("#home .jCarouselLite ul li").length < 2) {
        $("#home .carousel .next").addClass("disabled");
    }

    // Delete all buttons on admin dashboard
    $("a.delete-all").click(function (e) {
        if (confirm("Are you sure?") != true) {
            e.preventDefault();
        }
    });

    // Delete buttons on individual items
    $("a.delete-item").click(function (e) {
        e.preventDefault();

        var link = $(this);
        var itemContainer = link.closest(".item-container");

        if (confirm("Are you sure?")) {
            $.post(
                $(this).attr("href"),
                { json: true },
                function (data, textStatus, jqXHR) {
                    if (data && data.Success === true) {
                        itemContainer.fadeOut("slow").remove();
                    }
                    else {
                        alert("Sorry but the delete failed for some reason :-(");
                    }
                },
                "json"
            );
        }
    });

    // Contracts tree on admin dashboard
    $("div.contract-menu").each(function() {
        var contractId = this.id;
        $(this)
            .jstree({
                // Configure the plugin that handles the data first - in this case JSON
                "json_data": {
                    // I chose an ajax enabled tree - again - as this is most common, and maybe a bit more complex
                    // All the options are the same as jQuery's except for `data` which CAN (not should) be a function
                    "ajax": {
                        // the URL to fetch the data
                        "url": "/Contracts/GetContractsMenuTree",
                        // this function is executed in the instance's scope (this refers to the tree instance)
                        // the parameter is the node being loaded (may be -1, 0, or undefined when loading the root nodes)
                        "data": function(n) {
                            // the result is fed to the AJAX request `data` option
                            return {
                                // "operation": "get_children",
                                "id": contractId,
                                "nodeId": n.attr ? n.attr("id") : ""
                            };
                        }
                    }
                },

                // the UI plugin - it handles selecting/deselecting/hovering nodes
                "ui": {
                    // no node initially selected onload
                    "initially_select": [GUID_EMPTY]
                },
                        
                // the core plugin - not many options here
                "core": {
                    // define speed of branch opening in milliseconds
                    "animation": 250,
                    // allow html in the node titles
                    "html_titles": true,
                    // open any nodes up
                    "initially_open": []
                },
                
                // the list of plugins to include
                "plugins": ["themes", "json_data", "ui"]
            })
            .bind("select_node.jstree", function(event, data) {
                var url = $.trim(data.rslt.obj.find("a").attr("href"));
                if (url != "") {
                    document.location.href = url;
                }
            });
    });
});
