/* nécessite jquery */

$(document).ready(function() {
	liaisonlogos();
	btnhautbaspage();
});

$(window).resize(function() {
	btnhautbaspage();
});

function liaisonlogos() {
	$(".logo").each(function() {
		var obj = $(" a", this);
		if (obj[0].title) $("> img", this).attr({"title" : obj[0].title}); // scb
		$(" img", this)
			.wrap(("<a href=\"" + obj[0].href + "\"><\/a>"))
			.hover(function() {
				obj.addClass("lien");
			}, function() {
				obj.removeClass("lien");
			});
	});
}

function btnhautbaspage() {
  var btn = $("#hautpage");
  if (btn == null) return;
	var h = hauteur_ecran();
	var pos = calculOffset("baspage");
	var etat = (pos[1] + 15 > h) ? "visible" : "hidden";
	btn.css({"visibility":etat});
	$("#baspage").css({"visibility":etat});
}

function largeur_ecran() {
	var d = document.documentElement;
	var w = window.innerWidth || self.innerWidth || (d&&d.clientWidth) || document.body.clientWidth;
	return w;
}

function hauteur_ecran() {
	var d = document.documentElement;
	var h = window.innerHeight || self.innerHeight || (d&&d.clientHeight) || document.body.clientHeight;
	return h;
}

function calculOffset(nom) {
	var x = 0, y = 0, obj = $("#"+nom)[0];
	while (obj) {
		x += obj.offsetLeft;
		y += obj.offsetTop;
		obj = obj.offsetParent;
	}
	return [x, y];
}

function choixsection(n) {
	var s = 'index.php'
	switch (n) {
		case 1: s += '?section=alpin'; break;
		case 2: s += '?section=loisir'; break;
		case 3: s += '?section=nordic'; break;
		case 4: s += '?section=surf'; break;
		case 5: s += '?section=free'; break;
		case 6: s += '?section=cross'; break;
	}
	location = s;
}

function chgpg(p) { f.pg.value = p; f.submit(); }

function chgpage(p) { f.nupage.value = p; envoi(); }

function paginer(n, m) {
	var x = f.pagine.value;
	if (x == 1 && n > m) {
		var s = "Nombre total de lignes : " + n + "\nConfirmation de l'affichage sur une seule page ?";
		if (!confirm(s)) return;
	}
	f.pagine.value = 1 - x;
	envoi();
}

function envoi() { z(0, 0);}

function a(x) { f.op.value = x; f.submit(); }

function z(n, x) { f.num.value = n; a(x); }

function msg(c, s) { alert('La saisie ' + s + ' est obligatoire.'); c.focus(); }

function nonvide(c, s) {var ok = (c.value != ''); if (!ok) msg(c, s); return ok; }

function trim(s) {
	if (window.RegExp) s = s.replace(/^\s+/, "").replace(/\s+$/, "");
	return s;
}

function maj(s) {
	var ch = "", c;
	s = trim(s).toLowerCase()
	for (var i = 0; i < s.length; i++) {
		c = s.charAt(i);
		switch(c) {
			case "à" : c ="a"; break;
			case "â" : c ="a"; break;
			case "ç" : c ="c"; break;
			case "é" : c ="e"; break;
			case "è" : c ="e"; break;
			case "ê" : c ="e"; break;
			case "ë" : c ="e"; break;
			case "ï" : c ="i"; break;
			case "î" : c ="i"; break;
			case "ô" : c ="o"; break;
			case "ù" : c ="u"; break;
			case "û" : c ="u";
			}
		if (( c >= "a" && c <= "z") || c == "-" || c == "'" || c == " " || c == ".") ch += c;
		}
	return ch.toUpperCase();
	}

function np(s) {
	var ch = "", c = "", m = true;
	s = trim(s).toLowerCase();
	for (var i = 0; i < s.length; i++) {
		c = s.charAt(i);
		if (m) c = maj(c);
		if (c == " ") c = "-";
		m = (c == "-");
		ch += c;
		}
	return ch;
	}

function finpara(s) {
	if (window.RegExp) s = s.replace(/¶/g, "\n");
	return s;
}

function cfinpara(s) {
	if (window.RegExp) s = s.replace(/(\r|\n){1,2}/g, "¶");
	return s;
}

function EstNum(c) {
	var s = c.value;
	if (s == "") return false;
	if (!window.RegExp) return true;
	var ok = /^[0-9]+$/.test(s);
	if (!ok) {alert("Erreur de saisie"); c.select(); c.focus();}
	return ok;
}

function adr(n, d, s) {
	if (window.RegExp) s = s.replace(/ /g, "%20");
	if (s != "") s = "?subject=" + s;
	var c = n + "&#" + eval(33 + 31) + ";" + d;
	c = "<a href='mai" + "lto" + ":" + c + s +"'>" + c + "</a>";
	return c;
	}

function verifemail(champ) {
	var s = champ.value;
	if (s == "") return  true;
	var ok = true;
	if (window.RegExp) {
		var c = "^[a-zA-Z0-9_\.\-]+@([a-zA-Z0-9\-]+[\.])+[a-zA-Z0-9]{2,4}$";
		var r = new RegExp(c)
		if (!r.test(s)) ok = false;
	}
	if (!ok) {alert("Adresse e-mail invalide."); champ.focus(); champ.select();}
	return ok;
}

function lien(id, e) {
	$("#" + id)[0].className = (e == 1) ? "lien" : "";
}
