/************************************************************************************************************
(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 = 'cart_add_product.php';
var url_removeProductFromBasket = 'cart_remove_product.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,divname)
{
	if(!shopping_cart_div)shopping_cart_div = document.getElementById('shoppingcartlink');
	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(divname + 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);
	document.getElementById('sc_empty_msg').style.display='none';
	//document.getElementById('cart_summary').style.display='block';
	
}


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 showAjaxBasketContent(ajaxIndex)
{
	// Getting a reference to the shopping cart items table
	var itemBox = document.getElementById('shopping_cart_items');
	var productItems = ajaxObjects[ajaxIndex].response.split('|||');	// Breaking response from Ajax into tokens
	currentqty =parseInt($("#sc_item_qty"+productItems[0]).html());
		
	if($("#shoping_cart_items_product"+ productItems[0]).length>0){	// A product with this id is allready in the basket - just add number items
		$("#sc_item_qty"+productItems[0]).html(parseInt(currentqty+1))
	}else{	
		// Product isn't allready in the basket - add a new row
		if(productItems[2] < 0){
			productItems[2] = "P.O.A";
		}
		var newrow = '<tr id="shoping_cart_items_product'+productItems[0]+'"><td id="sc_item_qty'+productItems[0]+'">1</td><td id="sc_item_name'+productItems[0]+'">'+productItems[1]+'</td><td id="sc_item_price'+productItems[0]+'">$'+productItems[2]+'</td><td id="sc_item_delete'+productItems[0]+'"><a href="javascript:removeProductFromBasket('+productItems[0]+');" title="Remove Item"><img src="/images/icons/cart_delete.gif" alt="RemoveIitem" border="0" /></a></td></tr>';
		
		$('#shopping_cart_items > tbody:last').append(newrow);
		
	} 


	updateTotalPrice();
		
	ajaxObjects[ajaxIndex] = false;		
	
}

function updateTotalPrice()
{
	
	// Calculating total price and showing it below the table with basket items
	var totalprice = 0;
	var totalqty = 0;
	
	$("td[id^='sc_item_qty']").each(function() {
			var i = 1;
			
			row = $(this).attr("id").replace('sc_item_qty','');
			iqty =parseInt($("#sc_item_qty"+row).html());
			totalqty = parseInt(totalqty + iqty);
			iprice = parseInt($("#sc_item_price"+row).html().replace(/[^0-9]/g,""));
			
			if(!(iprice < 0 || isNaN(iprice))){
				totalprice =	totalprice + iqty * iprice;
			}
			i++;
		});
		cartitems = totalqty;
		cartvalue = totalprice;
		$(".btn_cart").button({icons: { primary: 'ui-icon-cart',secondary:"ui-icon-triangle-1-e"}, label:set_cart_btn_label() }) 
}

function removeProductFromBasket(productId)
{
	ajaxRemoveProduct(productId);
	currentqty =parseInt($("#sc_item_qty"+productId).html());
	if(currentqty > 1){
		$("#sc_item_qty"+productId).html(parseInt(currentqty-1))
	}else{
		$("#shoping_cart_items_product"+productId).remove()
	}
	updateTotalPrice();
	var totalitems = $("td[id^='sc_item_qty']").size();
		
	if(totalitems==0){
		document.getElementById('sc_empty_msg').style.display='block';
		$('#shopping_cart_items').hide();
	}
}

function ajaxValidateRemovedProduct(ajaxIndex)
{
	if(ajaxObjects[ajaxIndex].response!='OK')alert('Error while removing product from the database');
	
}

function ajaxRemoveProduct(productId)
{
	var ajaxIndex = ajaxObjects.length;
	ajaxObjects[ajaxIndex] = new sack();
	ajaxObjects[ajaxIndex].requestFile = url_removeProductFromBasket + '?item_id=' + productId;	// Saving product in this file
	ajaxObjects[ajaxIndex].onCompletion = function(){ ajaxValidateRemovedProduct(ajaxIndex); };	// Specify function that will be executed after file has been found
	ajaxObjects[ajaxIndex].runAJAX();		// Execute AJAX function		
}

function ajaxAddProduct(productId)
{
	var ajaxIndex = ajaxObjects.length;
	ajaxObjects[ajaxIndex] = new sack();
	ajaxObjects[ajaxIndex].requestFile = url_addProductToBasket + '?item_id=' + productId;	// Saving product in this file
	ajaxObjects[ajaxIndex].onCompletion = function(){ showAjaxBasketContent(ajaxIndex);showalert(); };	// Specify function that will be executed after file has been found
	ajaxObjects[ajaxIndex].runAJAX();		// Execute AJAX function		
	
}

function showalert(){
   $("#dialog").dialog("destroy");
	
		$("#dialog-message").dialog({
			modal: true,
			resizable:false,
			
			buttons: {
				Ok: function() {
					$(this).dialog('close');
					
				}
				
			}
		});
		
}

