$ = jQuery.noConflict();
$(document).ready(function() {
    // jQuery Cycle (Home Page Slideshow)
    $('#slideshow').cycle();
    
    // Directors Left Hand Carousel Navigation
    // Hide subcategories except the section we are in
    $('ul#categories li.cat-item:not(.current-cat) ul.children').hide();
    if ($('ul#categories li.current-cat').length > 0) {
        $('ul#categories').find('li.current-cat').parent().show();
    } else {
        $('ul.children', $('ul#categories').find('a.selected').parent()).show();
    }
    
    // Show subcategories when a category is clicked
    $('ul#categories li.cat-item a.top-cat').bind('click', function(e) {
        e.preventDefault(); // Don't load URL
        $('ul#categories a.selected').removeClass('selected'); // Remove selected class
        $(this).addClass('selected'); // Add selected class
        $('ul#categories li.cat-item ul.children').hide(); // Hide all
        $('ul.children', $('ul#categories').find('a.selected').parent()).show();
    });
    
    // Quotes on Ethos Page
    if ($('div.quote').length > 0) {
        $('div.quote').children('div').each(function(i) {
            if (i > 0) $(this).addClass('hidden');
        });
    }
    $('ul#quote-paginator li a').bind('click', function(e) {
        e.preventDefault(); // Don't reload page
        $('ul#quote-paginator li').removeClass('selected'); // Remove selected from all carousel boxes
        $(this).parent().addClass('selected'); // Mark clicked box as selected
        quoteID = $(this).attr('href').split('?quote=')[1]; // Get quote ID we've clicked
        $('#ethos blockquote p').fadeOut(500, function() {
            $('#ethos blockquote p').html($('div.quote-'+quoteID).html()); // Change quote    
            $('#ethos blockquote p').fadeIn(500); // Fade in selected quote
            Cufon.replace('#ethos blockquote p', { fontFamily: 'Variable' });    
        }); // Fade out existing quote        
    });
    
    // Videos
    $('div#leader-video a[rel=video]').bind('click', function(e) { e.preventDefault(); });
    
    // Menu for IE6
    if ($.browser.msie && $.browser.version <= 6) {
        $('#menu ul li').hover(
            function() {
                $('ul', $(this)).css('visibility', 'visible');
            },
            function() {                
                $('ul', $(this)).css('visibility', 'hidden');
            }
        );
    }
});

/**
* Vertically centres an image with class .scale within the parent DIV container
* 
* Scaling is performed in CSS 
*/
function RepositionImage() {
    imageWindowHeight = $('img.scale').parent().height(); // Height of div image is contained in
    imageWidth = $('img.scale').width(); // Height of image (already scaled by CSS)
    imageHeight = $('img.scale').height(); // Height of image (already scaled by CSS)
    overhang = imageHeight - imageWindowHeight; // Height of image hidden
    if (overhang > 0) $('img.scale').css('margin-top', '-'+(overhang/2)+'px');    
}

