var auto;
var t;
function playerStart() {auto = false; clearTimeout(t);}
function playerStop() {auto = false; clearTimeout(t);}
function playerFullScreen() {auto = false; clearTimeout(t);}
function playerNormalScreen() {auto = false; clearTimeout(t);}

$(function() {

	var ulLength = $('.slide li').length;
	if (ulLength==1) {$('.btn_arrowLeft,.btn_arrowRight,.slide_info #pagin a').css('opacity',0);}
	var liWidth = $('.slide li').width();
	var ulWidth = ulLength * liWidth;
	//var auto;
	if ($('#resultat_details div').attr('id') == 'slider') {
		t = setTimeout(function() {Right();auto = true;},4500);
	}
	
	if ($('#main div').attr('id') == 'bulle_bg') {
		$('#main').css('background','url("' + images[1] + '") no-repeat');
	}
	
	bullePaginWidth = ($('#bulle #pagin ul li').width()+3)*$('#bulle #pagin ul li').length;
	$('#bulle #pagin,#bulle #pagin ul').css('width',bullePaginWidth);
	sliderPaginWidth = ($('.slide_info #pagin ul li').width()+3)*$('.slide_info #pagin ul li').length;
	$('.slide_info #pagin ul,.slide_info #pagin').css('width',sliderPaginWidth);
	
	$('#bulle').css({'opacity':0,'display':'none'});
	$('#bulle_bg').css({'display':'none'});
	setTimeout(function() {
		$('#bulle,#bulle_bg').css('display','block');
		$('#bulle #panel .slide li').css('paddingTop',0);
		$('#bulle #panel .slide li').each(function(){
			var HeightLi =  $(this).height();
			if (HeightLi <= 43) {
				$(this).css('padding','36px 0');
			}else if (HeightLi <= 68) {
				$(this).css('padding','24px 0');
			}else if (HeightLi <= 93) {
				$(this).css('padding','11px 0');
			}
		});
		$('#bulle').animate({opacity:1},700,'easeInOutExpo',function () {
		auto = true;if (auto == true) {t = setTimeout(function() {Right();
		},5000)}});
	},1000);
	
	$('.slide').css('width',ulWidth);
	var next ; var last;
	var pagin = false;
	//var auto = false;
	parseInt(next);
	parseInt(last);

	function Right() {
		last = parseInt($('.current').attr('id'));
		if (pagin == false) { next = last + 1;}
		//$('.btn_arrowLeft').css('opacity',1);
		if (last<ulLength) {
			MarginLeft = (1 - next ) * parseInt(liWidth) ;
			$('.slide').stop().animate({marginLeft:MarginLeft},1000,'easeInOutExpo',function(){if (auto == true) {t = setTimeout(function() {Right();},5000);}});
			if ($('#main div').attr('id') == 'bulle_bg') {
				$('#main').css('background','url("' + images[next] + '") no-repeat');
			}
			$('#pagin ul li a').removeClass('current');
			$('#pagin ul li #'+next).addClass('current');
			if (next==ulLength) {$(this).css('opacity',0.4);}
			pagin = false;
		} else {
			$('#pagin ul li a#1').click();
		}
	}

	function Left() {
		last = parseInt($('.current').attr('id'));
		if (pagin == false) { next =  last - 1;}
		if (last>1) {
			MarginLeft = (1 - next ) * parseInt(liWidth) ;
			$('.slide').stop().animate({marginLeft:MarginLeft},1000,'easeInOutExpo',function(){if (auto == true) {t = setTimeout(function() {Right();},5000);}});
			$('#pagin ul li a').removeClass('current');
			$('#pagin ul li #'+next).addClass('current');
			if (next==1) {$(this).css('opacity',0.4);}
			pagin = false;
		}
		else {
			$('#pagin ul li a#'+ulLength).click();
		}
	}
	
	$('.btn_arrowRight').stop().click(function(e) {e.preventDefault();auto = true;clearTimeout(t);Right();});
	$('.btn_arrowLeft').stop().click(function(e) {e.preventDefault();auto = true;clearTimeout(t);Left();});	


	$('.slide_info #pagin ul li a').stop().click(function(e) {
		e.preventDefault();
		last = $('.current').attr('id');
		next = $(this).attr('id') ;	
		pagin = true;	
		auto = true;
		clearTimeout(t);
		if ($(this).attr('id') > $('.current').attr('id')) { Right();} 
		else if ($(this).attr('id') < $('.current').attr('id') ) { Left();}	
	});	
	
	$('#bulle #pagin ul li a').stop().click(function() {
		last = $('.current').attr('id');
		next = $(this).attr('id') ;	
		pagin = true;	
		$('#main').css('background','url("' + images[next] + '") no-repeat');
		clearTimeout(t);
		if ($(this).attr('id') > $('.current').attr('id')) { Right();} 
		else if ($(this).attr('id') < $('.current').attr('id') ) { Left();}	
		$('#bulle').stop().css('opacity',1);
	});	
	
	var input_txt = $('.search_resultat .input_txt').attr('value');
	
	$('.input_txt').focus(function() {
		if ($(this).attr('value') == 'Tout savoir sur l’Agence Fantastic*' || input_txt != '') {
			$(this).attr('value','');
			input_txt = '';
		}
	});
	
	
	$('.input_txt').keypress(function() {
		$(this).css('color','#000000');
	});
	
	$('.input_txt').focusout(function() {
		if ( $(this).attr('value') == '' ) {
			$(this).attr('value','Tout savoir sur l’Agence Fantastic*').css('color','#8C8C8C');
		}
	});
	
	$('.btn_recherche').click(function(e) {
		e.preventDefault();
		if ( $('.input_txt').attr('value') == 'Tout savoir sur l’Agence Fantastic*' ) {
			$('.input_txt').attr('value','');
		}
		$('#form_ensavoir').submit();
	});
	
	if ( $('.input_txt').attr('value') == '' ) {
		$('.input_txt').attr('value','Tout savoir sur l’Agence Fantastic*').css('color','#8C8C8C');
	}


	
	$(".pause").click( function() {
		$(this).hide();
		$(".play").css('display','block');
		$("#player").jPlayer("pause");
	});

	$(".play").click( function() {
		$(this).hide();
		$(".pause").css('display','block');
		$("#player").jPlayer("play");
	});
		
	/*if(!$('#main').hasClass('home')) {
		$("#player").jPlayer({
			ready: function() {
					$("#player").jPlayer("setFile", "http://www.agencefantastic.com/upload/15_Utopia.mp3");
					$('.pause').hide();
					$(".play").css('display','block');
			},
			swfPath: "/js/frontoffice",
			nativeSupport: false
		});	
		
	}else {
	$("#player").jPlayer({
		ready: function() {
				$("#player").jPlayer("setFile", "http://www.agencefantastic.com/upload/15_Utopia.mp3").jPlayer("play");
		},
		swfPath: "/js/frontoffice",
		nativeSupport: false
	});		
	}*/
});
