/*
 * $Id$
 * (c) 2014 Tallence GmbH
 */
/**
 * This file contains all distinctions which have to be considered
 * between hamburg.de and clients
 **/

$(window).load(function() {
    // Masonry for three-columns, another columnWidth is needed
    var $contentLeft = $('.content-left');
    if($contentLeft.hasClass("three-columns")) {
        $contentLeft.imagesLoaded(function(){
            $contentLeft.masonry({
                isAnimated: true,
                columnWidth: 320,
                gutterWidth: 20
            }).masonry();
        });
    } else if($contentLeft.children(".article").length) {
        var $threeColumns = $(".three-columns");
        $threeColumns.children().addClass("masonry-content");

        $threeColumns.imagesLoaded(function(){
            $threeColumns.masonry({
                isAnimated: true,
                columnWidth: 320,
                gutterWidth: 20
            }).masonry();
        });
    }

    /* set topbar-categories position, can be variable because of logged-in name */
    $("#topbar-categories").each(function() {
        var $this = $(this);
        var tbLogInPos = $("#topbar-login").offset();
        var tbCatPos = tbLogInPos.left - $this.width();
        $this.offset({left: tbCatPos});
    })
});

$(document).ready(function() {
    var $subNavigationBox = $(".sub-navigation-box");
    var $breadcrumb = $("#navigation .breadcrumb");

    // Main Navigation
    $("#navigation .main-navigation li.main-nav").not('#navigation .subpage-nav li.main-nav').hoverIntent({
        interval: 100,
        over: function(){
            var itemHeight = $(this).find("ul").outerHeight() -1;
            var that = $(this);
            $(this).parents("#navigation").find('.breadcrumb').css("marginTop", itemHeight);
            $(this).parents("#navigation").find('.main-navigation .main-nav').removeClass("hover");
            $(this).addClass("hover");
            //$(this).find("ul").slideDown();
            $(this).find("ul").fadeIn(100).css("z-index","100");

            if(!$("#navigation .breadcrumb").hasClass("isClosed")) {
                $(".sub-navigation-box").trigger("mouseleave");
            }
        },
        out: function(){
            var that = $(this);
            $(this).parents("#navigation").find('.breadcrumb').css("marginTop", -1);
            /* $(this).find("ul").slideUp(function(){
             that.removeClass("hover");
             });*/
            $(this).find("ul").fadeOut(100);
            that.removeClass("hover");
        }
    });

    $("#navigation .subpage-nav li.main-nav:not('.active)").hoverIntent({
        interval: 100,
        over: function(){
            var itemHeight = $(this).find("ul").outerHeight() -1;
            var that = $(this);

            //that.parents("#navigation").find('.breadcrumb').css("marginTop", itemHeight);
            that.parents("#navigation").find('.main-navigation .main-nav').removeClass("hover");
            that.parents('#navigation').find('.breadcrumb, .sub-navigation-box, .subportal-head').css("left", "-99999px");
            //$("#navigation").css("margin-bottom", "31px");
            that.parents("#navigation").find(".active").removeClass('hover').find('ul').delay(400).fadeOut(100);

            if ($(this).parents('#navigation').find(".active ul").is(':visible')) {
                setTimeout(function(){
                    that.addClass("hover");
                    //that.find('ul').slideDown();
                    $(this).find("ul").fadeIn(100).css("z-index","100");
                }, 800);

            } else {
                that.parents("#navigation").not('.subportal').find(that).addClass('hover').find('ul').fadeIn(100);
                that.parents('#navigation.subportal').find(that).addClass('hover').find('ul').fadeIn(100);
            }

            if(!$("#navigation .breadcrumb").hasClass("isClosed")) {
                $(".sub-navigation-box").trigger("mouseleave");
            }

        },
        out: function(){
            var that = $(this);
            $(this).parents("#navigation").css("margin-bottom",0);
            $(this).parents("#navigation").find('.breadcrumb').css("left","0");
            /* $(this).find("ul").slideUp(function(){
             that.removeClass("hover");
             }); */
            $(this).find("ul").fadeOut(100);
            that.removeClass("hover");
        }
    });

  $("#navigation .subpage-nav li.active").hoverIntent({
            interval: 300,
            over: function(){
                var itemHeight = $(this).find("ul").outerHeight() -1;
                var that = $(this);
                //$(this).parents("#navigation").find('.breadcrumb').css("marginTop", itemHeight);
                $(this).parents("#navigation").find('.main-navigation .main-nav').removeClass("hover");
                $(this).addClass("hover");
                that.parents('#navigation').find('.breadcrumb, .sub-navigation-box, .subportal-head').css("left","-99999px");
                //$("#navigation").css("margin-bottom", "31px");
                $(this).find("ul").fadeIn(100);

                if(!$("#navigation .breadcrumb").hasClass("isClosed")) {
                    $(".sub-navigation-box").trigger("mouseleave");
                }
            },
            out: function(){
                var that = $(this);
                $(this).parents("#navigation").css("margin-bottom",0);
                $(this).parents("#navigation").find('.breadcrumb').css("left","0");
              $(this).find("ul").fadeOut(100);
              that.removeClass("hover");
            }
        });

        $(".subpage-nav").bind('mouseleave', function(){
            var itemHeight = $(this).find("li.active ul").outerHeight() -1;
            var that = $(this);

            setTimeout(function(){
                if ($('.breadcrumb').length == 0) {
                    $('#navigation .subpage-nav li.active').not('.subportal .subpage-nav li.active').addClass('hover');
                    $('#navigation .subpage-nav li.active ul').not('.subportal .subpage-nav li.active ul').fadeIn(100);
                } else {

                }

                $('.subportal-head, .breadcrumb, .sub-navigation-box').fadeIn(100);
            }, 300);
        });

    // Windowsize Kingteaser
    $(".home-slideshow-container.kingsize-small").addClass("slide-small");
    $(".home-slideshow-container.kingsize-small").removeClass("kingsize-small");

    $(window).resize(function() {
        var subPageNavHeight = $('.subpage-nav').find(".hover ul").outerHeight() -1;
        $(".subpage-nav").css({marginBottom: subPageNavHeight});

        $(".home-slideshow-container.kingsize-small").addClass("slide-small");
        $(".home-slideshow-container.kingsize-small").removeClass("kingsize-small");
    });

    // Sub Navigation Close
// -------------------------------------------------------------------------------------
    $(".sub-navigation-box .close").mousedown(function(){
        $(".breadcrumb").addClass("isClosed");
        $(this).parents(".sub-navigation-box").slideUp();
        $(".breadcrumb li:last-child").removeClass("last");
    });

    $(".breadcrumb li:last-child").mouseenter(function(){
        if($(this).parent().hasClass("isClosed")){
            $(this).parent().removeClass("isClosed");
            $subNavigationBox.css("visibility", "visible").css("left", "10px");
            $(".sub-navigation-box").fadeIn(100);
            $(this).parent().find("li:last-child").addClass("last");
        }
    });

    $(".sub-navigation-box").mouseleave(function(){
        $(".breadcrumb").addClass("isClosed");
        $(this).parent().find(".sub-navigation-box").fadeOut(100);
        $(".breadcrumb li:last-child").removeClass("last");
    });

    if ($('.breadcrumb').length == 0) {
        $(".subportal-head").mouseenter(function(){
            $(".sub-navigation-box").slideDown();
        });
    }


    /* remove numbers in slideshow-container navigation elements */
    if($(".home-slideshow-container").length) {
        var $slideShowTabs = $(".home-slideshow-container .slideshow_tabs");
        if($slideShowTabs.length) {
            $slideShowTabs.find("li > a").each(function() {
                $(this).html("");
            });
        }
    }

    /* if subnavigation is visible, hide it */
    if($subNavigationBox.css("display") === "block") {
        $subNavigationBox.css("visibility", "hidden");
    }
    if(!$breadcrumb.hasClass("isClosed")) {
        $breadcrumb.addClass("isClosed");
    }

    /* check if custom browser colors are used */
    checkCustomColors();

    /* if slogan text is wider than the background image, set background-size attribute */
    var $slogan = $("#header .slogan");
    if($slogan.innerWidth() > 400) {
        $slogan.css("background-size", "100% 101%");
    }
});

/* check if custom browser colors are used */
function checkCustomColors() {
    var bgColor = $('body').css('background-color');
    var fgColor = $('body').css('color');
    if(	bgColor !== "rgb(169, 169, 169)" ||
        fgColor !== "rgb(34, 34, 34)") {
        $('#topbar').css('background', 'rgb(60, 60, 60)');
    }
}