$(document).ready(function(){
    
    // Category Boxes
    
    if($('html').hasClass('ie6') || $('html').hasClass('ie7')  || $('html').hasClass('ie8')){
        $('.large .cbox:not(.active) .cboxoverlay').show().css('height',0);
        $('.small .cbox:not(.active) .cboxoverlay').show().css('height','100%');   
    }else{
        $('.large .cbox:not(.active) .cboxoverlay').show().css('opacity',0);
        $('.small .cbox:not(.active) .cboxoverlay').show().css('opacity',1);      
    }
    
    $('.large .cbox').hover(
        function(){
            if(!$(this).hasClass('active')){
                if($('html').hasClass('ie6') || $('html').hasClass('ie7')  || $('html').hasClass('ie8')){
                    $('.cboxoverlay',this).stop().animate({height:'100%'});
                }else{
                    $('.cboxoverlay',this).stop().animate({'opacity':1});
                }
                var currheight = $('.cboxlinks',this).css('height').replace('px','');
                var height = $('.cboxlinks',this).css('height','auto').height();
                $('.cboxlinks',this).stop().css({'height':currheight}).animate({'height':height});
                $('.cboxcontent',this).stop().animate({'top':50,'opacity':1});
            }
        },function(){
            if(!$(this).hasClass('active')){
                if($('html').hasClass('ie6') || $('html').hasClass('ie7')  || $('html').hasClass('ie8')){
                    $('.cboxoverlay',this).stop().animate({height:'0'});
                }else{
                    $('.cboxoverlay',this).stop().animate({'opacity':0});
                }
                $('.cboxlinks',this).stop().animate({'height':0});
                $('.cboxcontent', this).stop().animate({'top':'130','opacity':0.5});
            }
        }
    );
    
    $('.small .cbox').hover(
        function(){
            if(!$(this).hasClass('active')){
                if($('html').hasClass('ie6') || $('html').hasClass('ie7')  || $('html').hasClass('ie8')){
                    $('.cboxoverlay',this).stop().animate({height:'0'});
                }else{
                    $('.cboxoverlay',this).stop().animate({'opacity':0});
                }
                $('.cboxcontent', this).stop().animate({'opacity':1});
            }
        },function(){
            if(!$(this).hasClass('active')){
                if($('html').hasClass('ie6') || $('html').hasClass('ie7')  || $('html').hasClass('ie8')){
                    $('.cboxoverlay',this).stop().animate({height:'100%'});
                }else{
                    $('.cboxoverlay',this).stop().animate({'opacity':1});
                }
                $('.cboxcontent', this).stop().animate({'opacity':0.5});
            }
        }
    );
    
    
    
    
    // Site credits
    
    $('#wcreditsbt').click(
        function(){
            $(this).toggleClass('open');
            if($(this).hasClass('open')){
                $('#wcredits').slideDown();
            }else{
                $('#wcredits').slideUp();
            }
        }
    );
    
    
    
    
    
// Search
    
    // Focus the keywords field after the category has been selected
    $('#srchbarmini .srchfield_cat option').click(function(){
        $(this).parent().siblings('.srchfield_keyw').focus();
    });
    
    
    // Synchronise Category Drop downs
    $('#srchbarmini .srchfield_cat option, #srchbarbig .srchfield_cat option').click(function(){
        var val = $(this).parent().val();
        var id = $(this).attr('id').replace('mini_','').replace('big_','');
        $('#srchbarmini .srchfield_cat option, #srchbarbig .srchfield_cat option').each(function(){
            $(this).removeAttr('selected');
        });
        $('#big_'+id+', #mini_'+id).attr('selected','selected');
    });
    
    // Synchronise keyword fields
    $('#srchbarmini .srchfield_keyw, #srchbarbig .srchfield_keyw').blur(function(){
        var val = $(this).val();
        $('#srchbarmini .srchfield_keyw, #srchbarbig .srchfield_keyw').val(val);
    });
    
    $('#srchadvancedbt, #srchadvancedclosebt').click(function(){
        $('#srchadvancedbt').toggleClass('open');
        if($('#srchadvancedbt').hasClass('open')){
            $('#srchbarbig').slideDown();
        }else{
            $('#srchbarbig').slideUp();
        }
    });
    
    $('#srchadvancedsubmit').click(function(){
        $('#search form').submit();
    })
    
    $('.srchfield_cat').change(function(){
        var val = $(this).val();
        console.log(val);
    })
});

