$(document).ready(function() {

    /* Navigation Functionality */

    $("div.level2, div.level3").hide().prev().hide();

    $("div.level1 a").click(function(event) {
        var separator = $(this).next();
        var sibling = separator.next();

        // check if sibling is a link or submenu
        if (sibling.attr("class") != undefined && sibling.attr("href") == undefined) {

            // check if submenu should be en- or disabled
            if (sibling.css("display") == "none") {

                // disable all other submenues incl. separators
                sibling.siblings("div." + sibling.attr("class") + ":visible").slideUp().prev().slideUp();

                separator.slideDown();
                sibling.slideDown();
            } else {
                separator.slideUp();
                sibling.slideUp();
            }

            event.preventDefault();
            $(this).blur();
        }
    });

    $("div.level1 a.current").parentsUntil("div.level1").show().prev().show();

    /* Search Field Functionality */

    var searchField = $('#wrapper #header input[type="text"]');
    var searchText = searchField.attr("value");

    searchField.focusin(function() {
        if ($(this).val() == searchText) {
            $(this).val("");
        }
    });

    searchField.focusout(function() {
        if (!$(this).val()) {
            $(this).val(searchText);
        }
    });

    /* Redirect Functionality */

    $("a.redirect").each(function() {
        var target = $(this).attr("href");
        window.setTimeout(function() {
            window.location.href = target;
        }, 1500);
    });

    /* Fading Functionality */

    $(".fade").delay(5000).slideUp();

    /* Commenting Functionality */

    $("form.comment.hide").hide();

    $("a.comment").toggle(function() {
        $("form.comment").slideDown();
    }, function() {
        $("form.comment").slideUp();
    });

    /* Submit Functionality */

    $('input[type="submit"]').click(function() {
        var me = $(this);
        me.css("visibility", "hidden");

        window.setTimeout(function() {
            me.css("visibility", "visible");
        }, 1500);
    });

    /* Delete Backup */

    $("a.delete_backup").click(function(event) {
        var decision = window.confirm("Soll das wirklich gelöscht werden?");

        if (!decision) {
            event.preventDefault();
        }
    });

    /* Image File Functionality */

    $("div.imagefile").each(function() {
        var title = $(this).find("td.file_title").html();
        var description = $(this).find("td.file_description").html();
        var link = $(this).find("td.file_link").html();

        // create cover layer
        $(this).append('<div class="layer">' + title + '<div class="small" style="margin-bottom: 5px;">' + description + '</div>' + link + '</div>');
        $(this).children("div.layer").hide();

        // remove table rows
        $(this).find("td.file_img").removeAttr("rowspan");
        $(this).find("td.file_title").parent().remove();
        $(this).find("td.file_description").parent().remove();
        $(this).find("td.file_link").parent().remove();
    });

    $("div.imagefile").hover(function() {
        $(this).children("div.layer").fadeIn(200);
    }, function() {
        $(this).children("div.layer").fadeOut(600);
    });

});
