var Funky = {
	cart: null
};

jQuery().ready(function(E) {
	jQuery.noConflict();

	Nucleus.Cart.prototype.ajaxEnabled = true;
	Nucleus.Cart.prototype.onAdded = function (elm, respXML, respText) {
		var xmlLastAdded = respXML.getElementsByTagName('cart-added')[0];
		var xmlElement = xmlLastAdded.getElementsByTagName('product')[0];
		var xmlItems = respXML.getElementsByTagName('items')[0];
				
		//jQuery.jGrowl("<strong>'" + Nucleus.Elements.xmlText(xmlElement) + "'</strong> was added to your shopping cart. You now have <a href=\"/shop/list/\"><strong>"+ xmlItems.childNodes.length +"</strong> items</a>.");
		//jQuery('cartarea').animate({b) #b1d24e  #90bf52
		
		// make it visually added to the user
		elm.className += ' added';
		
		// Update the number of cart items and cost
		jQuery('span.cartmsg strong').text(xmlItems.childNodes.length);		
		
		// #90bf52
		jQuery('li.cartarea').css('backgroundColor','#B1D24E')
			.animate({'backgroundColor':'#198e45'}, 1000, function(E) { 
				jQuery(this).animate({'backgroundColor':'#B1D24E'}); });
		
		return true;
	};
	
	// Slide up the delivery details, if present and not returning because of errors...
	if (jQuery('#deliveryOpen').attr('class') == 'open') {
		window.location.hash = "deliveryOpen";
	} else {
		jQuery('#deliveryOpen').hide();
	
		jQuery('#showDelivery').bind('click', function(e) { 
			jQuery('#deliveryOpen').slideDown(1000, function(e) { 
				var cssBGColor = jQuery('#deliveryName').css('background-color');
				jQuery('#deliveryName').animate({'marginLeft': '+=10px'},200, 'swing', function(e) { 
						jQuery(this).animate({'marginLeft': "-=10px"}, 200); 
					}).focus();
			});
		});
	}
	
	if (jQuery('#shopForinternationalRow').length) {
		var intRow = jQuery('#shopForinternationalRow');
		var selector = document.getElementById('shopCountry');
		
		// If we currently have the AUstralia selected, hide the international blurb
		if (selector.options[selector.selectedIndex].value == 'au') {
			intRow.hide();
		}
		
		jQuery(selector).bind('change', function() {
			if (this.options[this.selectedIndex].value == 'au') {
				intRow.slideUp();
			} else {
				intRow.slideDown();
			}
		});
	}
	
	// Scroll to the first error, if any.  
	//  it will be identified by an element with the classname being 'error', eg. <p class="error">....</p>
	if (jQuery('.error').length) jQuery('html,body').animate({scrollTop: jQuery('.error').offset().top}, 1000);
	
	Funky.cart = new Nucleus.Cart();
});

function addItem(productid, formElement) {
	if (window.webkit) {
		return false;
	}
	return Funky.cart.addItem(productid, formElement);
}
