//Do all this on load
$(function(){
	//Ajouter un title en utilisant le alt
	$('.altToTitle').each(function(){
		$(this).attr('title', $(this).attr('alt'));
	});
	
	//jQuery LB
	if ($('.gal').length > 0){
		$('a.gal').lightBox({
			fixedNavigation:true,
			imageLoading: '/img/lightbox/'+_LNG+'/ico-loading.gif',
			imageBtnClose: '/img/lightbox/'+_LNG+'/btn-close.gif',
			imageBtnPrev: '/img/lightbox/'+_LNG+'/btn-prev.gif',
			imageBtnNext: '/img/lightbox/'+_LNG+'/btn-next.gif',
			imageBlank: '/img/lightbox/'+_LNG+'/blank.gif',
			txtImage: 'Image',
			txtOf: '/'
		});
   }

   //Div with Link inside, to make full clickable
   $('.divA').click(function(e){
	   if (e.target.nodeName != 'A'){
		   l = $(this).find('a:first').attr('href');
		   t = $(this).find('a:first').attr('target');
		   $(e.target).bind('click',function(){return false;});//désactive le premier lien, qui devient le lien global du div
		   if (t == '_blank'){
			   window.open(l);
		   }else{
			   window.location = l;
		   }
	   }else{
		   return true;
	   }
	   return false;
   });

   //captions animation
   $('.box-animate-huge').hover(function(){
		$(".boxcaption", this).stop().animate({left:'670px'},{queue:false,duration:400,easing:'easeOutQuad'});
	}, function() {
		$(".boxcaption", this).stop().animate({left:'995px'},{queue:false,duration:400,easing:'easeOutQuad'});
	});
   $('.box-animate-home1').hover(function(){
		$(".boxcaption", this).stop().animate({top:'0px'},{queue:false,duration:400,easing:'easeOutQuad'});
	}, function() {
		$(".boxcaption", this).stop().animate({top:'-200px'},{queue:false,duration:400,easing:'easeOutQuad'});
	});
   $('.box-animate-medium').hover(function(){
		$(".boxcaption", this).stop().animate({top:'23px'},{queue:false,duration:400,easing:'easeOutQuad'});
	}, function() {
		$(".boxcaption", this).stop().animate({top:'-240px'},{queue:false,duration:400,easing:'easeOutQuad'});
	});
   $('.box-animate-zoom').hover(function(){
	   if ((!fGal_Run) && !$(this).hasClass('vimeo') && !$(this).hasClass('flv') && !$(this).hasClass('qt')){
		$(".boxcaption", this).stop().animate({bottom:'0px'},{queue:true,duration:400,easing:'easeOutQuad'});
	   }
	}, function() {
		$(".boxcaption", this).stop().animate({bottom:'-50px'},{queue:false,duration:400,easing:'easeOutQuad'});
	});
   $('.box-animate-gal').hover(function(){
	   if (!fGal_Run){
			$(".boxcaption", this).stop().animate({top:'0px'},{queue:false,duration:400,easing:'easeOutQuad'});
	   }
	}, function() {
		$(".boxcaption", this).stop().animate({top:'-90px'},{queue:false,duration:400,easing:'easeOutQuad'});
	});

	//affiche, covers, etc. resize du div qui contient l'image de hauteur variable
	$('img.posterLoad').load(function(){
		t = $(this).parents('.actuH3,.actuH3S').get(0);
		$(t).show();
		$(t).height($(this).height());
//		$('t').height($(this).height()).show();
	});

	$('#film_source_ttl').click(function(){$('#film_source').slideToggle('slow');});

	//Gallery watch
	$('#galleryMain').delegate('.th', 'click', fGalH_Zoom);

	//Form transform
	//$('#galFiltersForm').jqTransform();

	//Affichage des autres articles
	$('.selectArticle a:first').click(function(){$('#autresArticles').slideToggle();});
	$('.galLinksContent a:first').click(function(){$('#galLinksUL').slideToggle();});

	//Vide le champ de recherche la première fois qu'on clique dessus
	$('.cof').one('click, focus',function(){$(this).val('')});

});

