
$(function ( ) {
    var height = 0,
        wHeight = parseInt($(window).height()),
        smallWindow = 720;
    
    try {
        height = $(document.getElementById('background-cover').parentNode).height();
    } catch ( e ) {
        height = 0;
    }
    
    function isSmallWindow ( ) {
        return parseInt($(window).height()) < smallWindow;
    }
    
    $('#background-cover').css('opacity', 0.75);
    
    $('.gallery-images').fancybox();
    $('.fancybox').fancybox();
    
    $('.image-hover')
        .find('img')
            .css('opacity', 0)
        .end()
        .bind('mouseover', function ( ) {
            $(this).find('img').stop(false, false).fadeTo(250, 1);
        })
        .bind('mouseout', function ( ) {
            $(this).find('img').stop(false, false).fadeTo(250, 0);
        });
    
    $('#mcs_container').mCustomScrollbar('vertical', 400, 'easeOutCirc', 1.05, 'auto', 'yes', 'yes', 10);
    
    (function ( ) {
        if ( isSmallWindow() ) {
            $('#page-panel-shadow').css('margin-top', '405px');
            $('#locations-panel').css({'height': '65px', 'margin-top': '0'});
        }
        
        setTimeout(function ( ) {
            if ( isSmallWindow ) {
                $('#page-panel-shadow').animate({ 'margin-top': '0' }, 1000);
            }
            
            $('#page-panel')
                .stop(false, false)
                .animate({ height: '450px', 'margin-top': '0' }, 1000, function ( ) {
                    $('#locations-panel')
                        .stop(false, false)
                        .animate({'height': '40px'}, 1000)
                        .bind('mouseover', function ( ) {
                            $('#locations-panel').stop(false, false).animate({'height': '130px', 'margin-top': '-90px'}, 500)
                        })
                        .bind('mouseout', function ( ) {
                            $('#locations-panel').stop(false, false).animate({'height': '40px', 'margin-top': '0'}, 1000)
                        });
                })
                .find('a')
                    .removeClass('open')
                    .addClass('close');
        }, 3000);
    })();
    
    $('#panel-toggle a').bind('click', function ( e ) {
        e.preventDefault();
        
        if ( this.className === 'close' ) {
            if ( isSmallWindow() ) {
                $('#page-panel-shadow').stop(false, false).animate({'margin-top': '405px'}, 500);
            }
            
            $('#page-panel').stop(false, false).animate({ height: '45px' }, 500);
            this.className = 'open';
        } else {
            $('#page-panel-shadow').stop(false, false).animate({'margin-top': '0'}, 500);
            $('#page-panel').stop(false, false).animate({'height': '450px'}, 500);
            this.className = 'close';
        }
        
        return false;
    });
    
    
    function adjustPanels ( ) {
        wHeight = parseInt($(window).height());
        
        if ( isSmallWindow() ) {
            $('#panel-container').addClass('small-window');
            
            if ( $('#panel-toggle a').is('.open') ) {
                $('#page-panel-shadow').css('margin-top', '405px');
            } else {
                $('#page-panel-shadow').css('margin-top', '0');
            }
        } else {
            $('#panel-container').removeClass('small-window');
        }
    }
    
    $(window).resize(adjustPanels);
    adjustPanels();
    
    
    if ( /MSIE 7/.test(window.navigator.userAgent) ) {
        $('.shadow_s').width($(window).width() + 'px');
        $('.shadow_n').width($(window).width() + 'px');
    }
    
    
    if ( $('#sidebar-image').size() > 0 ) {
        var img = document.getElementById('sidebar-image');
        var sidebar_i = 1;
        
        setInterval(function ( ) {
            
            if ( sidebar_i > 5 ) {
                sidebar_i = 1;
            }
            
            img.src = '/wp-content/themes/twentyeleven/images/sidebar-image_' + sidebar_i + '.jpg';
            
            sidebar_i++;
        }, 3000);
    }
});
