﻿var headerHeight = "";
var headerMinHeight = "";

var quotHeaderContent = 0.6;
var defHeaderHeight = 330;
if (location.pathname == "/" || location.pathname == "/de/default.aspx" || location.pathname == "/en/default.aspx" || location.pathname == "/fr/default.aspx" || location.pathname == "/es/default.aspx") {
    defHeaderHeight = 590;
}

$(window).resize(function () {
    fitHeader()
});

$(window).load(function () {
    fitHeader();
});

function fitHeader() {
    $("#header-img img, #header-img-start img").each(function () {
        if ($(this).parent().width() > 1280) {
            $(this).width($(window).width());
            $.cookie("imgWidth", $(window).width() );
        } else {
            $(this).width(1280);
            $.cookie("imgWidth", 1280);
        }
    });

    if (parseInt($(window).height() * quotHeaderContent) < defHeaderHeight) {
        $('#header-img, #header-img-start').height(parseInt($(window).height() * quotHeaderContent));
        $.cookie("imgHeight", parseInt($(window).height() * quotHeaderContent));
    } else {
        $('#header-img, #header-img-start').height(defHeaderHeight);
        $.cookie("imgHeight", defHeaderHeight);
    }

}

var time = 200;


$(document).ready(function () {

    if ($.browser.msie) {
        time = 1;
        $.cookie("isMSIE", "true");
    }

    // Layer bei Click auf Subnav ausblenden    
    $("#left a").each(function () {
        var lnkSub = $(this);
        var nHrefSub = lnkSub.attr("href");
        lnkSub.attr("href", "#" + lnkSub.text());
        lnkSub.click(function () {
            $.cookie("showLayer", "false");
            window.location = nHrefSub;
        });
    });

    // Layer bei Click auf Mainnav anzeigen
    $("#header a").each(function () {
        var lnkMain = $(this);
        var nHrefMain = lnkMain.attr("href");
        lnkMain.attr("href", "#" + lnkMain.text());
        lnkMain.click(function () {
            $.cookie("showLayer", "true");
            window.location = nHrefMain;
        });
    });

    // Execute
    if ($.cookie("showLayer") != "true") {
        $('#header .links li').not('.active').stop(true, true).slideUp(0);
        $("#overlay").stop(true, true).fadeOut(0);
        $('#header .links a.active').css({ color: '#ffffff', borderLeftColor: '#ffffff' });
        if ($(window).scrollTop() > headerHeight - 40) {
            $('#header .links a.active').css({ color: '#000000', borderLeftColor: '#000000' });
        } else {
            $('#header .links a.active').css({ color: '#ffffff', borderLeftColor: '#ffffff' });
        }
    }

    // Höhe Logo
    var logoHeight = $("#logo").height();

    // Höhe Bild
    var headerHeight = $('#header-img, #header-img-start').height();

    // Höhe Navigation
    var headerMinHeight = 0;
    $("#header ul").each(function () {
        if ($(this).height() > headerMinHeight) {
            headerMinHeight = $(this).height() + 55;
            headerMinHeight = Math.max(headerMinHeight, layerMinHeight);
        }
    });

    if (parseInt($(window).height() * quotHeaderContent) < defHeaderHeight) {
        $('#navArea').height(parseInt($(window).height() * quotHeaderContent));
        $('#overlay').height(parseInt($(window).height() * quotHeaderContent));
    } else {
        $('#navArea').height(defHeaderHeight);
        $('#overlay').height(defHeaderHeight);
    }


    // LINKS
    $('#header .links a').not('.active').animate({ color: '#999999' }, time);
    $('#header .links a.active').animate({ color: '#FFFFFF', borderLeftColor: '#FFFFFF' }, 800);

    // OVER/OUT
    $('#navArea').hover(function () {
        if (location.pathname == "/" || location.pathname == "/de/default.aspx" || location.pathname == "/en/default.aspx" || location.pathname == "/fr/default.aspx" || location.pathname == "/es/default.aspx") {
            $('#header .links li').show()
        } else {
            NavOver();
        }

    }, function () {
        if (location.pathname == "/" || location.pathname == "/de/default.aspx" || location.pathname == "/en/default.aspx" || location.pathname == "/fr/default.aspx" || location.pathname == "/es/default.aspx") {
            $('#header .links li').show()
        } else {
            NavOut();
        }

    });

    checkBackground();

    $(window).scroll(function () {
        checkBackground();
        LayerHeight(headerMinHeight);
    });

    $('#header .links a').not('.active').hover(function () {
        $(this).stop(true, true).animate({ color: '#FFFFFF' }, time);
    },
		function () {
		    $(this).stop(true, true).animate({ color: '#B3B3B3' }, time);
		});

    // OVER
    function NavOver() {
        // Navigation fährt aus
        $('#header .links li').not('.active').stop(true, true).slideDown(time);
        $("#overlay").stop(true, true).fadeIn(time);
        $('#header').css({ 'background': 'none' });
        $('#header .links a.active').stop(true, true).animate({ color: '#ffffff', borderLeftColor: '#ffffff' });

        LayerHeight(headerMinHeight);

        $(window).scroll(function () {
            LayerHeight(headerMinHeight);
        });
    }

    // OUT
    function NavOut() {
        // Navigation fährt hoch
        $('#header .links li').not('.active').stop(true, true).slideUp(time);
        $("#overlay").stop(true, true).fadeOut(time);

        checkBackground();

        $('#header .links a.active').stop(true, true).animate({ color: '#ffffff', borderLeftColor: '#ffffff' });
        if ($(window).scrollTop() > headerHeight - 40) {
            $('#header .links a.active').stop(true, true).animate({ color: '#000000', borderLeftColor: '#000000' });
        } else {
            $('#header .links a.active').stop(true, true).animate({ color: '#ffffff', borderLeftColor: '#ffffff' });
        }

        LayerHeight(logoHeight);

        $(window).scroll(function () {
            LayerHeight(logoHeight);
        });
    }


    function LayerHeight(limit) {
        // Höhe Bild
        var headerHeight = $('#header-img').height();
       
        if ($.browser.msie) {
            $("#navArea,#overlay").height(headerHeight);
        } else {
            if (headerHeight - $(window).scrollTop() > limit) {
                var cHeight = parseInt(headerHeight - $(window).scrollTop()) + "px";
                $("#navArea").height(cHeight);
                $("#overlay").height(cHeight);
                //alert(headerHeight - $(window).scrollTop() + " " + $(window).scrollTop() + " " + headerHeight)
            } else {
                $("#navArea").height(limit + "px");
                $("#overlay").height(limit + "px");
            }
        }



    }

    function checkBackground() {
        if ($(window).scrollTop() > headerHeight - 40) {
            $('#header').css({ 'background': 'url(/images/bg_white_transparent.png) repeat 0 0' });
            $('#header .links a.active').stop(true, true).animate({ color: '#000000', borderLeftColor: '#000000' });
        } else {
            $('#header').css({ 'background': 'none' });
            $('#header .links a.active').stop(true, true).animate({ color: '#ffffff', borderLeftColor: '#ffffff' });
        }
    }
});
