var isIE = false;
var imgWidth = 1920;
var imgHeight = 1112;
var portfolio = [];
var portfolioActuel = 1;
var preloadActuel = 1;
var imageActuelle = 1;
var imagePath = "uploads/articles/bigimages/";
var menuactif ="pageweb"

$(document).ready(function(){

	$('#projets_preload').flash({
		swf: 'swf/loading.swf',
		width: '50px',
		height: '25px',
		hasVersion: 8,
		flashvars: {},
		params: { wmode: 'transparent'}
	});
	
	$.ajax({
		type: "GET",
		url: "ajax/xml.php?type=1",
		dataType: "xml",
		success  : parseXml 
	});
	
	function parseXml(xml){
		portfolio=[];
		portfolio[0] = null;
		portfolioActuel = 1;
		preloadActuel = 1;
		imageActuelle = 1;
		$(xml).find("portfolio").each(function(){
		    var id = $(this).attr("id");
			var name = $(this).find('name').text();
			portfolio[id] = [name, false];
		 });
		isIE = $.browser.msie;
		ChargeImage();
		
	}

	//gestion des évents
	
	$('.webportfolio').click(function() {
		$('#menuhome').removeClass(menuactif);
		$('#menuhome').addClass("pageweb"); 
		menuactif="pageweb";
		$.ajax({
			type: "GET",
			url: "ajax/xml.php?type=1",
			dataType: "xml",
			success  : parseXml 
		});						  
	});
	$('.printportfolio').click(function() {
		$('#menuhome').removeClass(menuactif);
		$('#menuhome').addClass("pageprint"); 
		menuactif="pageprint";		
		$.ajax({
			type: "GET",
			url: "ajax/xml.php?type=2",
			dataType: "xml",
			success  : parseXml 
		});						  
	});	
	$('.identityportfolio').click(function() {
		$('#menuhome').removeClass(menuactif);
		$('#menuhome').addClass("pageidentites"); 
		menuactif="pageidentites";		
		$.ajax({
			type: "GET",
			url: "ajax/xml.php?type=3",
			dataType: "xml",
			success  : parseXml 
		});						  
	});		
	
	$('#projets').hover(
		function() {
			$('#cache_top').css("display","block");
			$('#cache_top').css({opacity:0}).fadeTo(1000, 0.5);
			$('#cache_bottom').css("display","block");
			$('#cache_bottom').css({opacity:0}).fadeTo(1000, 0.5);
			$('#navProjets').css("display","block");
			$('#navProjets').css({opacity:0}).fadeTo(1000, 1);			
		},
		function() {
			$('#cache_top').fadeOut('slow');
			$('#cache_bottom').fadeOut('slow');
			$('#navProjets').fadeOut('slow');
		});
	$('#projets_previous').hover(
		function() {
			$('.projet_previous').show();
			$('.projet_previous').css("display","block");
//			$('.projet_previous').css({opacity:1}).fadeTo(1000, 1);				
		},
		function() {
			$('.projet_previous').hide();
		});
	
	$('#projets_next').hover(
		function() {
			$('.projet_next').css("display","block");
//			$('.projet_next').css({opacity:1}).fadeTo(1000, 1);			
			$('.projet_next').show();
		},
		function() {
			$('.projet_next').hide();
		});
	$('.projet_previous').click(function(e) {
		$(this).oneTime(150, "next", showPreviousProject);
		e.preventDefault();
	});
	
	$('.projet_next').click(function(e) {
		$(this).oneTime(150, "next", showNextProject);
		e.preventDefault();
	});
	


	


});

$(window).resize(function(){
	// gestion de la fenêtre
	resizeBackgrounds();
});

var imgCenter = "#background_default";
var imgLeft = "#background_previous";
var imgRight = "#background_next";

function resizeBackgrounds() {

	// ratio proportionel du navigateur
	var navWidth = $(window).width();
	var navHeight = $(window).height();
	var navRatio = navWidth / navHeight;

	// ration proportionel de l'image
	if ($('#main_image').width() > 1) imgWidth = $('#main_image').width();
	if ($('#main_image').height() > 1) imgHeight = $('#main_image').height();
	picRatio = imgWidth / imgHeight;
	
	// échelle résultante
	if (navRatio > picRatio) {
		// mise à l'échelle des images pour les ajuster à la largeur du navigateur
		var newHeight = (navWidth / imgWidth) * imgHeight;
		var newWidth = navWidth;
	} else {
		// mise à l'échelle des images pour les ajuster à la hauteur du navigateur
		var newHeight = navHeight;
		var newWidth = (navHeight / imgHeight) * imgWidth;
	}
	
	// centrage des images
	newTop = 0 - ((newHeight - navHeight) / 2);
	newLeft =  0 - ((newWidth - navWidth) / 2);
	newProjet = navHeight / 3;

	// afficher tout
	$('#backgrounds').css({height: navHeight, width: navWidth});
	$('#backgrounds').css({visibility:"visible", display:"block"});
	$('#projets_browse').css({height: newProjet});
	$('#cache_top').css({height: newProjet});
	$('#cache_bottom').css({height: newProjet});
	$('#projets').css({top: newProjet});
	$('#navProjets').css({top: newProjet-28});
	$('.main_background').css({height: newHeight, width: newWidth});
	
	$(imgCenter).css({top: newTop, left: newLeft});
	$(imgLeft).css({top: newTop, left: newLeft-newWidth});
	$(imgRight).css({top: newTop, left: newLeft+newWidth});
	

	resizeContenus();
}

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 PreChargeImage(source, nextEvent) {
		if(!isIE) $('#projets_masque').show();
		if (imageActuelle!=1 || !portfolio[portfolioActuel][1]){	
			if (isIE) {
				$('<img />').attr('src', source+'?'+Math.random()*99999).load(function(){
					if (imageActuelle==1) portfolio[portfolioActuel][1] = true;
					$(this).oneTime(500, "preload", nextEvent);
				});

			}else{
  				$('<img />').attr('src', source)			.load(function(){
					if (imageActuelle==1) portfolio[portfolioActuel][1] = true;
					$(this).oneTime(500, "preload", nextEvent);
				});
		   }
		} else {			
			$(this).oneTime(250, "preload", nextEvent);
		}
}

