function updateCartPopup() {
	if ($j('#cart_popup_div').length == 0) {
		$j('body').prepend('<div class="cart_popup" id="cart_popup_div"></div>');
	}
	var ajax_url = (("https:" == document.location.protocol) ? "/acheckout/securecart/viewAjax?" + Math.floor(Math.random()*1001).toString() : "/acheckout/cart/viewAjax?" + Math.floor(Math.random()*1001).toString());
	$j.get(ajax_url, function (html_contents) {
		var pos = $j('#cart-btn').offset();
		$j('.cart_popup').css({
			'left':(pos.left-175) + 'px',
			'top':(pos.top+20) + 'px'
			});
		$j('.cart_popup').html(html_contents);
	});
}

function addUpdateCartPopup(html) {
	var pos = $j('#cart-btn').offset();
	$j('.cart_popup').css({
		'left':(pos.left-175) + 'px',
		'top':(pos.top+20) + 'px'
		});
	$j('.cart_popup').html(html);
	displayCartPopup();
}

var Addtocart = Class.create();
Addtocart.prototype = {
	initialize: function(form, saveUrl){
		this.form = form;
		if ($(this.form)) {
			$(this.form).observe('submit', function(event){
				this.save();
				Event.stop(event);
			}.bind(this));
		}
		var ajax_url = (("https:" == document.location.protocol) ? "/acheckout/securecart/addAjax?" + saveUrl : "/acheckout/cart/addAjax?" + saveUrl);
		this.saveUrl = ajax_url;
		this.validator = new Validation(this.form);
		this.onSave = this.nextStep.bindAsEventListener(this);
		this.onFailure = this.failure.bindAsEventListener(this);
		this.is_valid = false;
	},

	validate: function() {
		this.is_valid = this.validator.validate();
		return this.is_valid;
	},

	save: function(){

		if (this.validate()) {
			
			this.form.action='';
			$j('#ao_add_to_cart').hide();
			$j('#ao_add_wait').show();
			var request = new Ajax.Request(
				this.saveUrl,
				{
					method:'post',
					onSuccess: this.onSave,
					onFailure: this.onFailure,
					parameters: Form.serialize(this.form)
				}
				);
		}
	},

	nextStep: function(transport){
		if (transport && transport.responseText){
			try{
				response = eval('(' + transport.responseText + ')');
			}
			catch (e) {
				response = {};
			}
		}

		$j('#ao_mini_cart').html(response.html);			
		
		$j('#ao_mini_cart_contents').fadeIn();
		setTimeout('$j("#ao_mini_cart_contents").fadeOut();', 6000);
		
		$j('.cart-container .button').mouseover(function() {
			$j(this).parent().find('.cart-dd').show();
		});
		$j('.cart-dd').mouseleave(function() {
			$j(this).hide();
		});

		$j('#ao_add_wait').hide();
		$j('#ao_add_to_cart').show();
		
		$j.colorbox.close();

	},

	failure: function(transport){
		$j('#ao_add_wait').hide();
		$j('#ao_add_to_cart').show();
		alert("Error submitting add to cart request");
	}
}

