var flyingSpeed = 25;
var shopping_cart_div = false;
var flyingDiv = false;
var currentProductDiv = false;
var shopping_cart_x = false;
var shopping_cart_y = false;
var diffX = false;
var diffY = false;
var currentXPos = false;
var currentYPos = false;

function getTop(obj) {
  var returnValue = obj.offsetTop;
  while((obj = obj.offsetParent) != null)if(obj.tagName!='HTML')returnValue += obj.offsetTop;
  return returnValue;
}

function getLeft(obj){
  var returnValue = obj.offsetLeft;
  while((obj = obj.offsetParent) != null)if(obj.tagName!='HTML') returnValue += obj.offsetLeft;
  return returnValue;
}
	
function addToBasket(productId,catRef,tbQuantity,photo)
{
	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);
	}
	currentProductDiv = document.getElementById(productId);	
	shopping_cart_x = getLeft(shopping_cart_div);
	shopping_cart_y = getTop(shopping_cart_div);	
	currentXPos = getLeft(document.getElementById(productId.replace('fly','base')));
	currentYPos = getTop(document.getElementById(productId.replace('fly','base')));
								
	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.innerHTML = flyingDiv.innerHTML.replace(' style="DISPLAY: none"', '','i');    // IE
	flyingDiv.innerHTML = flyingDiv.innerHTML.replace(' style="DISPLAY: none;"', '','i');   // Firefox
	flyingDiv.innerHTML = flyingDiv.innerHTML.replace(' style="display:none"', '','i');     // Safari
	flyingDiv.style.display='block';
	flyingDiv.style.width = currentProductDiv.offsetWidth + 'px';
	flyToBasket(productId,catRef,tbQuantity,photo);	
}

function flyToBasket(productId,catRef,tbQuantity,photo)
{
	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 + '","' + catRef + '","' + tbQuantity + '","' + photo + '")',10); else updateBasket();
}

/////////////////////////////////////////////////////////////////////////\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

var basketMinHeight = 28
var yOffset = basketMinHeight
var basketMaxHeight = 228
var keepOpen = false

function dropTheBasket(direction, andUp)
{
    if (keepOpen == false) 
        yOffset += (direction * 10)	
    else 
        yOffset = basketMaxHeight
    
    if (yOffset < basketMinHeight) yOffset=basketMinHeight;
    document.getElementById("downBasket").style.height = yOffset + 'px';	
    
	if (direction > 0) {
	    if (yOffset < basketMaxHeight) 
	        setTimeout('dropTheBasket(1,' + andUp + ')',30); 	        
        else if (andUp==true) setTimeout('dropTheBasket(-1)',1500);
    } else {
        if (keepOpen == false)
	        if (yOffset > basketMinHeight) 
	            setTimeout('dropTheBasket(-1)',30);
    } 
}

function dropBasket() {    
    if (document.getElementById("downBasket").style.height == '' || parseInt(document.getElementById("downBasket").style.height) == 0) {
        yOffset = basketMinHeight;
        dropTheBasket(1, false);
    }
}

function hideBasket() {
    yOffset = basketMaxHeight;
    dropTheBasket(-1,false);
}

function updateBasket() {
    var c = parseInt(document.getElementById("ctl00_ContentPlaceHolder1_lblBasket").innerHTML) + 1;
    document.getElementById("ctl00_ContentPlaceHolder1_lblBasket").innerHTML = c;
    document.getElementById("ctl00_ContentPlaceHolder1_lblFooterTotal").innerHTML = c;
        
    dropTheBasket(1,true);
}