/**
 * Définition des fonctions diverses
 * 
 * 
 */
var offset = 0;
var speed = 0, default_speed = 400;
var labels = new Array();
var play = false;
var delayTime = 6 * 10;
var timebarWidth = 0;

/**
 * Initialisation du défilement des photos
 * 
 * @name	initSlideshow
 * @author	Rémy Vuong <remy@upian.com>
 * @date	2009-03-17
 * @version	1.0
 * @return	bool false
 */
function initSlideshow() {
	// Play/pause
	speed = default_speed;
	
	// -------------------------------
	// Gestion des hover sur contrôles
	// -------------------------------
	// -> image précédente (thin)
	$('.slideshow-control-prev a').hover(
		function(){ $(this).find('img').attr('src', base_url+'/theme/front/img/interface/bt_slideshow_prev.png'); },
		function(){ $(this).find('img').attr('src', base_url+'/theme/front/img/interface/bt_slideshow_prev_off.png'); }
	);
	// -> image suivante (thin)
	$('.slideshow-control-next a').hover(
		function(){ $(this).find('img').attr('src', base_url+'/theme/front/img/interface/bt_slideshow_next.png'); },
		function(){ $(this).find('img').attr('src', base_url+'/theme/front/img/interface/bt_slideshow_next_off.png'); }
	);
	// -> image précédente (fat)
	$(".slideshow-container-overlay_left").hover(
		function() { $(".slideshow-container-overlay_left img").fadeIn('fast'); },
		function() { $(".slideshow-container-overlay_left img").fadeOut('fast'); }
	);
	// -> image suivante (fat)
	$(".slideshow-container-overlay_right").hover(
		function() { $(".slideshow-container-overlay_right img").fadeIn('fast'); },
		function() { $(".slideshow-container-overlay_right img").fadeOut('fast'); }
	);
	
	// ------------------------------------
	// Gestion des clicks sur les contrôles
	// ------------------------------------
	$('.slideshow-control-prev a, .slideshow-container-overlay_left a').click(goPrev);
	$('.slideshow-control-next a, .slideshow-container-overlay_right a').click(goNext);
	
	// Initialise le slideshow automatique
	$('.slideshow-container-overlay_center a').click(function(){
		if (play) { stopAutoSlide(); }
		else { startAutoSlide(); }
		play = !play;
		return false;
	});
	
	// ------------------------------------------
	// Chargement des autres photos de la galerie
	// ------------------------------------------
	var i = 0;
	$('.slideshow-photo-container .scrollcontainer').find('div').each(function(){
		if ($(this).find('img').attr('src')==undefined) {
			var src = $(this).html();
			var src_arr = src.split('|');
			var img_label = src_arr[0];
			var img_src = src_arr[1];
			var img_width = src_arr[2];
			var img_margin = src_arr[3];
			$(this).html('<img class="photo-full" src="'+img_src+'" alt="'+img_label+'" style="width: '+img_width+'px; margin-left: '+img_margin+'px;" />');
			
			labels[i] = img_label;
		}
		else {
			labels[i] = $(this).find('img').attr('alt');
		}
		
		i++;
	});
	
	return false;
}

/**
 * Affiche la photo précédente
 * 
 * @name	goPrev
 * @author	Rémy Vuong <remy@upian.com>
 * @date	2009-03-18
 * @version	1.0
 * @return	bool false
 */
function goPrev() {
	// Calcul de l'offset et défilement jusqu'à la photo pointée
	offset--;
	if (offset<0) {
		offset = $('.slideshow-photo-container').find('div').length - 2;
	}
	
	return goTo(offset);
}

/**
 * Affiche la photo suivante
 * 
 * @name	goNext
 * @author	Rémy Vuong <remy@upian.com>
 * @date	2009-03-18
 * @version	1.0
 * @return	bool false
 */
function goNext() {
	// Calcul de l'offset et défilement jusqu'à la photo pointée
	offset++;
	if (offset > $('.slideshow-photo-container').find('div').length-2) {
		offset = 0;
	}
	
	return goTo(offset);
	
	return false;
}

function goTo(offset) {
	$('.slideshow-photo-container').scrollTo($('.scrollcontainer > div').get(offset), speed, {axis:'x'});
	
	// Mise à jour index photo
	updateIndex(offset);
	
	// Mise à jour de la légende
	if (labels[offset]!=undefined) { $('.slideshow-caption-inner').html(labels[offset]); }
	else { $('.slideshow-caption-inner').html(''); }
	
	// Remet à 0 la largeur de la timebar
	timebarWidth = 0;
	$('.slideshow-control-timer img').attr('style', 'width:'+timebarWidth+'%; height:5px;');
	
	return false;
}

/**
 * Affiche l'index de la photo affichée
 * 
 * @name	updateIndex
 * @author	Rémy Vuong <remy@upian.com>
 * @date	2009-03-18
 * @version	1.0
 * @param	int offset
 * @return	bool false
 */
function updateIndex(offset) {
	var offset_str = $('.slideshow-control-infos > strong').html();
	var offset_pos = offset_str.indexOf('/');
	$('.slideshow-control-infos > strong').html((offset+1)+offset_str.substring(offset_pos));
}

/**
 * Lance le défilement automatique des slides
 * 
 * @name	startAutoSlide
 * @author	Rémy Vuong <remy@upian.com>
 * @date	2009-03-18
 * @version	1.0
 * @return	bool false
 */