/* Gallery */
var fGal_idx = 0;
var fGal_Run = false;
function fGal_loadPage(){
	$('#galDiv .galThumb:has(.temp)').each(function(idx){
		u = $('span:first', this).attr('title');
		n = $('span:first', this).text();
		i = $('<img/>', {
			'src':'/image/gallery'+u,
			'title':n,
			'class':'click th',
			'alt':idx
		}).bind({'click':fGal_zoom,'load':function(){$(this).show();}});
		$(this).empty().append(i);
//			console.log(u);
	});
	return false;
}
function fGal_show(){
	//la taille de la galerie va dépendre du nombre d'image au départ
	$('.largeTopMedia').slideUp();
	t = $('#galDiv .galThumb').length;
	if (t > 0){
		l = parseInt(t/6);
		if (t%6 > 0){l++};
		h = ( (l * (75+8))  + 30 );
		$('#galDiv').css({'height':h+'px'}).slideDown('slow', function(){
		    fGal_loadPage();
		    fGal_idx = 0;
		    $('#galDiv .galThumb:eq('+fGal_idx+') img.th').trigger('click');
		    $('#galDiv').css({'height':'auto'});//Rétablissement de la taille auto du div
		});
	}
	//On scroll en haut de la page
	window.scroll(0, 0);
	return false;
}
function fGal_zoom(){
    s = $(this).attr('src').replace('/gallery/','/zoom/');
    fGal_idx = parseInt($(this).attr('alt'));
    i = $('<img/>', {
		    'src':s,
		    'class':'click'
	    }).bind({'load':fGal_rsz,'click':fGal_next});
    var caption = $(this).attr('title');
    caption = caption.split('©');
    var copyright = '', title = caption[0];
    if (caption.length > 1){
	if ($.trim(caption[1]) != ''){
	    copyright = ' | ' + $.trim(caption[1]);
	}
    }
    h4 = $('<h4/>',{'text':title});
    cp = $('<p/>',{'text':_COPY + copyright,'class':'small'});
    $('#galZoom img').remove();
    $('#galZoom div.boxcaption').empty().append(h4).append(cp);
    $('#galZoom').append(i).show();
}

function fGalH_Zoom(){
    $('#galDiv').show();
    $('html').scrollTop(0);
//	$('.boxcaption', this).css('top','-90px');
    //On regarde si c'est une photo ou vimeo
    if ($(this).hasClass('vimeo')){
	    fGalH_Vimeo($(this));
    }else if ($(this).hasClass('flv')){
	    fGalH_FLV($(this));
    }else{
	    fGalH_Image($(this));
    }
	
}
function fGalH_Image(e){
	s = $('img:first',e).attr('src').replace('/gal/','/zoom2/');
	fGal_idx = $('#galleryMain .th').index(e);
	i = $('<img/>', {
			'src':s,
			'class':'click'
		}).bind({'load':fGal_rsz,'click':fGalH_next});
	var caption = $('.mediaTitle:first',e).text();
	caption = caption.split('©');
	var copyright = '', title = caption[0];
	if (caption.length > 1){
	    if ($.trim(caption[1]) != ''){
		copyright = ' | ' + $.trim(caption[1]);
	    }
	}
	h4 = $('<h4/>',{'text':title});
	cp = $('<p/>',{'text':_COPY + copyright,'class':'small'});
	$('#galZoom *').remove('img, object, embed, a, .largeDesc');
	$('#galZoom').removeClass('vimeo flv');
	$('#galZoom div.boxcaption').empty().append(h4).append(cp);
	$('#galZoom').append(i).show();
}

function fGalH_Vimeo(e){
	v = $('span:first',e).text();
	if (v!=''){
	    fGal_Run = true;
	    $('#galZoom *').remove('img, object, embed, a, .largeDesc');
	    $('#galZoom').removeClass('vimeo flv').addClass('vimeo');
	    $('#galZoom div.boxcaption').empty();
	    var swf = $('<div/>',{'id':'swfTemp'});
	    var largeDesc = $('<div/>',{
		    'class':'largeDesc',
		    'html':''
		    });
	    $('#galZoom').append(swf).append(largeDesc);

	    var flashvars = false;
	    var params = {};
	    var attributes = {
		wmode: "transparent",
		allowfullscreen: "true",
		allowscriptaccess: "always"
	    };
	    swfobject.embedSWF(v, "swfTemp", "660", "380", "9.0.0","expressInstall.swf", flashvars, params, attributes);
	    $('#galZoom').animate({'height':380}, {queue:true,duration:500,easing:'easeOutQuad','complete':function(){fGal_Run = false;gGalH_VimeoGetInfos(v);}});
	}
}

function fGalH_FLV(e){
	v = $('span:first',e).text();
	t = $('span:last',e).text();
	if (v!=''){
		fGal_Run = true;
		$('#galZoom *').remove('img, object, embed, a, .largeDesc');
		$('#galZoom').removeClass('vimeo flv').addClass('flv');
		$('#galZoom div.boxcaption').empty();
		var swf = $('<a/>',{
		    'id':'galFlowPlayer',
		    'href':v
		    });
		var largeDesc = $('<div/>',{
			'class':'largeDesc',
			'html':'<h4>'+t+'</h4>'
			});
		$('#galZoom').append(swf).append(largeDesc);

		$('#galZoom').animate({'height':480}, {queue:true,duration:500,easing:'easeOutQuad','complete':function(){
			fGal_Run = false;
			flowplayer("galFlowPlayer", "/flowplayer/flowplayer-3.1.5.swf");
		}});
	}
}

