YUI().use('dump', 'node', 'io-base', 'json', function(Y){
	
	/**
	 * Skrypt pozwala na usuwanie zdjęć z koszyka w widokach nizwiązanych ze sklepem
	 */
	
	var CHECKOUT = {
	    ajaxUrl: '',
	    hash: null,
	    
		setEvents: function(){
			if( Y.one('#ajax_url_store') ){
				CHECKOUT.ajaxUrl = Y.one('#ajax_url_store').get('innerHTML');
			}	
			Y.on("click", this.callRemoveProduct, ".remove_product_from_basket");
			Y.on("click", this.callRemoveAllProducts, ".remove_all_products_from_basket");	
		},
		
		callRemoveAllProducts: function(){
			Y.io( CHECKOUT.ajaxUrl + '?action=remove_all_products', 
					CHECKOUT.configFactory( CHECKOUT.transactionFactory( 'remove_all_products' ) ));
		},	

		callRemoveProduct: function(e){
			// Tutaj to co ma być obsłużone przed wysłaniem żądania asynchronicznego
			var hash = this.getAttribute('id').substr(20);
			CHECKOUT.hash = hash; 
			
			Y.io( CHECKOUT.ajaxUrl + '?action=remove_product' +
					    '&hash=' + hash, 
					    CHECKOUT.configFactory( CHECKOUT.transactionFactory( 'remove_product' ) ));
		},
		
		configFactory: function( tH ){
			/* configuration object for transactions */
			var cfg = {
				on: {
					success: tH.success,
					failure: tH.failure,
					start: CHECKOUT.start,
					complete: CHECKOUT.complete,
				},
				context: tH,
				headers: { 
					'Content-Type': 'application/json', 
						},
				arguments: {}
			};
			return cfg;
			/* end configuration object */		
		},
		start: function(){
			var ajax_loader_path = Y.one("#basket_loader").get('innerHTML');
			ajax_loader_image = new Image(); 
			ajax_loader_image.src = ajax_loader_path;
			Y.one("#self_photo_store_basket_inside").setStyle('opacity', '0.4');
			Y.one("#event_basket_loader").append(ajax_loader_image);
		},
		
		complete: function(){
			Y.one("#event_basket_loader").setContent('&nbsp;');			
		},
		
		handleFailure: function(ioId, o, args, messages){
			var html = '<ul>';
			for( message in messages){
				var html = html + '<li>' + messages[message] + '</li>';
			}	
			var html = html + '</ul>';
			
			Y.one("#basket_failure").setContent(html);
			Y.one("#self_photo_store_basket_inside").setStyle('opacity', '1.0');
		},

		transactionFactory: function( transaction ){
			
			if( transaction === 'remove_product' ){
					/* transaction event object */
					var transactionRemoveProduct = {
					success: function(id, o, args){
						try{ 
							var json = Y.JSON.parse(o.responseText);
						}catch(e){
							PRODUCT.handleFailure(id, o, args, ["Nieczytelna odpowiedź z serwera."]);
							return;
						}
					
						if(json.success === true){
							//1. Usunięcie widoku małego koszyka
							Y.one("#self_photo_store_basket_inside").remove();
							//2. Wklejenie widoku małego koszyka
							Y.one("#self_photo_store_basket").append(json.basketHTML);
							//Ustawienie eventów dla wstrzykniętego kodu.
							Y.on("click", CHECKOUT.callRemoveProduct, ".remove_product_from_basket");
							Y.on("click", CHECKOUT.callRemoveAllProducts, ".remove_all_products_from_basket");
							
							// Sprawdzenie ilości produktów
							var products = Y.all(".checkout_product");
							
							if ( products.size() > 1 )
							{
								var product = Y.one("#product_checkout_" + CHECKOUT.hash );
								var productClear = Y.one("#clear_product_checkout_" + CHECKOUT.hash );
								product.remove();
								productClear.remove();
							}
							
						}
						else{
							PRODUCT.handleFailure(id, o, args, ["Usunięcie produktu nie powiodło się."]);							
						}
				
					},
					failure: function(id, o, args) {
						PRODUCT.handleFailureJson(id, o, args, json.errors );
					},
					start: CHECKOUT.start,
					complete: CHECKOUT.complete,
				};
				
				/* transaction event object */
				return transactionRemoveProduct;
			}
			
			if( transaction === 'remove_all_products' ){
			    /* transaction event object */
				var transactionRemoveAllProducts = {
					success: function(id, o, args){
						try{ 
							var json = Y.JSON.parse(o.responseText);
						}catch(e){
							PRODUCT.handleFailure(id, o, args, ["Nieczytelna odpowiedź z serwera."]);
							return;
						}
					
						if(json.success === true){
							//1. Usunięcie widoku małego koszyka
							Y.one("#self_photo_store_basket_inside").remove();
							//2. Wklejenie widoku małego koszyka
							Y.one("#self_photo_store_basket").append(json.basketHTML);
							//Ustawienie eventów dla wstrzykniętego kodu.
							Y.on("click", this.callRemoveAllProducts, ".remove_all_products_from_basket");
						}
						else{
							PRODUCT.handleFailureJson(id, o, args, json.errors );							
						}
					},
					failure: function(id, o, args) {
						PRODUCT.handleFailure(id, o, args, ["Usunięcie produktów nie powiodło się."]);
					},
				};
				
				/* transaction event object */
				return transactionRemoveAllProducts;
			}
		}			
	}

	CHECKOUT.setEvents();
	
});
