// ajax cart summary section
$(document).ready(function() {

    $('#cart_link').append('<div id="cart_summary"></div>') ;

    wc_summary_on = false; // track whether mouse is over the cart links or summary box
    cstimer = null; 

    $('#cart_link, #cart_summary').mouseover(function() {
        wc_summary_on = true;
        display_cartsummary();

    }).mouseout(function() {
        wc_summary_on = false;

        // we use a timer to avoid instant hiding of the summary 
        // (in case there are gaps between the links and the summary box)
        if (!cstimer) cstimer = setTimeout(display_cartsummary, 500);
    });

    // basically, we turn a flag on/off when we rollover/out of the desired objects
    // then use this function to check what to do
    function display_cartsummary() {
        if (wc_summary_on) {
            if ($('#cart_summary').html()=='') {
            // only loads the view if the summary box is empty (avoid multiple ajax calls while first one is loading)

                $('#cart_summary').html('Loading...');
                $('#cart_summary').load('/cart/ajax/cart-summary/');
                $('#cart_summary').show('fast');
            }
        }
        else {
            $('#cart_summary').hide().html('');
        }
        cstimer = null;
    }
    $('#cart_summary').hide();
});

