var mouse_is_inside = false;

$j(document).ready(function() {
	updateCartPopup();

	$j('.cart_popup').hover(function(){
        mouse_is_inside=true;
    }, function(){
        mouse_is_inside=false;
    });

	$j("body").click (function(e) {
		if ((e.target.className !== "cart_popup") && (!mouse_is_inside)) {
			hideCartPopup();
		}
	});

});

function displayCartPopup() {
	$j('.cart_popup').fadeIn();
}

function hideCartPopup() {
	$j('.cart_popup').fadeOut();
}

function toggleCartPopup() {
	$j('.cart_popup').toggle();
}

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) ? "/checkout/securecart/viewAjax?" + Math.floor(Math.random()*1001).toString() : "/checkout/cart/viewAjax?" + Math.floor(Math.random()*1001).toString());
	$j.get(ajax_url, function (html_contents) {
		var pos = $j('#cart-icon').offset();
		$j('.cart_popup').css({'left':(pos.left-225) + 'px','top':(pos.top+20) + 'px'});
		$j('.cart_popup').html(html_contents);
	});
}

function addUpdateCartPopup(html) {
	var pos = $j('#cart-icon').offset();
	$j('.cart_popup').css({'left':(pos.left-225) + '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));
        }
        this.saveUrl = saveUrl;
        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()) {
			$j('#cart_add_button').hide();
			$j('#addtocart-please-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 = {};
            }
        }

        if (response.r=='failure') {
        	$j('#cart_popup_messages').html(response.messages);
        	displayCartPopup()
        } else {
	        addUpdateCartPopup(response.html);

	        $j('#cart-qty').html(response.count);
	        $j('#cart-total').html(response.total);

        }

	    $j('#cart_add_button').show();
		$j('#addtocart-please-wait').hide();

    },


    failure: function(transport){
		$j('#cart_add_button').show();
		$j('#addtocart-please-wait').hide();
    	alert("Error submitting add to cart request");
    }
}