function gGalH_VimeoGetInfos(v){
    $.post('/ajax.php',{
	'func':'vimeoGetInfos',
	'v':v
	},
	function(data){
	    if (data.status == 'OK'){
		h = $('<h4/>',{'html':data.title});
		$('#galZoom .largeDesc').append(h);
		if (data.description != ''){
		    p = $('<p/>',{'html':data.description});
		    $('#galZoom .largeDesc').append(p);
		}
	    }
	},
	'json'
    );
}

function fGal_rsz(){
	h = $('#galZoom img').height();
	fGal_Run = true;
	$('#galZoom').animate({'height':h}, {queue:true,duration:500,easing:'easeOutQuad','complete':function(){fGal_Run = false}});
}
function fGal_next(){
	var t = fGal_idx + 1;
	var e = $('#galDiv .galThumb:eq('+t+') img.th');
	if (e.length > 0){
		//thumb exist
		e.click();
	}
}
function fGalH_next(){
    var t = fGal_idx + 1;
    var e = $('#galleryMain .th:eq('+t+')');
    if (e.length > 0){
	//thumb exist
	e.click();
    }
}

function fGalH_setFilter(e){
	t = $(':selected',e).val();
	$('#galFiltersForm select').val('');
	e.val(t);
	$('#galFiltersForm').submit();
	return false;
}
function DLink(pStr, p2){
	var pS, pE;
	pS = '';
	pE = '';
	for (i=0;i<pStr.length;i=i+2){
		pS += pStr.substr(i,1);
		pE = pStr.substr(i+1,1) + pE;
	}
	if (p2 == 0){
		return (pS+pE);
	}else{
		document.location.href = pS+pE;
	}
}
function js_ShowNewsletter(){
	if ($('object').length > 0){
		$('#nlSub').show();
	}else{
		$('#nlSub').slideDown('normal');
	}
	return false;
}
function js_nlSubscribe(){
	var e = $.trim($('#nlEmail').val());
	if (e != ''){
		$.post(
			'/ajax.php',
			{
				'func':'subscribeNewsletter',
				'mod':'common',
				'e':e
			},
			function(response){
				switch (response.status){
					case 'OK':
					case 'KO':
						$('#nlRes').removeClass('red green1').addClass('green1').html(response.message);
						$('#nlEmail').val('');						
						break;
					default:
						$('#nlRes').removeClass('red green1').addClass('red').html(response.message);
						break;
				}
			},
			'json'
		);
	}
	return false;
}

function js_SendMsg(){
	var fn,ln,e,p,m;
	fn = $('#contact_fn').val();
	ln = $('#contact_ln').val();
	e = $('#contact_e').val();
	p = $('#contact_p').val();
	m = $('#contact_m').val();

	if ((m != '') && (e!=''||p!='') && (fn!='') && (ln!='')){
		$.post(
		'/ajax.php',
		{
			'func':'sendMessage',
			'fn':fn,
			'ln':ln,
			'e':e,
			'p':p,
			'm':m
		},
		function(data){
			if (data.status == 'OK'){
				$('#MsgSendForm').remove();
				$('#MsgSendResult').html('<b>'+data.message+'</b>').show();
			}else{
				$('#MsgSendResult').html('<p class="red">'+data.message+'</p>').show();
			}
		},
		'json');
	}

	return false;
}

function js_SendRelai(){
	var fn,ln,e,p,m,ca1,ca2,sa,sz,sv,sp,sabo;
	fn = $('#contact_fn').val();
	ln = $('#contact_ln').val();
	e = $('#contact_e').val();
	p = $('#contact_p').val();
	ca1 = $('#struc_cat1').val();
	ca2 = $('#struc_cat2').val();
	sa = $('#struc_a').val();
	sz = $('#struc_z').val();
	sv = $('#struc_v').val();
	sp = $('#struc_p').val();
	sabo = $('#struc_abo').val();

	if ((e!=''||p!='') && (fn!='') && (ln!='')){
		$.post(
		'/ajax.php',
		{
			'func':'sendRelai',
			'fn':fn,
			'ln':ln,
			'e':e,
			'p':p,
			'ca1':ca1,
			'ca2':ca2,
			'sa':sa,
			'sz':sz,
			'sv':sv,
			'sp':sp,
			'sabo':sabo
		},
		function(data){
			if (data.status == 'OK'){
				$('#MsgSendForm').remove();
				$('#MsgSendResult').html('<b>'+data.message+'</b>').show();
			}else{
				$('#MsgSendResult').html('<p class="red">'+data.message+'</p>').show();
			}
		},
		'json');
	}else{
		alert('Merci de préciser votre nom, prénom et votre adresse e-mail ou numéro de téléphone.');
	}

	return false;
}
