


/************************************************************************************************************
(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 flyingSpeed = 8;
var shopping_cart_div = false;
var flyingDiv = false;
var currentProductDiv = false;
var shopping_cart_x = false;
var shopping_cart_y = false;
var shopping_cart_width = false;
var shopping_cart_height = false;
var slide_xFactor = false;
var slide_yFactor = false;
var diffX = false;
var diffY = false;
var diffWidth = false;
var diffHeight = false;
var flytime = false;

var currentXPos = false;
var currentYPos = false;
var currentWidth = false;
var currentHeight = false;
//var origScale = 100;
//var currentScale = 100;
//var origOpacity = 100;
//var currentOpacity = 100;

var ajaxObjects = new Array();
var dropdown = false;
var op = false;

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 shoppingCart_getWidth(inputObj)
{
  children = inputObj.getElementsByTagName("IMG");
  if (children.length>0) inputObj = children[0];
  var returnValue = inputObj.offsetWidth;
//  while((inputObj = inputObj.offsetParent) != null){
//  	if(inputObj.tagName!='HTML')returnValue += inputObj.offsetWidth;
//  }
  return returnValue;
}

function shoppingCart_getHeight(inputObj)
{
  children = inputObj.getElementsByTagName("IMG");
  if (children.length>0) inputObj = children[0];
  var returnValue = inputObj.offsetHeight;
//  while((inputObj = inputObj.offsetParent) != null){
//  	if(inputObj.tagName!='HTML')returnValue += inputObj.offsetHeight;
//  }
  return returnValue;
}



function addToBasket(productId,divId, getVars)
{

//	if (!checkRequiredOptions()) return false;

	if (!checkRequiredOptions() ) {
		alert('Please choose from the required options');
		return false;

	}
	var shopping_cart_div = document.getElementById(divId);

	if(!shopping_cart_div){
		var shopping_cart_div = document.getElementById('catch');

	}


	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);
	shopping_cart_width = shoppingCart_getWidth(shopping_cart_div);
	shopping_cart_height = shoppingCart_getHeight(shopping_cart_div);

	currentProductDiv = document.getElementById(productId);
	currentXPos = shoppingCart_getLeftPos(currentProductDiv);
	currentYPos = shoppingCart_getTopPos(currentProductDiv);
	currentWidth = shoppingCart_getWidth(currentProductDiv);
	currentHeight = shoppingCart_getHeight(currentProductDiv);
	if(shopping_cart_width >60){
		shopping_cart_width=40;
	}
	if(shopping_cart_height <60){
		shopping_cart_height=40;
	}

	diffX = shopping_cart_x - currentXPos;
	diffY = shopping_cart_y - currentYPos;
	diffWidth = shopping_cart_width - currentWidth;
	shopping_cart_height = (shopping_cart_width * currentHeight)/currentWidth
	diffHeight = shopping_cart_height - currentHeight;
	var shoppingContentCopy = currentProductDiv.cloneNode(true);
	shoppingContentCopy.id='clone';
	flyingDiv.style.zIndex = 10000000;
	flyingDiv.innerHTML = '';
	flyingDiv.style.left = currentXPos + 'px';
	flyingDiv.style.top = currentYPos + 'px';

	flyingDiv.appendChild(shoppingContentCopy);
	flyingDiv.style.display='block';
	flyingDiv.style.border = '0px groove #999999';
	shoppingContentCopy.style.width='70px';
   //  if ( (navigator.appName).indexOf('Internet Explorer') > 0 ) {  flyingDiv.style.filter = 'alpha(opacity='+currentOpacity+')'; }
   // else { flyingDiv.style.MozOpacity = '.'+currentOpacity; }

	flyToBasket(productId);
	new Ajax.Updater(divId,'/index.php?ajax=1&'+getVars,{asynchronous:true, parameters:Form.serialize('addtobasket'), onLoading:function(){ cartLoading();},onComplete: function(){ cartLoaded(); }  } );
}

function flyToBasket(productId)
{
	var maxDiff = Math.max(Math.abs(diffX),Math.abs(diffY));
	var moveX = (diffX / maxDiff) * flyingSpeed;
	var moveY = (diffY / maxDiff) * flyingSpeed;
	var flyingScaleSpeed = Math.max(Math.abs(diffX)/moveX,Math.abs(diffY)/moveY);
	var moveWidth = (diffWidth / flyingScaleSpeed);
	var moveHeight = (diffHeight / flyingScaleSpeed);
	//alert("MoveX=" + moveX + ", MoveY=" + moveY + ", MaxDiff=" + maxDiff + ", MoveWidth=" + moveWidth + ", MoveHeight=" + moveHeight + ", MaxScale=" + maxScale);
	if(moveWidth < 0){
		moveWidth=moveWidth*(-1);
	}
	if(moveHeight < 0){
		moveHeight=moveHeight*(-1);
	}
	currentXPos = currentXPos + moveX;
	currentYPos = currentYPos + moveY;
	currentWidth = currentWidth - moveWidth;
	currentHeight = currentHeight - moveHeight;

	flyingDiv.style.left = Math.round(currentXPos) + 'px';
	flyingDiv.style.top = Math.round(currentYPos) + 'px';

	flyingDivChildren = flyingDiv.getElementsByTagName("IMG");
	flyingDivImage = flyingDivChildren[0];

	flyingDivImage.style.width = Math.round(currentWidth) + 'px';
	flyingDivImage.style.height = Math.round(currentHeight) + '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);
}

