/**
 * ************************************
 * Gestion des slideshows
 * 
 * @author	Rémy Vuong <remy@upian.com>
 * @date	2009-03-17
 * @version	1.0
 * ************************************
 */

/*
var speed = 0, default_speed = 400;
var offset = 0;
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() {
	/*
	// Positionnement automatique sur la slide cliquée
	// -> inutile puisqu'on est toujours sur la première
	var offset_html = $('.slideshow-control-infos > strong').html();
	if (offset_html == null) {
		return false;
	}
	var offset_pos = offset_html.indexOf('/');
	offset = parseInt(offset_html.substring(0, offset_pos))-1;
	$('.slideshow-photo-container').scrollTo($('.scrollcontainer > div').get(offset), speed, {axis:'x'});
	*/
	
	// 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'); }
	);
	
	/*
	// On cache le bouton "play" pour désactiver le slideshow automatique
	$('.slideshow-container-overlay_center').hide();
	// On scrolle automatiquement jusqu'au titre de la galerie
	$.scrollTo($('.article'));
	
	// ------------------------------------
	// 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
	// ------------------------------------------
	/*
	$('.slideshow-photo-container .scrollcontainer').find('div').each(function(){
		if ($(this).find('img').attr('alt').indexOf('!') != -1) {
			var img_alternate = $(this).find('img').attr('alt');
			var img_src = img_alternate.substring(0, img_alternate.indexOf('!'));
			var img_alt = img_alternate.substring(img_alternate.indexOf('!')+1);
			$(this).find('img').attr('src', img_src);
			labels[labels.length] = (img_alt!=undefined && img_alt.length>0) ? img_alt : '&nbsp;';
		}
		else {
			labels[labels.length] = ($(this).find('img').attr('alt').length>0) ? $(this).find('img').attr('alt') : '&nbsp;';
		}
	});
	*/
	
	/*
	// On empêche le click sur photo
	$('.column-photos a').click(function(){return false;});
	*/
	
	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;');
	
	StatsUpdate();
	
	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;
}

/**
 * Augmente le compteur de stats
 */
function StatsUpdate() {
	/*
	var tmp_offset = 0;
	var tmp_url = '';
	
	$('.scrollcontainer div').each(function(){
		if (tmp_offset==offset) {
			// Appel de la page en JQuery pour activation du compteur de stats
			tmp_url = $(this).find('a').attr('href');
			
			// Change l'url affichée
			$.ajax({
					url: tmp_url,
					type: 'GET',
					dataType: "html",
					error: function(){alert('error');},
					success: function() {
						// Reload les bannières de pub, d'abord celle du haut
						var url_Top = base_url + '/clubbing/oas/Top/'+Math.random();
						var html_Top = '<iframe name="oas_Top" src="'+url_Top+'" width="728" height="90" frameborder="0" scrolling="0" align="auto"></iframe>';
						$('#ads_top .fl').fadeOut('slow');
						$('#ads_top .fl').html(html_Top);
						$('#ads_top .fl').fadeIn('slow');
						// Puis celle de la colonne de droite
						var url_Middle = base_url + '/clubbing/oas/Middle/'+Math.random();
						var html_Middle = '<iframe name="oas_Top" src="'+url_Middle+'" width="300" height="250" frameborder="0" scrolling="0" align="auto"></iframe>';
						$('#oas_right').fadeOut('slow');
						$('#oas_right').html(html_Middle);
						$('#oas_right').fadeIn('slow');
					}
				});
			
			return false;
		}
		tmp_offset++;
	});
	*/
}

/**
 * Initialisation des fonctions chargées sur la page de diaporamas
 * 
 * @author	Rémy Vuong <remy@upian.com>
 * @date	2009-03-16
 * @version	1.0
 */
$(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();
	
	// Photo
	initSlideshow();
	
	// Commentaires
	$('.com .repondre').click(function() {
		var answer_div = $(this).attr('href').substring(1);
		$('#'+answer_div).slideDown('fast');
		return false;
	});
	
	$('.close a').click(function(){
		$(this).parent().parent().slideUp('fast');
		return false;
	});
	
	// Ouvrir le panneau d'alerte
	$('.alerter').click(function(){
		var $alert_panel = $(this).parents('.comment');
		$alert_panel.next().slideDown(500, function() {
			// ajustColHeight("up");
		});
		return false;
	});
	
	// Fermer le panneau d'alerte
	$('.alert_msg .close a').click(function() {
		$(this).parents('.alert_msg').slideUp(500, function() {
			// ajustColHeight("down");
		});
		return false;
	});
	
});
