$(document).ready(function() {

	$.preloadCssImages();

	var preload = new Array();
    var contPreload = 0;

	$('#projets_preload').flash({
		swf: 'swf/loading.swf',
		width: '50px',
		height: '25px',
		hasVersion: 8,
		flashvars: {},
		params: { wmode: 'transparent'}
	});
	
	ChargeImage();
	
	function preloadImage() {
    	$('<img />')
	    .attr('src', preload[contPreload][0]+ '?random=' + (new Date()).getTime())
	    .attr('alt', preload[contPreload][1])
    	.load(function(){
			$('#'+this.alt).parents("a").fadeIn('slow');
			preloadNextImage();
    	});
    }
    
    function preloadNextImage(){
    	contPreload++;
    	if (contPreload < preload.length) preloadImage(contPreload);
    }
	
	function PreChargeImage(source, nextEvent) {
		$('#projets_masque').show();
		$('#tag-'+source).css("color","#E52313");
		$("#realisations img").each(function() {
			$(this).parents("a").hide();
			preload.push([this.src, this.id]);
		});
		$(this).oneTime(250, "preload", nextEvent);
	}
	function ChargeImageFin() {
		if (tagSelected == "all"){	
			if (preload.length > 0) {
				preloadImage(contPreload);
			}
			$('#projets_masque').stop().fadeOut('slow');
		} else {
			var ids = $('#tag-'+tagSelected).attr('class').split(" ")[0].split("|");
			
			$("#realisations img").each(function() {
				var idThumb = this.id.split("thumb-")[1];
				var ocultar = true;
				for (var i=0; i<ids.length; i++) if (idThumb==ids[i]) ocultar = false;
				$(this).parents("a").hide();
				if (!ocultar) $(this).parents("a").fadeIn('slow');
			});	
			$('#projets_masque').stop().fadeOut('slow');
		}
	}
	
	function resizeContenus() {
		var navWidth = $(window).width();
		var navHeight = $(window).height();
		
		// centrage du preloading
		var preloadTop = (navHeight-40)/2;
		var preloadLeft = (navWidth-40)/2;
		$('#projets_preload').css({top: preloadTop, left: preloadLeft});
	}

    
	function ChargeImage() {
		resizeContenus();
		var imageSource = tagSelected;
		PreChargeImage(imageSource, ChargeImageFin);
	}	

	$('#tags li').click(function() {
		var idTag = this.id.split("tag-")[1];
		
		$('#tag-'+tagSelected).css("color","");					
		tagSelected = idTag;
		$('#tag-'+tagSelected).css("color","#E52313");
		
		if (tagSelected == "all"){
			$("#realisations img").each(function() {
				$(this).parents("a").fadeIn('slow');
			});
		} else {
			var ids = $(this).attr('class').split(" ")[0].split("|");
			
			$("#realisations img").each(function() {
				var idThumb = this.id.split("thumb-")[1];
				var ocultar = true;
				for (var i=0; i<ids.length; i++) if (idThumb==ids[i]) ocultar = false;
				$(this).parents("a").hide();
				if (!ocultar) $(this).parents("a").fadeIn('slow');
			});
		}
	});
	
	$('.boxgrid.captionfull').hover(function(){
		$(".cover", this).stop().animate({top:'5px'},{queue:false,duration:160});
	}, function() {
		$(".cover", this).stop().animate({top:'90px'},{queue:false,duration:160});
	});
	
//	$(document).pngFix(); 
	$('#trame').css({height: $(document).height()});
});


