var header_cart_timout;

function open_header_cart(text)
{
    old_text = document.getElementById("header_cart_last_item").innerHTML;
    if (text == '' && old_text == '') {
        window.location = '/cart';
        return;
    }

    if (text != '') {
        document.getElementById("header_cart_last_item").innerHTML = text;
    }

    showCart();

    return false;   // don't follow link
}

var cart;
var timeOut = 5000;
function showCart() {
    if(wishlist.panel != null){
        wishlist.panel.hide();
    }
    cart =
            new YAHOO.widget.Panel(
                'header_cart_last_item',
                {
                  width:'250px',
                  context: [ document.getElementById("top_cart_count") , 'tr', 'br'],
                  iframe:true,
                  close:false,
                  zindex:300,
                  modal:false,
                  constraintoviewport: false,
                  scope:this
                }
            );

    cart.render();
    cart.show();
    buildIframe('header_cart_last_item');
    setTimeout('window.scrollTo(0, 0)',1);
    setTimeout("cart.hide();cart.setBody('');",timeOut);
    setTimeout("destroyIframe();",timeOut);
}

function beginCheckout(){
    cart.hide();
    window.location = "/cart";
}

function close_header_cart()
{
    animate_header_cart(-1);
}
function animate_header_element(id, rate, max)
{
    e = document.getElementById(id);

    // skip the animation for browsers that don't support max-height
    if (typeof(e.style.maxHeight) == "undefined") {
        if (rate > 0) {
            e.style.display = "block";
        } else {
            e.style.display = "none";
        }
        return false;
    }

    h = parseInt(e.style.maxHeight) + rate;
    if (h > max) h = max;
    if (h < 0) h = 0;
    e.style.maxHeight = h + 'px';

    if (h > 0) {
        e.style.display = "block";
    } else {
        e.style.display = "none";
    }

    return (rate > 0 && h < max) || (rate < 0 && h > 0);
}

var header_cart_timer;

function animate_header_cart(dir)
{
    if (header_cart_timer) clearTimeout(header_cart_timer);

    if (dir > 0) {
        if (animate_header_element("header_cart_closed", -dir*50, 30)) {}
        else if (animate_header_element("header_cart_open", dir*50, 200)) {}
        else return;
    } else {
        if (animate_header_element("header_cart_open", dir*50, 200)) {}
        else if (animate_header_element("header_cart_closed", -dir*50, 30)) {}
        else {
            clearTimeout(header_cart_timout);
            return;
        }
    }

    header_cart_timer = setTimeout("animate_header_cart("+dir+")", 100);
}
