(function($){

	/**
	* Skeleton for jQuery plugin
	* @param {Object} options
	*/
	$.fn.basket = function(options) {

		// extends options with the default one
		var opts = $.extend({}, $.fn.basket.defaults, options);
		
		// refresh the basket
		$.fn.basket.update(opts.basket, opts.url);
		
		// iterate and process each matched element
		return this.each(function() {
			new $.basket(this, opts);
		}); 
		
	}; 
	
	// samplePlugin default options
	$.fn.basket.defaults = {};
	
	$.basket = function (e, o){
		this.options = o || {};
		
		this.container = $(e);
		this.id = $(e).children(".catID").eq(0).val();
		this.spinner = $(e).children(".catSpinner").eq(0);
		this.notify= $(e).children(".catNotify").eq(0);
		this.button = $(e).children(".catAdd").eq(0);
		this.qa = $(e).children(".catQA").eq(0);
		
		// Only one textarea activated at a time, the one being used
		this.init();
	};
	
	jQuery.basket.fn = jQuery.basket.prototype = {
	    basket: '0.1'
    };
	
	jQuery.basket.fn.extend = jQuery.basket.extend = jQuery.extend;
	
	$.fn.basket.update = function(basket, url) {				
		_debug("Refresh Basket");
		$(basket).load( url, {action: "show"});
	};

	jQuery.basket.fn.extend({
						 
		init: function() {			
			var self = this;			
			
			// arrange un peu tout ca
			this.spinner.hide();
			this.notify.hide();
			
			// ajout des evenements
			this.button.click(function(){
				var qa = parseInt(self.qa.val());
				var id = self.id;
				_debug("Adding " + qa + " product[" + id + "]");
				
				if ( qa == NaN ) qa = 1;
				
				self.spinner.show();
				$.post( self.options.url, {action: "add", p: id, q: qa}, function(msg){ 
					if ( msg.indexOf("OK ") == 0 ) RK = msg.substring( 3 );
					self.spinner.hide();
					self.notify.fadeIn();
					setTimeout( function(){ self.notify.fadeOut(); } , 2500 );
					// met à jour le caddie
					$.fn.basket.update(self.options.basket, self.options.url);
					// animation
					self.moveEffect();	
				} );
			});
			/*
			this.textarea.css({overflow: 'hidden', display: 'block'});
			this.textarea.bind('focus', function() { self.startExpand() } ).bind('blur', function() { self.stopExpand() });
			this.checkExpand();	
			*/
		},
						 
		moveEffect: function() {				
		  	_debug("Move Effect");
		  	var self = this;
			
			var pid = self.id;
			
			var start = self.container.offset();
			start.width = self.container.css("width");
			start.height = self.container.css("height");
			
			var end = $(self.options.basket).offset();
			end.width = $(self.options.basket).css("width");
			end.height = $(self.options.basket).css("height");
			
			// création du bloc
			$('body').append("<div id='BasketMove-" + pid + "' class='basketMove'>&nbsp;</div>");
			$('#BasketMove-' + pid).css({
				"position": "absolute",
				"left": start.left + "px",
				"top": start.top + "px",
				"width": start.width,
				"height": start.height,
				"opacity": .5
			});
			$('#BasketMove-' + pid).animate({
					left: end.left + 'px',
					top: end.top + 'px',
					width: end.width,
					height: end.height
				}, 1500
				, function(){
					// Animation complete.
					$(this).remove();
				}
			);
		}
	});
	
	
	//
	// private function for debugging
	//
	function _debug($msg) {
		if (window.console && window.console.log)
			window.console.log("[plugin.basket] " + $msg);
	};
	
	function _error($msg) {
		if (window.console && window.console.error)
			window.console.error("[plugin.basket] " + $msg);
	};

})(jQuery);

