
var cartCoords, cartEndCoords, cartDeltas, cartDiv, cartSteps, cartCurrentStep, cartDestElement, cartBalloon, cartCounter, cartTimeout;
/***
	Creates a div on the fly that moves across the page from the link up to the quote cart at the top of the page. 
	Purpose is to indicate to the user that their click IS doing something.
**/
function cartPrepare() {
	cartDiv = document.createElement("div");
	cartDiv.style.display = 'none';
	cartDiv.style.position = 'absolute';
	cartDiv.style.border = '2px solid #C0C0C0';
	cartDiv.style.zIndex = 99999;
	document.body.appendChild(cartDiv);
	cartDestElement = document.getElementById("quoteCart");
	cartBalloon = document.getElementById("quoteBalloon"); //identifies element that acts as the pop-up balloon
	cartCounter = document.getElementById("quoteItems"); //identifies element that acts as an item counter
}
function cartAnimate(element) {
	clearTimeout(cartTimeout);
	var temp = cartGetOffsetCoords(element);
	cartCoords = [temp[0]-100, temp[1]-100, element.offsetWidth+200, element.offsetHeight+200];
	temp = cartGetOffsetCoords(cartDestElement);
	cartEndCoords = [temp[0], temp[1], cartDestElement.offsetWidth, cartDestElement.offsetHeight];
	var xDist = Math.abs(cartEndCoords[1] - cartCoords[1]);
	var yDist = Math.abs(cartEndCoords[0] - cartCoords[0]);
	cartSteps = Math.floor(Math.sqrt((xDist*xDist)+(yDist*yDist)) / 10);
	cartDeltas = [(cartEndCoords[0] - cartCoords[0])/cartSteps, (cartEndCoords[1] - cartCoords[1])/cartSteps, (cartEndCoords[2] - cartCoords[2])/cartSteps, (cartEndCoords[3] - cartCoords[3])/cartSteps];
	cartCurrentStep = 0;
	cartStepAnimation();
}
function cartStepAnimation() {
	cartCoords[0] += cartDeltas[0];
	cartCoords[1] += cartDeltas[1];
	cartCoords[2] += cartDeltas[2];
	cartCoords[3] += cartDeltas[3];
	cartDiv.style.left = Math.round(cartCoords[0]) + "px";
	cartDiv.style.top = Math.round(cartCoords[1]) + "px";
	cartDiv.style.width = Math.round(cartCoords[2]) + "px";
	cartDiv.style.height = Math.round(cartCoords[3]) + "px";
	cartDiv.style.display = 'block';
	if (cartCurrentStep++ < cartSteps) {
		cartTimeout = setTimeout(cartStepAnimation, 10);
	} else {
		cartDiv.style.display = 'none';
	}
}
function cartGetOffsetCoords(element) {
	if (element == undefined) {
		return [0,0];
	}
	
	var coords = [
		element.offsetLeft,
		element.offsetTop
	];

	var temp = cartGetOffsetCoords(element.offsetParent);
	coords[0] += temp[0];
	coords[1] += temp[1];

	return coords;
}


/***
	Creates and works with two cookies.
	
		One is a 30 minute timer, if this cookie does not exist then a div "quoteBalloon" is displayed, instructing the user that
		their item has been added to the quote cart and they can either submit the cart or continue to add items to the cart. 
		After 30 minutes, the cookie expires and the user is reminded that they can submit now or from the top of the page later.
	
		The other cookie is a list of product numbers held in a string. The code explodes the string and works with it as an array.
		When a new product is added, it is added to the array and the entire array is joined together to create a string and added back to the cookie.
		
***/

function addToQuoteCart(product_id, element) {
	cartAnimate(element);
	
	var products = readCookie('ttcookie');
	if (products != null) {
		products = products.split(",");
		var found = false;
		for (var i=0; i<products.length; i++) {
			if (products[i] == product_id) {
				found = true;
				break;
			}
		}
		if (!found) {
			products.push(product_id);
		}
	} else {
		products = [product_id];
	}
	createCookie('ttcookie', products.join(","), 1);
	
	cartCounter.innerHTML = products.length + " item" + (products.length==1?"":"s");
	
	var doShowBalloon = readCookie('ttballoon');
	if (doShowBalloon == null) {
		cartBalloon.style.display = 'block';
	}
	createCookie('ttballoon', 1, .02);
}

function clearQuoteCart() {
	eraseCookie('ttcookie');
	eraseCookie('ttballoon');
	cartCounter.innerHTML = '0 items';
}

function hideBalloon() {
	cartBalloon.style.display = 'none';
}
function closeBalloon() {
	eraseCookie('ttballoon');
	cartBalloon.style.display = 'none';
}


function createCookie(name,value,days) {
   if (days) {
       var date = new Date();
       date.setTime(date.getTime()+(days*24*60*60*1000));
       var expires = "; expires="+date.toGMTString();
   }
   else var expires = "";
   document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
   var nameEQ = name + "=";
   var ca = document.cookie.split(';');
   for(var i=0;i < ca.length;i++) {
       var c = ca[i];
       while (c.charAt(0)==' ') c = c.substring(1,c.length);
       if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
   }
   return null;
}

function eraseCookie(name) {
   createCookie(name,"",-1);
}
