// JavaScript Document
var pageWidth = 0;
var pageHeight = 0;
var nbOfPages = 0;
var nbOfProjects = 0;
var nbOfMenuItems = 0;
var nbOfProjectsTeasers = 0;
var nbOfProjectsMenuItems = 0;
var currentPage = 0;
var currentProject = 0;
var minPageWidth = 900;
var minPageHeight = 680;
var menuTop = 100;
var menuTopProjets = 100;
var projectTeaserHeight = 126;
var projectTeaserWidth = 180;
var projectsDisplayed = false;
var pagesContainerMarginTop = 0;
var projectsContainerTop = 0;
var currentPageHeight = 0;
var changePageSpeed = 900;
var tooltipSpeed = 300;
var screensMarginLeft = 0;
var pageContentHeight = 350;
var dragScreenshots = false;
var mouseOverScreen = false;
var websiteBaseUrl = 'http://www.philippeguetault.fr/';
var websiteBaseTitle = 'Philippe GUETAULT | Webdesign - ';
var nbOfImages = 0;
var imgCounter = 0;


$(document).ready(function(){
	
	nbOfPages = $('#pg_pages_container .pg_page').length;	
	nbOfProjects = $('#pg_projects_container .pg_page').length;	
	
	nbOfImages = $('img').length;
	
	/*  On numérote des elements du menu
	-------------------------------------------------------------------------------------*/
	$('#menu ul li a').each(function(){
		nbOfMenuItems ++;
		$(this).attr('name', nbOfMenuItems);
	});
	/*  On numérote des teasers de projets
	-------------------------------------------------------------------------------------*/
	$('#projects_teasers_list li a').each(function(){
		nbOfProjectsTeasers ++;
		$(this).attr('name', nbOfProjectsTeasers);
	});
	/*  On numérote des elements de menu projects
	-------------------------------------------------------------------------------------*/
	$('#menu_projects li a').each(function(){			
		theAnchor = $('#pg_projects_container .pg_page').eq(nbOfProjectsMenuItems).attr('id');
		theTitle = $('#pg_projects_container .pg_page').eq(nbOfProjectsMenuItems).find('h1').html();
		thePageNumber = nbOfProjectsMenuItems + 1;
		$('#pg_projects_container .pg_page').eq(nbOfProjectsMenuItems).find('.pg_page_number span').html(thePageNumber);
		nbOfProjectsMenuItems ++;	
		$(this).attr('name', nbOfProjectsMenuItems);
		$(this).attr('href', '#'+theAnchor);
		$(this).attr('title', theTitle);
		$(this).html(thePageNumber);
	});
	/*  On resize le site + place le tooltip
	-------------------------------------------------------------------------------------*/	
	resizeWebsite();
	defineNewUrl(window.location, false);
	/*
	var limitLoad = nbOfImages-1;
	loadOne(0);
	
	function loadOne(i) {
		if ($('img').eq(i).length>0) {
			$('img').eq(i).load(function(){
				i++;
				$('#img_loader div').html(i+' /'+nbOfImages);
				loadTwo(i);				
			});
		} else {
			$('#img_loader div').html();
			$('#img_loader').fadeOut('slow');					
		}
	}
	function loadTwo(j) {
		if ($('img').eq(j).length>0) {
			$('img').eq(j).load(function(){
				j++;
				$('#img_loader div').html(j+' /'+nbOfImages);
				loadOne(j);				
			});
		} else {
			$('#img_loader div').html();
			$('#img_loader').fadeOut('slow');					
		}
	}*/
	
	
	/*
	if ( $.browser.msie ) {
		//imgLimit = nbOfImages - 10;
		$('#img_loader').delay(5000).fadeOut('slow');
	} else {
		imgLimit = nbOfImages - 3;

		var finished = false;
		$('img').each(function(){
			$(this).load(function(){
				imgCounter ++;
				//$('#accueil .pg_content_body').html(nbOfImages+' -> '+imgCounter);
				if (imgCounter > imgLimit) {
					if (finished) { } else {
						finished = true;
						$('#img_loader div').html();
						$('#img_loader').fadeOut('slow');
					}
				} 
			});			
		});	
	}
	*/
	
	$('#img_loader').delay(6000).fadeOut('slow');
	
	$(window).resize(function() {
		resizeWebsite();
	});
	
	$('.pg_content p a[target!="_blank"]').click(function(){
		//alert('click');
		var theHref = $(this).attr('href');
		if (theHref.indexOf("mailto") >= 0) {
			return true;
		} else {
			$('#menu ul li a').each(function(){
				itemMenu = $(this);
				itemMenuHref = itemMenu.attr('href');
				if (theHref.indexOf(itemMenuHref) >= 0) {
					theItemMenu = itemMenu;	
				}
			})
			itemNb = theItemMenu.attr('name');
			changePage(itemNb, theItemMenu, 'page', true);		
			return false;			
		}
	});
	
		
	/*  Survol des elements du menu
	-------------------------------------------------------------------------------------*/
	$('#menu ul li a').hover(function(e){
		overNavItem($(this), e);		
	});
	$('#pg-logo a').hover(function(e){
		overNavItem($(this), e);		
	});
	
	/*  Replacement du tooltip quand on ne survole plus le menu
	-------------------------------------------------------------------------------------*/
	$('#menu').hover(function(e){
	}, function(){
		var currentNavItem = $('#menu ul li a[name='+currentPage+']');
		if (currentNavItem.length < 1) { // si on est sur la 1ere page
			currentNavItem = $('#pg-logo a'); 
			newTooltipLeft = currentNavItem.offset().left;
		} else {
			newTooltipLeft = currentNavItem.offset().left - 10;
		}
		$('#pg_tooltip').animate({left: newTooltipLeft}, tooltipSpeed);
		$('#pg_tooltip').find('.label').html(currentNavItem.attr('title'));
		$('#pg_tooltip').find('.number').html(currentNavItem.attr('name'));	
	});
	/*  Replacement du tooltip quand on ne survole plus le menu projets
	-------------------------------------------------------------------------------------*/
	$('#menu_projects').hover(function(e){
	}, function(){
		if (projectsDisplayed) {
			var currentNavItem = $('#menu_projects ul li a[name='+currentProject+']');
			newTooltipLeft = currentNavItem.offset().left;
			$('#pg_tooltip').animate({left: newTooltipLeft}, tooltipSpeed);
			$('#pg_tooltip').find('.label').html(currentNavItem.attr('title'));
			$('#pg_tooltip').find('.number').html(currentNavItem.attr('name'));	
		}
	});
	
	/*  click des elements du menu
	-------------------------------------------------------------------------------------*/
	$('#menu ul li a').click(function(e){
		var itemNb = $(this).attr('name');
		changePage(itemNb, $(this), 'page', true);
		return false;
	});
	$('#pg-logo a').click(function(){
		if ($(this).hasClass('frontpage')) {
			changePage(0, $(this), 'page', true);
			return false;	
		}
	});	
	
	/*  Survol des teasers de projets
	-------------------------------------------------------------------------------------*/
	$('#projects_teasers_list li a').hover(function(){
		teaserMove = 0 - projectTeaserHeight;
		$(this).stop(true, true).animate({top : teaserMove}, 300, 'easeOutExpo');
	}, function(){
		$(this).stop(true, true).animate({top : 0}, 300, 'easeInExpo');
	});
	/*  Survol des elements du menu projets
	-------------------------------------------------------------------------------------*/
	$('#menu_projects ul li a').hover(function(e){
		overNavItem($(this), e);		
	});
	
	/*  Click des teasers de projets
	-------------------------------------------------------------------------------------*/
	$('#projects_teasers_list li a').click(function(e){
		showProject($(this), $(this).attr('name'),e, true);
		return false;
	});
	
	/*  Click sur retour aux projets
	-------------------------------------------------------------------------------------*/
	$('.back_to_projects').each(function(){
		$(this).click(function(){
			backToProjects();
			return false;
		});
	});
	/*  Click sur projet suivant
	-------------------------------------------------------------------------------------*/
	$('.next_project').each(function(){		
		$(this).click(function(){
			nextProject = currentProject+1;
			var currentNavItem = $('#menu_projects ul li a[name='+nextProject+']');
			changePage(nextProject, currentNavItem, 'project', true);
			return false;
		});
	});
	$('.prev_project').each(function(){		
		$(this).click(function(){
			prevProject = currentProject-1;
			var currentNavItem = $('#menu_projects ul li a[name='+prevProject+']');
			changePage(prevProject, currentNavItem, 'project', true);
			return false;
		});
	});
	
	
	
	/*  click des elements du menu projets
	-------------------------------------------------------------------------------------*/
	$('#menu_projects ul li a').click(function(e){
		var itemNb = $(this).attr('name');
		changePage(itemNb, $(this), 'project', true);
		return false;
	});
	
	
	/*  Slider projet > prev/next
	-------------------------------------------------------------------------------------*/
	$('.pg_screens_nav a').click(function(){
			slideScreenshot($(this));
			return false;
	});
	
	/*  Drag des screenshots
	-------------------------------------------------------------------------------------*/
	$('.project_screens_list').each(function(){
		
		var dragItem = $(this);
		var dragContainer = dragItem.parent();
		
		var dragTop = 0;
		var dragBottom = 0;
		
		var dragContainerLeftString = dragContainer.css('left').replace("px", "");
		var dragContainerLeft = parseInt(dragContainerLeftString, 10);
		var dragItemWidth = dragItem.width() ;
		var dragLeft = 0 - dragItem.width() + dragContainerLeft + 500;
		var dragRight = dragContainerLeft;

		dragItem.draggable({ 
			axis : "x",
			containment: [dragLeft, dragTop, dragRight, dragBottom] 				
		});
		
	});
	
	/*  Overlayer des screenshots
	-------------------------------------------------------------------------------------*/
	$('.pg_project_screens').hover(function(){
		$(this).find('.screens_overlayer').fadeOut('normal');
		mouseOverScreen = true;
	}, function(){
		mouseOverScreen = false;
		if (!dragScreenshots) {
			$(this).parent().find('.screens_overlayer').stop(true,true).fadeIn('slow');
		}
	});
	$('.pg_project_screens').mousedown(function(){
		dragScreenshots = true;
	});
	$('html').mouseup(function() {
		if (dragScreenshots && !mouseOverScreen) {
			currentId = findCurrentProjectId();			
			$('#'+currentId).find('.screens_overlayer').stop(true,true).fadeIn('slow');
			dragScreenshots = false;
			placeScreensOnMouseUp(currentId);
		} else if (dragScreenshots) {
			dragScreenshots = false;
			currentId = findCurrentProjectId();	
			placeScreensOnMouseUp(currentId);
		}
	});
	
	
	/*  survol des images (page a propos)
	-------------------------------------------------------------------------------------- */
	$('.page_screens_list li img').hover(function(){
		$(this).stop(true, true).animate({'opacity' : 1}, 300);
	}, function(){
		$(this).stop(true, true).animate({'opacity' : 0.5}, 300);
	});
	
	/* défilement des images (page à propos)
	-------------------------------------------------------------------------------------- */
	$('.pg_page_screens').mousemove(function(e) {			
		var posX = e.pageX - $('.pg_page_screens').offset().left;
		var screensListNewMarginLeft = (0 - posX) * 2.5 + posX;
		$('.page_screens_list').css('margin-left', screensListNewMarginLeft);		
	});
	
	
});


