var flyingSpeed = 10;

var shopping_cart_div = false;
var flyingDiv = false;
var currentProductDiv = false;

var shopping_cart_x = false;
var shopping_cart_y = false;

var slide_xFactor = false;
var slide_yFactor = false;

var diffX = false;
var diffY = false;

var currentXPos = false;
var currentYPos = false;

function addToBasket(productId)
{
	if(!shopping_cart_div)shopping_cart_div = document.getElementById('winkelwagen');
	if(!flyingDiv){
		flyingDiv = document.createElement('DIV');
		flyingDiv.style.position = 'absolute';
		document.body.appendChild(flyingDiv);
	}

	shopping_cart_x = shoppingCart_getLeftPos(shopping_cart_div);
	shopping_cart_y = shoppingCart_getTopPos(shopping_cart_div);

	currentProductDiv = document.getElementById('productItem_' + productId);

	currentXPos = shoppingCart_getLeftPos(currentProductDiv);
	currentYPos = shoppingCart_getTopPos(currentProductDiv);

	diffX = shopping_cart_x - currentXPos;
	diffY = shopping_cart_y - currentYPos;



	var shoppingContentCopy = currentProductDiv.cloneNode(true);
	shoppingContentCopy.id='';
	flyingDiv.innerHTML = '';
	flyingDiv.style.left = currentXPos + 'px';
	flyingDiv.style.top = currentYPos + 'px';
	flyingDiv.appendChild(shoppingContentCopy);
	flyingDiv.style.display='block';
	flyingDiv.style.width = currentProductDiv.offsetWidth + 'px';
	flyToBasket(productId);
}


function flyToBasket(productId)
{
	var maxDiff = Math.max(Math.abs(diffX),Math.abs(diffY));
	var moveX = (diffX / maxDiff) * flyingSpeed;;
	var moveY = (diffY / maxDiff) * flyingSpeed;

	currentXPos = currentXPos + moveX;
	currentYPos = currentYPos + moveY;

	flyingDiv.style.left = Math.round(currentXPos) + 'px';
	flyingDiv.style.top = Math.round(currentYPos) + 'px';


	if(moveX>0 && currentXPos > shopping_cart_x){
		flyingDiv.style.display='none';
	}
	if(moveX<0 && currentXPos < shopping_cart_x){
		flyingDiv.style.display='none';
	}

	if (flyingDiv.style.display == 'block')
	{
	    setTimeout('flyToBasket("' + productId + '")',10);
	}
	else
	{
	    //ajaxAddProduct(productId);
	    //alert('hoi');
	    //return true;
	}
}

function shoppingCart_getTopPos(inputObj)
{
  var returnValue = inputObj.offsetTop;
  while((inputObj = inputObj.offsetParent) != null){
  	if(inputObj.tagName!='HTML')returnValue += inputObj.offsetTop;
  }
  return returnValue;
}

function shoppingCart_getLeftPos(inputObj)
{
  var returnValue = inputObj.offsetLeft;
  while((inputObj = inputObj.offsetParent) != null){
  	if(inputObj.tagName!='HTML')returnValue += inputObj.offsetLeft;
  }
  return returnValue;
}

function showCart()
{
    if (document.getElementById('shoppingCart').style.display == 'none')
    {
        Effect.SlideDown('shoppingCart');
    }
}


function addToShoppingCart(product_code, product)
{
    // set the request url
    url = 'add_item.php';
    if (confirm('Plaats in winkelmandje:\n'+ product))
    {
        if (product_code != '')
        {
            // if it is not IE
            if (window.XMLHttpRequest)
            {
                req = new XMLHttpRequest();
                req.onreadystatechange = processReqChange;
                req.open("GET", url + '?ref=xml&product_id=' + product_code, true);
                req.send(null);
            }
            // if IE
            else if (window.ActiveXObject)
            {
                req = new ActiveXObject("Microsoft.XMLHTTP");
                if (req)
                {
                    req.onreadystatechange = processReqChange;
                    req.open("GET", url + '?ref=xml&product_id=' + product_code, true);
                    req.send();
                }
            }
        }
    }
    else
    {
        //alert('Het product: '+ product +' \nis NIET in uw mandje geplaatst');
    }

    // function if the request is done
    function processReqChange()
    {
        var product_name = product;
        // only if req shows "loaded"
        if (req.readyState == 4)
        {
            // only if "OK"
            if (req.status == 200)
            {
                showResult();
            }
            else
            {
                alert("There was a problem retrieving the XML data:\n" + req.statusText);
            }
        }
    }

    function showResult()
    {
        var numerOfItems = new Array();
        var totalPrice = new Array();

        var items = req.responseXML.getElementsByTagName("item");

        for (var i = 0; i < items.length; i++) {
            numerOfItems[i] = getElementTextNS("", "totalitems", items[i], 0);
            totalPrice[i] = getElementTextNS("", "price", items[i], 0);
        }
        obj_totalItems = document.getElementById('cartItems');
        obj_totalPrice = document.getElementById('cartPrice');

        tmp = obj_totalItems.innerHTML;

        obj_totalItems.innerHTML = numerOfItems;
        obj_totalPrice.innerHTML = '&euro; ' + totalPrice;

        if (tmp < numerOfItems)
        {
            //alert('Het product is toegevoegd aan uw winkelmandje');
            addToBasket(product_code);
        }
        else
        {
            alert('Het product: '+ product +'\n is al aanwezig in uw winkelmandje.\n\nProductaantallen kunt u in het winkelmandje aanpassen... ');
        }
    }

    function getElementTextNS(prefix, local, parentElem, index) {
        var result = "";
        if (prefix && isIE) {
            // IE/Windows way of handling namespaces
            result = parentElem.getElementsByTagName(prefix + ":" + local)[index];
        } else {
            result = parentElem.getElementsByTagName(local)[index];
        }
        if (result) {
            // get text, accounting for possible
            // whitespace (carriage return) text nodes
            if (result.childNodes.length > 1) {
                return result.childNodes[1].nodeValue;
            } else {
                return result.firstChild.nodeValue;
            }
        } else {
            return "n/a";
        }
    }
}
