/**
* 2008 ArmazemL
*/

var flyingDiv = false;
var flyingSpeed = 25;

var DIVMiniCesto = false;
var DIVMiniCestoX = false;
var DIVMiniCestoY = false;
var IMGMiniCesto = false;

var DIVProduto=false;
var DIVProdutoX=false; DIVProdutoW=false;
var DIVProdutoY=false; DIVProdutoH=false;

var diffX = false; var diffW = false;
var diffY = false; var diffH = false;

var ajaxObjects = new Array();

function LojaAddProduto(IDProduto) {
	/*try { var usaAjax = SNAjax; } catch(err) { var usaAjax=false; }
    if (!usaAjax) {
        try { var novaQtd = document.getElementById('QTDProduto'+IDProduto).value; } catch(err) { var novaQtd =1; }
    
        document.Listagem.accao.value='AdicionarProduto';
        document.Listagem.IDProduto.value=IDProduto;
        document.Listagem.QTDProduto.value=novaQtd;
        document.Listagem.submit();
    } else {*/
    		changeIMGCart(true);
    		// sets the image for a given time
    		setTimeout("changeIMGCart(false)",1250);
        if(!DIVMiniCesto) DIVMiniCesto = document.getElementById('shopppingCart');
        if(!flyingDiv){
            flyingDiv = document.createElement('DIV');
            flyingDiv.style.position = 'absolute';
            document.body.appendChild(flyingDiv);
        }
        DIVMiniCestoX = DIVGetPosX(DIVMiniCesto);
        DIVMiniCestoY = DIVGetPosY(DIVMiniCesto);
    
        DIVProduto = document.getElementById('inputs'+IDProduto);    
        DIVProdutoX = DIVGetPosX(DIVProduto); DIVProdutoW = DIVProduto.offsetWidth;
        DIVProdutoY = DIVGetPosY(DIVProduto); DIVProdutoH = DIVProduto.offsetHeight;
    
        diffX = DIVMiniCestoX - DIVProdutoX; diffW = DIVMiniCesto.offsetWidth - DIVProdutoW;
        diffY = DIVMiniCestoY - DIVProdutoY; diffH = DIVMiniCesto.offsetHeight - DIVProdutoH;

        var inputs = document.getElementById('inputs'+IDProduto); inputs.style.display='none';
        var TMPProdutoClone = DIVProduto.cloneNode(true);
        inputs.style.display='';
        flyingDiv.innerHTML = '';
        flyingDiv.style.left = DIVProdutoX + 'px';
        flyingDiv.style.top = DIVProdutoY + 'px';
        flyingDiv.style.border = '1px solid #808080';
        flyingDiv.appendChild(TMPProdutoClone);
        flyingDiv.style.display='block';
        flyingDiv.style.width = DIVProduto.offsetWidth + 'px';
        flyToBasket(IDProduto);
    //}    
}

function flyToBasket(IDProduto) {
    var maxDiff = Math.max(Math.abs(diffX),Math.abs(diffY));
    var moveX = (diffX / maxDiff) * flyingSpeed;
    var moveY = (diffY / maxDiff) * flyingSpeed;    
    
    DIVProdutoX = DIVProdutoX + moveX; flyingDiv.style.left = Math.round(DIVProdutoX) + 'px';
    DIVProdutoY = DIVProdutoY + moveY; flyingDiv.style.top = Math.round(DIVProdutoY) + 'px';

    var tamDiff = Math.max(Math.abs(diffW),Math.abs(diffH));
    var novoW = (diffW / tamDiff) * flyingSpeed;
    var novoH = (diffH / tamDiff) * flyingSpeed;    

    DIVProdutoW = DIVProdutoW + novoW; if (DIVProdutoW>0) { flyingDiv.style.width = Math.round(DIVProdutoW) + 'px'; }
    DIVProdutoH = DIVProdutoH + novoH; if (DIVProdutoH>0) { flyingDiv.style.height = Math.round(DIVProdutoH) + 'px'; }

    if(moveX>0 && DIVProdutoX > DIVMiniCestoX) { flyingDiv.style.display='none'; }
    if(moveX<0 && DIVProdutoX < DIVMiniCestoX) { flyingDiv.style.display='none'; }
    
    if(flyingDiv.style.display=='block')setTimeout('flyToBasket("' + IDProduto + '")',10); //else ajaxLojaAdProduto(IDProduto);
}

function changeIMGCart(enhanced) {
	if(!IMGMiniCesto) IMGMiniCesto = document.getElementById('shoppingCartIMG');
	if (enhanced == true){
		IMGMiniCesto.src = 'images/header_cart_full.png'
	} else {
		IMGMiniCesto.src = 'images/header_cart.png'
	}
}