/**
 * @author Glucoz
 */
$(window).load(function(){	
	
	$('#nouvelleRechercheBur').click(function(){
			// on efface la liste deja presente
			$('#ulResBur').remove();
			switchAff('#homelocaliserUnBur','#resultatLocaliserUnBureau');
			$('#nouvelleRechercheBur').css('display','none');
			$('#submitBur').css('display','block');
			return false;
	})
	function formatJour(jourJ)
	{
		var aux =jourJ;
		aux = aux.replace('jour1','Lundi');
		aux = aux.replace('jour2','Mardi');
		aux = aux.replace('jour3','Mercredi');
		aux = aux.replace('jour4','Jeudi');
		aux = aux.replace('jour5','Vendredi');
		aux = aux.replace('jour6','Samedi');
		aux = aux.replace('jour7','Dimanche');
		return aux;
	}
	function formatTel(tel)
	{
		var aux = '';
		$.each(tel, function(i){
			aux += this;
			if ((i + 1) % 2 == 0) {
				aux += ' ';
			}
		});
		tel = aux;
		return tel;
	}
	function renameService(objServ)
	{
		var services = '';
		if (objServ.dab=="O") services = services + "<br> - Distributeur de billets";
        if (objServ.moneo=="O") services = services + "<br> - MONEO";
        if (objServ.cyberposte=="O") services = services + "<br> - Espace Cyberposte";
        if (objServ.cofi=="O") services = services + "<br> - Espace Conseiller financier";
        if ((objServ.cofi=="O") && (objServ.telephone_cofi != "")) services = services + ", t&eacute;l&eacute;phone : " + formatTel(objServ.telephone_cofi);
        if (objServ.lisa=="O") services = services + "<br> - Libre service d'affranchissement";
        if (objServ.photocopieur=="O") services = services + "<br> - Photocopieurs en libre service ";
        if (objServ.automate=="O") services = services + "<br> - Espace automate";
        if (objServ.acces_bureau_mobilite_reduite=="O") services = services + "<br> - Pour les personnes &agrave; mobilit&eacute; r&eacute;duite, l'acc&egrave;s au bureau de poste est possible.";
        if ((objServ.acces_bureau_mobilite_reduite=="O") && (objServ.acces_automate_mobilite_reduite=="O")) services = services + " Un guichet sp&eacute;cifique vous est r&eacute;serv&eacute;.";
        if ((objServ.acces_bureau_mobilite_reduite=="O") && (objServ.guichet_specifique_mobilite_reduite=="O")) services = services + " L'automate est accessible.";
		
		return services;
		
	}
	function moreBur()
	{
		$('#pasActus').css('display','none');
		var code_site = $('.code_saisie',$(this).parents('li')).text();
		// on vide le bloc
		$('.jcarousel-container').css("display","none");
		$('#AddLocaliserUnBur').html('');
		$('#zone_actus').slideDown('normal');
		$('#carousel_actus').css('display','none');
		$('#AddLocaliserUnBur').css('display','block');	
		//$('#widgetTabee').css('max-height','1030px');
		$('#widgetTabee').scrollTo('#zone_actus',400);	
		$.getJSON("http://www.laposte.fr/widgetcourrier/php/localiserBureauDetailJSONproxy.php?code_site="+code_site+"&callback=?",
			 function(data){
			 		var bureau_poste = 	data.bureau_poste;			
			 		// recuperation des donn�es
					var libelle_site = bureau_poste.libelle_site;
					var adresse = bureau_poste.adresse;
					var code_postal = bureau_poste.code_postal;
					var localite = bureau_poste.localite;
					var dab = bureau_poste.dab;
					var monero = bureau_poste.moneo;
					var cyberposte = bureau_poste.cyberposte;
					var tel = bureau_poste.telephone_indigo;
					if (tel == '') {
						tel = bureau_poste.telephone_standard;;
					}										
					tel = formatTel(tel);					
					var horaires   = bureau_poste.horaires;
					var txtPeriode ='';
					var htmlJour ='';
					var services = bureau_poste.services;
					var txtService = renameService(services);
					var txtServiceAux = '';
					var auxPeriode ="";
					if(txtService.length > 0)
					{
						txtServiceAux = '<strong><em>Liste des services</em></strong>'+txtService;
						
					}
					var taillePeriode = horaires.periode.length;					
					// gestion des periode multiple					
					if(horaires.periode.length)
					{
						$.each(horaires.periode,function(){
							var txtJourAux = '';
							  auxPeriode += '<ul class="listeHor">';
							  auxPeriode += '<li><strong><em>Horaires d\'ouvertures : P&eacute;riode du '+this.attributes.debut_periode +" au "+this.attributes.fin_periode+'</em></strong></li>';
							
								$.each(this.jour,function(){
									if(this.plage_horaire.length){										
										txtJourAux = "";
										$.each(this.plage_horaire,function(){											
											txtJourAux += this.debut_plage.substr(0,this.debut_plage.length-3) +' &aacute; '+this.fin_plage.substr(0,this.fin_plage.length-3)+' puis ';
										});
										txtJourAux = txtJourAux.substr(0,txtJourAux.length-5);
										
									}
									else
									{										
										txtJourAux = this.plage_horaire.debut_plage.substr(0,this.plage_horaire.debut_plage.length-3)+' &aacute; '+this.plage_horaire.fin_plage.substr(0,this.plage_horaire.fin_plage.length-3);
									}
									auxPeriode +='<li><strong>'+ formatJour(this.attributes.value) +'</strong> de '+txtJourAux+'</li>';
								});
							auxPeriode +='</ul>';
							
							 
						})
							$('#AddLocaliserUnBur').append('<h5>'+libelle_site+'</h5><p>'+adresse+'<br />'+code_postal+' '+localite+'</p><div class="horraireBur">'+auxPeriode+txtServiceAux+'</div>');

					}
					else{
							txtPeriode += horaires.periode.attributes.debut_periode +" au "+horaires.periode.attributes.fin_periode;
							var jour = horaires.periode.jour;
							var txtJourAux2;
							var indiceAux = 2;
							htmlJour = '<ul class="listeHor">';
								$.each(jour,function(){
									txtJourAux2='';
									
									if(this.plage_horaire.length){										
										
										$.each(this.plage_horaire,function(){
											txtJourAux2 += this.debut_plage +' &agrave; '+this.fin_plage+' puis de ';
										});
										txtJourAux2 = txtJourAux2.substr(0,txtJourAux2.length-8);
										
									}
									else
									{
										txtJourAux2 = this.plage_horaire.debut_plage+' &aacute; '+this.plage_horaire.fin_plage;
									}
									htmlJour +='<li><strong>'+ formatJour(this.attributes.value) +'</strong> de '+txtJourAux2+'</li>';
								});
							htmlJour +='</ul>';
							$('#AddLocaliserUnBur').append('<h5>'+libelle_site+'</h5><p>'+adresse+'<br />'+code_postal+' '+localite+'</p><div class="horraireBur"><strong><em>Horaires d\'ouvertures : P&egrave;riode du '+txtPeriode+'</em></strong>'+htmlJour+txtServiceAux+'</div>')

								taillePeriode = indiceAux;
					}
				//alert(taillePeriode);
				$('#widgetTabee').css('height',(taillePeriode*115)+257+'px');
				$(window).scrollTo('0',400);
				//console.log(3*115+257)	 
		});
		
	}
	$('#forminiBur').submit(function(e){
		$('#test10').css('display','block')
		 var dab = $('#distriBillet:checked').val();
		 var cyberposte =$('#espaceCyber:checked').val();
		 var moneo =$('#moneo:checked').val();
		 var mobred =$('#accespmr:checked').val();
		 var photocopieur =$('#photocBillet:checked').val();
		 var changeur =$('#changeur:checked').val();
		 var cofi =$('#conseillerF:checked').val();
		 var lisa =$('#libreServ:checked').val();
		 var automate =$('#automate:checked').val();
		 var CodePostal =$('#burInput').val();
		 var NomVille =$('#txtCommuneBur').val();
		 if (NomVille != '' || CodePostal != '') {
		 	$('#submitBur').css('display', 'none');
			$('#chargement').css('display', 'block');
		 	$('#ulResBur').remove();
		 	$.getJSON("http://www.laposte.fr/widgetcourrier/php/localiserBureauJSONproxy.php?NomVille=" + NomVille + "&dab=" + dab + "&moneo="+moneo+"&cyberposte=" + cyberposte + "&mobred=" + photocopieur + "&changeur=" + changeur + "&cofi=" + cofi + "&lisa=" + lisa + "&automate=" + automate + "&CodePostal=" + CodePostal + "&callback=?", function(data){
					$('#resultatLocaliserUnBureau .zoneSaisie').text(NomVille + ' ' + CodePostal);
		 			$('#test10').before('<ul id="ulResBur"></ul>');
					//console.log("data.errorID    "+data.errorID);
					if (data.errorID != undefined) {
						$('#ulResBur').remove();
						$('#nbResultatBurx').text('Aucun');
						//$('#ulResBur').append('<li class="rstBur"><div class="englobeRslt"><p style="padding-left:10px;" class="blanc">' + data.errorID + '</p></div></li>');
						$('#test10').css('display','none');
					}
					else {
						var htmlBur = new Array();
						
						$.each(data, function(){
												
							$('#nbResultatBurx').text(this.bureau.length);							
							if(this.bureau.length)
							{
								
								if(this.bureau.length <10 ){$('#test10').css('display','none')}
								for(r in this.bureau){
								//$.each(this.bureau, function(){
									var tel = this.bureau[r].telephone_indigo;
									if (tel == '') {
										tel = this.bureau[r].telephone_standard;
									}
									tel = formatTel(tel);
//									$('#ulResBur').append('<li class="rstBur"><div class="englobeRslt"><table><tr><td class="gauche"><h5 class="bleu">' + this.libelle_site + '</h5><p class="blanc">' + this.adresse + '<br />' + tel + '</p></td><td class="droite" valign="middle"><a href="http://www.laposte.fr/widgetcourrier/localiser_bureau/index.php?bureau=' + this.libelle_site + '&adresse=' + this.adresse + '&telephone=' + tel + '" target="_blank"> <img src="img/skin1/carte.png" alt="carte" title="carte" /></a><span class="ecran code_saisie">' + this.code_site + '</span></td></tr></table></div></li>');
									//$('#ulResBur').append('<li class="rstBur"><div class="englobeRslt"><table><tr><td class="gauche"><h5 class="bleu">' + this.libelle_site + '</h5><p class="blanc">' + this.adresse + '<br />' + tel + '</p></td><td class="droite" valign="middle"><a href="http://maps.google.fr/?q='+this.adresse+',FRANCE "> <img src="img/skin1/carte.png" alt="carte" title="carte" /></a><span class="ecran code_saisie">' + this.code_site + '</span></td></tr></table></div></li>');
									htmlBur.push('<li class="rstBur"><div class="englobeRslt"><table><tr><td class="gauche"><h5 class="bleu">' + this.bureau[r].libelle_site + '</h5><p class="blanc">' + this.bureau[r].adresse + '<br />' + tel + '</p></td><td class="droite" valign="middle"><a class="carteMaps" href="http://maps.google.fr/?q='+this.bureau[r].adresse+',FRANCE "> <img src="img/skin1/carte.png" alt="carte" title="carte" /></a><span class="ecran code_saisie">' + this.bureau[r].code_site + '</span></td></tr></table></div></li>');
								}
								//});
								$('#ulResBur').html(htmlBur.join(''));
							}
							else{	
								$('#test10').css('display','none');	
								$('#nbResultatBurx').text('1');												
								var tel = this.bureau.telephone_indigo;
									if (tel == '') {
										tel = this.bureau.telephone_standard;
									}
									tel = formatTel(tel);
									$('ul#ulResBur').append('<li class="rstBur"><div class="englobeRslt"><table><tr><td class="gauche"><h5 class="bleu">' + this.bureau.libelle_site + '</h5><p class="blanc">' + this.bureau.adresse + '<br />' + tel + '</p></td><td class="droite" valign="middle"><a class="carteMaps" href="http://maps.google.fr/?q='+this.bureau.adresse+',FRANCE" target="_blank"> <img src="img/skin1/carte.png" alt="carte" title="carte" /></a><span class="ecran code_saisie">' + this.bureau.code_site + '</span></td></tr></table></div></li>');
							}
						});
						$("ul#ulResBur li:gt(9)").css("display", "none");
						$("ul#ulResBur li:lt(9)").css("display", "block");
						$("ul#ulResBur li:lt(9)").addClass('visible');
					}
		 			switchAff('#resultatLocaliserUnBureau', '#homelocaliserUnBur');
		 			$('.rstBur .gauche').click(moreBur);
					$('.rstBur .droite').click(function(){
						var url = $('a',this).attr('href');
						window.location.href=url;
					});
		 		
		 	});
			$('#chargement').css('display', 'none');
			
			$('#nouvelleRechercheBur').css('display', 'block');
		 }
		return false;
			});
$('#test10').click(function(){
	$("#ulResBur li:not(.visible):lt(11)").css("display", "block");
	//$('#widgetTabee').scrollTo($("#ulResBur li.visible:last"),800);
	$("#ulResBur li:not(.visible):lt(11)").addClass('visible');	
	if($("#ulResBur li.visible").length == $("#ulResBur li").length ){
		$('#test10').css('display','none');
	}
});
$('#conteSerz').css('display','block');
$('#loader').remove();
});
 
