/************************************************************************************************************
(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 = 20;
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 widthX = 228;
var heightY = 345;
var colorb = 255;
var direction = -1;
var ajaxObjects = new Array();
var intervalID = 0;

function sprintf( ) {var regex = /%%|%(\d+\$)?([-+#0 ]*)(\*\d+\$|\*|\d+)?(\.(\*\d+\$|\*|\d+))?([scboxXuidfegEG])/g;var a = arguments, i = 0, format = a[i++];var pad = function(str, len, chr, leftJustify) {var padding = (str.length >= len) ? '' : Array(1 + len - str.length >>> 0).join(chr);return leftJustify ? str + padding : padding + str;};var justify = function(value, prefix, leftJustify, minWidth, zeroPad) {var diff = minWidth - value.length;if (diff > 0) {if (leftJustify || !zeroPad) {value = pad(value, minWidth, ' ', leftJustify);} else {value = value.slice(0, prefix.length) + pad('', diff, '0', true) + value.slice(prefix.length);}}return value;};var formatBaseX = function(value, base, prefix, leftJustify, minWidth, precision, zeroPad) {var number = value >>> 0;prefix = prefix && number && {'2': '0b', '8': '0', '16': '0x'}[base] || '';value = prefix + pad(number.toString(base), precision || 0, '0', false);return justify(value, prefix, leftJustify, minWidth, zeroPad);};var formatString = function(value, leftJustify, minWidth, precision, zeroPad) {if (precision != null) {value = value.slice(0, precision);}return justify(value, '', leftJustify, minWidth, zeroPad);};var doFormat = function(substring, valueIndex, flags, minWidth, _, precision, type) {if (substring == '%%') return '%';var leftJustify = false, positivePrefix = '', zeroPad = false, prefixBaseX = false;for (var j = 0; flags && j < flags.length; j++) switch (flags.charAt(j)) {case ' ': positivePrefix = ' '; break;case '+': positivePrefix = '+'; break;case '-': leftJustify = true; break;case '0': zeroPad = true; break;case '#': prefixBaseX = true; break;}if (!minWidth) {minWidth = 0;} else if (minWidth == '*') {minWidth = +a[i++];} else if (minWidth.charAt(0) == '*') {minWidth = +a[minWidth.slice(1, -1)];} else {minWidth = +minWidth;}if (minWidth < 0) {minWidth = -minWidth;leftJustify = true;}if (!isFinite(minWidth)) {throw new Error('sprintf: (minimum-)width must be finite');}if (!precision) {precision = 'fFeE'.indexOf(type) > -1 ? 6 : (type == 'd') ? 0 : void(0);} else if (precision == '*') {precision = +a[i++];} else if (precision.charAt(0) == '*') {precision = +a[precision.slice(1, -1)];} else {precision = +precision;}var value = valueIndex ? a[valueIndex.slice(0, -1)] : a[i++];switch (type) {case 's': return formatString(String(value), leftJustify, minWidth, precision, zeroPad);case 'c': return formatString(String.fromCharCode(+value), leftJustify, minWidth, precision, zeroPad);case 'b': return formatBaseX(value, 2, prefixBaseX, leftJustify, minWidth, precision, zeroPad);case 'o': return formatBaseX(value, 8, prefixBaseX, leftJustify, minWidth, precision, zeroPad);case 'x': return formatBaseX(value, 16, prefixBaseX, leftJustify, minWidth, precision, zeroPad);case 'X': return formatBaseX(value, 16, prefixBaseX, leftJustify, minWidth, precision, zeroPad).toUpperCase();case 'u': return formatBaseX(value, 10, prefixBaseX, leftJustify, minWidth, precision, zeroPad);case 'i':case 'd': {var number = parseInt(+value);var prefix = number < 0 ? '-' : positivePrefix;value = prefix + pad(String(Math.abs(number)), precision, '0', false);return justify(value, prefix, leftJustify, minWidth, zeroPad);}case 'e':case 'E':case 'f':case 'F':case 'g':case 'G':{var number = +value;var prefix = number < 0 ? '-' : positivePrefix;var method = ['toExponential', 'toFixed', 'toPrecision']['efg'.indexOf(type.toLowerCase())];var textTransform = ['toString', 'toUpperCase']['eEfFgG'.indexOf(type) % 2];value = prefix + Math.abs(number)[method](precision);return justify(value, prefix, leftJustify, minWidth, zeroPad)[textTransform]();}default: return substring;}};return format.replace(regex, doFormat);}

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 FadeUpBasket(){
colorb = colorb + direction*80;
if(colorb < 16){colorb = 16;direction = 1;}
if(colorb>254){colorb =255;}
var newcol="#ff"+"ff"+colorb.toString(16);
document.getElementById('shopping_cart').style.backgroundColor = newcol;
if(colorb>254){colorb = 255;direction = -1;clearInterval(intervalID);}
}

function showAjaxBasketContent(ajaxIndex)
{
var productItems = ajaxObjects[ajaxIndex].response.split('|||');
var items = document.getElementById('shopping_cart_totalitems').innerHTML /1;
var total = parseFloat(document.getElementById('shopping_cart_totalprice').innerHTML );
items = items + 1;
total = (total + parseFloat(productItems[0]));
document.getElementById('shopping_cart_totalitems').innerHTML = items;
document.getElementById('shopping_cart_totalprice').innerHTML = sprintf("%01.2f",total);
clearInterval(intervalID);
intervalID=setInterval("FadeUpBasket()",100);
ajaxObjects[ajaxIndex] = false;
}

function removeProductFromBasket(productId)
{
var productRow = document.getElementById('shopping_cart_items_product' + productId);
var numberOfItemCell = productRow.cells[0];
if(numberOfItemCell.innerHTML == '1'){productRow.parentNode.removeChild(productRow);}
else{numberOfItemCell.innerHTML = numberOfItemCell.innerHTML/1 - 1;}
updateTotalPrice();
ajaxRemoveProduct(productId);
}

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;
ajaxObjects[ajaxIndex].setVar('productIdToRemove',productId);
ajaxObjects[ajaxIndex].onCompletion = function(){ ajaxValidateRemovedProduct(ajaxIndex); };
ajaxObjects[ajaxIndex].runAJAX();
}

function ajaxAddProduct(productId)
{
var ajaxIndex = ajaxObjects.length;
ajaxObjects[ajaxIndex] = new sack();
ajaxObjects[ajaxIndex].requestFile = url_addProductToBasket;
ajaxObjects[ajaxIndex].setVar('productId',productId);
ajaxObjects[ajaxIndex].onCompletion = function(){ showAjaxBasketContent(ajaxIndex); };
ajaxObjects[ajaxIndex].runAJAX();
}