	
	/***
	 * Confort Visuel
	 * Author: Uppercut (http://www.uppercut-interactive.com/)
	 ***/
	var glasses = {
		
		params:{ webservice:null , action:"save-and-refresh" , productId:null },
		
		initialize:function(pParams,pReady) {
			
			if (pReady) {
				
				jQuery.extend(this.params,pParams);
				
				jQuery(".brands-list input").change(function() { glasses.refresh({ changedField:"marque" }); });
				jQuery(".types-list input").change(function() { glasses.refresh({ changedField:"type" }); });
				jQuery(".prescription select, .prescription input").change(function() { glasses.refresh({ changedField:"prescription" }); });
				jQuery(".ecart-pupillaire select").change(function() { glasses.refresh({ changedField:"prescription" }); });
				jQuery(".thickness-list input").change(function() { glasses.refresh({ changedField:"epaisseur" }); });
				jQuery(".traitements-list input").change(function() { glasses.refresh({ changedField:"traitements" }); });
				jQuery(".teinte-list select").change(function() {
					jQuery(this).parents("div.label").find("input").attr("checked",true);
					glasses.refresh({ changedField:"colors" });
				});								
				
				jQuery(".teinte-list input").change(function() {
					if (jQuery(this).val() != "incolore") {
						if (jQuery(this).parents("div.label").find("select option").length > 0) {
							jQuery(this).parents("div.label").find("select").val(jQuery(jQuery(this).parents("div.label").find("select option")[0]).val());
							glasses.refresh({ changedField:"teintes" });
						}
					} else { glasses.refresh({ changedField:"teintes" }); }
				});
				
				jQuery("#wrap").floatingPrice();
				jQuery("#floating-price").hide();
				
				glasses.refresh({ changedField:"marque" });
				
			} else { jQuery(function() { glasses.initialize(pParams,true); }); }
			
		},
		
		refresh:function(pParams) {
			
			jQuery(".product-step-content").glassesWaitingBegin();
			
			var _data = jQuery("#glasses-form").serialize();
			_data += "&action=" + this.params.action;
			_data += "&monture=" + this.params.productId;
			_data += "&changed_field=" + pParams.changedField;
			
			jQuery.ajax({
				type:"post",
				url:this.params.webservice,
				data:_data,
				dataType:"xml",
				success:function(pData,pTextStatus,pXMLHttpRequest) {
					
					//pData = pXMLHttpRequest.responseXML;
					var _d = jQuery(pData).find("result data");
					
					// Sélection de la marque
					
					jQuery(_d).find("marques marque").each(function() {
						var _input = jQuery("#brand-" + jQuery(this).attr("id"));
					//	if (jQuery(this).attr("available") == "yes") { jQuery(_input).parents("label").show(); } else { jQuery(_input).parents("label").hide(); }
						$(_input).attr("checked",jQuery(this).text() == "yes");
					});
					
					var _p;
					
					// Sélection du type
					
					if (jQuery(_d).find("types simple").attr("available") == "yes") { jQuery("#type-simple").parents("label").show(); } else { jQuery("#type-simple").parents("label").hide(); }
					jQuery("#type-simple").attr("checked",jQuery(_d).find("types simple").text() == "yes");
					if (jQuery(_d).find("types progressif").attr("available") == "yes") { jQuery("#type-progressif").parents("label").show(); } else { jQuery("#type-progressif").parents("label").hide(); }
					jQuery("#type-progressif").attr("checked",jQuery(_d).find("types progressif").text() == "yes");
					
					// Sélection de l'épaisseur
					
					if (jQuery(_d).find("epaisseurs normal").attr("available") == "yes") { jQuery("#thickness-normal").parents("label").show(); } else { jQuery("#thickness-normal").parents("label").hide(); }
					if (jQuery(_d).find("epaisseurs normal").attr("reco") == "yes") { jQuery("#thickness-normal").parents("label").addClass("selected"); } else { jQuery("#thickness-normal").parents("label").removeClass("selected"); }
					jQuery("#thickness-normal").attr("checked",jQuery(_d).find("epaisseurs normal").text() == "yes");
					
					if (jQuery(_d).find("epaisseurs fin").attr("available") == "yes") { jQuery("#thickness-fin").parents("label").show(); } else { jQuery("#thickness-fin").parents("label").hide(); }
					if (jQuery(_d).find("epaisseurs fin").attr("reco") == "yes") { jQuery("#thickness-fin").parents("label").addClass("selected"); } else { jQuery("#thickness-fin").parents("label").removeClass("selected"); }
					jQuery("#thickness-fin").attr("checked",jQuery(_d).find("epaisseurs fin").text() == "yes");					
					
					if (jQuery(_d).find("epaisseurs extra_fin").attr("available") == "yes") { jQuery("#thickness-extra-fin").parents("label").show(); } else { jQuery("#thickness-extra-fin").parents("label").hide(); }
					if (jQuery(_d).find("epaisseurs extra_fin").attr("reco") == "yes") { jQuery("#thickness-extra-fin").parents("label").addClass("selected");} else { jQuery("#thickness-extra-fin").parents("label").removeClass("selected"); }
					jQuery("#thickness-extra-fin").attr("checked",jQuery(_d).find("epaisseurs extra_fin").text() == "yes");		
					
					if (jQuery(_d).find("epaisseurs ultra_fin").attr("available") == "yes") { jQuery("#thickness-ultra-fin").parents("label").show(); } else { jQuery("#thickness-ultra-fin").parents("label").hide(); }
					if (jQuery(_d).find("epaisseurs ultra_fin").attr("reco") == "yes") { jQuery("#thickness-ultra-fin").parents("label").addClass("selected"); } else { jQuery("#thickness-ultra-fin").parents("label").removeClass("selected"); }
					jQuery("#thickness-ultra-fin").attr("checked",jQuery(_d).find("epaisseurs ultra_fin").text() == "yes");
					
					
					
					// Valeurs de la prescription
					
					jQuery("#od-sph").val(jQuery(_d).find("prescription sph").attr("right"));
					jQuery("#og-sph").val(jQuery(_d).find("prescription sph").attr("left"));
					jQuery("#od-cyl").val(jQuery(_d).find("prescription cyl").attr("right"));
					jQuery("#og-cyl").val(jQuery(_d).find("prescription cyl").attr("left"));
					jQuery("#od-axi").val(jQuery(_d).find("prescription axi").attr("right")); if (jQuery("#od-axi").val() == "0") jQuery("#od-axi").val("Aucun");
					jQuery("#og-axi").val(jQuery(_d).find("prescription axi").attr("left")); if (jQuery("#og-axi").val() == "0") jQuery("#og-axi").val("Aucun");
					jQuery("#od-addition").val(jQuery(_d).find("prescription addition").attr("right"));
					jQuery("#og-addition").val(jQuery(_d).find("prescription addition").attr("left"));
					jQuery("#od-ecart").val(jQuery(_d).find("prescription ecart").attr("right"));
					jQuery("#og-ecart").val(jQuery(_d).find("prescription ecart").attr("left"));
					
					
					if (jQuery("#type-progressif").attr("checked")) { jQuery(".col-addition").css({ visibility:"visible" }); } else { jQuery(".col-addition").css({ visibility:"hidden" }); }
					
					// Traitements des verres
					
					if (jQuery(_d).find("traitements anti_reflets").attr("available") == "yes") { jQuery("#traitement-anti-reflets").parents("label").show(); } else { jQuery("#traitement-anti-reflets").parents("label").hide(); }
					jQuery("#traitement-anti-reflets").attr("checked",jQuery(_d).find("traitements anti_reflets").text() == "yes");
					if (jQuery(_d).find("traitements anti_rayures").attr("available") == "yes") { jQuery("#traitement-anti-rayures").parents("label").show(); } else { jQuery("#traitement-anti-rayures").parents("label").hide(); }
					jQuery("#traitement-anti-rayures").attr("checked",jQuery(_d).find("traitements anti_rayures").text() == "yes");
					if (jQuery(_d).find("traitements anti_eau").attr("available") == "yes") { jQuery("#traitement-anti-eau").parents("label").show(); } else { jQuery("#traitement-anti-eau").parents("label").hide(); }
					jQuery("#traitement-anti-eau").attr("checked",jQuery(_d).find("traitements anti_eau").text() == "yes");
					if (jQuery(_d).find("traitements anti_uv").attr("available") == "yes") { jQuery("#traitement-anti-uv").parents("label").show(); } else { jQuery("#traitement-anti-uv").parents("label").hide(); }
					jQuery("#traitement-anti-uv").attr("checked",jQuery(_d).find("traitements anti_uv").text() == "yes");
					
					_p = parseFloat(jQuery(_d).find("traitements anti_reflets").attr("price")*2);
					jQuery("label[for=traitement-anti-reflets] .col-price").html(( _p > 0 ) ? _p + " €" : "Inclus" );
					jQuery("#traitement-anti-reflets").attr("disabled",_p == 0);
					_p = parseFloat(jQuery(_d).find("traitements anti_rayures").attr("price")*2);
					jQuery("label[for=traitement-anti-rayures] .col-price").html(( _p > 0 ) ? _p + " €" : "Inclus" );
					jQuery("#traitement-anti-rayures").attr("disabled",_p == 0);
					_p = parseFloat(jQuery(_d).find("traitements anti_eau").attr("price")*2);
					jQuery("label[for=traitement-anti-eau] .col-price").html(( _p > 0 ) ? _p + " €" : "Inclus" );
					jQuery("#traitement-anti-eau").attr("disabled",_p == 0);
					_p = parseFloat(jQuery(_d).find("traitements anti_uv").attr("price")*2);
					jQuery("label[for=traitement-anti-uv] .col-price").html(( _p > 0 ) ? _p + " €" : "Inclus" );
					jQuery("#traitement-anti-uv").attr("disabled",_p == 0);
					
					// Couleurs des verres
					
					if (jQuery(_d).find("teintes incolore").attr("available") == "yes") {
						jQuery("label[for=teinte-incolore]").show();
						jQuery("#teinte-incolore").attr("checked",jQuery(_d).find("teintes incolore").text() == "yes");
					} else { jQuery("label[for=teinte-incolore]").hide(); }
					
					if (glasses.hasCategory(_d,"cat0")) {
						jQuery("label[for=teinte-categorie_0]").show();
						//jQuery(".teinte-cat0 select").html("<option value=''>Aucune</option>");
						jQuery(".teinte-cat0 select").empty();
						var _wasSelected = glasses.fillCategoryColors(_d,"cat0");
						jQuery("#teinte-categorie_0").attr("checked",_wasSelected);
					} else { jQuery("label[for=teinte-categorie_0]").hide(); }
					
					if (glasses.hasCategory(_d,"cat1")) {
						jQuery("label[for=teinte-categorie_1]").show();
						//jQuery(".teinte-cat1 select").html("<option value=''>Aucune</option>");
						jQuery(".teinte-cat1 select").empty();
						var _wasSelected = glasses.fillCategoryColors(_d,"cat1");
						jQuery("#teinte-categorie_1").attr("checked",_wasSelected);
					} else { jQuery("label[for=teinte-categorie_1]").hide(); }
					
					if (glasses.hasCategory(_d,"cat2")) {
						jQuery("label[for=teinte-categorie_2]").show();
						//jQuery(".teinte-cat2 select").html("<option value=''>Aucune</option>");
						jQuery(".teinte-cat2 select").empty();
						var _wasSelected = glasses.fillCategoryColors(_d,"cat2");
						jQuery("#teinte-categorie_2").attr("checked",_wasSelected);
					} else { jQuery("label[for=teinte-categorie_2]").hide(); }
					
					if (glasses.hasCategory(_d,"cat3")) {
						jQuery("label[for=teinte-categorie_3]").show();
						//jQuery(".teinte-cat3 select").html("<option value=''>Aucune</option>");
						jQuery(".teinte-cat3 select").empty();
						var _wasSelected = glasses.fillCategoryColors(_d,"cat3");
						jQuery("#teinte-categorie_3").attr("checked",_wasSelected);
					} else { jQuery("label[for=teinte-categorie_3]").hide(); }
					
					if (glasses.hasCategory(_d,"degrade")) {
					  jQuery("label[for=teinte-degrade]").parents("div.teinte-degrade").show();
						//jQuery("label[for=teinte-degrade]").show();
						//jQuery(".teinte-degrade select").html("<option value=''>Aucune</option>");
						jQuery(".teinte-degrade select").empty();
						var _wasSelected = glasses.fillCategoryColors(_d,"degrade");
						jQuery("#teinte-degrade").attr("checked",_wasSelected);
					} else { jQuery("label[for=teinte-degrade]").parents("div.teinte-degrade").hide();
                   //jQuery("label[for=teinte-degrade]").hide(); 
                 }
					
					if (glasses.hasCategory(_d,"photochromique")) {
						jQuery("label[for=teinte-photochromique]").show();
						jQuery(".teinte-photochromique").show();
						//jQuery(".teinte-photochromique select").html("<option value=''>Aucune</option>");
						jQuery(".teinte-photochromique select").empty();
						var _wasSelected = glasses.fillCategoryColors(_d,"photochromique");
						jQuery("#teinte-photochromique").attr("checked",_wasSelected);
					} else { jQuery(".teinte-photochromique").hide(); }
					
					
					//Floating Cart
					// Prix 
					
					jQuery("#fp-monture .fp-monture-price").html(jQuery(pData).find("prices").attr("monture"));
					jQuery("#fp-verre .fp-verre-price").html(jQuery(pData).find("prices").attr("verres"));
					
					jQuery("#floating-price .fp-price").html(jQuery(pData).find("prices").attr("str_total"));
					jQuery("#floating-price ul.fp-infos").empty();
					jQuery(pData).find("prices item").each(function() {
						jQuery("#floating-price ul.fp-infos").append("<li>- " + jQuery(this).text() + "</li>");
					});
					
					//if (!jQuery("#floating-price").is(":visible")) jQuery("#floating-price").animate({ opacity:"show" },500);
					if (!jQuery("#floating-price").is(":visible")) jQuery("#floating-price").show();
					
					//Product Details
					
					jQuery("#fp-monture img").attr('src',jQuery(pData).find("product_monture").attr("img"));
					jQuery("#fp-verre img").attr('src',jQuery(pData).find("product_glasses").attr("img"));
					
					jQuery("#floating-price .fp-monture-desc").html(jQuery(pData).find("product_monture").attr("desc"));
					jQuery("#floating-price .fp-verre-desc").html(jQuery(pData).find("product_glasses").attr("desc"));
					
					
					jQuery(".product-step-content").glassesWaitingStop();
					
				}
			});
			
			return(true);
			
		},
		
		hasCategory:function(pXml,pCategory) {
			
			var _found = false;
			jQuery(pXml).find("teintes").children().each(function() {
				var _nn = jQuery(this)[0].nodeName;
				if (_nn.indexOf(pCategory + "_") != -1) {
					if (jQuery(this).attr("available") == "yes") _found = true;
				}
			});
			
			return(_found);
			
		},
		
		fillCategoryColors:function(pXml,pCategory) {
			
			var _selectedColor = "";
			var _colorsEq = { gris:"Gris" , vert:"Vert" , brun:"Brun" };
			
			jQuery(pXml).find("teintes").children().each(function() {
				var _nn = jQuery(this)[0].nodeName;
				if (_nn.indexOf(pCategory + "_") != -1 && jQuery(this).attr("available") == "yes") {
					var _currentColor = _nn.split("_")[1];
					if (jQuery(this).text() == "yes") { _selectedColor = _currentColor; }
					jQuery(".teinte-" + pCategory + " select").append("<option value=\"" + _currentColor + "\">" + _colorsEq[_currentColor] + " (+ " + jQuery(this).attr("price")*2 + " €)</option>");
				}
			});
			
			jQuery(".teinte-" + pCategory + " select").val(_selectedColor);
			return(_selectedColor != "");
			
		}
		
	};
