/*
	more compact version of shop which incorporates fly-to-basket.js
	
	shopping, wishlist, checkout, gift
	shopping_cart, wishlist_cart, checkout_cart, gift_cart
*/





var _shop_previousOnload	= window.onload;
window.onload = function () { if(_shop_previousOnload) _shop_previousOnload(); initBaskets(); }


function initBaskets()
{	
	if (document.getElementById('shopping_cart')) {
		ajaxGetItems('shopping');				
	}	
	else if (document.getElementById('checkout_cart')) {
		ajaxGetItems('checkout');
	}				
	else if (document.getElementById('gift_cart')) {
		ajaxGetItems('gift');
	}
}

/************************************************************************************************************

************************************************************************************************************/
function processGet(item_type)
{
	processGetPost(item_type + '_cart');
}

function processGetPost(element_id){	
	var myajax		= ajaxpack.ajaxobj;
	var myfiletype	= ajaxpack.filetype;
	
	if (myajax.readyState == 4){ 			
		if (myajax.status==200 || window.location.href.indexOf("http")==-1) { 			
			if (myfiletype=="txt") {
				var obj = document.getElementById(element_id);
				//alert(myajax.responseText);
				obj.innerHTML = myajax.responseText;
			}			
		}
	}
}


/************************************************************************************************************

************************************************************************************************************/
var _shop_ajax_url			= '/ajax/shop_request.asp';

function ajaxAddItem(product_id, item_type)
{
	ajaxpack.getAjaxRequest(_shop_ajax_url, "id=" + product_id + "&a=" + item_type + "&b=add" + _stripped, function() {processGet(item_type);}, "txt")		
}
function ajaxRemoveItem(product_id, item_type)
{		
	ajaxpack.getAjaxRequest(_shop_ajax_url,"id=" + product_id + "&a=" + item_type + "&b=remove" + _stripped , function() {processGet(item_type);}, "txt")		
}
function ajaxGetItems(item_type)
{	
	ajaxpack.getAjaxRequest(_shop_ajax_url,"a=" + item_type + _stripped, function() {processGet(item_type);}, "txt")		
}

/************************************************************************************************************
 Fly to basket code based on code from dhtmlgoodies
 (C) www.dhtmlgoodies.com, March 2006
************************************************************************************************************/
var _flying_speed		= 10;
var _cart_div			= false;
var _cart_x				= false;
var _cart_y				= false;

var flyingDiv			= false;
var currentProductDiv	= false;

var diffX				= false;
var diffY				= false;

var currentXPos			= false;
var currentYPos			= false;


function addToCart(product_id, item_type)
{
	if(!_cart_div)_cart_div = document.getElementById(item_type + '_cart');
	if(!flyingDiv){
		flyingDiv = document.createElement('DIV');
		flyingDiv.style.position = 'absolute';
		document.body.appendChild(flyingDiv);
	}
	
	_cart_x = shoppingCart_getLeftPos(_cart_div);
	_cart_y = shoppingCart_getTopPos(_cart_div);

	currentProductDiv = document.getElementById('slidingProduct' + product_id);
	
	currentXPos = shoppingCart_getLeftPos(currentProductDiv);
	currentYPos = shoppingCart_getTopPos(currentProductDiv);
	
	diffX = _cart_x - currentXPos;
	diffY = _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';
	flyToCart(product_id, item_type);	
}

function flyToCart(product_id, item_type)
{
	var maxDiff = Math.max(Math.abs(diffX),Math.abs(diffY));
	var moveX = (diffX / maxDiff) * _flying_speed;;
	var moveY = (diffY / maxDiff) * _flying_speed;	
	
	currentXPos = currentXPos + moveX;
	currentYPos = currentYPos + moveY;
	
	flyingDiv.style.left = Math.round(currentXPos) + 'px';
	flyingDiv.style.top  = Math.round(currentYPos) + 'px';	
		
	if(moveX>0 && currentXPos > _cart_x){
		flyingDiv.style.display='none';		
	}
	if(moveX<0 && currentXPos < _cart_x){
		flyingDiv.style.display='none';		
	}		
	if(flyingDiv.style.display=='block') {
		setTimeout('flyToCart("' + product_id + '","' + item_type + '")',10); 
	} else {
		ajaxAddItem(product_id, item_type)					
	}
}

function shoppingCart_getTopPos(inputObj)
{		
  var returnValue = inputObj.offsetTop +20;
  while((inputObj = inputObj.offsetParent) != null){
  	if(inputObj.tagName!='HTML')returnValue += inputObj.offsetTop;
  }
  return returnValue;
}

function shoppingCart_getLeftPos(inputObj)
{
  var returnValue = inputObj.offsetLeft+20;
  while((inputObj = inputObj.offsetParent) != null){
  	if(inputObj.tagName!='HTML')returnValue += inputObj.offsetLeft;
  }
  return returnValue;
}

	












