/* Center element usung .center */
(function($) {
	$.fn.extend({
		center : function() {
			return this.each(function() {
				var top = ($(window).height() - $(this).outerHeight()) / 2;
				var left = ($(window).width() - $(this).outerWidth()) / 2;
				$(this).css({
					//position : 'absolute',
					//margin : 0,
					//top : (top > 0 ? top : 0) + 'px',
					//left : (left > 0 ? left : 0) + 'px'
				});
			});
		}
	});
})(jQuery);

/* Gets the actual product image an move / resize it to cart */
function runTransitionEffect(productImage) {

	//var productImage = productContainer.children(".image").find('img');

	var productImageX = productImage.offset().left;
	var productImageY = productImage.offset().top;
	var basketX = jQuery(".block-cart").offset().left;
	var basketY = jQuery(".block-cart").offset().top;
	var gotoX = basketX - productImageX;
	var gotoY = basketY - productImageY;
	//var newImageWidth = productImage.width() / 5;
	//var newImageHeight = productImage.height() / 5;
	var newImageWidth = jQuery(".block-cart").height();
	var newImageHeight = jQuery(".block-cart").height();


	productImage.clone().prependTo("body").css({
		'position' : 'absolute',
		'left' : productImageX,
		'top' : productImageY,
		'z-index' : '1003'
	}).animate({
		opacity : 0.9
	}, 100).animate({
		opacity : 0.6,
		marginLeft : gotoX,
		marginTop : gotoY,
		width : newImageWidth,
		height : newImageHeight
	}, 1000, 'easeInOutCubic', function() {
		$(this).remove();
	});
	
	productImage.removeAttr('id');
};

jQuery(document).ready(function() {
	jQuery(".products-grid .item .actions .ajax-add-to-cart").click(function(event) {
		var productLink = jQuery(this).attr('onclick').search(/product_type=configurable.+/);
		var productContainer = jQuery(this).parent().parent();
		var productImage = productContainer.find('.product-image img.thumbnail');
		
		// keine Animation bei Konfigurierbaren Produkten
		if (productLink == -1) {
			runTransitionEffect(productImage);
		}
		else {
			productImage.attr('id', 'image');
		}

		return false;
	});
});
