
jQuery(function($) {
    $('#lang_menu').hide();
    $('#lang_link').bind("click", function(e) {
        $('#lang_menu').slideToggle(120);
    });
    
    $('#imprint').bind('click', function() {
        if (!$('#imprint_content').is(':visible')) {
            $('#imprint_content').show().animate({ 'top': 0, 'opacity': 1 }, 360);
            $(this).addClass('active');
        } else {
            $('#imprint_content').animate({ 'top': -300, 'opacity': 0 }, 280, 'linear', function() { $(this).hide(); });
            $(this).removeClass('active');
        }
        return false;
    });
    $('#imprint_close').bind('click', function() {
        $('#imprint').click();
        return false;
    });
    
    $('a').each(function() {
        if ($(this).attr('rel') == 'external') {
            $(this).attr('target', '_blank');
        }
        $(this).click(function() {
            if (this.blur) {this.blur();}
        });
    });
    
});

function fillBlock(num, h) {
    $('#fill_blocks div:eq(' + num + ')').css({ 'opacity': 1, 'height': h });
}

function clearBlock(num) {
    $('#fill_blocks div:eq(' + num + ')').css('height', 0);
}

$(window).load(function() {
    $('a.hidden_toggler').each(function(i, t) {
        var c = $('a.hidden_toggler').parent().parent().parent().children('.hidden_content:eq('+i+')');
        if (c.length) {
            var o_height = $(c).height();
            $(t).bind('click', function(e) {
                if ($(t).hasClass('visible')) {
                    $(c).css('overflow','hidden').animate({ 'height':0 }, 120, 'easeOutCubic', 
                        function() { 
                            $(this).hide();
                        } 
                    );
                    $(t).removeClass('visible');
                } else {
                    $(c).css({ 'overflow':'hidden', 'height':0 }).show().animate({ 'height':o_height }, 180, 'easeOutCubic');
                    $(t).addClass('visible');
                }
                return false;
            });
            if ($(t).parent().is(':header')) {
                $(t).parent().css('cursor', 'pointer')
                    .bind('click', function() {
                        $(t).click();
                    })
                    .hover(
                        function() { $(t).addClass('hovered'); },
                        function() { $(t).removeClass('hovered'); }
                    );
            }
        } else {
            $(t).hide();
        }
    });
    
    var init_height = 210;
    $("ul.sf-menu").superfish({
        autoArrows: false,
        dropShadows: false,
        pathClass: 'current',
        pathLevels: 3,
        animation: {opacity:'show'},
        speed: 0,
        delay: 260,
        onBeforeShow: function() {
            if ($(this).find('li').length) {
                clearBlock(1);
            }
            if ($(this) && $(this) !== null) {
                if (init_height === null) {
                    init_height = $('#bottom_nav').height();
                }
                var p_top = $(this).parent().position() ? $(this).parent().position().top : 0;
                var new_top =  $(this).height() - $(this).parent().height();
                new_top =  - (new_top > p_top ? p_top : new_top - 1);
                
                if ($(this).find('li').length) {
                    var ret_position = true;
                } else {
                    var ret_position = false;
                }
                
                if ($(this).height() + p_top > init_height) 
                {
                    
                    if ($(this).height() <= init_height) {
                        $(this).css('top', new_top);
                        
                        if ( (p_top + new_top) > 0) {
                            fillBlock(1, (p_top + new_top - 1));
                        }
                    } else {
                        var correction = init_height - $(this).height() - p_top;
                        
                        if ($('.menu-pushup').length) {
                            $('.menu-pushup').css('marginBottom',- (init_height - $(this).height()))
                        }
                        ret_position = false;
                        $(this).css('top', correction + 'px');
                        $(this).addClass('lightblack');
                    }
                }
                else if (p_top > 0 && $(this).find('li:first').siblings('li').andSelf().length > 1)
                {
                    $(this).css('top', new_top );
                    
                    if ( (p_top + new_top) > 0) {
                        fillBlock(1, (p_top + new_top - 1));
                    }
                }
                else if (p_top > 0 && $(this).find('li:first').siblings('li').andSelf().length == 1) 
                {
                    if ( (p_top + new_top) > 0) {
                        fillBlock(1, (p_top + new_top));
                    }
                }
                else 
                {
                    $(this).css('top', '0px');
                    if ($(this).hasClass('lightblack')) {
                        $(this).removeClass('lightblack');
                    }
                }
                
                if ($('.menu-pushup').length && ret_position === true) {
                    $('.menu-pushup').css('marginBottom', 0);
                }
            }
        },
        onShow: function() { rightBannerPos(); },
        onHide: function() { rightBannerPos(); if ($("ul.sf-menu").find('li.sfHover').length < 1) { clearBlock(1); } }
    });
    rightBannerPos();
});