var cart = new Class({
	//initialization
	initialize: function(psIDCart) {
		var loUrl = window.location.href;
		var lsRegexp = "^http://(.*)/(.*)/(.._..)/.*$";
		var loRegExp = new RegExp(lsRegexp,"g");
		var laGroups = loRegExp.exec(loUrl);
		this.url = "http://" + laGroups[1]+"/"+laGroups[2]+"/"+laGroups[3]+"/jSonCart/";
		this.productLigne = $(psIDCart);
		this.build();
	},
		
	build: function(){	

		var jsonRequest = new Request.JSON({url: this.url, onSuccess: function(responseJSON, responseTEXT){

		if(responseJSON != null) {

			this.data = responseJSON.cartResult;
			this.searchType = responseJSON.searchType;
			
			//top panier
			this.newTop = new Element('div', {'id': "top-panier"});
			this.newTop.inject(this.productLigne, 'bottom');
			
			//panier title
			this.myCartTitle = new Element('div',{
				'class' : 'title contentTitleCart',
				'html' 	: "<a id='fermer' class='close' href='#' onclick='goModalLayer.close();'><span>" + goLabels["Cdl_Close_Button"] + "</span></a>"
							+ "<h1>" + goLabels["Cdl_Mycart_Title"] +"</h1>"});
			this.myCartTitle.inject(this.newTop, 'after');		
			
			//column names		
			this.newLigneTitle = new Element('div', {'class': "ligne"});
			this.newNomTitle = new Element('div', {'class': "nom-article"});
			this.newPrixTitle = new Element('div', {
				'class'	:	"prix-article",
				'html'	:	"<strong>" + goLabels["Cdl_Unit_Price_Title"] + "</strong>"});
			this.newQuantiteTitle = new Element('div', {
				'class'	:	"quantite-article",
				'html'	:	"<strong>" + goLabels["Cdl_Quantity_Title"] + "</strong>"});
			this.newTotalTitle = new Element('div', {
				'class'	:	"total-article",
				'html'	:	"<strong>" + goLabels["Cdl_Total_Title"] + "</strong>"});
			
			this.newNomTitle.inject(this.newLigneTitle, 'bottom');
			this.newPrixTitle.inject(this.newLigneTitle, 'bottom');
			this.newQuantiteTitle.inject(this.newLigneTitle, 'bottom');
			this.newTotalTitle.inject(this.newLigneTitle, 'bottom');
			this.newLigneTitle.inject(this.myCartTitle, 'after');

			this.lastLigne = this.newLigneTitle;
			
			//Article part title
			if(this.data.productCartLineList.length > 0){
				this.newLigneBleu = new Element('div', {
					'class': "ligne", 
					'text'	: goLabels["Cdl_Items"]});
				this.newLigneBleu.addClass("bleuClair");
				this.newLigneBleu.inject(this.newLigneTitle, 'after');
				this.lastLigne = this.newLigneBleu;
			}
			
			//Build all Product rows
			for(var i=0; i<this.data.productCartLineList.length; i++) {
				this.loProduct = this.data.productCartLineList[i];
				
				if(!this.loProduct.completeWheel){

					//*******************
					// Build Product row
					//*******************

					this.newLigneProduct = new Element('div', {'class': "ligne"});
					
					//Product Name
					this.newProduct = new Element('div', {
						'class'	:	"nom-article",
						'html'	:	this.loProduct.brandName + " <strong>" + this.loProduct.fullName + " </strong>" + this.loProduct.size});

					this.newProduct.inject(this.newLigneProduct, 'bottom');
					this.newLigneProduct.inject(this.lastLigne, 'after');
					
					//Unit Price
					this.newPrixUnitDiv = new Element('div', {
						'class'	: 	"prix-article",
						'html'	:	"<strong>" + currencyFormat(this.loProduct.unitPrice) + "</strong>"});
					
					
					this.newPrixUnitDiv.inject(this.newLigneProduct, 'bottom');
					
					//Quantity
					this.newQuantiteDiv1 = new Element('div', {
						'class'	: 	"quantite-article",
						'html'	:	"<span " + "id=\"id__" + this.loProduct.productId + "__P\">" + goLabels["Cart_Multiplicator"] + " " + this.loProduct.quantity + "</span>"});
					
					this.newQuantiteDiv2 = new Element('div', {'class': "floatLeft"});
					
					this.newQuantiteA1 = new Element('a', {
						'href'	: 	"javascript:addProduct('"+this.loProduct.productId+"');",
						'class'	: 	"btn-plus",
						'text'	: 	"+"});
					this.newQuantiteA2 = new Element('a', {	
						'href'	: 	"javascript:decreaseProduct('"+this.loProduct.productId+"');",
						'class'	: 	"btn-moins",
						'text'	: 	"-"});
									
					this.newQuantiteA1.inject(this.newQuantiteDiv2, 'bottom');
					this.newQuantiteA2.inject(this.newQuantiteDiv2, 'bottom');
					this.newQuantiteDiv2.inject(this.newQuantiteDiv1, 'bottom');
					this.newQuantiteDiv1.inject(this.newLigneProduct, 'bottom');

					//Total Price
					this.newTotalPriceProductDiv = new Element('div', {
						'class'	: 	"total-article",
						'html'	:	"<strong>" + currencyFormat(this.loProduct.price) + "</strong>"});
					
					this.newTotalPriceProductDiv.inject(this.newLigneProduct, 'bottom');
					
					this.lastLigne = this.newLigneProduct;
					
					//Build all Product rows
					for(var j=0; j<this.loProduct.mandatoryPrestationList.length; j++) {
						
						this.loService = this.loProduct.mandatoryPrestationList[j];

						//*******************
						// Build Service row
						//*******************
						this.newLigneService = new Element('div', {'class'	: 	"ligne"});
						this.newLigneService.inject(this.lastLigne, 'after');
							
						//Service Name
						this.newService = new Element('div', {
							'class': "nom-article",
							'text': this.loService.fullName});
						this.newService.inject(this.newLigneService, 'bottom');
					
						//Unit Price						
						this.newPrixUnitDiv = new Element('div', {
							'class'	: 	"prix-article",
							'html'	:	"<strong>" + currencyFormat(this.loService.unitPrice) + "</strong>"});
						this.newPrixUnitDiv.inject(this.newLigneService, 'bottom');
						
						//Quantity
						this.newQuantiteDiv = new Element('div', {
							'class'	: 	"quantite-article",
							'html'	:	"<span " + "id=\"id__" + this.loProduct.productId + "__" +this.loService.productId + "__MS\">" + goLabels["Cart_Multiplicator"] + " " + this.loService.quantity+ "</span>"});	
						this.newQuantiteDiv.inject(this.newLigneService, 'bottom');
						
										
						//Total Price
						this.newTotalPriceServiceDiv = new Element('div', {
							'class'	: 	"total-article",
							'html'	:	"<strong>" + currencyFormat(this.loService.price) + "</strong>"});
						this.newTotalPriceServiceDiv.inject(this.newLigneService, 'bottom');
						this.lastLigne = this.newLigneService;
					}

				} else { //complete wheels pack
					
					//*******************
					// Build Product row
					//*******************
					
					//If it's a complete wheels product and if 
					//it's a rim then gather all info in one 
					//line without price but with rim + tyre label
					
					//if type == rim
					if (this.loProduct.type == 1) {
						this.newLigneProduct = new Element('div', {'class'	: 	"ligne"});
						//!!!!!!!!! escape="false" value="bmfPackageLabel" ligne 245 !!!!!!!!\\
						//Product Name
						this.newProduct = new Element('div', {
							'class'	: 	"nom-article",
							'html'	:	this.loProduct.bmfPackageLabel + " " + this.loProduct.size});
						
						this.newProduct.inject(this.newLigneProduct, 'bottom');
						this.newLigneProduct.inject(this.lastLigne, 'after');
						
						//Unit Price
						this.newPrixUnitDiv = new Element('div', {
							'class'	: 	"prix-article",
							'style'	:	"display: none;",
							'html'	:	"<strong>" + currencyFormat(this.loProduct.unitPrice) + "</strong>"});
						this.newPrixUnitDiv.inject(this.newLigneProduct, 'bottom');
						
						//Quantity
						this.newQuantiteDiv1 = new Element('div', {
							'class'	: 	"quantite-article",
							'style'	:	"padding-left: 92px;",
							'html'	:	"<span " + "id=\"id__" + this.loProduct.productId + "__P\">" + goLabels["Cart_Multiplicator"] + " " + this.loProduct.quantity + "</span>"});
						
						this.newQuantiteDiv2 = new Element('div', {'class': "floatLeft"});
						
						this.newQuantiteA1 = new Element('a', {
							'href'	: 	"javascript:addProduct('"+this.loProduct.productId+"');",
							'class'	: 	"btn-plus",
							'text'	: 	"+"});
						this.newQuantiteA2 = new Element('a', {	
							'href'	: 	"javascript:decreaseProduct('"+this.loProduct.productId+"');",
							'class'	: 	"btn-moins",
							'text'	: 	"-"});
										
						this.newQuantiteA1.inject(this.newQuantiteDiv2, 'bottom');
						this.newQuantiteA2.inject(this.newQuantiteDiv2, 'bottom');
						this.newQuantiteDiv2.inject(this.newQuantiteDiv1, 'bottom');
						this.newQuantiteDiv1.inject(this.newLigneProduct, 'bottom');

						//Total Price
						this.newTotalPriceServiceDiv = new Element('div', {
							'class'	: 	"total-article",
							'style'	:	"display: none;",
							'html'	:	"<strong>" + currencyFormat(this.loService.price) + "</strong>"});
						this.newTotalPriceServiceDiv.inject(this.newLigneService, 'bottom');
						this.lastLigne = this.newLigneProduct;
						
					} else {
						//If it's a complete wheel but not a rim, then don't display it 
						
						this.newLigneProduct = new Element('div', {
							'class'	: 	"ligne",
							'style'	:	"display: none;"});
						
						//Product Name
						this.newProduct = new Element('div', {
							'class'	: 	"nom-article",
							'html'	:	this.loProduct.brandName + " <strong>" + this.loProduct.Name + "</strong> " + this.loProduct.size});

						this.newProduct.inject(this.newLigneProduct, 'bottom');
						this.newLigneProduct.inject(this.lastLigne, 'after');
						
						//Unit Price
						this.newPrixUnitDiv = new Element('div', {
							'class'	: 	"prix-article",
							'style'	:	"display: none; padding-left: 92px;",
							'html'	:	"<strong>" + currencyFormat(this.loProduct.unitPrice) + "</strong>"});
						this.newPrixUnitDiv.inject(this.newLigneProduct, 'bottom');
						
						//Quantity
						this.newQuantiteDiv1 = new Element('div', {
							'class'	: 	"quantite-article",
							'style'	:	"padding-left: 92px;",
							'html'	:	"<span " + "id=\"id__" + this.loProduct.productId + "__P\">" + goLabels["Cart_Multiplicator"] + " " + this.loProduct.quantity + "</span>"});
						
						this.newQuantiteDiv2 = new Element('div', {'class': "floatLeft"});
						
						this.newQuantiteA1 = new Element('a', {
							'href'	: 	"javascript:addProduct('"+ this.loProduct.productId +"');",
							'class'	: 	"btn-plus",
							'text'	: 	"+"});
						this.newQuantiteA2 = new Element('a', {	
							'href'	: 	"javascript:decreaseProduct('"+ this.loProduct.productId +"');",
							'class'	: 	"btn-moins",
							'text'	: 	"-"});
										
						this.newQuantiteA1.inject(this.newQuantiteDiv2, 'bottom');
						this.newQuantiteA2.inject(this.newQuantiteDiv2, 'bottom');
						this.newQuantiteDiv2.inject(this.newQuantiteDiv1, 'bottom');
						this.newQuantiteDiv1.inject(this.newLigneProduct, 'bottom');

						//Total Price
						this.newTotalPriceServiceDiv = new Element('div', {
							'class'	: 	"total-article",
							'style'	:	"display: none;",
							'html'	:	"<strong>" + currencyFormat(this.loService.price) + "</strong>"});
						this.newTotalPriceServiceDiv.inject(this.newLigneService, 'bottom');
						this.lastLigne = this.newLigneProduct;
					}
					
					//Build all Product rows
					for(j=0; j<this.loProduct.mandatoryPrestationList.length; j++) {
						
						this.loService = this.loProduct.mandatoryPrestationList[j];

						//*******************
						// Build Service row
						//*******************
						this.newLigneService = new Element('div', {
							'class'	: 	"ligne",
							'style'	:	"display: none;"});
						this.newLigneService.inject(this.lastLigne, 'after');
							
						//Service Name
						this.newService = new Element('div', {
							'class': "nom-article",
							'text': this.loService.fullName});
						this.newService.inject(this.newLigneService, 'bottom');
					
						//Unit Price
						this.newPrixUnitDiv = new Element('div', {
							'class'	: 	"prix-article",
							'html'	:	"<strong>" + currencyFormat(this.loService.unitPrice) + "</strong>"});
						this.newPrixUnitDiv.inject(this.newLigneService, 'bottom');
						
						//Quantity
						this.newQuantiteDiv = new Element('div', {
							'class'	: 	"quantite-article",
							'html'	:	"<span " + "id=\"id__" + this.loProduct.productId + "__" +this.loService.productId + "__MS\">" + "x " + this.loService.quantity+ "</span>"});	
						this.newQuantiteDiv.inject(this.newLigneService, 'bottom');
										
						//Total Price
						this.newTotalPriceServiceDiv = new Element('div', {
							'class'	: 	"total-article",
							'html'	:	"<strong>" + currencyFormat(this.loService.price) + "</strong>"});
						this.newTotalPriceServiceDiv.inject(this.newLigneService, 'bottom');
						this.lastLigne = this.newLigneService;
					}
				}
			}
		
			if(this.data.optionalPrestationList.length != 0) {
				
				this.newLigneBleuOptPrest = new Element('div', {
					'class'	: 	"ligne bleuClair",
					'text'	:	goLabels["Cdl_Services"]});
				this.newLigneBleuOptPrest.inject(this.lastLigne, 'after');
				this.lastLigne = this.newLigneBleuOptPrest;
			}
			
			//Build all Optional Services rows
			for(i=0; i<this.data.optionalPrestationList.length; i++) {
				this.loOptionalPrestation = this.data.optionalPrestationList[i];
			
				//**********************
				// Build Prestation row
				//**********************
	
				this.newLignePrestation = new Element('div', {'class': "ligne"});
				
				//Prestation Name
				this.newPrestation = new Element('div', {
					'class'	:	"nom-article",
					'html'	:	this.loOptionalPrestation.fullName});
	
				this.newPrestation.inject(this.newLignePrestation, 'bottom');
				this.newLignePrestation.inject(this.lastLigne, 'after');
				
				//Unit Price
				this.newPrixUnitDiv = new Element('div', {
					'class'	: 	"prix-article",
					'html'	:	"<strong>" + currencyFormat(this.loOptionalPrestation.unitPrice) + "</strong>"});
				this.newPrixUnitDiv.inject(this.newLignePrestation, 'bottom');
				
				//Quantity
				if (this.loOptionalPrestation.onlyOnePerCart){
					this.newQuantiteDiv1 = new Element('div', {
						'class'	: 	"quantite-article",
						'html'	:	"<span id=\"id__" + this.loOptionalPrestation.productId + "__OS\">" + goLabels["Cart_Multiplicator"] + " " + this.loOptionalPrestation.quantity+ "</span>"});
					
					this.newQuantiteDiv2 = new Element('div', {'class': "floatLeft"});
					
					this.newQuantiteA = new Element('a', {
						'href'	: 	"javascript:removeOptionalPrestation('"+this.loOptionalPrestation.productId+"');",
						'class'	: 	"btn-fermer",
						'text'	: 	"x"});
												
					this.newQuantiteA.inject(this.newQuantiteDiv2, 'bottom');
					this.newQuantiteDiv2.inject(this.newQuantiteDiv1, 'bottom');
					this.newQuantiteDiv1.inject(this.newLignePrestation, 'bottom');
				
				} else {
					this.newQuantiteDiv1 = new Element('div', {
						'class'	: 	"quantite-article",
						'html'	:	"<span id=\"id__" + this.loOptionalPrestation.productId + "__OS\">" + goLabels["Cart_Multiplicator"] + " " + this.loOptionalPrestation.quantity+ "</span>"});
					
					this.newQuantiteDiv2 = new Element('div', {'class': "floatLeft"});
					
					this.newQuantiteA1 = new Element('a', {
						'href'	: 	"javascript:addOptionalPrestation('"+this.loOptionalPrestation.productId+"');",
						'class'	: 	"btn-plus",
						'text'	: 	"+"});
					this.newQuantiteA2 = new Element('a', {
						'href'	: 	"javascript:decreaseOptionalPrestation('"+this.loOptionalPrestation.productId+"');",
						'class'	: 	"btn-moins",
						'text'	: 	"-"});
					
					this.newQuantiteA1.inject(this.newQuantiteDiv2, 'bottom');
					this.newQuantiteA2.inject(this.newQuantiteDiv2, 'bottom');
					this.newQuantiteDiv2.inject(this.newQuantiteDiv1, 'bottom');
					this.newQuantiteDiv1.inject(this.newLignePrestation, 'bottom');
				}
	
				//Total Price
				this.newTotalPricePrestationDiv = new Element('div', {
					'class'	: 	"total-article",
					'html'	:	"<strong>" + currencyFormat(this.loOptionalPrestation.price) + "</strong>"});
				
				this.newTotalPricePrestationDiv.inject(this.newLignePrestation, 'bottom');
				this.lastLigne = this.newLignePrestation;
					
			}
			
			//**********************
			// Build Total row
			//**********************
				
			//Total Cart Price
			this.newLigneTotalPrix = new Element('div', {'class': "ligne"});
			this.newLigneTotalPrix.addClass("total-prix");
			this.newLigneTotalPrix.inject(this.lastLigne, 'after');
			
			if (this.data.totalDiscount != 0) {
				this.newTotalPrixGauche = new Element('div', {
					'class'	: 	"gauche",
					'text'	:	goLabels["Cart_Total_Include_Rebate"]});
				this.newTotalPrixGauche.addClass("total-text");
			} else {
				this.newTotalPrixGauche = new Element('div', {
					'class'	: 	"gauche",
					'text'	:	goLabels["Cart_Total"]});
				this.newTotalPrixGauche.addClass("total-text");
			}
			
			this.newTotalPrixGauche.inject(this.newLigneTotalPrix, 'bottom');
			
			this.newTotalPrixDroite = new Element('div', {
				'class'	: 	"droite",
				'html'	:	currencyFormat(this.data.totalPrice)});
			this.newTotalPrixDroite.addClass("totalPrice");
			
			this.newTotalPrixDroite.inject(this.newLigneTotalPrix, 'bottom');
	
			//Total Cart Value Of Discount
			if (this.data.totalDiscount != 0) {
				this.newTotalRemise = new Element('div', {
					'class'	: 	"ligne",
					'id'	:	"total-discount"});
				this.newTotalRemise.addClass("total-remise");
			} else {
				this.newTotalRemise = new Element('div', {
					'class'	: 	"ligne",
					'id'	:	"total-discount",
					'style'	:	"display:none;"});
				this.newTotalRemise.addClass("total-remise");
			}
			
			this.newTotalRemise.inject(this.newLigneTotalPrix, 'after');
	
			this.newTotalPrixDroite = new Element('div', {
				'class'	: 	"gauche",
				'html'	:	goLabels["Cart_Value_Of_Discount"]});
			this.newTotalPrixDroite.inject(this.newTotalRemise, 'bottom');
			
			this.newTotalPrixRemiseDroite = new Element('div', {
				'class'	: 	"droite",
				'html'	:	currencyFormat(this.data.totalDiscount)});
			this.newTotalPrixRemiseDroite.addClass("total-remise-value");
			
			this.newTotalPrixRemiseDroite.inject(this.newTotalRemise, 'bottom');
	
			//Total Cart Value Of Discount
			
			
			// Buttons
			this.newLigneResult = new Element('div', {'class'	: 	"ligne"});
			
			this.newBackResult = new Element('a', {
				'href'	: 	"javascript:$('resultListButton').click();",
				'class'	: 	"btn btnColD all",
				'id'	:	"revenir",
				'html'	: 	"<span>" + goLabels["Cdl_Mcw_Back_Result_Page_Button"] + "</span>"});
	
			
			this.newCheckout = new Element('a', {	
				'href'	: 	gsUrlMyCart,
				'class'	: 	"btn btnColD continue",
				'id'	:	"payer",
				'html'	: 	"<span>" + goLabels["Cdl_Checkout_Button"] + "</span>"});
			
		
			this.newLigneResultBottom = new Element('div', {'id'	: 	"bottom-panier"});
	
			this.newLigneResult.inject(this.newTotalRemise, 'after');
			this.newBackResult.inject(this.newLigneResult, 'bottom');
			this.newCheckout.inject(this.newLigneResult, 'bottom');
			
			this.newLigneResultBottom.inject(this.newLigneResult, 'after');
			
			var loAction = '';
			if (parseFloat(this.searchType) == 0) {
				loAction = gsUrlSearchByCar;
			} else if (parseFloat(this.searchType) == 1) {
				loAction = gsUrlSearchByTyre;
			} else if (parseFloat(this.searchType) == 2) {
				loAction = gsUrlSearchByOffers;
			}
			
			this.newFormBackResult = new Element('form', {
				'method'	:	'post',
				'action'	:	loAction,
				'onsubmit'	:	'return true;',
				'html'		:	'<input type="hidden" id="loadCriteriaFromSession" value="true" name="loadCriteriaFromSession"><input type="submit" style="display: none;" value="" id="resultListButton">'
			});
			
			this.newFormBackResult.inject(this.newLigneResultBottom, 'after');
			
		}}.bind(this)}).send();
	}
});


function currencyFormat(lnPrice){
	var lsPrice = Number(lnPrice).toFixed(2);
	
	if(gbIsCurrencySymbolBefore == 'true'){
		lsPrice = goLabels["Currency_Symbol"] + lsPrice;
	}else{
		lsPrice = lsPrice.replace(".",",") + goLabels["Currency_Symbol"];
	}
	return lsPrice;
}
