$(document).ready(
	function() {
		var site_url = 'http://www.boutiquedurangement.com/';
		
		$('.comment-overlay').css('width',$('body').width());
		$('.comment-overlay').css('height',$('body').height());
		$('.comment-overlay').show();
		$('.TB_window-comment').fadeIn('slow');
		
		// op noel
		$('#noel-iframe').css('width',$('body').width());
		$('#noel-iframe').css('height',$('body').height());
		$('#noel-overlay').css('width',$('body').width());
		$('#noel-overlay').css('height',$('body').height());		

		
		//Relais colis
		if ($('#id_carrier5').is(":checked")) { $("#rc_msg_alert").show(); }
		$('#id_carrier4').click(function() { rc_reset(); });
		$('#id_carrier8').click(function() { rc_reset(); });
		$('#id_carrier7,#modif_relaiscolis').click(function() { if ($("#i_relaiscolis").val() == 0) { $("#rc_msg_alert").show(); } $('#id_carrier7').attr("checked","checked"); $('#relais_colis_overlay').show(); $('#relais_colis').show(); });		
		$('#relais_close').click(function() { rc_close() });
		$("#relais_codepostal").val("Code postal...");
		$("#relais_codepostal").click(function() { if ($(this).val() == 'Code postal...') $(this).val(''); });
		$("#relais_codepostal").keypress(function (e) { if (e.which == 13) { ajaxSogepListeXml($("#relais_codepostal").val()); return false; } });
		//$("#relais_codepostal").change(function() { ajaxSogepVilleXml(); });
		$('#launch_RetournerListe').click(function () { ajaxSogepListeXml($("#relais_codepostal").val()); });
		$('#chooseit').click(function () { chooseRelaisXml(); });

		function rc_reset()
		{
			$("#rc_msg_alert").hide();
			$("#i_relaiscolis").val("0");
			$("#rc_horaires").slideUp("slow");
		}
		
		function rc_close()
		{
			$('#relais_colis_overlay').hide(); $('#relais_colis').hide();
		}
		
		function chooseRelaisXml()
		{
			data_commune = $('#relais').val().split(' | ');
			identifiantchronopost = data_commune[0]; cp = data_commune[1]; commune = data_commune[2];			
			$("#ajax_loading").show();
			data = $.get(site_url + "xml/chronopost/horaires.php", 
					{ ville: commune, code_postal: cp },
					function() { parseRelaisXml(data); }
				);
		}
		
		function parseRelaisXml(data)
		{
			$(data.responseXML).find("ArrayOfRelais").each(function() {
				//var erreur = $(this).find("Erreur");
				var relais = $(this).find("Relais");

				relais.each(function () {
					if ($(this).find("IdentifiantChronopost").text() == identifiantchronopost)
					{
						var horaires = $(this).find("HorairesOuverture");
						var adresse = $(this).find("Adresse").text();
						var nom_relais = $(this).find("Nom_Relais").text();
						var date_arrive_colis = $(this).find("dateArriveColis").text();
						var coderelais = $(this).find("IdentifiantChronopost").text();
						var jours = new Array("LUNDI","MARDI","MERCREDI","JEUDI","VENDREDI","SAMEDI","DIMANCHE");
						var tab_horaire = new Array();

						for(i=0;i<7;i++)
						{
							tab_horaire[jours[i]] = new Array(
								$(horaires).find(jours[i]).find("heurouvmat").text(),
								$(horaires).find(jours[i]).find("heurfermat").text(),
								$(horaires).find(jours[i]).find("heurouvapm").text(),
								$(horaires).find(jours[i]).find("heurferapm").text()
							);
						}
						
						$("#i_relaiscolis").val("1");
						$("#i_rc_nom").val(nom_relais);
						$("#i_rc_coderelais").val(coderelais);
						$("#i_rc_cp").val(cp);
						$("#i_rc_commune").val(commune);
						$("#i_rc_adresse").val(adresse);
						
						$("#rc_adresse").html(nom_relais + "<br />" + adresse + "<br />" + cp + " " + commune);
						for(i=0;i<7;i++)
						{
							$("#" + jours[i].toLowerCase() + "_am").html(tab_horaire[jours[i]][0] + "<br />" + tab_horaire[jours[i]][1]);
							$("#" + jours[i].toLowerCase() + "_pm").html(tab_horaire[jours[i]][2] + "<br />" + tab_horaire[jours[i]][3]);
						}
						$("#rc_msg_alert").hide();
						//$("#delay6").text(date_arrive_colis);
						$("#rc_horaires").slideDown("slow");
					}
				});
			});
			$("#ajax_loading").hide();
			rc_close();
			
		}
		
		
		function ajaxSogepListeXml(code_postal)
		{
			$("#ajax_loading").show();
			data = $.get(site_url + "xml/chronopost/relais.php", 
					{ code_postal: code_postal },
					function() { parseSogepListeXml(data); }
				);
			return data;
		}
		
		
		/*
			function ajaxSogepVilleXml()
			{
				$("#ajax_loading").show();
				$("#select_relais").hide();
				$("#chooseit").hide();
				$("#launch_RetournerListe").show();
				data = $.get(site_url + "xml/VerifieCP_Ville.php", 
						{ code_postal: $("#relais_codepostal").val() },
						function() { parseSogepVilleXml(data); }
					);
				return data;
			}
			
			function parseSogepVilleXml(data)
			{
				html_select = '';
				$(data.responseXML).find("ArrayOfVille").each(function() {
					var erreur = $(this).find("Erreur");
					var ville = $(this).find("Ville");
					if (erreur.text() > 0)
					{
						html_select = 'Aucune ville pour ce code postal';
					}
					else
					{
						html_select = '<select name="relais_ville" id="relais_ville"><option value="0">- Choisissez -</option>';
						ville.each(function () {
							var nom_ville = $(this).find('nomVille').text();
							html_select += '<option value="' + nom_ville.toLowerCase() + '">' +
												nom_ville + 
											'</option>';
						});
						html_select += '</select>';
					}
				});

				$("#select_ville").html(html_select);
				$("#ajax_loading").hide();
			}
		*/
		
		
		function parseSogepListeXml(data)
		{
			html_select = '';
			$(data.responseXML).find("ArrayOfRelais").each(function() {
				var erreur = $(this).find("Erreur");
				var relais = $(this).find("Relais");
				if (erreur.text() > 0)
				{
					html_select = 'Aucun point relais pour cette ville';
				}
				else
				{
					html_select = '<select name="relais" id="relais" style="width:540px"><option value="0">- Choisissez -</option>';
					relais.each(function () {
						html_select += '<option value="' + 
											$(this).find("IdentifiantChronopost").text() +  ' | ' + 
											$(this).find("Code_Postal").text() + ' | ' + 
											$(this).find("Commune").text() + 
										'">' + 
											$(this).find("Nom_Relais").text() + ' | ' + 
											$(this).find("Adresse").text() + ' | ' + 
											$(this).find("Code_Postal").text() + ' | ' + 
											$(this).find("Commune").text() + 
										'</option>';
					});
					html_select += '</select>';
				}
			});			
			
			$("#select_relais").show();
			$("#select_relais").html(html_select);
			$("#chooseit").show();
			$("#ajax_loading").hide();
		}
	}
)