﻿function GetSubNavigation(obj) {

    $.ajax({
        url: "/Home/SubNavigation?mainNavigationHref=" + $(obj).attr("href"),
        success: function (data) {
            $("#pnlSubNavigation").html(data);
            RepositionSubNavigation($(obj).parent());
        }
    });
}

function RepositionNavigation() {

    var holeWidth = $(".pnlPageWrapper").width();
    var mainNavigationPadding = 0;
    var mainNavigationCount = 0;
    var mainNavigationWidth = 0;

    $(".ulMainNavigationNoButton li").each(function () {
        mainNavigationWidth += $(this).width();
        mainNavigationCount++;
    });

    mainNavigationPadding = (holeWidth - mainNavigationWidth - 1) / mainNavigationCount / 2;
    $(".ulMainNavigationNoButton li a").css("padding", "0 " + mainNavigationPadding + "px");

    // Subnavi
    RepositionSubNavigation($(".ulMainNavigationNoButton li.active"));

}

function RepositionSubNavigation(activeLiElement) {

    var holeWidth = $(".pnlPageWrapper").width();
    var subNavigationWrapperWidth = $(".ulSubNavigationNoButton").width();
    var subNavigationPadding = parseInt($(".ulSubNavigationNoButton li a").css("padding-left"));
    var mainNavigationPadding = parseInt($(".ulMainNavigationNoButton li a").css("padding-left"));

    var subNavigationWidthWithActiveLi = 0;
    var subNavigationWidthWithoutActiveLi = 0;
    $(".ulMainNavigationNoButton li").each(function () {
        subNavigationWidthWithActiveLi += $(this).width();
        if ($(this)[0] == activeLiElement[0]) return false;
        subNavigationWidthWithoutActiveLi += $(this).width();
    });

    try {
        var subNavigationMarginRight = holeWidth - subNavigationWidthWithActiveLi + mainNavigationPadding - subNavigationPadding;
        var subNavigationMarginLeft = subNavigationWidthWithoutActiveLi + mainNavigationPadding - subNavigationPadding;
        if (holeWidth - subNavigationMarginRight >= subNavigationWrapperWidth)
            $(".pnlSubNavigation").css("float", "right").css("margin-right", subNavigationMarginRight + "px");
        else
            $(".pnlSubNavigation").css("float", "left").css("margin-left", subNavigationMarginLeft + "px");
    } catch (e) {

    }
    

}



$(function () {
    ShowEditButtons();
    InitEditor();
    RepositionNavigation();

    $(".ulMainNavigationNoButton li a").mouseover(function () { GetSubNavigation($(this)); });
});

function ShowEditButtons() {
    $(".pnlModuleWrapper.UserIsLoggedIn").hover(
    function () {
        //$(this).css("background-color", "#EEEEEE");
        $(this).children(".pEditHeader").css("display", "block");
    }, function () {
        //$(this).css("background-color", "transparent");
        $(this).children(".pEditHeader").css("display", "none");
    });
}

// show delete Comment Button on hover 
$(function () {
    $(".pnlComments.UserIsLoggedIn").hover(
    function () {
        $(".pCommentDate a.lnkDelete").css("visibility", "visible");
    },
    function () {
        $(".pCommentDate a.lnkDelete").css("visibility", "hidden");
    });
});

function InitEditor() {

    $(".lnkShowEditor").each(function () {
        var a = $(this);
        a.click(function () {
            $(this).tooltip().hide();
            $.ajax({
                url: $(this).attr("href"),
                success: function (data) {
                    a.parent().parent("div").html(data);
                    $('.pEditHeaderActiveEditor a').each(function () {
                        $(this).tooltip();
                    });

                    // prevent editor from getting BgColor through hover
                    $(".pnlEditEditor.UserIsLoggedIn").parent().hover(
                    function () {
                        $(this).css("background-color", "transparent");
                    });
                }
            });
            return false;
        });
    });
}

$(function () {
    $('.pnlMainNavigation ul li:not(".first"):last-child').addClass('last');
    $('.pnlSubNavigation ul li:not(".first"):last-child').addClass('last');
    $('.pnlSubNavigation ul ul li:not(".first"):last-child').addClass('last');
});

// Paging for Comments
$(function () {
    $("ul.paging").quickPager({ pagerLocation: "both" });
});

// Moduldesign --> set background-color & border-color
$(function () {

    // set NL input background-color
    $('.pnlNewsletterInputBackgroundColor .txtFullWidth, .pnlNewsletterInputBackgroundColor select').css({ "background": "0", "background-color": $(".pnlNewsletterWrapper").attr("data-input-background-color") });
    // set NL input border
    $('.pnlNewsletterInputBorderColor .txtFullWidth, .pnlNewsletterInputBackgroundColor select').css({ "border": "1px solid " + $(".pnlNewsletterWrapper").attr("data-input-border-color") });

    // set Comments input background-color
    $('.pnlCommentsInputBackgroundColor .txtFullWidth, .pnlCommentsInputBackgroundColor .txtMultiline').css({ "background": "0", "background-color": $(".pnlCommentPostsForm").attr("data-input-background-color") });
    // set Comments input border
    $('.pnlCommentsInputBorderColor .txtFullWidth, .pnlCommentsInputBorderColor .txtMultiline').css({ "border": "1px solid " + $(".pnlCommentPostsForm").attr("data-input-border-color") });

    // set Guestbook input background-color
    $('.pnlGuestbookInputBackgroundColor .txtFullWidth, .pnlGuestbookInputBackgroundColor .txtMultiline').css({ "background": "0", "background-color": $(".pnlGuestbooksForm").attr("data-input-background-color") });
    // set Guestbook input border
    $('.pnlGuestbookInputBorderColor .txtFullWidth, .pnlGuestbookInputBorderColor .txtMultiline').css({ "border": "1px solid " + $(".pnlGuestbooksForm").attr("data-input-border-color") });

    // set SimpleForm input background-color
    $('.pnlSimpleFormInputBackgroundColor .txtFullWidth, .pnlSimpleFormInputBackgroundColor .txtMultiline').css({ "background": "0", "background-color": $(".pnlSimpleForm").attr("data-input-background-color") });
    // set SimpleForm input border
    $('.pnlSimpleFormInputBorderColor .txtFullWidth, .pnlSimpleFormInputBorderColor .txtMultiline').css({ "border": "1px solid " + $(".pnlSimpleForm").attr("data-input-border-color") });
});

