var cartContents = new Array();
var addtocart_anim_effect;
var addtocart_anim_timer;
var discount_price = 0.0;
var discount_percent = 0.0;
var discount_items = 0;

function addCartMenuItem(id, name, price, slug)
{	
	var add_status = addtocart(id);
	if (add_status == 1)
	{
		var tr = new Element('tr', { id:'menu-cart-item-'+id });
		var infocol = new Element('td'); infocol.className = 'info-col'; tr.appendChild(infocol);
		var pricecol = new Element('td'); pricecol.className = 'price-col'; tr.appendChild(pricecol);
		var actioncol = new Element('td'); actioncol.className = 'action-col'; tr.appendChild(actioncol);
		
		infocol.update('<h3>'+name+'</h3>(Single User)');
		pricecol.update('$'+parseInt(price));
		cartContents[id] = price;
		updateCartMenuSubtotal();
		
		var a = new Element('a', { href: SITEURL+'/cart/remove/'+slug });
		a.onclick = function() { return removeCartMenuItem(id); };
		var delete_icon = new Element('img', { src:	TEMPLATEPATH+'/images/delete_icon_menu.png', alt:'X', title:'' });
		delete_icon.className = 'imgover';
		a.appendChild(delete_icon);
		actioncol.appendChild(a);
		
		$('menu-cart-contents').appendChild(tr);
		
		if ($('menu-cart-item-empty')) $('menu-cart-item-empty').remove();
	}
	
	clearTimeout(addtocart_anim_timer);
	if (addtocart_anim_effect && addtocart_anim_effect.state == 'running') 
	{
		addtocart_anim_effect.cancel();
	}
	if (add_status == 1)
		$('popup_message_content').update('<b>'+name+'</b> has been added to your cart.');
	else if (add_status == 2)
		$('popup_message_content').update('<b>'+name+'</b> is already in your cart.');
	else if (add_status == 3)
		$('popup_message_content').update('You have already purchased <b>'+name+'</b>. Payment may still be pending before you can access it.');
	else
		$('popup_message_content').update('An unknown error occurred while trying to add to your cart.');
		
	if (ie6)
		$('popup_message').setStyle({ top: document.viewport.getScrollOffsets().top + 85 + 'px', margin: '0px', 
			left: document.viewport.getWidth() / 2 + 'px' });		
	else
		$('popup_message').setStyle({ top: document.viewport.getScrollOffsets().top + 85 + 'px', margin: '0px', 
			left: document.viewport.getWidth() / 2 - $('popup_message').getWidth() / 2 + 'px' });
			
	if (ie)
	{
		$('popup_message').show();
		addtocart_anim_timer = setTimeout("$('popup_message').hide();", 4000);
	}
	else
	{
		if (!$('popup_message').visible()) 
			addtocart_anim_effect = new Effect.Appear('popup_message', { duration:0.5 });
		else if (parseFloat($('popup_message').style.opacity) < 1)
			addtocart_anim_effect = new Effect.Appear('popup_message', { 
				from: parseFloat($('popup_message').style.opacity), duration:0.5 });
				
		addtocart_anim_timer = setTimeout("addtocart_anim_effect = new Effect.Fade('popup_message');", 4000);
	}
	
	return false;
}
function removeCartMenuItem(id)
{
	if (removefromcart(id))
	{
		$('menu-cart-item-'+id).remove();
		if ($('cart-item-'+id)) $('cart-item-'+id).remove();
		cartContents[id] = 0;
		updateCartMenuSubtotal();
		if ($$('#menu-cart-contents tr').length == 0)
		{
			var tr = new Element('tr', { id:'menu-cart-item-empty' });
			var td = new Element('td', { colspan:'3' }); td.className = 'empty-cart-col';
			td.update('Your cart is empty.');
			tr.appendChild(td);
			$('menu-cart-contents').appendChild(tr);
			
			if ($('cart-contents'))
			{
				tr = new Element('tr', { id:'cart-item-empty' });
				td = new Element('td', { colspan:'5' }); td.className = 'empty-cart-col';
				td.update('Your cart is empty.');
				tr.appendChild(td);
				$('cart-contents').appendChild(tr);
			}
		}
	}
	return false;
}
function upgradeCartMenuItem(id, new_price, downgrade)
{
	if (typeof(downgrade) == 'undefined') downgrade = false;
	if (upgradecart(id, downgrade ? -1 : 1))
	{
		cartContents[id] = new_price;
		updateCartMenuSubtotal();
		
		if (downgrade) $('cart-item-upgrade-'+id).show(); else $('cart-item-upgrade-'+id).hide();
		if (downgrade) $('cart-item-downgrade-'+id).hide(); else $('cart-item-downgrade-'+id).show();
		if ($('menu-item-license-'+id)) $('menu-item-license-'+id).update(downgrade ? 'Single User' : 'Developer');
		if ($('menu-item-price-'+id)) $('menu-item-price-'+id).update('$'+parseFloat(new_price));
		if ($('cart-item-price-'+id)) $('cart-item-price-'+id).update('$'+parseFloat(new_price).toFixed(2));
	}
	return false;
}
function downgradeCartMenuItem(id, new_price)
{
	return upgradeCartMenuItem(id, new_price, true);
}
function updateCartMenuSubtotal()
{
	var cart = readCookie('shopping-cart-contents');
	cart = cart == null || cart == '' ? new Array() : cart.split('|');
	
	$('cart-count').update(cart.length);
	
	var subtotal = 0.0;
	cartContents.each(function(price) {
		if (!isNaN(parseFloat(price))) subtotal += parseFloat(price);
	});
	
	//discount calc goes here
	discount = discount_price;
	discount += subtotal * discount_percent;
	
	$('menu-subtotal').update('$'+subtotal);
	if ($('cart-subtotal')) $('cart-subtotal').update('$'+subtotal.toFixed(2));
	if ($('discount-decimal')) $('discount-decimal').update(discount.toFixed(2));
	if ($('cart-total')) $('cart-total').update('$'+(subtotal - discount).toFixed(2));
}
function emptyCartMenu()
{
	if (emptycart())
	{
		$$('.cart-item, .menu-cart-item').each(function(item) {
			item.remove();
		});
		
		if (!$('menu-cart-item-empty'))
		{
			var tr = new Element('tr', { id:'menu-cart-item-empty' });
			var td = new Element('td', { colspan:'3' }); td.className = 'empty-cart-col';
			td.update('Your cart is empty.');
			tr.appendChild(td);
			$('menu-cart-contents').appendChild(tr);
		
			if ($('cart-contents'))
			{
				tr = new Element('tr', { id:'cart-item-empty' });
				td = new Element('td', { colspan:'5' }); td.className = 'empty-cart-col';
				td.update('Your cart is currently empty.');
				tr.appendChild(td);
				$('cart-contents').appendChild(tr);
			}
		}
		
		cartContents = new Array();
		updateCartMenuSubtotal();
	}
	return false;
}