/* -----------------------------------------------------------
		Functions
--------------------------------------------------------------*/

function resizeWebsite() {
	pageWidth = $(window).width();
	pageHeight = $(window).height();
	
	if(pageWidth < minPageWidth) {
		pageWidth = minPageWidth;
	}
	if (pageHeight < minPageHeight) {
		pageHeight = minPageHeight;
	}
	currentPageHeight = $('#pg_pages_container .pg_page').eq(currentPage).height() + 300;
	/*if (currentPageHeight > pageHeight) {
		pageHeight = currentPageHeight;	
	}*/
	
	$('body').css('width',pageWidth);
	$('.pg_page').css('width',pageWidth);
	
	
	//taille et marge des conteneurs de pages & de projets	
		$('#pg_pages_container').css('width', pageWidth*nbOfPages);
		$('#pg_projects_container').css('width', pageWidth*nbOfProjects);	
		pagescontainerMargin = 0 - (currentPage*pageWidth);
		projectscontainerLeft = 0 - ((currentProject-1)*pageWidth);
		$('#pg_pages_container').css('margin-left',pagescontainerMargin);
		$('#pg_projects_container').css('left',projectscontainerLeft);
		titleAndMenuLeft = -1 * projectscontainerLeft;
		$('#pg_projects_list_title').css('left', titleAndMenuLeft );
		$('#menu_projects').css('left', titleAndMenuLeft );
	
	// Placement du bloc page_content
		pageContentMarginTop = pageHeight - pageContentHeight - 300;
		$('.pg_page_content').css('margin-top', pageContentMarginTop);
		$('.pg_page_screens').each(function(){
			newLeft = $(this).parent().width() + 30;
			$(this).css('left', newLeft);	
		});
		
	// gestion hauteur de page 
		$('#pg_main_container').css('height',pageHeight);
		$('.pg_radial_gradient').css('height',pageHeight);
		$('.pg_losange').css('height',pageHeight);
		$('.pg_stripes').css('height',pageHeight);	
		$('.pg_page').css('height',pageHeight);
		$('#img_loader').css('height', pageHeight);
		
	// page projets visible ou non	
		if (projectsDisplayed) {
			projectsContainerTop = 0;
			$('#pg_projects_container').css('top', projectsContainerTop);
			$('#pg_pages_container').css('margin-top', pageHeight);
		} else {
			projectsContainerTop = pageHeight;
			$('#pg_projects_container').css('top', projectsContainerTop);
		}
		
	
}


