/*
 * flytoCart v1.0
 * http://jquery.com/
 *
 * Copyright 2011, Iv�n S�nchez (Girona)
 * Dual licensed under the MIT or GPL Version 2 licenses.
 * http://jquery.org/license.
 *
 * Date: Mon Nov 14 12:10:21 2011
 */

$.fn.flytoCart = function(callback, options){  	

	//create the variable stop to avoid a double click with two movements of the same image
	var fstop = false;
	
	// Default settings
	$.fn.flytoCart.defaults = {  
	    destination 	: 	".destination",
	    velocity		:	500	,
	    offsetX 		: 0,
	    offsetY 		: 0
	};  
	
	// Override default settings
	var opts = $.extend({}, $.fn.flytoCart.defaults, options);  

//			if (!fstop){
					var fparent 					= 		$('.leftmenu');
					fstop 							=		true;
					
					// Calculate image position
					var fproductX 				= 		$(this).offset().left - $(fparent).offset().left;
					var fproductY 				= 		$(this).offset().top - $(fparent).offset().top;

					// Calculate basket // shopping cart  position
					var fbasketX 					= 		$(opts.destination).offset().left - $(fparent).offset().left;
					var fbasketY 					= 		$(opts.destination).offset().top - $(fparent).offset().top;

					// Calculate the width and height of the new image that we will move
					var fnewImageWidth 		= 		$(this).width() / 3;
					var fnewImageHeight		= 		$(this).height() / 3;
					
					// Calculate the image�s movement
					var fgotoX 					= 		fbasketX - fproductX;
					var fgotoY 					= 		fbasketY - fproductY;
					
					//begin the transition
					var flyer = $(this).clone();
					$(flyer).find('form').submit(function() {return false});
					$(flyer).prependTo(fparent)
					.css({'position' : 'absolute', 'left' : (fproductX + opts.offsetX) + 'px', 'top' : (fproductY + opts.offsetY) + 'px', 'z-index' : '999'})
					.animate({opacity: 0.7}, 100 )
					.animate({opacity: 0.1, marginLeft: fgotoX, marginTop: fgotoY, width: fnewImageWidth, height: fnewImageHeight}, opts.velocity, function() {
						
						// Remove the image that moved
						$(flyer).remove();	
						
						// Call the callback
					    if (typeof callback == 'function') { 
					        callback.call(this); // brings the scope to the callback
					    }

						// reset of the variable stop to allow a new movement
//						fstop = false;
					});	
//			}
};
