    
    jQuery(document).ready(function($){
        
        // Contact panel
        $('#contact').click(function(){
            var top = parseInt($('#contact-opened').css('top'));
            if (top < 0) {
                $('#contact').animate({
                    height: 350
                }, 750, 'swing');
                $('#contact-opened').animate({
                    top: 98
                }, 750, 'swing');
            } else {
                $('#contact-opened').animate({
                    top: -134
                }, 750, 'swing');
            }
        })
        
        
        // Related websites
        $('#related-websites li').hover(function(){
            $(this).children('p').fadeIn();
        }, function(){
            $(this).children('p').fadeOut();
        });
        
        
        // Calls to action
        $('#cta li a').hover(function(event){
            $(this).children('span').animate({
                height: 228
            }, 800);
        }, function(){
            $(this).children('span').animate({
                height: 117
            }, 'slow');
        });
        
        
        // Top navigation
        $('.menu-top ul li').hover(function(event){
            if (!$(this).hasClass('selected')) {
                $(this).children('span').fadeIn();
            }
        }, function(){
            if (!$(this).hasClass('selected')) {
                $(this).children('span').fadeOut();
            }
        });
        
        
        // Main navigation - Top level
        $('ul.menu').children('li').each(function(){
            $(this).css('height', $(this).height());
        });
        $('ul.menu').children('li').children('a').click(function(event){
            //event.preventDefault();
            
            var li_active = $('li.active')[0];
            
            var close_active = false;
            if ($('li.active')[0] == $(this).parent('li')[0]) {
                close_active = true; 
            }
            
            // Close any previously opened sub-levels
            $('ul.menu li.inside').each(function(){
                var ul = $(this).children('ul');
                if (ul.css('display') == 'block') {
                    var ul_h = ul.height();
                    ul.css('top', -ul_h).hide();
                    ul.parent('li').removeClass('open').animate({
                        height: '-='+ul_h
                    });
                }
            })
            
            // Shrink active
            var ul_h = $('li.active ul').height();
            $('li.active ul').animate({
                top: -ul_h + 'px'
            });
            var a_h = $('li.active a').height();
            $('li.active').animate({
                height: a_h + 'px'
            }, function(){
                $(this).removeClass('active');
            });
            
            // Open selected
            if (!close_active) {
                var ul = $(this).parent('li').children('ul');
                if (ul.size()) {
                    var ul_h = 0;
                    ul.show();
                    ul.children('li').each(function(){
                        ul_h += $(this).outerHeight();
                    });
                    ul.css('top', -ul_h + 'px').show().animate({
                        top: 0 + 'px'
                    });
                    
                    var li = ul.parent('li');
                    var li_closed_height = li.height();
                    var li_open_height = li.addClass('active').css('height', 'auto').height();
                    li.removeClass('active').css('height', li_closed_height);
                    li.addClass('active').animate({
                        height: li_open_height
                    }, function(){
                        //$(this).css('height', 'auto');
                    });
                }
            }
            
        });
        
        
        // Main navigation - Sub level
        $('ul.menu ul').children('li').children('a').click(function(event){
            //event.preventDefault();
            
            // If the list element has a ul inset
            if ($(this).parent('li').children('ul').size()) {
                
                var parent_li = $(this).parent().parent().parent();
                var li = $(this).parent('li');
                
                var ul = li.children('ul');
                var ul_h = ul.height();
                var li_h = li.height();
                if (ul.css('display') == 'none') {
                    li.addClass('open').animate({
                        height: '+='+ul_h
                    });
                    parent_li.animate({
                        height: '+='+ul_h
                    });
                    ul.css('top', '0px').show();
                } else {
                    li.removeClass('open').animate({
                        height: '-='+ul_h
                    }, function(){
                        ul.hide();
                    });
                    parent_li.animate({
                        height: '-='+ul_h
                    });
                }
            }
        });
        
        
    });
    
    

    // Scroll top 
    jQuery(document).ready(function($){
    
        $('body').append('<a id="scrollTop"></a>');
        
        var _c = $('#main').offset();
        $('#scrollTop').css('left', (_c.left + 1020)+'px').fadeIn('slow');
        $(window).resize(function() {
            var _c=$('#main').offset();
            $('#scrollTop').css('left',(_c.left+1020)+'px');
        });
        
        if ($(window).scrollTop() != 0) {
            $('#scrollTop').css('opacity', 1).stop().animate({
                top: $(window).scrollTop() + 300
            }, 750, 'swing');
        }
        
        $(window).scroll(function() {
            $('#scrollTop').css('opacity', 1).stop().animate({
                top: $(window).scrollTop() + 300
            }, 750, 'swing');}
        );
        
        $('#scrollTop').click(function(){
            $('html,body').stop().animate({
                scrollTop: 0
            }, 500, 'swing');
        })
    
    });
    
