var isIE6 = false;

$(window).load(function(){

    // Fade in main image (done in window.load since this consistently fires across all browsers)
    var mainImg = $('#image img');  
    if(mainImg){
        mainImg.css({opacity: 0, display: 'block'});
        mainImg.animate({opacity: 1}, 1000);
    }
    
    // Fade text images in, unless they're part of a #slideshow, in which case let the cross-slide plugin handle it
    $('#text img').filter(function(){
        return $(this).parent('#slideshow').length == 0; 
    }).each(function(){
        $(this).css({opacity: 0, visibility: 'visible'});
        $(this).animate({opacity: 1}, 1000);    
    });
    
    // Topbar menu
    $('#topbar a').each(function(){
        
        $(this).css({color: '#cecfec'});
        $(this).attr({title: ''});
        
        $(this).hover(
            function(){
                $(this).stop();
                $(this).animate({color: '#ffffff'}, 200);

            },
            function(){
                $(this).stop();
                $(this).animate({color: '#cecfec'}, 200);      
            
            }       
        );

        // Animate the dropdowns (if they exist)
        var li = $(this).parent('li');
        var subNav = li.find('ul');         
        if(subNav){

            subNav.css({left: $(this).position().left, opacity: 0, top: 10});
            li.hover(
                function(){                 
                    subNav.stop();
                    subNav.css({display: 'block'});
                    subNav.animate({top: 33, opacity: 1}, 300);

                },
                function(){                 
                    subNav.stop();
                    subNav.animate({top: 10, opacity: 0}, 300, function(){
                        $(this).css({display: 'none'});
                    });
                }  
            );
        }        
    });    

});

$(document).ready(function(){


    // Resize function to make sure menu works properly
    $(window).resize(function(){    
    
        var w = $(window).width();   
        if(!isIE6 && w > 815){
            $('#topbar').css({position: 'fixed'});        
        } else {
            $('#topbar').css({position: 'absolute'});
        }

        if (w < 910){
            $('#footer .leaves').css({display: 'none'});
        } else {
            $('#footer .leaves').css({display: 'block'});
        }
    });
    $(window).trigger('resize');     
    

    
    // Main actions animation
    $('#image ul a').each(function(){
        
        var strong = $(this).find('strong');
        var underline = strong.find('span');
        var text = $(this).find('span.text');
        
        strong.css({color: '#9eacd5'});
        underline.css({borderBottomColor: '#c5c7e8'});

        $(this).hover(
            function(){
                strong.stop();
                strong.animate({color: '#0c109e', left: 5}, 300);
                
                underline.stop();
                underline.animate({borderBottomColor: '#0c109e'}, 300);
                
                text.stop();
                text.animate({left: 5}, 300);                
            },
            function(){
                strong.stop();
                strong.animate({color: '#9eacd5', left: 0}, 300);  
                
                underline.stop();
                underline.animate({borderBottomColor: '#c5c7e8'}, 300);                
                
                text.stop();
                text.animate({left: 0}, 300);                 
            }
        );        
    });
    
    
    // Image link animations
    $('a.img-link').each(function(){
    
        var strong = $(this).find('strong');
        var span = $(this).find('span.text');
        span.css({backgroundPosition: '-50px 50%'});
        
        $(this).hover(
            function(){
                $(this).stop();
                $(this).animate({color: '#ffffff'}, 300);
                
                strong.stop();
                strong.animate({borderBottomColor: '#b3b4db'}, 300);    
                
                if(!isIE6) {
                    span.stop();
                    span.animate({backgroundPosition: '0px 50%'}, 300);                  
                }
            },
            function(){
                $(this).stop();
                $(this).animate({color: '#c3bdd3'}, 300);
                
                strong.stop();
                strong.animate({borderBottomColor: '#5c5eb3'}, 300);
                
                if(!isIE6) {
                    span.stop();
                    span.animate({backgroundPosition: '-50px 50%'}, 300);                 
                }
            }
        );    
    });
    
    
    // Big link animations
    $('ul.big-links a').each(function(){
    
        $(this).css({backgroundPosition: '-50px 1px'});
    
        var strong = $(this).find('strong');
        strong.css({borderBottomColor: '#b4ccee'});
        
        $(this).hover(
            function(){
                if(!isIE6) {
                    $(this).stop();
                    $(this).animate({backgroundPosition: '0px 1px'}, 300);
                }
                
                strong.stop();
                strong.animate({borderBottomColor: '#0c109e'}, 300);    
           
            },
            function(){
                if(!isIE6) {
                    $(this).stop();
                    $(this).animate({backgroundPosition: '-50px 1px'}, 300);
                }
                
                strong.stop();
                strong.animate({borderBottomColor: '#b4ccee'}, 300);
              
            }
        );    
    }); 
    
    
    // Breadcrumbs
    $('#breadcrumbs a').each(function(){
    
        $(this).css({borderBottomColor: '#b4ccee'});
        
        $(this).hover(
            function(){
                $(this).stop();
                $(this).animate({borderBottomColor: '#0c109e'}, 300);   
           
            },
            function(){                
                $(this).stop();
                $(this).animate({borderBottomColor: '#b4ccee'}, 300);              
            }
        );    
    });     
});


window.onbeforeunload = function () { 
    $('ul.big-links a').each(function(){ 
        if(!isIE6) {
            $(this).stop();
            $(this).animate({backgroundPosition: '-50px 1px'}, 300);
        }
   
    }); 
    
    $('a.img-link span.text').each(function(){
        if(!isIE6) {
            $(this).stop();
            $(this).animate({backgroundPosition: '-50px 50%'}, 300);
        }
    });
};

