$(function() {
	$("a[rel='contenu_ppbox[galerie]']").prettyPhoto();
	$('a.jsPPhoto').prettyPhoto(); // ! bug si on merge les sélécteurs
	$('table.tableau_zebre').zebre('tr', 'zebre_vert'); //@#$ IE

	$('textarea.jsAutoGrow').autogrow();
	$('textarea.jsCountCar').countCar();

	// GEOLOC V3
	if( $('div.jsGeolocalisation').length) {
		$insertScript( URL_CHARTE_COMMUN + 'composant/geolocalisation/geolocalisation_f.js');
		if( typeof window.google !== 'undefined' && window.google.maps) {
			return geolocv3_init();
		} else {
			$insertScript( 'http://maps.google.com/maps/api/js?sensor=false&language=' + LANGUE_META_SITE + '&callback=geolocv3_init');
		}
	}

	// Vote
	vote();
	vote_ouinon();

	// Commentaire / pagination
	$('div.commentaires').each( function() {
		var jqT = $(this),
			iComVisible = jqT.data('visible');

		if( iComVisible > 0) {
			var sComAncre = window.location.hash, jqLastVisible;

			if( /^#com\d+$/.test(sComAncre) && $(sComAncre, this).length && jqT.find('li').index( $(sComAncre)) + 1 >= iComVisible) {
				// Si ancre vers com hors zone visible
				$(sComAncre).nextAll().hide();
				jqLastVisible = $(sComAncre);
			} else {
				jqT.find('li:gt(' + ( iComVisible - 1) + ')').hide();
				jqLastVisible = jqT.find('li:eq(' + ( iComVisible - 1)  + ')');
			}
			if( jqLastVisible.next().length) {
				var jqBtn = $('<input type="button" class="commmentaire_btn_suite" value="' + _lg('Lire la suite') + '" />').click( function() {
					var i = 0;

					while( jqLastVisible.next().length && i < iComVisible) {
						jqLastVisible = jqLastVisible.next().fadeIn();
						i++;
					}

					! jqLastVisible.next().length && $(this).hide();
				});

				jqT.append( jqBtn);
				jqBtn.wrap('<div class="center"></div>');
			}
		}
	});

	$().UItoTop();
});

/*** COMMENTAIRE ***/
$(document).on('click', 'a.commentaire_repondre, a.jsCommentaireSup', function() {
	var jqT = $(this);

	if( jqT.hasClass('commentaire_repondre')) {
		 // on deplace le form si besoin
		if( ! jqT.closest('li').find('form').length) {
			jqT.closest('li').append( $( this.href.replace(/.*?_(.*_\d+)$/, '#postercom$1')));
			$( this.href.replace(/.*?_(.*_\d+)$/, '#commentaire_contenu_$1')).val( '@' + jqT.attr('title') + ' : ').focus();
		}
	} else if( jqT.hasClass('jsCommentaireSup')) {
		var oCont = jqT.closest('li');
		oCont.empty().contenuAjax({
			url: this.href + '_ajax',
			place: null,
			loadclass: 'ajaxloading_medium',
			error_affiche: false,
			success_callback: function(data) {
				if( data == 'erreur') {
					alert( constante('ajaxError'));
				} else {
					oCont.remove();
				}
			}
		});
	}
	return false;
});
function commentaire_submit( form, bIdent) {
	$.ajax({
		url: $(form).attr('action') + '_ajax',
		type: 'post',
		data: $(form).serialize(),
		dataType: 'xml',
		error: function( oXhr, request, settings) {
			$('#' + form.id + '-rapport').html( d_box_alert( { id: null, text: constante( oXhr.status || 'ajaxError')}));
		},
		success: function(xml) {
			if( $(xml).find('Commentaire').length) {
				var sIdBloc = form.id.replace('frmcom', '#com');
				$(xml).find('Commentaire > com').each( function() {
					if( ! $(sIdBloc + ' ul').length) {
						$(sIdBloc).append('<h3>' + _lg('Commentaires') + '</h3><ul></ul>');
					}
					var sOrdre = $(sIdBloc).data('ordre').toUpperCase();

					$(sIdBloc + ' > ul')[ sOrdre == 'DESC' ? 'prepend' : 'append']( $(this).text());
					( sOrdre == 'DESC') && $.scrollTo( $(sIdBloc));
				}).end().find('form').each( function() {
					$(sIdBloc.replace('#com', '#postercom')).replaceWith( $(this).text());
				});

				$(sIdBloc).after( $(sIdBloc.replace('#com', '#postercom')));
			} else {
				$('#' + form.id + '-rapport').html( d_box_alert( { id: null, text: constante('ajaxError')}));
			}
		},
		complete: function() {
			disableForm.hide();
		}
	});
}

