/************************************************************************************************************
(C) www.dhtmlgoodies.com, March 2006

This is a script from www.dhtmlgoodies.com. You will find this and a lot of other scripts at our website.	

Terms of use:
You are free to use this script as long as the copyright message is kept intact. However, you may not
redistribute, sell or repost it without our permission.

Version:
	1.0	Released	March. 3rd 2006

Thank you!

www.dhtmlgoodies.com
Alf Magne Kalleland

************************************************************************************************************/

var i_shrink=0; //Variable to store shrink factor
var flyingSpeed = 25;
var url_addProductToBasket = '/javascript/flytobasket/addProduct.php';
var url_removeProductFromBasket = '/javascript/flytobasket/removeProduct.php';
var txt_totalPrice = '&pound;';


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;

var stop_moving = false;

var ajaxObjects = new Array();

var productquant = 0;


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, url, flag, quant)
{
	if (quant == 'undefined'){
		quant = 1;
	}
	productquant = quant;

	if(!shopping_cart_div)shopping_cart_div = document.getElementById('shopping_cart');
	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('slidingProduct' + productId);
	
	currentXPos = shoppingCart_getLeftPos(currentProductDiv);
	currentYPos = shoppingCart_getTopPos(currentProductDiv);
	
	diffX = shopping_cart_x - currentXPos;
	diffY = shopping_cart_y - currentYPos;
	
	scroll(shopping_cart_x, shopping_cart_y);

	
	var shoppingContentCopy = currentProductDiv.cloneNode(true);
	shoppingContentCopy.id='main_copy';

	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';
	stop_moving=false;
	flyToBasket(productId, url, flag);
	
}


function flyToBasket(productId, url, flag)
{
var y=0;	

	if(stop_moving == false){
		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(moveY>0 && currentYPos >= shopping_cart_y){
			stop_moving=true;
		}
		y=currentYPos - shopping_cart_y;
		if(moveY < 0 ){
		   if( y < 0 ){
			stop_moving=true;
 		   }
		}
		setTimeout('flyToBasket("' + productId + '", "'+ url +'", "'+ flag +'")',10); 
	}else{
		//alert("Scaling");
		//ToggleVert( 'main_copy', 5, 100);
		//alert("Adding to db");
		flyingDiv.style.display='none';
		ajaxAddProduct(productId, url, flag);

	}

	// Fly the image so that the top corner is on the basekt position
	// Shrink the image so that it is the height of the basekt
	// Fade the image to nothing into the basket ????

	//if(flyingDiv.style.display=='block')
			//setTimeout('flyToBasket("' + productId + '")',10); 
	//else 

}

function showAjaxBasketContent(ajaxIndex)
{
	if (productquant == undefined){
		productquant = '1';
	}
/****
	<ul id=shopping_cart_items>
		<li id='shopping_cart_items_productX' quant=n >Quant Desc</li>
		<li id='shopping_cart_items_productY' quant=n >Quant Desc</li>
		<li id='shopping_cart_items_productZ' quant=n >Quant Desc</li>
	</ul>
****/
	// Getting a reference to the shopping cart items table
	var list = document.getElementById('shopping_cart_items');

	var productItems = ajaxObjects[ajaxIndex].response.split('|||');
	if(productItems[0] == 'alert'){
		alert(productItems[1]);
		ajaxObjects[ajaxIndex] = false;		
		return(false);
	}
	if(document.getElementById('shopping_cart_items_product0')){
	    //This is an empty cart so we need to remove the text
	    document.getElementById('shopping_cart_items_product0').style.display='none';
	}

	if(document.getElementById('shopping_cart_items_product' + productItems[0])){	// A product with this id is allready in the basket - just add number items
		var li = document.getElementById('shopping_cart_items_product' + productItems[0]);
		var quant = li.getAttribute('quant');
		quant=parseInt(quant)+parseInt(productquant);
		li.setAttribute('quant', quant);
		li.innerHTML = "<strong>"+quant+"&nbsp;x&nbsp;</strong><u>"+productItems[1]+"</u>&nbsp;";

	}else{
		// Create the list item item
		var li = document.createElement('div');

		// Add the part number, quantity and price as an attribute
		li.setAttribute('quant', productquant);
		li.id = 'shopping_cart_items_product'+productItems[0];
		li.setAttribute('price', productItems[2]);
		li.setAttribute('class', 'basket_list');	
		li.setAttribute('className', 'basket_list');	
		li.setAttribute('desc', productItems[1]);

		// Add the description and quantity as part of the text
		li.innerHTML = "<strong>"+productquant+"&nbsp;x&nbsp;</strong><u>"+productItems[1]+"</u>&nbsp;";

		// Append the item to the end of the list
		list.appendChild(li);

    }
		// Add the "remove item link"
		var a = document.createElement('A');
		li.appendChild(a);
		a.href = '#';
		a.onclick = function(){ removeProductFromBasket(productItems[0]); };
		var img = document.createElement('IMG');
		img.src = '/javascript/flytobasket/images/remove.gif';
		a.appendChild(img);	

	
	updateTotalPrice();
	
	ajaxObjects[ajaxIndex] = false;		
}

function updateTotalPrice()
{
    
	// Calculating total price and showing it below the table with basket items
	var totalPrice = 0;
	var quant = 0;
	var price = 0;

	if(document.getElementById('shopping_cart_totalprice')){
	    var ul = document.getElementById('shopping_cart_items');
	    var count = ul.childNodes.length;
	
	    for(var no=1; no<count; no++){
		    var li = ul.childNodes[no];
		    if(li.getAttribute){
			    price = li.getAttribute('price');
			    quant = li.getAttribute('quant');

			    gross = price * quant;
			    totalPrice+=gross;
		    }
	    }

	    document.getElementById('shopping_cart_totalprice').innerHTML = txt_totalPrice + totalPrice.toFixed(2);	
	}	
	
}
/**
*   Remove the product item from the list, or reduce the quantity by one
*   Then send the command to the server
*/
function removeProductFromBasket(productId)
{
	var li = document.getElementById('shopping_cart_items_product' + productId);

	var quant = li.getAttribute('quant');
	quant--;

	var desc = li.getAttribute('desc');
	
	if(quant > 0){
		li.setAttribute('quant', quant);
		li.innerHTML = "<b>"+quant+"</b> "+desc;

		// Add the "remove item link"
		var a = document.createElement('A');
		li.appendChild(a);
		a.href = '#';
		a.onclick = function(){ removeProductFromBasket(productId); };
		var img = document.createElement('IMG');
		img.src = '/javascript/flytobasket/images/remove.gif';
		a.appendChild(img);	

	}else{
		var list = document.getElementById('shopping_cart_items');
		list.removeChild(li);
	}

	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_removeProductFromBasket;	// 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, url, flag)
{
	if(flag == 'undefined') flag = '';
	var ajaxIndex = ajaxObjects.length;
	ajaxObjects[ajaxIndex] = new sack();
	ajaxObjects[ajaxIndex].requestFile = url_addProductToBasket;	// Saving product in this file
	ajaxObjects[ajaxIndex].setVar('productId',productId);
	ajaxObjects[ajaxIndex].setVar('url',url);
	ajaxObjects[ajaxIndex].setVar('flag',flag);
	ajaxObjects[ajaxIndex].setVar('quant',productquant);

	ajaxObjects[ajaxIndex].onCompletion = function(){ showAjaxBasketContent(ajaxIndex); };	// Specify function that will be executed after file has been found
	ajaxObjects[ajaxIndex].runAJAX();		// Execute AJAX function		
}