// AWC Fore/Background switch
$(function () {

    // add classes to awc --> depends on z-index
    $(".pnlAnyWhereContentModule").each(function () {
        var awcModule = $(this);

        if (awcModule.css("z-index") > 0) {
            awcModule.addClass("flexInForeground");
        } else {
            awcModule.addClass("flexInBackground");
        }
    });

    // Show Flexies Button if design-awc is on THIS page
    if ($(".flexInBackground").length) {
        $("#lnkSwitchFlexLayer").css("visibility", "visible");
    }

    // Flexies Button click event to manipulate awc with class (flexInBackground)
    $("#lnkSwitchFlexLayer").click(function () {
        var item = $(this).find(".aFirst");
        if (item.hasClass("flexiesInBackground")) {
            item.removeClass("flexiesInBackground").addClass("flexiesInForeground").text("Flex in den Hintergrund");
            $(".flexInBackground").css("z-index", 111);
        } else {
            item.removeClass("flexiesInForeground").addClass("flexiesInBackground").text("Flex in den Vordergrund");
            $(".flexInBackground").css("z-index", -1);
        }
    });
});

function ClearPager() {
    $('#pageCounter').attr('value', '');
}

function ToggleProductFilter() {
    $(".pnlSlideContent").slideToggle("slow");
    $('.pnlFilterHeader span').toggleClass('active');
}

function ToggleArticleFilter() {
    $(".pnlSlideContent").slideToggle("slow");
    $('.pnlFilterHeader span').toggleClass('active');
}

function DisplayImage(id) {
    $('.images').each(function () { $(this).css('display', 'none') });
    $('#' + id).css('display', 'inline');

}

// popup für videos
// breite wird im link als attribut gesetzt (data-width), höhe ebenso (data-height)
$(function () {
    $(".lnkPlayVideo").click(function () {

        var popupLink = $(this),
            width = popupLink.attr("data-width"),
            height = popupLink.attr("data-height");

        // setzte defaults wenn attribut nicht vorhanden
        if (typeof width == "undefined") width = 858;
        if (typeof height == "undefined") height = 512;

        popupLink.colorbox({
            iframe: true,
            innerWidth: width,
            innerHeight: height
        });
    });
});

$(function () {

    $('#txtCreateSearchIframeCode').click(function (e) {

        // define variables
        var $textbox = $(this),
            protocol = document.location.protocol,
            hostname = document.location.hostname,
            controllerAction = $textbox.attr("data-url"),
            queryString = document.location.search,
            iframeSrc = '',
            iframeSourceCode = '';
        
        // wenn querystring leer, dann "?css"
        if (queryString.length > 0) {
            queryString += '&css=';
        } else {
            queryString += '?css=';
        }

        // Ausnahme: lokal brauchen wir die portnummer
        if (hostname.indexOf('localhost') != -1) {
            hostname = document.location.host;
        }

        // set iframe src
        iframeSrc = protocol + "//" + hostname + controllerAction + queryString;

        // create iframe tag
        iframeSourceCode = '<iframe src="' + iframeSrc + '" width="600" height="400"></iframe>';

        // befülle textbox und makiere deren inhalt
        $textbox.val(iframeSourceCode).select();

    });

});

$(function () {

    // Termine - setzte aktiv Klasse für Table Header
    var sortiertNach = urlParams["sortiertNach"];

    // nicht undefined und nicht ""
    if (typeof sortiertNach !== "undefined" && sortiertNach) {
        $("#" + sortiertNach).addClass("sortiertNach");
    } else {
        // beim ersten Laden der Seite wird nach Datum sortiert
        $("#DatumVon").addClass("sortiertNach");
    }

    // Termine - Sortierung
    $(".sortMe").click(function () {

        var el = $(this),
            elID = el.attr("id"),
            richtung = urlParams["dir"],
            sortiertNach = urlParams["sortiertNach"];

        // schreibe Feldname in input Feld
        $("#txtSoriertNach").val(elID);

        // falls Parameter existieren 
        if (typeof richtung !== "undefined" && typeof sortiertNach !== "undefined") {

            // ändere "asc" auf "desc" nur wenn dasselbe Element ein zweites mal angeclickt wird
            if (richtung === "asc" && $("#" + sortiertNach).attr("id") === elID) {
                $("#txtDirection").val("desc");
            } else {
                $("#txtDirection").val("asc");
            }
        } else {
            $("#txtDirection").val("asc"); // beim ersten click sind "richtung" und "sortiertNach" undefined
        }

        // schicke Formular ab
        el.closest("form").submit();
    });
});


/*
 * http://stackoverflow.com/questions/901115/get-query-string-values-in-javascript
 *
 * get Querystring Parameters
 */
var urlParams = {};
(function () {
    var e,
        a = /\+/g,  // Regex for replacing addition symbol with a space
        r = /([^&=]+)=?([^&]*)/g,
        d = function (s) { return decodeURIComponent(s.replace(a, " ")); },
        q = window.location.search.substring(1);

    while (e = r.exec(q))
        urlParams[d(e[1])] = d(e[2]);
})();



    



