/*function ajuste_taille(){
	var max_width = 730;
	if (document.body.clientWidth > max_width) {
		document.getElementById('conteneur').style.width = max_width + 'px';
	} else {
		document.getElementById('conteneur').style.width = (document.body.clientWidth -30) + 'px';
	}
	document.getElementById('conteneur').style.display = 'block';
}*/
var DEBUG = false;


$(document).ready(function(){

	$('.fancybox').fancybox({cyclic: true});
	if ($('#diaporama').length > 0) {
		var current_image	= 0;
		var nb_images		= $('#diaporama li').length;
		// reculer
		$('#nav-diapo a:eq(0)').bind('click', function (){
			if (current_image > 0) {
				current_image--;
				$('#diaporama li').hide();
				$('#diaporama li:eq('+current_image+')').show();
				if (current_image == 0) {
					$('#nav-diapo a:eq(0)').addClass('disabled');
				}
				if (nb_images > 1) {
					$('#nav-diapo a:eq(1)').removeClass('disabled');
				}
			}
		});

		// avancer
		$('#nav-diapo a:eq(1)').bind('click', function (){
			if (current_image < (nb_images - 1)) {
				current_image++;
				$('#diaporama li').hide();
				$('#diaporama li:eq('+current_image+')').show();
			}
			if (current_image == (nb_images - 1)) {
					$('#nav-diapo a:eq(1)').addClass('disabled');
			}
			if (nb_images > 1) {
				$('#nav-diapo a:eq(0)').removeClass('disabled');
			}
		});

		if ($('#album').length > 0) {
			$('.lien-mignature').bind('click', function(){
				//alert(current_image);
				idx_match = $(this).attr('id').match(/mign\-([0-9]+)/);
				current_image	= idx_match[1];
				if (current_image == 0) {
					$('#nav-diapo a:eq(0)').addClass('disabled');
					$('#nav-diapo a:eq(1)').removeClass('disabled');
				} else {
					if (current_image == (nb_images - 1)) {
						$('#nav-diapo a:eq(1)').addClass('disabled');
					} else {
						$('#nav-diapo a:eq(1)').removeClass('disabled');
					}
					$('#nav-diapo a:eq(0)').removeClass('disabled');
				}
				$('#diaporama li').hide();
				$('#diaporama li:eq(' + idx_match[1] + ')').show();
				return false;
			});
			$('#album').show();
		}

		// enchaînement des albums
		$('.album-photos:eq(0)').addClass('album-photos-actif');
		var nb_albums = $('.album-photos').length;
		if (nb_albums > 1) {
			//alert(nb_albums);
			var idx_album = 0;

			$('.album-precedent').addClass('disabled');
			$('.album-precedent').bind('click', function(){
				if (idx_album > 0) {
					idx_album--;
					$('.album-photos-actif').removeClass('album-photos-actif');
					$('.album-photos:eq(' + idx_album + ')').addClass('album-photos-actif');
					$('.album-suivant').removeClass('disabled');
					if (idx_album == 0) {
						$('.album-precedent').addClass('disabled');
					}
				}
			});

			$('.album-suivant').bind('click', function(){
				if (idx_album < (nb_albums - 1)) {
					idx_album++;
					$('.album-photos-actif').removeClass('album-photos-actif');
					$('.album-photos:eq(' + idx_album + ')').addClass('album-photos-actif');
					$('.album-precedent').removeClass('disabled');
					if (idx_album == (nb_albums - 1)) {
						$('.album-suivant').addClass('disabled');
					}
				}
			});
		} else {
			$('.nav-albums').hide();
		}
	}

	$('a').bind('focus', function(){
		this.blur();
	});

	switch($('body').attr('id')){
	case 'index' :
		$(".scrollable").scrollable({circular: true, easing: 'linear'}).autoscroll({ autoplay: true, interval: 1500});;

	break;
	case 'rubrique-2' :


	break;
	case 'rubrique-6' :
		/* formulaire de contact
		masque le formulaire si présence prévisu */
		if ($('.previsu').length) {
			$('form fieldset:eq(1)').hide();
		}

		/* confirmation envoi */
		if ($('.reponse_formulaire').length){
			$('.reponse_formulaire p').html('Votre message a bien &eacute;t&eacute; envoy&eacute;');
		}
	break;
	}
});

function insere_infobulles(html_elements){
	nb = typeof(html_elements) == 'object' ? html_elements.length : 0;
	for (i = 0; i < nb; i++) {
		if (html_elements[i].getAttribute('title')) {

			html_elements[i].insertBefore(document.createElement('span'), html_elements[i].firstChild);
			html_elements[i].firstChild.setAttribute('className', 'cartouche-def');
			html_elements[i].firstChild.setAttribute('class', 'cartouche-def');

			html_elements[i].firstChild.appendChild(document.createElement('span'));
			html_elements[i].firstChild.appendChild(document.createElement('span'));

			html_elements[i].firstChild.getElementsByTagName('span')[0].setAttribute('className', 'cartouche-def-haut');
			html_elements[i].firstChild.getElementsByTagName('span')[0].setAttribute('class', 'cartouche-def-haut');
			html_elements[i].firstChild.getElementsByTagName('span')[1].setAttribute('className', 'cartouche-def-int');
			html_elements[i].firstChild.getElementsByTagName('span')[1].setAttribute('class', 'cartouche-def-int');
			html_elements[i].firstChild.getElementsByTagName('span')[1].appendChild(document.createTextNode(html_elements[i].getAttribute('title')));
			html_elements[i].setAttribute('title', '');

			html_elements[i].onmouseover = function () {
				this.firstChild.style.display = 'block';
				//alert(this.nodeName + this.getAttribute('title') + this.firstChild.nodeName);
			}

			html_elements[i].onmouseout = function () {
				this.firstChild.style.display = 'none';
				//alert(this.getAttribute('title'));
			}
		}
	}
}


