$(function () {
    $(".sortByTime").live("click", function () {
        var table = $(this).parent().nextAll("table:first");
        var rows = table.children().children().get();
        rows.sort(function (a, b) {
            var keyA = $(a).children(".day_time_order").text();
            var keyB = $(b).children(".day_time_order").text();
            if (keyA < keyB) {
                return -1;
            }
            if (keyA > keyB) {
                return 1;
            }
            return 0;
        });
        $.each(rows, function (index, row) {
            table.append(row);
        });
        return false;
    });
    $(".sortByPart").live("click", function () {
        var table = $(this).parent().nextAll("table:first");
        var rows = table.children().children().get();
        rows.sort(function (a, b) {
            var keyA = $(a).children(".day_part_order").text();
            var keyB = $(b).children(".day_part_order").text();
            if (keyA < keyB) {
                return -1;
            }
            if (keyA > keyB) {
                return 1;
            }
            return 0;
        });
        $.each(rows, function (index, row) {
            table.append(row);
        });
        return false;
    });
    var ajaxFormFunction = function () {
        var form = $(this);
        var data = {"ajaxForm": "true"};
        data[form.find("select").attr("name")] = form.find("option:selected").attr("value");
        if (form.find("option:selected").attr("value") !== "likesession")
        {
            var parentForm = form.parent().parent().parent().parent().prev();
            if (parentForm.is(".ajaxForm") && parentForm.find("option:selected").attr("value") === "none")
            {
                parentForm.find("option:nth(1)").attr("selected", "selected");
            }
        }
        if (form.find("option:selected").attr("value") === "none")
        {
            form.next().find(".ajaxForm").find("option:nth(0)").attr("selected", "selected");
        }
        form.find("img").attr("src", "/static/load.gif");
        $.ajax({
            url: form.attr("action"),
            type: "POST",
            data: data,
            success: function (data) {
                form.find("img").attr("src", "/static/spacer.gif");
            }
        });
        return false;
    };
    $("img.ajaxTree").live("click", function () {
        var img = $(this);
        var tr = img.parent().parent().parent();
        if (img.attr("src") === "/static/minus.gif")
        {
            tr.next().children().slice(1, 2).html("");
            img.attr("src", "/static/plus.gif");
        }
        else
        {
            img.attr("src", "/static/load.gif");
            var data = {xhr: "1"};
            if (img.parents("table").is(".showSelection"))
            {
                data.showSelection = "1";
            }
            $.ajax({
                url: img.parent().attr("href"),
                type: "GET",
                data: data,
                success: function (data) {
                    var html = tr.next().children().slice(1, 2).html(data);
                    html.find(".ajaxForm").submit(ajaxFormFunction);
                    html.find(".ajaxForm select").change(function () {
                        $(this).submit();
                    });
                    html.find(".ajaxForm input[type=submit]").hide();
                    img.attr("src", "/static/minus.gif");
                }
            });
        }
        return false;
    });
    $(".ajaxForm").submit(ajaxFormFunction);
    $(".ajaxForm select").change(function () {
        $(this).submit();
    });
    $(".ajaxForm input[type=submit]").hide();
    $("#submit_time_range").bind("click submit", function () {
        var now = new Date();
        var minutes = parseInt($("#minutes").val());
        if (! isNaN(minutes)) { now = new Date(now.getTime() + minutes*60*1000); }
        var date = now.getFullYear().toString() + "-";
        if (now.getMonth() < 9) { date = date + "0"; }
        date = date + (now.getMonth() + 1).toString() + "-";
        if (now.getDate() < 10) { date = date + "0"; }
        date = date + now.getDate().toString();
        if ($("#from_date option[value='" + date + "']").length !== 0) {
            $("#from_date option[value='" + date + "']").attr("selected", true);
            $("#to_date option[value='" + date + "']").attr("selected", true);
            var time = now.getHours().toString() + ":";
            if (now.getMinutes() < 10) { time = time + "0"; }
            time = time + now.getMinutes().toString();
            $("#from_time").val(time);
            $("#to_time").val(time);
        }
        return false;
    });
    $("#set_time_range").show();
    $('#update').submit(function(){
      $(':submit', this).attr('disabled', 'disabled').val('Be patient while updates are downloaded and applied (if there are new updates available) …');
    });
    $("#terminate").bind("click submit", function () {
        $.ajax({
            url: $(this).parents().find("form").attr("action"),
            type: "POST"
        });
        $(".content").html('<p>The application was terminated. You can now close this window.</p><hr><p>Die Anwendung wurde beendet. Sie können dieses Fenster nun schließen.</p>')
        return false;
    });
});
function stopRKey(evt) {
    var evt = (evt) ? evt : ((event) ? event : null);
    var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
    if ((evt.keyCode === 13) && ((node.type === "text") || (node.type === "password")) && (node.name !== "query"))  {
        return false;
    }
}
document.onkeypress = stopRKey;
