var flyingSpeed = 20;
var url_addProduct = 'addProduct.php';
var url_removeProduct = 'removeProduct.php';
var txt_totalPrice = 'Totaal: ';


var ShopDiv = false;
var gotoBasket = false;
var ProdDiv = 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;

var ajaxObjects = new Array();


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 addToBasket(productId)
{
	if(!ShopDiv)ShopDiv = document.getElementById('shopping_cart');
	if(!gotoBasket){
		gotoBasket = document.createElement('DIV');
		gotoBasket.style.position = 'absolute';
		document.body.appendChild(gotoBasket);
	}
	
	shopping_cart_x = shoppingCart_getLeftPos(ShopDiv);
	shopping_cart_y = shoppingCart_getTopPos(ShopDiv);
	
	
	if(productId.substring(0,1) == "a")
	{
		aanbieding = "a";
		productId = productId.substring(1);
	}
	else
	{
		aanbieding = "";	
	}
	
	ProdDiv = document.getElementById(aanbieding+'productX' + productId);
	
	currentXPos = shoppingCart_getLeftPos(ProdDiv);
	currentYPos = shoppingCart_getTopPos(ProdDiv);
	
	diffX = shopping_cart_x - currentXPos;
	diffY = shopping_cart_y - currentYPos;
	

	
	var shoppingContentCopy = ProdDiv.cloneNode(true);
	shoppingContentCopy.id='';
	gotoBasket.innerHTML = '';
	gotoBasket.style.left = currentXPos + 'px';
	gotoBasket.style.top = currentYPos + 'px';
	gotoBasket.appendChild(shoppingContentCopy);
	gotoBasket.style.display='block';
	gotoBasket.style.width = ProdDiv.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;
	
	gotoBasket.style.left = Math.round(currentXPos) + 'px';
	gotoBasket.style.top = Math.round(currentYPos) + 'px';	
	
	
	if(moveX>0 && currentXPos > shopping_cart_x){
		gotoBasket.style.display='none';		
	}
	if(moveX<0 && currentXPos < shopping_cart_x){
		gotoBasket.style.display='none';		
	}
		
	if(gotoBasket.style.display=='block')setTimeout('flyToBasket("' + productId + '")',10); else ajaxAddProduct(productId);	
}

function showAjaxBasketContent(ajaxIndex)
{
	// Getting a reference to the shopping cart items table
	var itemBox = document.getElementById('shopping_cart_items');
	
var productItems = ajaxObjects[ajaxIndex].response.split('|||');	// Breaking response from Ajax into tokens
	//var productItems = XproductItems[ajaxIndex].split('|||');
	if(document.getElementById('shopping_cart_items_product' + productItems[0])){	// A product with this id is allready in the basket - just add number items
		var row = document.getElementById('shopping_cart_items_product' + productItems[0]);
		var aantal = document.getElementById('aantal_' + productItems[0]);
		 var items = aantal.innerHTML /1;
		items = items + 1;
		aantal.innerHTML = items;
		$temp = productItems[2] * items;
		row.cells[3].innerHTML = $temp.toFixed(2);
		if(items == 2)
		{
			linkje = '<a onclick="removeProductFromBasket(' + productItems[0] + '); return false;" href="#"><img src="images/min.gif" alt="-"/></a>'
			$("#shopping_cart_items_product" + productItems[0] + " td:first").prepend(linkje);
		}
		
		
	}else{	// Product isn't allready in the basket - add a new row
		var tr = itemBox.insertRow(-1);
		tr.id = 'shopping_cart_items_product' + productItems[0]
		
		var td = tr.insertCell(-1);
		
		
		
		var span = document.createElement('span');
		span.id= "aantal_" + productItems[0];
		span.innerHTML = "1";
		span.name = "aantal";
		span.style.marginLeft = '5px';
		span.style.marginRight = '5px';
		td.appendChild(span);
		
		var a2 = document.createElement('A');
		td.appendChild(a2);
		a2.href = '#';
		a2.onclick = function(){ addToBasket(productItems[0]); return false; };
		var img = document.createElement('IMG');
		img.src = 'images/plus.gif';
		img.alt = '+';
		a2.appendChild(img);
		
		var td = tr.insertCell(-1);
		td.innerHTML = productItems[1]; 	// Description
		
		var td = tr.insertCell(-1);
		td.innerHTML = parseInt(productItems[2]).toFixed(2); 	// stuksprijs
		
		var td = tr.insertCell(-1);
		td.innerHTML = parseInt(productItems[2]).toFixed(2); 	// Price	
		
		var td = tr.insertCell(-1);
		var a = document.createElement('A');
		td.appendChild(a);
		a.href = '#';
		a.onclick = function(){ removeProductFromBasket(productItems[0]); return false; };
		var img = document.createElement('IMG');
		img.src = 'images/remove.gif';
		a.appendChild(img);
		//td.innerHTML = '<a href="#" onclick="removeProductFromBasket("' + productItems[0] + '");return false;"><img src="images/remove.gif"></a>';	
	} 


	updateTotalPrice();
	
	ajaxObjects[ajaxIndex] = false;		
	
}

