﻿
var timerID = 0;
var timeOut = 600;
var shoppingCartID = '.shopping-cart';
var elementID = '.shopping-cart .cart-frame';
var elementHeight;
var isScrollingDown = false;

$(document).ready(function () {
    elementHeight = $(elementID).height();
    $(elementID).css('height', 0);

    $(".shopping-cart").hover(
                function () {
                    openCart();
                },
                function () {
                    closeCart();
                }
            );
});

function closeTimer() {
    if (timerID) {
        clearTimeout(timerID);
        timerID = 0;
    }
}

function openCart() {
    // Close the timer
    this.closeTimer();

    // Show the window
    $('.shopping-cart .cart-frame').show();
    $('.shopping-cart').addClass('active');
    isScrollingDown = true;
    $('.shopping-cart .cart-frame').animate({ 'height': elementHeight }, 500, function () {
        isScrollingDown = false;
    });
}

function closeCart() {
    this.closeTimer();

    timerID = setTimeout(function () {
        $('.shopping-cart .cart-frame').animate({ 'height': 0 }, 500, function () {
            if (!isScrollingDown) {
                $('.shopping-cart').removeClass('active');
                $('.shopping-cart .cart-frame').hide();
            }
        })
    }, timeOut);
}