/*** FAVORIS ***/
$(document).on('click', 'a.jsFavori', function() {
	var oLien = this, oConteneur = $(this).parent();

	oConteneur.empty().css({'padding-left':24}).contenuAjax({
		url: oLien.href + '_ajax',
		place: null,
		error_affiche: false,
		success_callback: function( data) {
			if( data == 'erreur') {
				alert( constante('ajaxError'));
			} else {
				$('#favori_encart').html( data);
				oConteneur.css({'padding-left':''}).text( _lg('En favoris'));
			}
		}
	});
	return false;
});
function ajax_favori_supprimer( oLien) {
	$('#favori_encart').empty().contenuAjax({
		url: oLien.href + '_ajax',
		loadclass: 'ajaxloading_medium',
		error_affiche: false
	});
}

/*** VOTE ***/
$(document).on('click', 'a.jsVoteRsSubmit', function() {
	$(this).parent('div').empty().width(32).contenuAjax({
		url: $(this).attr('href') + '_ajax',
		place: 'replaceWith',
		loadclass: 'ajaxloading_small'
	});
	return false;
}).on('click', '.jsVoteBooleen label', function() { // oui-non, aime-j'aime pas
	var jqT = $(this),
		jqForm = jqT.parent('div').parent('form'),
		sData;

	$id( jqT.attr('for')).checked = true;
	sData = jqForm.serialize();

	jqT.closest('form').parent('div').empty().width(32).contenuAjax({
		url: jqForm.attr('action') + '_ajax',
		type: 'post',
		data: sData,
		place: 'replaceWith'
	});
	return false;
});

function vote_ouinon() {
	$('.jsVoteBooleen').find('input:radio, input:submit').hide();
}
function vote( sSelecteur) { // util si ajax par ex pour éviter de dupliquer les events : sSelecteur = #id
	sSelecteur = $defined( sSelecteur) ? sSelecteur + ' ' : '';
	if( $(sSelecteur + 'form input:radio.star').length) {
		$(sSelecteur + 'form').find('input:radio.star').rating({
			focus: function(val, link) {
				if( ! this.disabled) {
					var tip = $('#help_' + this.id.replace(/(_\d+)$/, ''));
					tip[0].data = tip[0].data || tip.html();
					tip.html(link.title || val);
				}
			},
			blur: function(val, link) {
				if( ! this.disabled) {
					var tip = $('#help_' + this.id.replace(/(_\d+)$/, ''));
					tip.html(tip[0].data || '');
				}
			},
			required: true,
			callback: function(val, link) {
				var jqForm = $(this.form),
					sData = jqForm.serialize();

				$(this).rating('disable');
				$.ajax({
					url: jqForm.attr('action') + '_ajax',
					type: 'post',
					data: sData,
					timeout: 10000,
					error: function( oXhr, request, settings) { alert( constante( oXhr.status || 'ajaxError'));},
					beforeSend: function() {
						$(jqForm).find('span.vote_help').empty().addClass('ajaxloading_small ajax_star_rating');
					},
					success: function( data) {
						jqForm.replaceWith( data);
						vote_resultat();
					},
					complete: function() {
						$(jqForm).find('span.vote_help').removeClass('ajaxloading_small ajax_star_rating');
					}
				});
			}
		}).parent().find('label, input:submit').hide();
	}
	vote_resultat( sSelecteur);
}
function vote_resultat( sSelecteur) {
	sSelecteur = $defined( sSelecteur) ? sSelecteur + ' ' : '';
	$(sSelecteur + 'div.vote_rating').each( function() {
		if( ! $.data(this, 'rating')) {
			$(this).hover( function() {
				$(this).children('span.vote_help:first').hide().next('span.vote_help').show();
			}, function() {
				$(this).children('span.vote_help:first').show().next('span.vote_help').hide();
			}).data('rating', true);
		}
	});
}