function updateTotalPrice()
{
	var btw = '';
	var subtotaal = '';
	
	// Calculating total price and showing it below the table with basket items
	var totalPrice = 0;
	
	
	$("#shopping_cart_items tr:gt(0)").each(function(i) {
		aantal = $("#shopping_cart_items tr:eq(" + (i+1) + ") td:first span").html();
		prijs = $("#shopping_cart_items tr:eq(" + (i+1) + ") td:eq(2)").html();
        totalPrice = totalPrice + (aantal * prijs);
    });
	 	var btw = totalPrice / 119;
		btw = btw * 19;
		var subtotaal = totalPrice - btw;
		document.getElementById('shop_totaalprijs').innerHTML = txt_totalPrice + totalPrice.toFixed(2);
		document.getElementById('shop_btw').innerHTML = 'BTW: ' + btw.toFixed(2);
		document.getElementById('shop_subtotaal').innerHTML = 'Subtotaal: ' + subtotaal.toFixed(2);
	
	
}

function removeProductFromBasket(productId)
{
	var productRow = document.getElementById('shopping_cart_items_product' + productId);
	
	
	
	var numberOfItemCell = productRow.cells[0];
	if($("#shopping_cart_items_product" + productId + " td:first span").html() == 1)
	{
		$("#shopping_cart_items_product" + productId).remove();
	}
	else{
		
		$oldNumber = $("#shopping_cart_items_product" + productId + " td:first span").html();
		$("#shopping_cart_items_product" + productId + " td:first span").html(($oldNumber/1) -1)
		$newNumber = $("#shopping_cart_items_product" + productId + " td:first span").html()
		$totaaal = $("#shopping_cart_items_product" + productId + " td:eq(3)").html()
		$totaaal = $totaaal / $oldNumber;
		$totaaal = $totaaal * $newNumber;
		$("#shopping_cart_items_product" + productId + " td:eq(3)").html($totaaal.toFixed(2));
		if($("#shopping_cart_items_product" + productId + " td:first span").html() == 1)
		{
			$("#shopping_cart_items_product" + productId + " td:first a:first").remove();
		}
	}
	
	
	updateTotalPrice();
	ajaxRemoveProduct(productId);	
}

function ajaxValidateRemovedProduct(ajaxIndex)
{
	if(ajaxObjects[ajaxIndex].response!='OK')alert('Error while removing product from the database');
	
}

function ajaxRemoveProduct(productId)
{
	var ajaxIndex = ajaxObjects.length;
	ajaxObjects[ajaxIndex] = new sack();
	ajaxObjects[ajaxIndex].requestFile = url_removeProduct;	// Saving product in this file
	ajaxObjects[ajaxIndex].setVar('productIdToRemove',productId);
	ajaxObjects[ajaxIndex].onCompletion = function(){ ajaxValidateRemovedProduct(ajaxIndex); };	// Specify function that will be executed after file has been found
	ajaxObjects[ajaxIndex].runAJAX();		// Execute AJAX function		
}

function ajaxAddProduct(productId)
{
	var ajaxIndex = ajaxObjects.length;
	ajaxObjects[ajaxIndex] = new sack();
	ajaxObjects[ajaxIndex].requestFile = url_addProduct;	// Saving product in this file
	ajaxObjects[ajaxIndex].setVar('productId',productId);

	ajaxObjects[ajaxIndex].onCompletion = function(){ showAjaxBasketContent(ajaxIndex); };	// Specify function that will be executed after file has been found
	ajaxObjects[ajaxIndex].runAJAX();		// Execute AJAX function		
}