﻿
function ocBasket(fctLoading) {

	this.add = function(id_Item, qte) {
		this.basketLoadingState();
		optavis.catalog.wsOptavisCatalog.add(id_Item, qte, true, on_Succes, on_Failed, 'majQte');
	}

	this.remove = function(id_Item, qte) {
		this.basketLoadingState();
		optavis.catalog.wsOptavisCatalog.remove(id_Item, on_Succes, on_Failed, 'majQte');
	}

	this.substract = function(id_Item, qte) {
		this.basketLoadingState();
		optavis.catalog.wsOptavisCatalog.substract(id_Item, qte, true, on_Succes, on_Failed, 'majQte');
	}

	this.clear = function(id_Item, qte) {
		this.basketLoadingState();
		optavis.catalog.wsOptavisCatalog.clear(on_Succes, on_Failed, 'majQte');
	}

	this.basketLoadingState = function() {
		if (fctLoading)
			fctLoading();
	}


	// *****************
	//      Results
	// *****************
	function on_Succes(result, context, methodName) {
		switch (context) {

			//Status
			case 'majQte':

				$('.ocItem' + result.idItemModified).html(result.qteItemModified);
				$('.ocTotalPriceItem' + result.idItemModified).html(result.totalPriceItemModified.toFixed(2));
				$('.ocTotalQte').html(result.totalQte);
				$('.ocTotalPrice').html(result.totalPrice.toFixed(2));

				break;

			//Resume                           
			case 'resume':
				//			if (gId('optaEcomBasketResume'))
				//				gId('optaEcomBasketResume').innerHTML = result;
				//			break;
				//Recap      

			case 'recap':

				//			gId('optaEcomBasketRecapQte' + result.idProduitArticleModified).innerHTML = result.qteArticleModified;
				//			gId('optaEcomBasketNbreArticle').innerHTML = result.totalQte;
				//			gId('optaEcomBasketTotalPrix').innerHTML = result.totalPrice.toFixed(2).replace(".", ",");

				//			if (result.qteArticleModified == 0)
				//				gId('optaEcomBasketArticle' + result.idProduitArticleModified).style.display = 'none';

				//			majBasketResume(result.totalQte, result.totalPrice);

				//            str = 'idProduitArticleModified: ' + result.idProduitArticleModified + '\n';
				//            str += 'qteArticleModified: ' + result.qteArticleModified + '\n';
				//            str += 'totalQte: ' + result.totalQte + '\n';
				//            str += 'totalPrice: ' + result.totalPrice + '\n';

				//            gId('optaEcomBasketResume').innerHTML = str;


				break;

			default:
				//gId('optaEcomBasket').innerHTML = result;
				break;
		}
	}

	function on_Failed(error) {
		alert(FailedCallback(error));
	}
}