$(document).ready(function(){
    //
    // Cart Popup
    var inner = $('#cart_popup .container').css('height', '0');
    var shadow = $('#cart_popup .shadow').css('width', '58px');
    $('#cart_popup')
        .mouseenter(function(){
            shadow.css('width', '170px');
            inner.stop(true,false).animate({'height': '72px'});
        })
        .mouseleave(function(){
            inner.stop(true,false).animate({'height': '0'}, {complete: function(){
               shadow.css('width', '58px');
            }});
        });
});

var updateCartInfo = function() {
    if (typeof(FC.json) === "undefined") {
        setTimeout("updateCartInfo()", 100);
    } else {
        $('#cart_popup .cart_count').html(FC.json.product_count);
        $('#cart_popup .cart_price').html('$'+FC.json.total_price);
    };
};

function validateSamples() {
    if (typeof(FC.json) === "undefined") {
        setTimeout("validateSamples()", 100);
    } else {
        // Add code for sample orders
        if (FC.json.custom_fields.sample_order) {
    		if (typeof(FC.json.coupons) == "undefined") {
    			$.getJSON('https://' + storedomain + '/cart?' + fcc.session_get() + '&coupon=sales-samples-promo&output=json&callback=?', function(data) {
    				//console.info(FC.json.coupons);
    			});
    		}
    	}
    }
}

fcc.events.cart.preprocess.add(function(){
	validateSamples();
});

fcc.events.cart.process.add(function(e){
	var href = '';
	if (e.tagName == 'A') {
		href = e.href;
	} else if (e.tagName == 'FORM') {
		href = 'https://'+storedomain+'/cart?'+jQuery(e).serialize();
	}
	if (href.match("cart=(checkout|updateinfo)") || href.match("redirect=")) {
		return true;
	} else {
		jQuery.colorbox({
			href: href,
			iframe: true,
			width: "700px",
			height: "70%",
			onClosed: function(){fcc.events.cart.postprocess.execute(e);}
		});
		return false;
	}
});

fcc.events.cart.postprocess.add(function(){
	updateCartInfo();
});

//var fc_BuildFoxyCart = function(){
//    updateCartInfo();
//};

