(function($) {
  var cache = [];
  // Arguments are image paths relative to the current page.
  $.preLoadImages = function() {
    var args_len = arguments.length;
    for (var i = args_len; i--;) {
      var cacheImage = document.createElement('img');
      cacheImage.src = arguments[i];
      cache.push(cacheImage);
    }
  }
})(jQuery)

$(document).ready(function() {
	if($('#flash_accueil').length && $.flash.available)  {
		$('#flash_accueil').html('');
		$('#flash_accueil').flash({   
			swf: '/medias/swf/banniere.swf',
			width: 896,
			height: 350,
			wmode: 'opaque',
			flashvars: {
				fichiers: '/medias/swf/'
			}
		});		
	}
	$('#haut_page a.btfermer').click(function() {
		$(this).parent().hide();
		return false;
	});
	$('form.validation').validate({
		errorPlacement: function() {
			$('form p.erreur').show();
		}
	});
		
	$('#selection_photos a').click(function() {
		if(!$('#photos>img').is(':animated')) {
			var strimg = $(this).attr('href');
			$('#photos>img:first').clone().appendTo('#photos');
			var ancimg = $('#photos>img:first');
			var nouvimg = $('#photos>img:last');
			
			nouvimg.hide();
			nouvimg.attr('src',strimg);
			
			ancimg.fadeOut(1000, function(){
				$('#photos>img:not(:last)').remove();
				$('#photos>img:last').fadeTo(0,1);
			});
			nouvimg.fadeIn(1000);
		}
		return false;
	});

	$('#scroller ul a').lightBox({
		txtOf: 'de',
		imageBtnPrev: '/images/lightbox-btn-prev.gif',
		imageBtnNext: '/images/lightbox-btn-next.gif',
		imageBtnClose: '/images/lightbox-btn-close.gif',
		imageLoading: '/images/lightbox-ico-loading.gif'
	});		
	
	
	//Gestion du scroller
	var intpagescroll = $('#scroller>div>div>ul').length;
	var inttaillepage = 830;
	var intpage = 1;
	if(intpagescroll) {
		$('#scroller>div>div').width(inttaillepage * intpagescroll);
	}
	$('#scroller>a#btscrolldroite').click(function() {
		if(intpage < intpagescroll) {
			$('#scroller>div>div').animate({
				'margin-left': '-=830'
			},1000);
			intpage++;
		}
		return false;
	});
	$('#scroller>a#btscrollgauche').click(function() {
		if(intpage > 1) {
			$('#scroller>div>div').animate({
				'margin-left': '+=830'
			},1000);
			intpage--;
		}
		return false;
	});	
			
	
	$('#haut_page.medias ul a').click(function() {
		var strphoto = $(this).attr('href');
		var oimg = $('<img/>');
		oimg.attr('src',strphoto);
		$('#popup>div').html('');
		$('#popup>div').append(oimg);
		$('#popup').show();
		$('#popup').height( $(document).height());
		return false;
	});		
	$('#popup a.btfermer').click(function() {
		$('#popup').hide();
		return false;
	});
	
	$('#selection_photos a').each(function() {
		var strimg = $(this).attr('href');
		$.preLoadImages(strimg);
	});
	
	$('#scroller ul a').each(function() {
		var strimg = $(this).attr('href');
		$.preLoadImages(strimg);
	});	
	
	$('#haut_page.medias ul a').each(function() {
		var strimg = $(this).attr('href');
		$.preLoadImages(strimg);
	});	
	
	$('#contenu p').each(function() {
		$(this).html($(this).html().replace(/\.$/,'<span class="point">.</span>'));
	});
});

function popup(astr_titre,astr_corp,astr_txtbouton) {
	$('#popup').html(astr_corp);
	$('#popup').dialog({
		modal: true,
		width: 400,
		buttons: {
					'Ok': function() {
						$(this).dialog('close');
				}
		},
		title: astr_titre
	})
};