function startAutoSlide() {
	$('.slideshow-container-overlay_center a img').attr('src', base_url+'/theme/front/img/interface/bt_slideshow_overlay_stop.png');
	$('.slideshow-control-timer').everyTime(delayTime, 'timer', function(){strechTimebar();});
}

/**
 * Stoppe le défilement automatique des slides
 * 
 * @name	stopAutoSlide
 * @author	Rémy Vuong <remy@upian.com>
 * @date	2009-03-18
 * @version	1.0
 * @return	bool false
 */
function stopAutoSlide() {
	$('.slideshow-container-overlay_center a img').attr('src', base_url+'/theme/front/img/interface/bt_slideshow_overlay_next.png');
	$('.slideshow-control-timer').stopTime('timer');
	
	// Remet à 0 la largeur de la timebar
	// timebarWidth = 0;
	$('.slideshow-control-timer img').attr('style', 'width:'+timebarWidth+'%; height:5px;');
}

/**
 * Redimensionne la barre de timer des photos
 * 
 * @name	strechTimebar
 * @author	Rémy Vuong <remy@upian.com>
 * @date	2009-03-18
 * @version	1.0
 * @return	bool false
 */
function strechTimebar() {
	timebarWidth++;
	
	if (timebarWidth > 100 ) {
		timebarWidth = 0;
		$('.slideshow-control-timer img').attr('style', 'width:'+timebarWidth+'%; height:5px;');
		goNext();
	}
	else {
		$('.slideshow-control-timer img').attr('style', 'width:'+timebarWidth+'%; height:5px;');
	}
	
	return false;
}

/**
 * Initialisation des sondages
 * 
 */
function initSondage() {
	
	// Teste si on est en mode question ou réponse
	if ($('.sondage form').attr('action')==undefined) {
		return false;
	}
	
	// Identifie le sondage
	var son_id = $('.sondage form').attr('action');
	son_id = parseInt(son_id.substr(son_id.indexOf('#') + 1, son_id.length));
	
	// Au click, on identifie la réponse choisie
	$('.sondage .bouton input').click(function() {
		
		var field_val = -1;
		$('.sondage').find('input').each(function() {
			if ($(this).attr('checked')==true) {
				field_val = $(this).val();
			}
		});
		
		// Si une réponse a bien été cochée
		if (field_val!=-1) {
			// JSon met à jour les réponses du sondage et actualise l'affichage
			$.ajax({
				url: base_url+'/sondage/',
				type: 'POST',
				data: '&son_id='+son_id+'&field_val='+field_val,
				dataType: "xml",
				beforeSend: function(){},
				error: function(){},
				success: function(xml){
					var user_voted = $(xml).find('sondage').find('user_voted').attr('value');
					user_voted = (user_voted=='1');
					var msg = (user_voted) ? '<strong>Merci, votre vote a bien &eacute;t&eacute; enregistr&eacute;.</strong>' : '<strong>Vous avez d&eacute;j&agrave; vot&eacute;.</strong><br /><strong>Si vous partagez un r&eacute;seau, les votes sont limit&eacute;s par adresse IP publique.</strong>';
					var hidden_answers = $(xml).find('sondage').find('hidden_answers').attr('value');
					hidden_answers = (hidden_answers=='1');
					
					var question = $(xml).find('sondage').find('question').text();
					var question_total_votes = $(xml).find('sondage').find('question').attr('total_votes');
					
					var html = '<div class="sondage_inner clearfix">';
					html += '	<div class="titre"><strong>Sondage</strong></div>';
					html += '		<div class="resultat">';
					html += '			<p class="question"><strong>'+question+'</strong> ('+question_total_votes+' votes)</p>';
					html += '			<p class="msg">'+msg+'</p>';
					// Parse les réponses pour affichage des graphes
					if (!hidden_answers) {
						$(xml).find('sondage').find('proposition').each(function() {
							var pct = $(this).attr('pct');
							var color = $(this).attr('color');
							var votes = $(this).attr('votes');
							var response = $(this).text();
							
							html += '<div class="reponse clearfix">';
							html += '	<div class="nombre">'+pct+'%</div>';
							html += '	<div class="graphique"><img src="'+base_url+'/theme/front/img/interface/graphique-sondage-'+color+'.png" alt="'+votes+' votes" width="'+pct+'" /></div>';
							html += '	<div class="texte">'+response+' ('+votes+' votes)</div>';
							html += '</div>';
						});
					}
					// Fin balisage
					html += '		</div>';
					html += '	</div>';
					html += '</div>';
					
					$('.sondage').fadeOut('slow', function() {
						$('.sondage').html(html);
						$('.sondage').fadeIn('slow');
					});
					
					return false;
				}
			});
		}
		
		return false;
	});
}

/**
 * Page chargée, on installe les listeners
 * 
 * 
 */
$(document).ready(function() {
	// Fix PNG transparence pour IE6
	$(".slideshow-container-overlay_left img").ifixpng();
	$(".slideshow-container-overlay_center img").ifixpng();
	$(".slideshow-container-overlay_right img").ifixpng();
	
	// Sondage
	initSondage();
	
	// Slideshow
	initSlideshow();
	$('.current_album .vignette').click(function(){return false;});
});