function PreChargeImageSuivante() {
	// prechargement d'un image
	preloadActuel++;
	if (preloadActuel==portfolio.length) preloadActuel = 1;
	preloadActuel = getNextToPreload();
	if (preloadActuel != -1){
		var source = PreChargeSourceImageActuelle();
		if (isIE) {
			$('<img />').attr('src', source+'?'+Math.random()*99999).load(function(){	
				portfolio[preloadActuel][1] = true;
				PreChargeImageSuivante();
			});
		}else{
			$('<img />').attr('src', source).load(function(){	
				portfolio[preloadActuel][1] = true;
				PreChargeImageSuivante();
			});
		}
	}
}

function getNextToPreload() {
	if (!portfolio[preloadActuel][1]) return preloadActuel;
	else {
		var someToPreload = -1;
		for (var i=preloadActuel; i<portfolio.length; i++){
			if (!portfolio[i][1]){
				someToPreload = i;
				break;
			}
		}
		if (someToPreload == -1){
			for (var i=1; i<preloadActuel; i++){
				if (!portfolio[i][1]){
					someToPreload = i;
					break;
				}
			}
		}
		return someToPreload;
	}
}


function SourceImageActuelle(){

	var idImage = portfolio[portfolioActuel][0].split('|')[imageActuelle-1];
	//var imageSource = imagePath+idImage+".jpg";
	var imageSource = imagePath+idImage;
	
	return imageSource;
}

function PreChargeSourceImageActuelle(){
	var idImage = portfolio[preloadActuel][0].split('|')[0];
	//var imageSource = imagePath+idImage+".jpg";
	var imageSource = imagePath+idImage;
	
	return imageSource;
}

function ChargeImage() {
	// chargement de l'image d'un projet 
	resizeContenus();

	// chartement de la source de l'image
	var imageSource = SourceImageActuelle();
	
	PreChargeImage(imageSource, ChargeImageFin);

}

function ChargeImageFin() {
	var imageSource = SourceImageActuelle();
	
	$(imgCenter)[0].src = imageSource;
	resizeBackgrounds();
	$('#backgrounds').css({opacity:0}).fadeTo(1000, 1);
	
	if (!isIE) {
		portfolio[portfolioActuel][1] = true;
		PreChargeImageSuivante();
	}
	
	$('#projets_masque').stop().fadeOut('slow',function(){ 
				if (!isIE) {							 
					$('.projet_previous').fadeIn(700).fadeOut(500, function(){$('.projet_next').fadeIn(700).fadeOut(500); }); 
					
				} else {
					$(this).oneTime(500, function(){ $('.projet_previous').show();});
					$(this).oneTime(700, function(){ $('.projet_next').show();});	
					$(this).oneTime(1500, function(){ $('.projet_next').hide();});		
					$(this).oneTime(1700, function(){ $('.projet_previous').hide();});
								
				}
							
	});

}
function showPreviousProject() {
	
	$(this).oneTime(10, function() {
		portfolioActuel -= 1;
		if (portfolioActuel == 0) portfolioActuel = (portfolio.length-1);
		imageActuelle = 1;
		
		var imageSource = SourceImageActuelle();
		PreChargeImage(imageSource, showPreviousProjectEnd);
  	});
	
}

function showPreviousProjectEnd() {
	var imageSource = SourceImageActuelle();

	var centerLeftIni = $(imgCenter).position().left;
	var centerLeftEnd = $(imgCenter).position().left + $(imgCenter).width();
	var leftLeftIni = $(imgLeft).position().left;
	var leftLeftEnd = $(imgCenter).position().left;
	
	$(imgLeft)[0].src = imageSource;
	
	$(imgCenter).animate({left:centerLeftEnd}, 750);
	$(imgLeft).animate({left:leftLeftEnd}, 750, function(){
		$(imgCenter)[0].src = imageSource;
		$(imgCenter).css({left:centerLeftIni});
		$(imgLeft).css({left:leftLeftIni});
	});
	
	$('#projets_masque').stop().fadeOut('slow');
}

function showNextProject() {
	$(this).oneTime(10, function() {
		portfolioActuel += 1;
		if (portfolioActuel == portfolio.length) portfolioActuel = 1;
		imageActuelle = 1;
	
		var imageSource = SourceImageActuelle();
		PreChargeImage(imageSource, showNextProjectEnd);	
  	});
  	
}

function showNextProjectEnd() {
	var imageSource = SourceImageActuelle();
	
	var centerLeftIni = $(imgCenter).position().left;
	var centerLeftEnd = $(imgCenter).position().left - $(imgCenter).width();
	var rightLeftIni = $(imgRight).position().left;
	var rightLeftEnd = $(imgCenter).position().left;
	
	$(imgRight)[0].src = imageSource;
	
	$(imgCenter).animate({left:centerLeftEnd}, 750);
	$(imgRight).animate({left:rightLeftEnd}, 750, function(){
		$(imgCenter)[0].src = imageSource;
		$(imgCenter).css({left:centerLeftIni});
		$(imgRight).css({left:rightLeftIni});
	});
	
	$('#projets_masque').stop().fadeOut('slow');
}


