﻿//Executes all functions if the document is fully loaded.
$(document).ready(function () {
    SetUpGlobalAjaxOptions();
    InitMainNavigationFadingEffects();
    InitMainNavigationActive();
});

//Setups global ajax configuration
function SetUpGlobalAjaxOptions() {
    $.ajaxSetup({
        "type": "POST",
        "data": "forceNewContent=" + (Math.random() * Math.PI)
    });
}

//The time in milliseconds in which the element is faded in and out.
var animateInTime = 300;
var animateOutTime = 300;
var flickerBuffer = 0;

//Initialized effects for the main navigation dropdown.
function InitMainNavigationFadingEffects() {
    //Remove css class "canHover" to avoid css fallback mechanism.
    $("body .canHover").removeClass("canHover");

    //The list items.
    var selectedMenuAnchor = $("#menucontainer li.submenuProvider");
    selectedMenuAnchor.hover(function () {
        flickerBuffer = animateInTime;
        $(this).find("ul").stop(true, true).slideDown(animateInTime, function () { flickerBuffer = 0; });
        $(this).siblings().find("ul").stop(true, true).fadeOut(animateOutTime);
    },
       function () {
           $(this).find("ul").delay(flickerBuffer).fadeOut(animateOutTime);
       }
    );
}

//changes active menu item when clicked
function InitMainNavigationActive() {
    $("#menu li a").click(function () {
        var active = $(this).parents("#menu > li");
        active.addClass("active");
        active.siblings().removeClass("active");
    });
}

function CloseMenu() {
    $("#menucontainer li.submenuProvider").find("ul").stop(true, true).fadeOut(animateOutTime);
}

//Inserts the response into the content container.
function InsertAjaxRequestIntoContent(linkTarget) {
    CloseMenu();
    var targetContainer = $("#contentWrapper");
    targetContainer.fadeOut(animateOutTime * 2, function () {
        $("#wait").show();
        $.ajax({
            "url": linkTarget,
            "success": function (data) {
                targetContainer.html(data);
                $("#wait").hide();
                targetContainer.fadeIn(animateInTime * 2);
                InitializeContentSlider();
            },
            "complete": function () {
                //Remove href tag from anchors in the content slider navigation to prevent flickering.
                $(".anythingControls .thumbNav a").removeAttr("href");
            }
        });
    });
}

//Initializes the content slider
function InitializeContentSlider() {
    var slider = $("#contentSlider").anythingSlider({
        "width": 930,
        "height": 400,
        "buildNavigation": true,
        "navigationFormatter": function (index, panel) {
            var title = panel.attr("alt");
            $($(".anythingControls .thumbNav a")[index - 1]).attr("title", title);
        },
        "autoPlay": false,
        "resizeContents": false,
        "easing": "linear",
        "forwardText": "",
        "backText": ""
    });
    slider.anythingSlider(GetIndexOfSelectedSlide(slider));
    InitDetailsDialog();
}

function GetIndexOfSelectedSlide(slider) {
    var slideItems = slider.find("li.slideableContent:not(.cloned)");
    for (var i = 0; i < slideItems.length; i++) {
        if ($(slideItems[i]).hasClass("selectedSlide")) {
            //Anythingslider starts by 1 and not by 0.
            return i+1;
        }
    }

    return 1;
}

/* Ajax Dialog
--------------------------------------------------------------*/
//Initializes the details dialog.
function InitDetailsDialog() {
    var targetDialog = $("#detailsDialog");
    targetDialog.dialog({
        "autoOpen": false,
        "resizable": false,
        "draggable": false,
        "modal": true,
        "closeText": "Schließen",
        "show": "fade",
        "hide": "fade",
        "width": "800"
    });
}

function ShowDialogForEmployee(linkTarget) {
    var targetDialog = $("#detailsDialog");
    $("#wait").show();
    $.ajax({
        "url": linkTarget,
        "success": function (data) {
            targetDialog.dialog({ "title": data.Title });
            targetDialog.find("#dialogContent").html(data.Vita);
            targetDialog.find("#detailPointsOfFocus").hide();
            $("#wait").hide();
            targetDialog.dialog("open");
        }
    });
}

function ShowDialogForAreaOfLaw(linkTarget) {
    var targetDialog = $("#detailsDialog");
    var dialogContent = targetDialog.find("#dialogContent");
    $("#wait").show();
    $.ajax({
        "url": linkTarget,
        "success": function (data) {
            targetDialog.dialog({ "title": data.Title });
            dialogContent.html(data.Description);
            targetDialog.find("#pointsOfFocus").html(BuildUnorderedList(data.PointsOfFocus));
            targetDialog.find("#detailPointsOfFocus").show();
            $("#wait").hide();
            targetDialog.dialog("open");
        }
    });
}

//Builds a UL tag with LIs from a JSON array.
function BuildUnorderedList(data) {
    var listTag = $("<ul class=\"withArrows\"></ul>");
    $(data).each(function (i, val) {
        $("<li></li>").html(val).appendTo(listTag);
    });

    return listTag;
}

/* jQuery extensions.
--------------------------------------------------------------*/
$.fn.extend({
    hasElements: function () {
        return $.length > 0;
    }
});
