function whichSibling( $child, $parent ) {
    if ( typeof( $parent ) == 'undefined' ) $parent = $child.parent();
    
    $children = $parent.children();
    
    for ( var i = 0; i < $children.length; i++ ) {
        if ( $( $children[i] )[0] == $child[0] ) return i;
    }
    
    return $children;
}

var clocks = [];

function fadeBoxIn(ev) {
    var $target = $( ev.target );
    var whichSib = whichSibling( $target.parents('.indexBox'), $bodyWrap );
    
    clearTimeout( clocks[ whichSib ] );
    $imgArr[ whichSib ].fadeIn(150);
}

function fadeBoxOut(ev) {
    var $target = $( ev.target );
    var whichSib = whichSibling( $target.parents('.indexBox'), $bodyWrap );
    
    clocks[ whichSib ] = setTimeout( function() {
        $imgArr[ whichSib ].queue([]).fadeOut(600);
    }, 300);
}

$(function() {
    $bodyWrap = $('#bodyContainer');

    $ctaPanel = $('.indexBox', $bodyWrap);
    $imgWraps = $('A', $ctaPanel);
    $imgs = $('IMG', $imgWraps);
    $headlines = $('H2', $ctaPanel);
    
    $imgs.hide();
    
    $imgArr = [];
    for ( var i = 0; i < $imgs.length; i++ ) $imgArr.push( $( $imgs[i] ) );
    
    $imgs.add($headlines);
        
    $imgWraps.hover( fadeBoxIn, fadeBoxOut);
    
    $headlines.hover( fadeBoxIn, fadeBoxOut);
    
    $headlines.click( function(ev) {
        ev.preventDefault();
        $target = $( ev.target );
        
        window.location = $target.siblings('A').attr('href');
    });
    
});