/* -----------------------------------------------------------
		overNavItem
--------------------------------------------------------------*/
function overNavItem(navItem, e){
		var menuItem = navItem;
		var tooltip = $('#pg_tooltip');
		var menuItemTitle = menuItem.attr('title'); 
		var menuItemNb = menuItem.attr('name');		
		var newX = navItem.offset().left;
		tooltip.stop(true, true).animate({left: newX}, tooltipSpeed);
		tooltip.find('.label').html(menuItemTitle);
		tooltip.find('.number').html(menuItemNb);	
}

/* -----------------------------------------------------------
		changePage
--------------------------------------------------------------*/
function changePage(nb, navItem, type, anim) {
	resizeWebsite();
	// gestion de l'url : -------------------------------------------------------------------
		var currentUrl = window.location;
		var currentUrlLength = currentUrl.length;
		var hashPosition = location.href.indexOf("#");
		var newPageName = '';
		
		if (hashPosition > 0) {
			var baseUrl = jQuery.trim(currentUrl).substring(0, hashPosition);
			var textAfterBaseUrl = jQuery.trim(currentUrl).substring(hashPosition, currentUrlLength);
		} else {
			baseUrl = currentUrl;	
		}	
		
		if (type == 'project') {
			indiceNb = currentProject - 1;
			$('#pg_projects_container .pg_page').eq(indiceNb).find('.back_to_projects').css('display', 'none');
			$('#pg_projects_container .pg_page').eq(indiceNb).find('.prev_project').css('display', 'none');
			$('#pg_projects_container .pg_page').eq(indiceNb).find('.next_project').css('display', 'none');
			currentProject = parseInt(nb, 10);
			$('#menu_projects ul li a').removeClass('active');
			navItem.addClass('active')
			nb --;
			newPageName = $('#pg_projects_container .pg_page').eq(nb).attr('id');
		} else if (type=='page') {
			currentPage = nb;
			newPageName = $('#pg_pages_container .pg_page').eq(nb).attr('id');
			$('#menu ul li a').removeClass('active');
			navItem.addClass('active');
		}
		
		newPageName = newPageName.replace(/^.*#/, '');	
		var newUrl = baseUrl+'#'+newPageName;	
		var newMarginLeft = 0 - (nb*pageWidth);	
		var newTitle = websiteBaseTitle+' '+newPageName.replace('-', ' ');	
		
	// Projet : ---------------------------------------------------------------------------
		if (type == 'project') {
			titleAndMenuLeft = -1 * newMarginLeft;
			if (anim) {
				$('#pg_projects_list_title').stop(true, true).animate({top : 0, left:titleAndMenuLeft}, changePageSpeed, 'easeOutExpo');
				$('#menu_projects').stop(true, true).animate({top : 0, left:titleAndMenuLeft}, changePageSpeed, 'easeOutExpo');			
				$('#pg_projects_container').stop(true, true).animate({'left': newMarginLeft}, changePageSpeed, 'easeOutExpo', function(){ 
					window.location.replace(newUrl); 
					document.title = newTitle;
					$('#pg_projects_container .pg_page').eq(nb).find('.back_to_projects').fadeIn('slow');
					$('#projects_subnav').fadeIn('slow');
					if (currentProject < nbOfProjects) {
						$('#projects_subnav .next_project').css('display', 'block');
					} else {
						$('#projects_subnav .next_project').css('display', 'none');
					}
					if (currentProject > 1) {
						$('#projects_subnav .prev_project').css('display', 'block');
					} else {
						$('#projects_subnav .prev_project').css('display', 'none');
					}
					//alert(currentProject);
				});
			} else {
				$('#pg_projects_list_title').css({'top' : 0, 'left' : titleAndMenuLeft});
				$('#menu_projects').css({'top' : 0, 'left' : titleAndMenuLeft});
				$('#pg_projects_container').css('left' , newMarginLeft);
				window.location.replace(newUrl); 
				document.title = newTitle;
				$('#pg_projects_container .pg_page').eq(nb).find('.back_to_projects').fadeIn('slow');	
				$('#projects_subnav').css('display', 'block');
				if (currentProject < nbOfProjects) {
					$('#projects_subnav .next_project').css('display', 'block');
				} else {
					$('#projects_subnav .next_project').css('display', 'none');
				}
				if (currentProject > 1) {
					$('#projects_subnav .prev_project').css('display', 'block');
				} else {
					$('#projects_subnav .prev_project').css('display', 'none');
				}
							
			}
		} else if (type=='page') {
	// Page : ---------------------------------------------------------------------------
			if (anim) {
				$('#pg_pages_container').stop(true, true).animate({'margin-left': newMarginLeft}, changePageSpeed, 'easeOutExpo', function(){ 
					window.location.replace(newUrl); 
					document.title = newTitle;	
				});
			} else {
				$('#pg_pages_container').css('margin-left', newMarginLeft);
				window.location.replace(newUrl);
				document.title = newTitle;
			}
			
		}
}

/* -----------------------------------------------------------
		showProject
--------------------------------------------------------------*/
function showProject(itemProject, nb, e, anim) {
	currentProject = parseInt(nb, 10);
	indiceNb = nb - 1;
	var newLeft = 0 - (indiceNb*pageWidth);
	var newLeftTitleMenu = indiceNb*pageWidth;
	
	// gestion de l'url : -------------------------------------------------------------------
		var currentUrl = window.location;
		var currentUrlLength = currentUrl.length;
		var hashPosition = location.href.indexOf("#");
		if (hashPosition > 0) {
			var baseUrl = jQuery.trim(currentUrl).substring(0, hashPosition);
			var textAfterBaseUrl = jQuery.trim(currentUrl).substring(hashPosition, currentUrlLength);
		} else {
			baseUrl = currentUrl;	
		}	
		var newPageName = itemProject.attr('href');
		newPageName = newPageName.replace(/^.*#/, '');	
		var newUrl = baseUrl+'#'+newPageName;
		
		var itemProjectLink = $('#menu_projects ul li a[name='+nb+']');
		$('#menu_projects ul li a').removeClass('active');
		itemProjectLink.addClass('active');
	
	
	// disparition pages "standard" + apparition projets : ------------------------------------
		pagesContainerMarginTop = 0 - pageHeight;
		$('#pg_projects_container').css('display', 'block');
		
		
		// le if /else ci-dessous est commenté car erreur d'affichage titre + menu... à checker ---------------------
		if (anim) {
			$('#pg_pages_container').stop(true, true).animate({'margin-top': pagesContainerMarginTop}, changePageSpeed, 'easeOutExpo', function(){window.location.replace(newUrl);});	
			$('#menu').stop(true, true).animate({'margin-top': pagesContainerMarginTop}, changePageSpeed, 'easeOutExpo', function(){$(this).css('display', 'none')});
			$('#pg-logo').stop(true, true).animate({'margin-top': pagesContainerMarginTop}, changePageSpeed, 'easeOutExpo', function(){$(this).css('display', 'none')});
			
			$('#pg_projects_list_title').stop(true, true).animate({top : 0, left:newLeftTitleMenu}, changePageSpeed, 'easeOutExpo');
			$('#menu_projects').stop(true, true).animate({top : 0, left:newLeftTitleMenu}, changePageSpeed, 'easeOutExpo');
			$('#pg_projects_container').stop(true, true).animate({top : 0, left:newLeft}, changePageSpeed, 'easeOutExpo', function(){ 
				projectsDisplayed = true; 
				newTooltipLeft = itemProjectLink.offset().left;
				$('#pg_tooltip').animate({top: menuTopProjets, left: newTooltipLeft}, tooltipSpeed);
				$('#pg_tooltip').find('.label').html(itemProjectLink.attr('title'));
				$('#pg_tooltip').find('.number').html(itemProjectLink.attr('name'));
				$('#pg_projects_container .pg_page').eq(indiceNb).find('.back_to_projects').fadeIn('slow');
				$('#projects_subnav').fadeIn('slow');
				if (currentProject < nbOfProjects) {
					$('#projects_subnav .next_project').css('display', 'block');
				} else {
					$('#projects_subnav .next_project').css('display', 'none');
				}
				if (currentProject > 1) {
					$('#projects_subnav .prev_project').css('display', 'block');
				} else {
					$('#projects_subnav .prev_project').css('display', 'none');
				}
			});
		} else {
			$('#pg_pages_container').css('margin-top', pagesContainerMarginTop); 
			
			
			$('#menu').css('margin-top', pagesContainerMarginTop);
			$('#menu').css('display', 'none');
			$('#pg-logo').css('margin-top', pagesContainerMarginTop);
			$('#pg-logo').css('display', 'none');			
			$('#pg_projects_list_title').css({'top' : 0, 'left' : newLeftTitleMenu});
			$('#menu_projects').css({'top' : 0, 'left' : newLeftTitleMenu});
			$('#pg_projects_container').css({'top' : 0, 'left' : newLeft}); 
				projectsDisplayed = true; 
				newTooltipLeft = itemProjectLink.offset().left;
				$('#pg_tooltip').css({'top' : menuTopProjets , 'left' : newTooltipLeft});
				$('#pg_tooltip').find('.label').html(itemProjectLink.attr('title'));
				$('#pg_tooltip').find('.number').html(itemProjectLink.attr('name'));
				$('#pg_projects_container .pg_page').eq(indiceNb).find('.back_to_projects').fadeIn('slow', function(){window.location.replace(newUrl);});		
				$('#projects_subnav').fadeIn('slow');
				if (currentProject < nbOfProjects) {
					$('#projects_subnav .next_project').css('display', 'block');
				} else {
					$('#projects_subnav .next_project').css('display', 'none');
				}
				if (currentProject > 1) {
					$('#projects_subnav .prev_project').css('display', 'block');
				} else {
					$('#projects_subnav .prev_project').css('display', 'none');
				}
		}
}


/* -----------------------------------------------------------
		Retour à la liste des projet
--------------------------------------------------------------*/

function backToProjects() {
	// centrage des menus
		marginMenu = (pageWidth/2) - ($('#menu ul').width()/2) + 5;
		$('#menu ul').css('margin-left',marginMenu);		
		$('#menu_projects').css('width', pageWidth);
		
	// gestion de l'url : -------------------------------------------------------------------
		var currentUrl = window.location;
		var currentUrlLength = currentUrl.length;
		var hashPosition = location.href.indexOf("#");
		if (hashPosition > 0) {
			var baseUrl = jQuery.trim(currentUrl).substring(0, hashPosition);
			var textAfterBaseUrl = jQuery.trim(currentUrl).substring(hashPosition, currentUrlLength);
		} else {
			baseUrl = currentUrl;	
		}	
		var newPageName = $('#pg_pages_container .pg_page').eq(currentPage).attr('id');
		newPageName = newPageName.replace(/^.*#/, '');	
		var newUrl = baseUrl+'#'+newPageName;
		
		
		var itemLink = $('#menu ul li a[name='+currentPage+']');
		$('#menu ul li a').removeClass('active');
		itemLink.addClass('active');
		
		
		$('#menu').css('display','block');
		$('#pg-logo').css('display','block');
		$('#pg_pages_container').stop(true, true).animate({'margin-top': 0}, changePageSpeed, 'easeOutExpo', function(){window.location.replace(newUrl);});	
		$('#menu').stop(true, true).animate({'margin-top': 0}, changePageSpeed, 'easeOutExpo');
		$('#pg-logo').stop(true, true).animate({'margin-top': 0}, changePageSpeed, 'easeOutExpo');
		
		projectsContainerTop = pageHeight;
		indiceNb = currentProject - 1;
		
		$('#pg_projects_container .pg_page').eq(indiceNb).find('.back_to_projects').css('display', 'none');
		$('#pg_projects_container .pg_page').eq(indiceNb).find('.prev_project').css('display', 'none');
		$('#pg_projects_container .pg_page').eq(indiceNb).find('.next_project').css('display', 'none');
		$('#projects_subnav').css('display', 'none');
		
		$('#pg_projects_container').stop(true, true).animate({top : projectsContainerTop}, changePageSpeed, 'easeOutExpo', function(){ 
			projectsDisplayed = false; 
			newTooltipLeft = itemLink.offset().left - 30;
			$('#pg_tooltip').animate({top: menuTop, left: newTooltipLeft}, tooltipSpeed);
			$('#pg_tooltip').find('.label').html(itemLink.attr('title'));
			$('#pg_tooltip').find('.number').html(itemLink.attr('name'));
			$('#pg_projects_container').css('display', 'none');	
			
			
		});
}


/* ----------------------------------------------------------------------------------------------------------
		Si l'url contient une ancre lors du chargement de la page, on positionne l'ensemble des elements
------------------------------------------------------------------------------------------------------------*/

function defineNewUrl(url, anim) {
	
	// gestion de l'url : -------------------------------------------------------------------
		var currentUrl = window.location; //url;
		var currentUrlLength = currentUrl.length;
		var hashPosition = location.href.indexOf("#");
		
	// si on trouve un "#" (sinon pas d'action) ---------------------------------------------
		if (hashPosition > 0) {
			var baseUrl = jQuery.trim(currentUrl).substring(0, hashPosition);			
			var textAfterBaseUrl = jQuery.trim(currentUrl).substring(hashPosition, currentUrlLength);
			var hashPlusOne = hashPosition +1;
			var textAfterHash = jQuery.trim(currentUrl).substring(hashPlusOne, currentUrlLength);
			//alert('baseUrl = '+baseUrl+' --- textAfterBaseUrl = '+textAfterBaseUrl+' --- hashPlusOne = '+hashPlusOne+' ---textAfterHash = '+textAfterHash);
			
	//on teste s'il s'agit d'une page ou d'un projet : ---------------------------------------
				if ($('#pg_pages_container').find('#'+textAfterHash).length > 0 ) {
					var menuItem = $('#menu ul li a[href="#'+textAfterHash+'"]');
					var menuItemNb = menuItem.attr('name');
					testString = menuItemNb + 5;
	// si on ne trouve pas l'element dnas le menu, c est qu on pointe sur la page d accueil --
					if (menuItem.length < 1) {
						changePage(0, $('#pg-logo a'), 'page', anim);
					} else {
						changePage(menuItemNb, menuItem, 'page', anim);
					}
	// gestion du tooltip : -------------------------------------------------------------------
					var currentNavItem = $('#menu ul li a[name='+currentPage+']');
					if (currentNavItem.length < 1) { // si on est sur la 1ere page
						currentNavItem = $('#pg-logo a'); 
						newTooltipLeft = currentNavItem.offset().left +50;
					} else {
						newTooltipLeft = currentNavItem.offset().left - 30;
					}
					$('#pg_tooltip').animate({left: newTooltipLeft}, tooltipSpeed);
					$('#pg_tooltip').find('.label').html(currentNavItem.attr('title'));
					$('#pg_tooltip').find('.number').html(currentNavItem.attr('name'));																
					
				} else if ($('#pg_projects_container').find('#'+textAfterHash).length > 0 ) {
	// il s'agit d'un projet : -----------------------------------------------------------------			
					var menuItem = $('#menu ul li a[href="#projets"]');
					var menuItemNb = menuItem.attr('name');
					changePage(menuItemNb, menuItem, 'page', anim);					
					
					var menuProjetItem = $('#projects_teasers_list a[href="#'+textAfterHash+'"]');
					var menuProjetItemName = menuProjetItem.attr('name');
					showProject(menuProjetItem, menuProjetItemName, '', anim);
				}
		} 
}

/* ------------------------------------------------------------------
		recherche de l'id du block projet en cours de visualisation
--------------------------------------------------------------------*/
function findCurrentProjectId(){
	currentIdHashed = $('#menu_projects ul li a[name='+currentProject+']').attr('href');	
	currentId = jQuery.trim(currentIdHashed).substring(1, currentIdHashed.length);
	return currentId;
}

/* -------------------------------------------------------------------
		Après le dragg des screenshots, on les replace proprement
--------------------------------------------------------------------*/
function placeScreensOnMouseUp(currentId) {
	var theScreensList = $('#'+currentId).find('.project_screens_list');
	var lastLeftValue = theScreensList.css('left').replace("px", "");
	var lastLeftValueNb = parseInt(lastLeftValue, 10);

	var modulo = (-1 *lastLeftValueNb) % 500;
	var delta = 500 - modulo;
	if (delta > 250) {
		newLeft = lastLeftValueNb + modulo;
	} else {
		newLeft = lastLeftValueNb - delta;
	}
	theScreensList.stop(true, true).animate({'left' : newLeft}, 1000,'easeOutExpo');
}






