/* ***********************
    Shopping Cart Management Functions
    *********************** */

var imgProductFlyStep;
var imgProductFlyStartDim = {x:0,y:0};
var imgProductFlyEndDim   = {x:0,y:0};
var productCartList       = new Array();

/* ***********************
    Showing the small shopping cart
    *********************** */
function productCartShow()
{
    // initialize objects
    var divShopCart          = document.getElementById ('divShopCart');
    var divShopCartProducts  = document.getElementById ('divShopCartProducts');
    var divShopCartLink      = document.getElementById ('divShopCartLink');
    var spanShopCartNumItems = document.getElementById ('spanShopCartNumItems');
    var imgShopCartProduct   = document.getElementById ('imgShopCartProduct');
    var imgShopCartWait      = document.getElementById ('imgShopCartWait');
    var imgShopCartArrow     = document.getElementById ('imgShopCartArrow');

    if (! (divShopCart && divShopCartProducts && divShopCartLink &&
           imgShopCartProduct && imgShopCartWait && imgShopCartArrow &&
           spanShopCartNumItems)
       )
       return;

    // stop flying product thumbnail & hide hourglass
    imgProductFlyStep = 1;
    imgShopCartWait.style.display = 'none';
    imgShopCartProduct.style.display = 'none';
    
    // show the number of items
    spanShopCartNumItems.innerHTML = productCartList.length.toString();
    
    // show products
    var productsHTML = '';
    for (var i=0; i<productCartList.length; i++)
    {
        if (i>=5)
            break;
        productsHTML += '<img src="'+imgShopCartArrow.src+'">&nbsp;&nbsp;<a href="'+productCartList[i].prod_url+'">'+productCartList[i].prod_title+'</a><br>';
    }
    divShopCartProducts.innerHTML = productsHTML;
    
    // show link to more products
    divShopCartLink.style.display = (productCartList.length > 5) ? 'block' : 'none';
    
    // show shopping cart
    divShopCart.style.display = (productCartList.length) ? 'block' : 'none';
}

/* ***********************
    Flying product thumbnail image
    *********************** */
function productFlyToCart()
{
    // initialize object
    var imgShopCartProduct = document.getElementById ('imgShopCartProduct');
    if (!imgShopCartProduct)
        return;
        
    // advance fly step and check for finish
    imgProductFlyStep +=0.1;
    if (imgProductFlyStep >= 1)
    {
        imgShopCartProduct.style.display = 'none';
        return;
    }
    
    // calculate currents coords
    var x = imgProductFlyStartDim.x * (1-imgProductFlyStep) + 
            imgProductFlyEndDim.x   *    imgProductFlyStep;
    var y = imgProductFlyStartDim.y * (1-imgProductFlyStep) + 
            imgProductFlyEndDim.y   *    imgProductFlyStep;

    // move flying product thumbnail
    imgShopCartProduct.style.left = x.toString()+'px';
    imgShopCartProduct.style.top  = y.toString()+'px';
    
    // wait for next step
    setTimeout ('productFlyToCart()',50);
}

/* ***********************
    Ordering the product
    *********************** */
function productOrder (prod_id,imgProductName,butObject)
{
    // initialize objects
    var divShopCart          = document.getElementById ('divShopCart');
    var imgShopCartProduct   = document.getElementById ('imgShopCartProduct');
    var imgShopCartWait      = document.getElementById ('imgShopCartWait');
    var imgProduct           = document.getElementById (imgProductName);

    if (! (divShopCart && imgShopCartProduct && imgShopCartWait && imgProduct) )
        return;

    // show shopping cart
    divShopCart.style.display = 'block';

    // start flying product thumbnail
    var butDim = getDim (butObject);
    var divDim = getDim (divShopCart);

    imgProductFlyStartDim.x = butDim.x+butDim.w/2-50;
    imgProductFlyStartDim.y = butDim.y+butDim.h/2-50;
    imgProductFlyEndDim.x   = divDim.x+divDim.w/2-50;
    imgProductFlyEndDim.y   = divDim.y+divDim.h/2-50;
    imgProductFlyStep = 0;

    imgShopCartProduct.src = imgProduct.src;
    imgShopCartProduct.style.width   = '100px';
    imgShopCartProduct.style.height  = '100px';
    imgShopCartProduct.style.left    = imgProductFlyStartDim.x.toString()+'px';
    imgShopCartProduct.style.top     = imgProductFlyStartDim.y.toString()+'px';
    imgShopCartProduct.style.display = 'block';
    setTimeout ('productFlyToCart()',50);

    // show waiting hourglass
    if (imgProductFlyEndDim.x < document.body.clientWidth / 2)
    {
        imgShopCartWait.style.left    = '90px';
        imgShopCartWait.style.top     = (imgProductFlyEndDim.y+34).toString()+'px';
    }
    else
    {
        imgShopCartWait.style.left    = (document.body.clientWidth-110).toString()+'px';
        imgShopCartWait.style.top     = (imgProductFlyEndDim.y+34).toString()+'px';
    }
    imgShopCartWait.style.display = 'block';
    

    // send server request
    var head  = document.getElementsByTagName('head').item(0);
    var jsOld = document.getElementById('orderCartAddScript');
    var jsNew = document.createElement('script');

    if (jsOld) head.removeChild(jsOld);

    jsNew.src   = 'order_cart_add.php?prod_id='+prod_id+'&r='+Math.random();
    jsNew.type  = 'text/javascript';
    jsNew.defer = true;
    jsNew.id    = 'orderCartAddScript';
    head.appendChild(jsNew);
}

