/************************************************************************************************************
(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 = 25;
var url_addProductToBasket = 'addToBasket.aspx';

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 ajax = new sack();

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,kw,browseref)
{
    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 = getLeft(shopping_cart_div);
	shopping_cart_y = getTop(shopping_cart_div);	
	currentProductDiv = document.getElementById(productId);
	currentXPos = getLeft(document.getElementById(productId.replace('flyX','base')));
	currentYPos = getTop(document.getElementById(productId.replace('flyX','base')));
	
	diffX = shopping_cart_x - currentXPos;
	diffY = shopping_cart_y - currentYPos;
	
	var ContentCopy = currentProductDiv.cloneNode(true);
	ContentCopy.id='';
	ContentCopy.style.display='';
	
	flyingDiv.innerHTML = '';
	flyingDiv.style.left = currentXPos + 'px';
	flyingDiv.style.top = currentYPos + 'px';
	flyingDiv.appendChild(ContentCopy);
	flyingDiv.style.display='block';
	flyingDiv.style.width = currentProductDiv.offsetWidth + 'px';
	flyToBasket(productId,catRef,tbQuantity,photo,kw,browseref);	
	
}


function flyToBasket(productId,catRef,tbQuantity,photo,kw,browseref)
{
	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 + '","' + kw + '","' + browseref + '")',10); else ajaxAddProduct(productId,catRef,tbQuantity,photo,kw,browseref);
}

/////////////////////////////////////////////////////////////////////////\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

var basketMinHeight = 28
var yOffset = basketMinHeight
var basketMaxHeight = 260
var keepOpen = false 

function dropTheBasket(direction, andUp)
{
    if(!shopping_cart_div)shopping_cart_div = document.getElementById('shopping_cart');
    if(shopping_cart_div.innerText==' My Basket: 0 items') return false;
    
    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 ClosePanes(obj) {
    var acc = $find(obj+'_AccordionExtender');
    acc.set_SelectedIndex(-1);
}
    
//\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\/////////////////////////////////////////////////////////////////////////

function ajaxAddProduct(productId,catRef,qty,photo,kw,browseref)
{
    var quant = document.getElementById(qty).value;
    document.getElementById("ctl00_lblbasketphoto").innerHTML = '<img src="' + photo + '" />';
	ajax = new sack();
	ajax.requestFile = url_addProductToBasket;
	ajax.setVar('CatRef',catRef);
	ajax.setVar('Quantity',quant);
	ajax.setVar('ProductID',productId);
	ajax.setVar('browseref',browseref);
	ajax.setVar('KW',kw);
	ajax.onCompletion = whenCompleted;
	ajax.runAJAX();
}

function whenCompleted(){
    var response = ajax.response.split(',');
    var productID = response[0];
    var quantity = response[1];
    var itemCount = response[2];
    var basketTotal = response[3];
    var lastcatref = response[4];
    var lastqty = response[5];
    
    if (itemCount=='1'){
       document.getElementById("ctl00_lblBasket").innerHTML = 'My Basket: 1 item';
       document.getElementById("ctl00_lblBasketFooter").innerHTML = '<u>1 item</u>';
        }
    else{
       try{
        $get("ctl00_lblbasket").innerHTML = 'My Basket: ' + itemCount + ' items';
       }
       catch (Err){
        
       }
        document.getElementById("ctl00_lblBasketFooter").innerHTML = '<u>' + itemCount + ' items</u>';
    }
    document.getElementById("ctl00_litlastitem").innerHTML = 'Last Item Added';
    document.getElementById("ctl00_lblbasketproductname").innerHTML = lastcatref + '<br />Qty = ' + lastqty;
  	dropTheBasket(1,true);
}

