var flyingSpeed = 12;
var url_addProductToBasket = 'addProduct.php';
var url_removeProductFromBasket = 'removeProduct.php';
var txt_totalPrice = 'Total: ';


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 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(!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;
	

	
	var shoppingContentCopy = currentProductDiv.cloneNode(true);
	shoppingContentCopy.id='';
	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';

	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;
	
	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 + '")',10); else ajaxAddProduct(productId);	
}

function ajaxAddProduct(productId)
{
	var ajaxIndex = ajaxObjects.length;
	
	if(document.getElementById('iQty'))
		Qty = document.getElementById('iQty').value;
	else
		Qty = 1;
	var url = "addProduct.php?iProductId="+productId+"&iQty="+Qty;
	if (window.XMLHttpRequest)
  	{
  		http=new XMLHttpRequest()
  		http.open("GET",url,true)
	  	http.onreadystatechange=ShoppingBag
	  	http.send(null)
  	}
	else if (window.ActiveXObject)
  	{
  		http=new ActiveXObject("Microsoft.XMLHTTP")
    	if (http)
    	{
    		http.open("GET",url,true)
    		http.onreadystatechange=ShoppingBag
    		http.send()
    	}
  	}
}

function ShoppingBag()
{
	if (http.readyState == 4) 
	{	
      	isWorking = false;
    	if (http.responseText.indexOf('invalid') == -1) 
		{
			var xmlDocument = http.responseXML; 
			if(xmlDocument.getElementsByTagName('sbag').item(0))
			{
				var mybag  = xmlDocument.getElementsByTagName('sbag').item(0).firstChild.data;
				var vadded  = xmlDocument.getElementsByTagName('msg').item(0).firstChild.data;
				if(xmlDocument.getElementsByTagName('act').item(0))
					var mode = xmlDocument.getElementsByTagName('act').item(0).firstChild.data;
				else
					var mode = "Add";
					
				if(mode == "Add")
				{
					if(vadded == "1")
						TB_show("My Cart","#TB_inline?height=70&width=450&inlineId=msg_1","");
					else
						TB_show("My Cart","#TB_inline?height=70&width=450&inlineId=msg_2","");
				}
				document.getElementById("shopping_cart").innerHTML = decode64(mybag);
			}	
			else
			{
				document.getElementById("shopping_cart").innerHTML = "";
			}
		}
	}		
}


function RemoveProduct(productId)
{
	var url = "addProduct.php?iProductId="+productId+"&mode=remove";
	
	if (window.XMLHttpRequest)
  	{
  		http=new XMLHttpRequest()
  		http.open("GET",url,true)
	  	http.onreadystatechange=ShoppingBag
	  	http.send(null)
  	}
	else if (window.ActiveXObject)
  	{
  		http=new ActiveXObject("Microsoft.XMLHTTP")
    	if (http)
    	{
    		http.open("GET",url,true)
    		http.onreadystatechange=ShoppingBag
    		http.send()
    	}
  	}

}

var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + //all caps
"abcdefghijklmnopqrstuvwxyz" + //all lowercase
"0123456789+/=";
function decode64(inp)
{
	//This is the output
	var out = ""; 

	//These are the 3 decoded bytes
	var chr1, chr2, chr3 = ""; 
	
	//These are the 4 bytes to be decoded
	var enc1, enc2, enc3, enc4 = ""; 
	
	//Position counter
	var i = 0; 
	
	// remove all characters that are not A-Z, a-z, 0-9, +, /, or =
	var base64test = /[^A-Za-z0-9\+\/\=]/g;

	//Do some error checking
	if (base64test.exec(inp))alert("There were invalid base64 characters in the input text.\n" +"Valid base64 characters are A-Z, a-z, 0-9, ?+?, ?/?, and ?=?\n" +"Expect errors in decoding.");
	inp = inp.replace(/[^A-Za-z0-9\+\/\=]/g, "");

	//Here’s the decode loop.
	do { 
		//Grab 4 bytes of encoded content.
		enc1 = keyStr.indexOf(inp.charAt(i++));
		enc2 = keyStr.indexOf(inp.charAt(i++));
		enc3 = keyStr.indexOf(inp.charAt(i++));
		enc4 = keyStr.indexOf(inp.charAt(i++));
	
		//Heres the decode part. There’s really only one way to do it.
		chr1 = (enc1 << 2) | (enc2 >> 4);
		chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
		chr3 = ((enc3 & 3) << 6) | enc4;
	
		//Start to output decoded content
		out = out + String.fromCharCode(chr1);
	
		if (enc3 != 64) {
			out = out + String.fromCharCode(chr2);
		}
		if (enc4 != 64) {
		out = out + String.fromCharCode(chr3);
	}

	//now clean out the variables used
	chr1 = chr2 = chr3 = "";
	enc1 = enc2 = enc3 = enc4 = "";

	} while (i < inp.length); //finish off the loop
	//Now return the decoded values.
	return out;
}
