var dataPanneauObg = new Array();
var dataPanneauSec = new Array();

var nbActus = 0;
var nbActusPresente = 3;
var MAX_SERVICES = 12; // nombre de service a afficher dans le panneau
						// principal
var idZoneActus = '#zone_actus';
var idZoneCentrale = '#zone_centrale';
var idZoneAdd = '#zone_add';
var dejaChoisiArray = new Array(); // tableau des service deja choisi
var dejaChoisiString = ''; // chaine de caract�re pour cookie - separateur
var dejaLu = new Array(); // tableau des actus deja lu
var dejaLuString;
// dejaLu.push("80");
/** cookie * */
// var widgetPref = new PlgPreferencies("Ecourier"); // � cr�er une seule fois
// pour le widget
// $.cookie('dejaChoisi',''); // pour l'enregistrer
// alert('widgetPref.get_preferency(dejaChoisi);
// '+widgetPref.get_preferency('dejaChoisi'));
$.cookie('dejaChoisi');// pour r�cuperer la valeur.
$.cookie('dejaLu');
// alert('$.cookie(dejaChoisi); '+$.cookie('dejaChoisi'));
/** carousel des service additionnel by salah * */
var nbLi = $('#service_add li').length; // on recupere tout les element
var hauteurLi = 95 * 2; // *2 pour defiler deux ligne en meme temps
var nbLiPerLigne = 5 * 2;
var nbLigne = Math.ceil(nbLi / nbLiPerLigne) - 1;
var hauteurUL = nbLigne * hauteurLi;
var currentLigne = 0;
var currentTop = 0;
/** fonction de la lecture du flux actus * */
function testNbElt() {
	nbLi = $('#service_add li').length; // on recupere tout les element
	hauteurLi = 95 * 2; // *2 pour defiler deux ligne en meme temps
	nbLiPerLigne = 5 * 2;
	nbLigne = Math.ceil(nbLi / nbLiPerLigne) - 1;
	hauteurUL = nbLigne * hauteurLi;

	if ($('#service_add li').length < 11) {
		$('#nextService').css('display', 'none');
		$('#prevService').css('display', 'none');
		$("#service_add").animate( {
			top : 0
		}, 500)
		currentLigne = 0;
		currentTop = 0;
	} else {
		if (currentLigne == 0)
			$('#nextService').css('display', 'block');
	}
}
function html_entity_decode(texte) {
	texte = texte.replace(/&quot;/g, '"'); // 34 22
	texte = texte.replace(/&amp;/g, '&'); // 38 26
	texte = texte.replace(/&#39;/g, "'"); // 39 27
	texte = texte.replace(/&lt;/g, '<'); // 60 3C
	texte = texte.replace(/&gt;/g, '>'); // 62 3E
	texte = texte.replace(/&circ;/g, '^'); // 94 5E
	texte = texte.replace(/&lsquo;/g, '‘'); // 145 91
	texte = texte.replace(/&rsquo;/g, '’'); // 146 92
	texte = texte.replace(/&ldquo;/g, '“'); // 147 93
	texte = texte.replace(/&rdquo;/g, '”'); // 148 94
	texte = texte.replace(/&bull;/g, '•'); // 149 95
	texte = texte.replace(/&ndash;/g, '–'); // 150 96
	texte = texte.replace(/&mdash;/g, '—'); // 151 97
	texte = texte.replace(/&tilde;/g, '˜'); // 152 98
	texte = texte.replace(/&trade;/g, '™'); // 153 99
	texte = texte.replace(/&scaron;/g, 'š'); // 154 9A
	texte = texte.replace(/&rsaquo;/g, '›'); // 155 9B
	texte = texte.replace(/&oelig;/g, 'œ'); // 156 9C
	texte = texte.replace(/&#357;/g, ''); // 157 9D
	texte = texte.replace(/&#382;/g, 'ž'); // 158 9E
	texte = texte.replace(/&Yuml;/g, 'Ÿ'); // 159 9F
	texte = texte.replace(/&nbsp;/g, ' '); // 160 A0
	texte = texte.replace(/&iexcl;/g, '¡'); // 161 A1
	texte = texte.replace(/&cent;/g, '¢'); // 162 A2
	texte = texte.replace(/&pound;/g, '£'); // 163 A3
	texte = texte.replace(/&curren;/g, ' '); // 164 A4
	texte = texte.replace(/&yen;/g, '¥'); // 165 A5
	texte = texte.replace(/&brvbar;/g, '¦'); // 166 A6
	texte = texte.replace(/&sect;/g, '§'); // 167 A7
	texte = texte.replace(/&uml;/g, '¨'); // 168 A8
	texte = texte.replace(/&copy;/g, '©'); // 169 A9
	texte = texte.replace(/&ordf;/g, 'ª'); // 170 AA
	texte = texte.replace(/&laquo;/g, '«'); // 171 AB
	texte = texte.replace(/&not;/g, '¬'); // 172 AC
	texte = texte.replace(/&shy;/g, '­'); // 173 AD
	texte = texte.replace(/&reg;/g, '®'); // 174 AE
	texte = texte.replace(/&macr;/g, '¯'); // 175 AF
	texte = texte.replace(/&deg;/g, '°'); // 176 B0
	texte = texte.replace(/&plusmn;/g, '±'); // 177 B1
	texte = texte.replace(/&sup2;/g, '²'); // 178 B2
	texte = texte.replace(/&sup3;/g, '³'); // 179 B3
	texte = texte.replace(/&acute;/g, '´'); // 180 B4
	texte = texte.replace(/&micro;/g, 'µ'); // 181 B5
	texte = texte.replace(/&para/g, '¶'); // 182 B6
	texte = texte.replace(/&middot;/g, '·'); // 183 B7
	texte = texte.replace(/&cedil;/g, '¸'); // 184 B8
	texte = texte.replace(/&sup1;/g, '¹'); // 185 B9
	texte = texte.replace(/&ordm;/g, 'º'); // 186 BA
	texte = texte.replace(/&raquo;/g, '»'); // 187 BB
	texte = texte.replace(/&frac14;/g, '¼'); // 188 BC
	texte = texte.replace(/&frac12;/g, '½'); // 189 BD
	texte = texte.replace(/&frac34;/g, '¾'); // 190 BE
	texte = texte.replace(/&iquest;/g, '¿'); // 191 BF
	texte = texte.replace(/&Agrave;/g, 'À'); // 192 C0
	texte = texte.replace(/&Aacute;/g, 'Á'); // 193 C1
	texte = texte.replace(/&Acirc;/g, 'Â'); // 194 C2
	texte = texte.replace(/&Atilde;/g, 'Ã'); // 195 C3
	texte = texte.replace(/&Auml;/g, 'Ä'); // 196 C4
	texte = texte.replace(/&Aring;/g, 'Å'); // 197 C5
	texte = texte.replace(/&AElig;/g, 'Æ'); // 198 C6
	texte = texte.replace(/&Ccedil;/g, 'Ç'); // 199 C7
	texte = texte.replace(/&Egrave;/g, 'È'); // 200 C8
	texte = texte.replace(/&Eacute;/g, 'É'); // 201 C9
	texte = texte.replace(/&Ecirc;/g, 'Ê'); // 202 CA
	texte = texte.replace(/&Euml;/g, 'Ë'); // 203 CB
	texte = texte.replace(/&Igrave;/g, 'Ì'); // 204 CC
	texte = texte.replace(/&Iacute;/g, 'Í'); // 205 CD
	texte = texte.replace(/&Icirc;/g, 'Î'); // 206 CE
	texte = texte.replace(/&Iuml;/g, 'Ï'); // 207 CF
	texte = texte.replace(/&ETH;/g, 'Ð'); // 208 D0
	texte = texte.replace(/&Ntilde;/g, 'Ñ'); // 209 D1
	texte = texte.replace(/&Ograve;/g, 'Ò'); // 210 D2
	texte = texte.replace(/&Oacute;/g, 'Ó'); // 211 D3
	texte = texte.replace(/&Ocirc;/g, 'Ô'); // 212 D4
	texte = texte.replace(/&Otilde;/g, 'Õ'); // 213 D5
	texte = texte.replace(/&Ouml;/g, 'Ö'); // 214 D6
	texte = texte.replace(/&times;/g, '×'); // 215 D7
	texte = texte.replace(/&Oslash;/g, 'Ø'); // 216 D8
	texte = texte.replace(/&Ugrave;/g, 'Ù'); // 217 D9
	texte = texte.replace(/&Uacute;/g, 'Ú'); // 218 DA
	texte = texte.replace(/&Ucirc;/g, 'Û'); // 219 DB
	texte = texte.replace(/&Uuml;/g, 'Ü'); // 220 DC
	texte = texte.replace(/&Yacute;/g, 'Ý'); // 221 DD
	texte = texte.replace(/&THORN;/g, 'Þ'); // 222 DE
	texte = texte.replace(/&szlig;/g, 'ß'); // 223 DF
	texte = texte.replace(/&agrave;/g, 'à'); // 224 E0
	texte = texte.replace(/&aacute;/g, 'á'); // 225 E1
	texte = texte.replace(/&acirc;/g, 'â'); // 226 E2
	texte = texte.replace(/&atilde;/g, 'ã'); // 227 E3
	texte = texte.replace(/&auml;/g, 'ä'); // 228 E4
	texte = texte.replace(/&aring;/g, 'å'); // 229 E5
	texte = texte.replace(/&aelig;/g, 'æ'); // 230 E6
	texte = texte.replace(/&ccedil;/g, 'ç'); // 231 E7
	texte = texte.replace(/&egrave;/g, 'è'); // 232 E8
	texte = texte.replace(/&eacute;/g, 'é'); // 233 E9
	texte = texte.replace(/&ecirc;/g, 'ê'); // 234 EA
	texte = texte.replace(/&euml;/g, 'ë'); // 235 EB
	texte = texte.replace(/&igrave;/g, 'ì'); // 236 EC
	texte = texte.replace(/&iacute;/g, 'í'); // 237 ED
	texte = texte.replace(/&icirc;/g, 'î'); // 238 EE
	texte = texte.replace(/&iuml;/g, 'ï'); // 239 EF
	texte = texte.replace(/&eth;/g, 'ð'); // 240 F0
	texte = texte.replace(/&ntilde;/g, 'ñ'); // 241 F1
	texte = texte.replace(/&ograve;/g, 'ò'); // 242 F2
	texte = texte.replace(/&oacute;/g, 'ó'); // 243 F3
	texte = texte.replace(/&ocirc;/g, 'ô'); // 244 F4
	texte = texte.replace(/&otilde;/g, 'õ'); // 245 F5
	texte = texte.replace(/&ouml;/g, 'ö'); // 246 F6
	texte = texte.replace(/&divide;/g, '÷'); // 247 F7
	texte = texte.replace(/&oslash;/g, 'ø'); // 248 F8
	texte = texte.replace(/&ugrave;/g, 'ù'); // 249 F9
	texte = texte.replace(/&uacute;/g, 'ú'); // 250 FA
	texte = texte.replace(/&ucirc;/g, 'û'); // 251 FB
	texte = texte.replace(/&uuml;/g, 'ü'); // 252 FC
	texte = texte.replace(/&yacute;/g, 'ý'); // 253 FD
	texte = texte.replace(/&thorn;/g, 'þ'); // 254 FE
	texte = texte.replace(/&yuml;/g, 'ÿ'); // 255 FF
	return texte;
}
function buildActus() {
	$
			.getJSON(
					"http://www.laposte.fr/widgetcourrier/php/wtpeNewsJSON.php?version=2008&cible=pub&module=iphone&callback=?",
					function(data) {
						// $('#zone_actus .contenu').remove('<ul
						// id="carousel_actus"></ul>');
						$('#zone_actus .contenu').append(
								'<ul id="carousel_actus"></ul>');
						$('#pasActus').css('display', 'block');
						if (data.length) {
							nbActusPresente = data.length;
							var html = "";
							$
									.each(
											data,
											function() {
												if (this.cible == "pub"
														|| this.cible == "tous") {
													var docId = this.docID;
													if (jQuery.inArray(docId,
															dejaLu) == -1) {
														nbActus++;
													}
													var chapo = this.chapo;
													var imgSrc = this.visuelURL;
													var Src = this.linkURL
													if (Src != '') {
														Src = '<a target="_blank" href="' + Src + '" class="enSavoirPlus">En savoir Plus</a>';
													}
													var titre = this.libelle;
													var texte = this.description;
													var date = '';
													if (this.date2publication) {
														date = this.date2publication
																.replace(/-/,
																		"/");
														date = date.replace(
																/-/, "/");
													}
													$('#pasActus').remove();
													html += '<li id="'
															+ docId
															+ '"><h3>'
															+ titre
															+ '&nbsp; &nbsp; &nbsp;<span class="date">'
															+ date
															+ '</span></h3><div class="spacer"></div><span class="borderGray"><img src="'
															+ imgSrc
															+ '" alt="" title=""  /></span><h4>'
															+ chapo
															+ '</h4><p>'
															+ texte
															+ '</p>'
															+ Src
															+ '<div class="spacer"></div></li>';
													// .append('<li
													// id="'+docId+'"><h3>'+titre+'&nbsp;
													// &nbsp; &nbsp;<span
													// class="date">'+date+'</span></h3><div
													// class="spacer"></div><span
													// class="borderGray"><img
													// src="'+imgSrc+'" alt=""
													// title=""
													// /></span><h4>'+chapo+'</h4><p>'+texte+'</p>'+Src+'<div
													// class="spacer"></div></li>');
												}
												$('#carousel_actus')
														.html(
																html_entity_decode(html));
											});
						} else {
							var chapo = data.chapo;
							var imgSrc = data.visuelURL;
							var Src = data.linkURL;
							if (Src != '') {
								Src = '<a target="_blank" href="' + Src + '" class="enSavoirPlus">En savoir Plus</a>';
							}
							var titre = data.libelle;
							var texte = data.description;
							var date = '';
							var docId = data.docID;
							if (jQuery.inArray(docId, dejaLu) != -1) {
								nbActus++;
							}
							if (data.date2publication) {
								date = data.date2publication.replace(/-/, "/");
								date = date.replace(/-/, "/");
							}
							if (texte) {
								var docId = this.docID;
								if (!jQuery.inArray(docId, dejaLu)) {
									nbActus++;
								}
								nbActusPresente = 1;
								$('#pasActus').remove();
								$('#carousel_actus')
										.append(
												'<li><h3>'
														+ titre
														+ '&nbsp; &nbsp; &nbsp;<span class="date">'
														+ date
														+ '</span></h3><div class="spacer"></div><span class="borderGray"><img src="'
														+ imgSrc
														+ '" alt="" title=""  /></span><h4>'
														+ chapo
														+ '</h4><p>'
														+ texte
														+ '</p>'
														+ Src
														+ '<div class="spacer"></div></li>');
							}

						}

						$('#nbActus').html(nbActus);
					});

}
/* fonction de la cr�ation du bloc de service centrale */
function saveDejaChoisi() {
	dejaChoisiString = '';
	// on recupere tout les ID des
	$.each($('#service_obg > li > a.ser_obg'), function() {
		var aux = this.id.substring(5, this.id.length);
		dejaChoisiArray.push(aux);
		dejaChoisiString += aux + '-';
	});
	dejaChoisiString = dejaChoisiString.substring(0,
			dejaChoisiString.length - 1); // on retire le dernier -
	// alert(dejaChoisiString);
	$.cookie('dejaChoisi', dejaChoisiString); // pour l'enregistrer
	// console.log(dejaChoisiString);
}
function getDejaChoisi() {
	var auxDejaChoisi = $.cookie('dejaChoisi'); // pour r�cuperer la valeur.
	// var auxDejaChoisi = '18-58-54-56'; // pour r�cuperer la valeur.
	if (auxDejaChoisi && auxDejaChoisi.length > 1) {
		dejaChoisiArray = auxDejaChoisi.split('-');
	}

}
function saveDejaLu(docId) {
	dejaLu.push(docId);
	dejaLuString = '';
	// on recupere tout les ID des
	$.each(dejaLu, function() {
		dejaLuString += this + '-';
	});
	dejaLuString = dejaLuString.substring(0, dejaLuString.length - 1); // on
																		// retire
																		// le
																		// dernier
																		// -
	// alert(dejaChoisiString);
	$.cookie('dejaLu', dejaLuString); // pour l'enregistrer
	// console.log(dejaChoisiString);
	// $.cookie('dejaLu','');
	nbActus--;
	if (nbActus < 0) {
		nbActus = 0;
	}
	$('#nbActus').html(nbActus);
	// $.cookie('dejaLu','');
}

function getDejaLu() {
	var auxDejaChoisi = $.cookie('dejaLu'); // pour r�cuperer la valeur.
	dejaLuString = auxDejaChoisi;

	// var auxDejaChoisi = '18-58-54-56'; // pour r�cuperer la valeur.
	if (auxDejaChoisi && auxDejaChoisi.length > 1) {
		dejaLu = auxDejaChoisi.split('-');
	} else if (auxDejaChoisi && auxDejaChoisi.length == 1) {
		dejaLu.push(auxDejaChoisi);
	}
}
function getVisible(carousel, item, idx, state) {
	// alert($(item).attr('id'));
	saveDejaLu($(item).attr('id'));
	// display('Item #' + idx + ' is now visible');
};

function buildServices() {
	dataPanneauObg = new Array();
	dataPanneauSec = new Array();
	$
			.getJSON(
					"http://www.laposte.fr/widgetcourrier/php/wtpeServicesJSON.php?cible=pub&&module=iphone&timeStamp=1236356476567&callback=?",
					function(data) {
						
						  data.push({"docID":"62", "libelle":"La Poste Mobile",
						  "libelle_long":"La Poste Mobile", "type":"link",
						  "cible":"tous", "ordre":"99", "obligatoire":"0",
						  "pictoURL":"http://www.laposte.fr/widgetcourrier/media/pictosServices/bn2gnryb_01.png",
						  "linkURL":"http://m.laposte.fr",
						  "date2debut":"2010-04-19", "date2fin":"2069-03-24",
						  "description":"Retrouvez tous les services de La Poste sur votre mobile"});
						 
						  data.push({"docID":"52",
								"libelle":"Calculez le tarif...",
								"libelle_long":"Calculez le tarif de vos envois",
								"type":"outil",
								"cible":"tous",
								"ordre":"3",
								"obligatoire":"1",
								"pictoURL":"http://www.laposte.fr/widgetcourrier/media/pictosServices/ghvrrz2y_01.png",
								"linkURL":"tarifs",
								"date2debut":"2010-04-19",
								"date2fin":"2069-03-24",
								"description":"Calculez le tarif de vos envois depuis la France"});
						var ordre = new Array();
						ordre[56] = 1; // suivi des envois
						ordre[52] = 2; // calculez le tarif de vos
						// envois
						ordre[51] = 3; // localisez un bureau de
						// Poste;
						ordre[54] = 4; // trouvez un code postal
						ordre[62] = 5; // m.laposte.fr

						ordre[6] = 6; // la boutique du timbre

						ordre[44] = 7; // Cartes postales
						ordre[41] = 8; // Mon timbre à moi
						ordre[17] = 9; // La poste.net
						ordre[58] = 10; // Colissimo
						ordre[59] = 11; // Chronopost

						ordre[9] = 99; // Lettre
						// Recommandée
						ordre[10] = 99; // Lettre
						// Prioritaire
						// en
						// Ligne
						ordre[11] = 99; // Réexpédition
						// -
						// Garde
						ordre[18] = 99; // monBureauDePoste
						ordre[19] = 99; // Tarifs
						// Courrier
						ordre[55] = 99; // Conditions
						// postales
						// à
						// l'international
						ordre[60] = 99; // MonTimbreEnLigne
						ordre[4] = 99; // Mon Courrier
						ordre[12] = 99; // Pack MNA

						var obl = new Array();

						obl[56] = 1; // suivi des envois
						obl[52] = 1; // calculez le tarif de vos
						// envois
						obl[51] = 1; // localisez un bureau de
						// Poste;
						obl[54] = 1; // trouvez un code postal
						obl[62] = 0; // m.laposte.fr

						obl[6] = 0; // la boutique du timbre

						obl[44] = 0; // Cartes postales
						obl[41] = 0; // Mon timbre à moi
						obl[17] = 0; // La poste.net
						obl[58] = 0; // Colissimo
						obl[59] = 0; // Chronopost

						obl[9] = 0; // Lettre
						// Recommandée
						obl[10] = 0; // Lettre
						// Prioritaire
						// en
						// Ligne
						obl[11] = 0; // Réexpédition
						// -
						// Garde
						obl[18] = 0; // monBureauDePoste
						obl[19] = 0; // Tarifs
						// Courrier
						obl[55] = 0; // Conditions
						// postales
						// à
						// l'international
						obl[60] = 0; // MonTimbreEnLigne
						obl[4] = 0; // Mon Courrier
						obl[12] = 0; // Pack MNA
						var data2 = new Array(data.length);
						// on trie les objects selon le nouvel ordre
						$.each(data, function() {
							this.ordre = ordre[this.docID];
							this.obligatoire = obl[this.docID];
							if (this.ordre < 99) {
								data2[this.ordre - 1] = this;

							} else {
								data2.pop();
							}

							;
						});

						$.each(data, function() {
							if (this.ordre >= 99) {
								data2.push(this);
							}
							;
						});
						data = data2;

						// premi�re lecture cookie
						$.each(data,
								function() {
									if (dejaChoisiArray.length > 0) {
										if (this.cible == "pub"
												|| this.cible == "tous") {

											if ($.inArray(this.docID,
													dejaChoisiArray) != -1) {
												dataPanneauObg.push(this)
											} else {
												dataPanneauSec.push(this)
											}

										}
									}
								});
						if (dataPanneauObg.length == 0) {
							$.each(data,
									function() {

										if (this.cible == "pub"
												|| this.cible == "tous") {

											if (this.ordre < 99) {
												dataPanneauObg.push(this);
											} else {

												dataPanneauSec.push(this);
											}
										}
									});
						}
						// on check si ya des service obligatoire dans les
						// services restant
						var dataPanneauAux = new Array();
						dataPanneauAux = dataPanneauSec;
						dataPanneauSec = new Array();
						// console.log(dataPanneauAux);
						if (dataPanneauAux.length > 0) {
							for (i = 0; i < dataPanneauAux.length; i++) {
								// on construit la liste des services
								// obliatoires
								if (dataPanneauAux[i].obligatoire == 1) {
									if (dataPanneauObg.length < MAX_SERVICES) {
										dataPanneauObg.push(dataPanneauAux[i]);
										// on le retire de la liste
									} else {
										dataPanneauSec.push(dataPanneauAux[i]);
									}
								} else {
									dataPanneauSec.push(dataPanneauAux[i]);
								}
							}
						}
						// on ne peux afficher que les MAX_SERVICES premiers
						var t1 = new Array();
						var t2 = new Array();

						if (dataPanneauObg.length > MAX_SERVICES) {
							for (i = 0; i < dataPanneauObg.length; i++) {
								// on ne garde que les premiers
								if (i < MAX_SERVICES) {
									t1.push(dataPanneauObg[i]);
								} else {
									t2.push(dataPanneauObg[i]);
								}
							}
							dataPanneauObg = t1;
							// on reconstitue la liste des autres services
							for (i = 0; i < dataPanneauSec.length; i++) {
								t2.push(dataPanneauSec[i]);
							}
							dataPanneauSec = t2;

						}
						buildServicesBloc(dataPanneauObg, true, 'service_obg');
						buildServicesBloc(dataPanneauSec, false, 'service_add');
						$('.spanTooltip').click(function() {
							return false;
						})
						/*
						 * $('.spanTooltip').tooltip({ track: false, delay: 0,
						 * showURL: false, showBody: " - ", fade: 250, left:50
						 * });
						 */
						$(".spanTooltip").each(
								function(i) {
									$(this).simpletip(
											{
												persistent : true,
												content : '<h3>'
														+ $(this).attr('title')
																.split('$')[0]
														+ '</h3><p>'
														+ $(this).attr('title')
																.split('$')[1]
														+ '</p>'
											});
								});

						onServicesLoaded();

					});

}

function onServicesLoaded() {

	// on services loaded
	// load services
	// suivi
	if ($('#serv_56').length !== 0) {
		load56();
	}
	// bureau
	if ($('#serv_51').length !== 0) {
		load51();
	}
	// tarif
	if ($('#serv_52').length !== 0) {
		load52();
	}
	// code postal
	if ($('#serv_54').length !== 0) {
		load54();
	}

	if ($('#service_obg li .ser_obg:not(.obligatoire)').length == 0) {
		$('#Supprimer').css('display', 'none');
	} else {
		$('#Supprimer').css('display', 'block');
	}
}
function buildServicesBloc(arraySer, Obg, idULaCreer) {

	if (Obg) {
		$('#zone_centrale #homeWi .contenu #conteSerz').append(
				'<ul id="' + idULaCreer + '" class="service"></ul>');
	} else {
		$('#zone_add  .contenu #containerServices').append(
				'<ul id="' + idULaCreer + '" class="service"></ul>');
		$('#zone_add  .contenu #containerServices').append(
				'<div class="spacer"></div>');

	}
	for (i = 0; i < arraySer.length; i++) {
		/* recuperation des infos */
		var idServ = "serv_" + this.docID;
		var titre = this.libelle;
		var libelle_long = this.libelle_long;
		var imgSrc = this.pictoURL;
		var date2Debut = this.date2debut;
		var date2Fin = this.date2fin;
		var type = this.type;

		var Src = this.linkURL;
		var texte = this.description;
		var obligatoire = arraySer[i].obligatoire;
		var classSup = '';
		if (obligatoire == 1)
			classSup = 'obligatoire';

		// console.log(arraySer[i]);
		if (arraySer[i].type == "outil") {
			$('#' + idULaCreer)
					.append(
							'<li><a id="serv_'
									+ arraySer[i].docID
									+ '" class="ser_obg outils '
									+ classSup
									+ '" href="#"><span class="editPicto"></span><img src="'
									+ arraySer[i].pictoURL
									+ '" alt="" title="" /></a><a href="#"  title="'
									+ arraySer[i].libelle_long + ' $ '
									+ arraySer[i].description
									+ '"  class="spanTooltip">'
									+ arraySer[i].libelle + '</a></li>');
		} else {
			$('#' + idULaCreer)
					.append(
							'<li><a target="_blank" id="serv_'
									+ arraySer[i].docID
									+ '" class="ser_obg '
									+ classSup
									+ '" href="'
									+ arraySer[i].linkURL
									+ '" ><span class="editPicto"></span><span class="raccourci"><img src="img/raccourci.gif" alt="x" title="x"  /></span><img src="'
									+ arraySer[i].pictoURL
									+ '" alt="" title="" /></a><a href="#" title="'
									+ arraySer[i].libelle_long + ' $ '
									+ arraySer[i].description
									+ '"  class="spanTooltip">'
									+ arraySer[i].libelle + '</a></li>');
		}
	}
}
function hideAll() {
	$('#msg1').fadeOut(150);
	$('#msg2').fadeOut(150);
	$('#msg3').fadeOut(150);
	$('#infoAddTrouverCP').css('display', 'none');
	$('.service a').removeClass('edit'); // desactivation du mode edit
	/* fermeture de la zone du bas */
	$(idZoneAdd).slideUp("fast");
	$('#ajouterSupprimer img').attr('src', 'img/skin1/plus.png');

	/* fermeture de la zone du haut */
	$(idZoneActus).slideUp("normal");
	$('#newActus img').attr('src', 'img/skin1/fleche_droit.png');
}
function setStat(actionId) {
	var module = "iphone";
	var action = actionId;
	var user = "";
	$.getJSON(
			"http://www.laposte.fr/widgetcourrier/php/wtpeStatsJSON.php?module="
					+ module + "&action=" + action + "&callback=?", function(
					data) {
				user = data.userIP;
			});
}
function editMode() {
	if (!$(this).hasClass('obligatoire')) {
		if ($(this).hasClass('edit')) {
			// console.log( $(this).parent().get(0));
			var par = $(this).parent().get(0);
			var leUL = $(this).parent().parent().get(0);
			var idStat = this.id.substr(5, this.id.length);
			if (leUL.id == 'service_obg') {
				$(par).appendTo('#service_add');
				saveDejaChoisi();
				// testNbElt();
				setStat("suppression du service : " + idStat);
			}

			else {
				if ($('#service_obg li').length < MAX_SERVICES) {
					$(par).appendTo('#service_obg');
					saveDejaChoisi();
					// testNbElt();
					setStat("ajout du service : " + idStat);
				} else {

					$('#msg2').fadeIn(150);
					$('#msg2').click(function() {
						$('#msg2').fadeOut(150);
					});
					$(window).click(function() {
						$('#msg2').fadeOut(150);
					});
				}

			}

			return false;
		}
	}
}
function switchAff(eltAafficher, eltAmasquer) {
	$('#ajouterSupprimer').css('display', 'block');
	$('#plusAide').css('display', 'block');
	hideAll();
	$(eltAafficher).css('display', 'block');
	$(eltAmasquer).css('display', 'none');

}
function restBack() {
	/*
	 * $('#viewportHeight').attr('content','height=363');
	 * $('#widgetTabee').css('height','363px');
	 * $('#zone_add').css('height','363px');
	 */
	$('#widgetTabee').css('height', 'auto');
	$('#widgetTabee').css('max-height', 'auto');
	$('#widgetTabee').scrollTo('#zone_centrale', 400);
	$('a.edit').unbind('click', editMode);
	$('#widgetTabee').removeClass('edit');
	$('.service a').removeClass('edit');
	if ($('#service_obg li .ser_obg:not(.obligatoire)').length == 0) // SLE
	{
		$('#Supprimer').css('display', 'none');
	} else {
		$('#Supprimer').css('display', 'block');
	}
	$('#Supprimer span').text('Supprimer');
	$('#Supprimer img').attr('src', 'img/skin1/moins.png');
	$('#ajouterSupprimer').css('display', 'block');
	$('#plusAide').css('display', 'block');
}
function setFooter() {

}
function afficheErreur(event) {
	$('#msg3').fadeIn(150);
	$('#msg3').click(function() {
		$('#msg3').fadeOut(150);
	});
	$(window).click(function() {
		$('#msg3').fadeOut(150);
	});
	return false; // bloque les href ?
}


$(function() {
	// So instead, readjust fixed positions
	/*
	 * $(window).scroll(function ()
	 * {$('.bottom').css({'bottom':document.body.clientHeight-window.pageYOffset-window.innerHeight,'display':'none'})});
	 * window.onscroll= function () {$('.bottom').css('display','block')};
	 */
	// $('#aide .contenu').jScrollPane({showArrows:true,scrollbarWidth:32});
	setStat("ouverture du widget");
	getDejaLu();
	// alert(dejaLu.length);
	getDejaChoisi(); // recuperation des deja choisi
	buildActus();
	buildServices();

	// saveDejaChoisi();
	/** gestion des evenement ouverture et fermeture * */
	$('#close_actus').click(function() {
		restBack();
	});
	$('#retourI').click(function(e) {
		e.preventDefault();
		restBack();
	})

	/* aide */
	$('#plusAide').click(function() {
		switchAff('#aide', '#homeWi');
		return false;
	})
	$('#RetourAide').click(function() {
		switchAff('#homeWi', '#aide');
		return false;
	});
	/* ajout supression */
	$('#ajouterSupprimer').click(function() {
		/*
		 * $('#blockService').css('display','block');
		 * $('#service_add').css('display','none');
		 * $('#nextService').css('display','none');
		 */
		$('#zone_add').css('display', 'block');
		$('#zone_actus').css('display', 'none');
	
		// SLE : cleaning ?
			$('a.edit').unbind('click', editMode);
			$('#widgetTabee').removeClass('edit');
			$('.service a').removeClass('edit');

			// SLE : apparition des (-) après le scroll
			$('#widgetTabee').scrollTo('#zone_add', {
				duration : 400,
				onAfter : function() {
					$('.service a:not(.obligatoire)').addClass('edit'); // sle
				// pas
				// les
				// obligatoire
				$('#widgetTabee').addClass('edit');
				$('a.edit:not(.spanTooltip)').bind('click', editMode);
				$('#widgetTabee').css('height', $('#zone_add').height() + 13);

			}
			});



	})
	$('#Supprimer')
			.click(function() {
				// si on click sur "retour"
					if ($('#widgetTabee').hasClass('edit')) {
						$('.service a.obligatoire ').unbind('click',
								afficheErreur);

						$('a.edit').unbind('click', editMode);
						$('#widgetTabee').removeClass('edit');
						$('.service a').removeClass('edit')

						// SLE on affiche supprimer s'il reste un service non
						// obligatoire
						if ($('#service_obg li .ser_obg:not(.obligatoire)').length == 0) {
							$('#Supprimer').css('display', 'none');
						} else {
							$('#Supprimer').css('display', 'block');
						}
						$('#Supprimer span').text('Supprimer');
						$('#Supprimer img').attr('src', 'img/skin1/moins.png');
						$('#ajouterSupprimer').css('display', 'block');
						$('#plusAide').css('display', 'block');
					} else {
						$('#widgetTabee').addClass('edit');
						$('.service a').addClass('edit');
						$('a.edit:not(.spanTooltip)').bind('click', editMode);

						// on affiche l'interdiction de suppression des
						// obligatoires
						$('.service a.obligatoire ').bind('click',
								afficheErreur);

						$('#Supprimer span').text('Retour à vos favoris');
						$('#Supprimer img').attr('src', 'img/skin1/retour.png');
						$('#Supprimer').css('display', 'block'); // SLE pb
						// avec le
						// style
						// CSS3
						$('#ajouterSupprimer').css('display', 'none');
						$('#plusAide').css('display', 'none');
					}
				});


});
$(document).ready(function() {
	$('#loader').remove();
	$('#conteSerz').css('display', 'block');
	// Astuce iphone
		setTimeout(function() {
			$(window).scrollTo(0, 1)
		}, 100);
		$('#newActus').click(function() {
			$('#zone_add').css('display', 'none');
			$('#zone_actus').css('display', 'block');
			$('#widgetTabee').scrollTo('#zone_actus', 400);
			$('#pasActus').css('display', 'block');
		$('#carousel_actus').css('display', 'block'); // on remet
				// l'actu en
				// visible
	
			if (nbActusPresente > 1) {
				if (!$('#carousel_actus').hasClass('.jcarousel-list')) {
					$('#carousel_actus').jcarousel( {
						scroll : 1,
						visible : 1,
						itemVisibleInCallback : getVisible
					});
					$(".jcarousel-prev").css('display', 'block');
					$(".jcarousel-next ").css('display', 'block');
				}
				/*
				 * $('#zone_actus .jcarousel-next').css('display', 'none');
				 * $('#zone_actus .jcarousel-prev').css('display', 'none');
				 */
			} else {
				if ($('#carousel_actus li:eq(0)').length > 0) {
					$('#carousel_actus li h3').css('padding', '0');
					saveDejaLu($('#carousel_actus li:eq(0)').attr('id'))
				}
			}
			// alert('out');
				return false;
			});
		// wtpeStatsJSON.php
		// alert('nbActus '+nbActus);
		$('a.ser_obg:not(.outils)').click(function() {
			var idStat = this.id.substr(5, this.id.length);
			setStat("clic sur le lien : " + idStat)

		});
		$('a.outils:(.edit)').click(function() {
			var idStat = this.id.substr(5, this.id.length);
			setStat("Click sur le service : " + idStat)
		});
		$('.retourHome').click(function() {
			hideAll();
			$('.ecran').css('display', 'none');
			$('#homeWi').css('display', 'block');
			return false;
		})
		/** carousel des service additionnel by salah * */
		$('#nextService').click(function() {
			currentLigne++;
			if (currentLigne > nbLigne) {
				currentLigne = 0;
				currentTop = 0;
			}
			currentTop = currentTop - (currentLigne * hauteurLi);
			//alert("currentTopNext   "+currentTop)

				$("#service_add").animate( {
					top : currentTop
				}, 500);
				if (currentLigne == nbLigne) {
					$(this).css('display', 'none');
					$('#prevService').css('display', 'block');
				}
				return false;

			})
		$('#prevService').click(function() {
			currentLigne--;
			currentLigne--;
			currentTop = currentTop + (currentLigne * hauteurLi)
			//alert("currentTopPrev   "+currentTop)
				if (currentLigne < 0) {
					currentLigne = 0;
					currentTop = 0;
					$(this).css('display', 'none');
					$('#nextService').css('display', 'none');
				}
				$("#service_add").animate( {
					top : currentTop
				}, 500);
				if (currentLigne == 0) {
					$(this).css('display', 'none');
					$('#nextService').css('display', 'block');
				}
				//$('#nextService').css('display','block');
				return false;
			})

		/*$('#zone_actus').css({'display':'none','position':'static','visibility':'visible'});*/

	});

// nettoyage d'un formulaire
$.fn.clearForm = function() {
	return this.each(function() {
		var type = this.type, tag = this.tagName.toLowerCase();
		if (tag == 'form')
			return $(':input', this).clearForm();
		if (type == 'text' || type == 'password' || tag == 'textarea')
			this.value = '';
		else if (type == 'checkbox' || type == 'radio')
			this.checked = false;
		else if (tag == 'select')
			this.selectedIndex = -1;
	});
};

