﻿/// <reference path="jquery.js" />
/// <reference path="jquery.background.js" />


var cHash = "";

// HashChange Watchdog (IE6 Workaround)
function hashWatch() {
    var hash = window.location.hash.replace(/^#/, '');

    if (cHash != hash) {
        loadContent(hash);
        removeIframe();
    }
    cHash = hash;
}


function ajaxian(navigation, container) {
    /**********************/
    /*** Initialisieren ***/
    /**********************/
    this.init = function () {
        if ((!isIE6)) {
            // Links (AJAX + iFrame)
            $(navigation + " a").each(function () {
                var cHref = $(this).attr("href");
                // Ausschluss
                if (cHref != undefined) {
                    if (!$(this).hasClass("noiFrame")) {
                        if ((cHref.indexOf("/") == 0 || cHref.indexOf(location.hostname) > -1) && cHref.indexOf(".aspx") > -1) {
                            makeItAjax($(this));
                        }
                        if (cHref.indexOf("http") > -1 && cHref.indexOf(location.hostname) < 0) {
                            makeItIFrame($(this));
                        }
                    }
                }
            });

            // Navigation-Events
            if (canHashChange()) {
                $(window).bind('hashchange', function () {
                    loadContent(getURL());
                    removeIframe();
                });
            } else {
                setInterval("hashWatch()", 500);
            }

            var regBaseUrl = new RegExp("^/([a-z]{2})/$");
            if (regBaseUrl.test(location.pathname)) {
                loadContent(getURL());
            }

            $("a.lnkThumb").tipTip({ maxWidth: "auto", edgeOffset: -22, delay: 100 });
            $(".title").tipTip({ maxWidth: "auto", edgeOffset: 5, delay: 100 });
        }
    }
}

function fitNav() {
    $("#nav ul").each(function () {
        $(this).show();
        var cWidth = 0;
        $(this).find("> li:visible").each(function () {
            cWidth = cWidth + $(this).width() + 20;
        });
        cWidth = cWidth + 5;
        $(this).width(cWidth);
        $(this).css("margin-left", (cWidth * -0.5) + 2);
        cWidth = 0;
    });

    $("#nav ul ul").css("margin-left", "-=2px");

    $("#nav ul ul").not($("#nav ul li.active ul")).hide();

    var ccWidth = 0;
    $("#headerNav ul li").each(function () {
        ccWidth = ccWidth + $(this).width() + 22;
    });

    $("#headerNav").width(ccWidth);
    $("#headerNav").css("margin-left", (ccWidth * -0.5) + $("#headerNav ul li").length - 4);

    // Werte in Cookie speichern
    $("#nav ul").each(function () {
        $.cookie($(this).attr("class") + "_width", $(this).css("width"));
        $.cookie($(this).attr("class") + "_margin-left", $(this).css("margin-left"));
    });
}

/*********************/
/*** Content laden ***/
/*********************/
function loadContent(path) {
    // Loading (START)...
    $("<img>")
    .attr("src", "/images/construct/ajax-loader-content.gif")
    .attr("id", "ajax-loader-content")
    .appendTo("body")

    loadImages(path);


    $("#iFrame").remove();
    $("#lnkOnlineBuchen").css("display", "block");

    $("#content").fadeOut(300, function () {
        // CSS Content
        $("link[href^='/App_Basics/styles/color/']").attr("href", "/App_Basics/styles/color/" + getColor(path) + ".css");

        // Farbe Navigation
        var cColorImg = $("#" + getColor(path));
        $(cColorImg).fadeIn(500);
        $("#frmMainNav img").not(cColorImg).fadeOut(500);

        // Startseite
        path = "/" + langKey + path;
        var regBase = new RegExp("^/([a-z]{2})/$");
        if (regBase.test(path)) {
            path = path + "default.aspx"
        }

        // Content
        var cPath = path;
        if (cPath.indexOf("?") > -1) {
            cPath = cPath + "&amp;ajax=1"
        } else {
            cPath = cPath + "?ajax=1"
        }

        $.get(cPath, function (data) {
            var content = $(data).filter("#content").html();
            $("#content").html(content);
        }).success(function () {
            $("#content").fadeIn(300, function () {

                // Bilder laden         
                fitMobile();

                createMap();
                // Loading (ENDE)...
                $("#ajax-loader-content").fadeOut(300, function () {
                    $("#ajax-loader-content").remove();
                });
                // Routenplaner
                calculateRoute();

                // Links (AJAX + iFrame)
                $("#content a").each(function () {
                    var cHref = $(this).attr("href");

                    if (cHref != undefined) {
                        if (!$(this).hasClass("noiFrame")) {
                            if ((cHref.indexOf("/") == 0 && cHref.indexOf(".aspx") > -1) || (cHref.indexOf(location.hostname) > -1 && cHref.indexOf(".aspx") > -1)) {
                                makeItAjax($(this));
                            }
                            if (cHref.indexOf("http") > -1 && cHref.indexOf(location.hostname) < 0 && cHref.indexOf("https") < 0) {
                                makeItIFrame($(this));
                            }
                        }
                    }
                });

                fitScrollingContent();
                $("a.lnkThumb").tipTip({ maxWidth: "auto", edgeOffset: -22, delay: 100 });
                $(".title").tipTip({ maxWidth: "auto", edgeOffset: 5, delay: 100 });

                //                $("#btnSubmit").click(function () {
                //                    submitFormViaAjax('#aspnetForm', 'ctl00$cphContent$Formulare$lnkSenden');
                //                })
            });
        }).error(function () { loadContent("/") });
        setActiveNodes();
    });
}

function fitScrollingContent() {
    var wHeight = $(window).height();
    var cntHeight = $(".iContent").height() + $("#header").height();
    if (wHeight <= (cntHeight + 80)) {
        $(".iContent").css("padding-bottom", "50px");
    } else {
        $(".iContent").css("padding-bottom", "30px");
    }
}

$(window).resize(function () {
    fitScrollingContent();
    fitBackground();
    fitContent();
});

$(window).load(function () {
    fitScrollingContent();
    $("a.lnkThumb").tipTip({ maxWidth: "auto", edgeOffset: -22, delay: 100 });
    $(".title").tipTip({ maxWidth: "auto", edgeOffset: 5, delay: 100 });
});

var myAjaxian = new ajaxian("#header", "#content");

$(document).ready(function () {
    myAjaxian.init();
    fitNav();
    fitBackground();
    sidebar();
    fitMobile();
    createMap();
    calculateRoute();
    fontSize();
    fitScrollingContent();

    $("a.lnkThumb").tipTip({ maxWidth: "auto", edgeOffset: -22, delay: 100 });
    $(".title").tipTip({ maxWidth: "auto", edgeOffset: 5, delay: 100 });
    $("#buttons ul li a.tt, #buttons .ui-slider-handle").tipTip({ maxWidth: "auto", edgeOffset: 12, delay: 100 });

    if (!isIE6) {
        $("#content a[href*='.aspx'][href^='/'], a.lnkImpressum").each(function () {
            makeItAjax($(this));
        });
    } else {
        var cH = Math.max($("#content").height() + 150, $(document).height() - 29);
        $("#footer").css("top", cH);
        $("#images").height(cH - 130);
    }

});

// Log all jQuery AJAX requests to Google Analytics
$(document).ajaxSend(function (event, xhr, settings) {
    if (typeof _gaq !== "undefined" && _gaq !== null) {
        _gaq.push(['_trackPageview', settings.url.replace(/\?ajax=1/g, "")]);
    }
});
