function init() {
	window.timeoutOn = true;
  adjustPageDimensions(false)

  var photoList = $('photosList');

	if (photoList != null) {
	  photoList.appear({duration: 0.5});
		$('left').setStyle({display: 'none'})

	  Event.observe($$('#left a').first(), 'click', function (event) {
	    Event.stop(event);
	    movePhotoList(-1);
	  })
	
	  Event.observe($$('#right a').first(), 'click', function (event) {
	    Event.stop(event);
	    movePhotoList(1);
	  })
	
	  Event.observe($$('#photoBox ul').first(), 'mouseover', function (event) {
		  $$('#photoBox ul').first().appear({duration: 0.1});
		  window.timeoutOn = false;
	  })
	
	  Event.observe($$('#photoBox ul').first(), 'mouseout', function (event) {
		  window.timeoutOn = true;
			  window.timeout = setTimeout(function(){ hidePhotoNav(); }, 500);
	  })
	
		Event.observe(document, 'mousemove', function(event) {
		  if (window.timeoutOn)
		  {
			  $$('#photoBox ul').first().appear({duration: 0.1});
			  clearTimeout(window.timeout);
			  window.timeout = setTimeout(function(){ hidePhotoNav(); }, 500);
		  
		  } else {
		  	clearTimeout(window.timeout);
			}
		});
	
	  
	  $$('#photoBox ul').first().appear({duration: 0.3})
	  
    window.thumbEffect = new Effect.Fade($('thumbsListBox'), {duration: 1.0, delay: 1.0, to: 0.0001})
	  
	  Event.observe($('thumbsListBox'), 'mouseover', function (event) {
	    window.thumbEffect.cancel()
	    window.thumbEffect = new Effect.Appear($('thumbsListBox'), {duration: 0.3, to: 1.0})
	  });

	  Event.observe($('thumbsListBox'), 'mouseout', function (event) {
	    window.thumbEffect.cancel()
	    window.thumbEffect = new Effect.Fade($('thumbsListBox'), {duration: 0.3, to: 0.0001})
	  });
	  
	}

  window.currentPhotoIndex = 0;

}


function adjustPageDimensions(firstRun) {
    
  // Trouve la largeur et la hauteur de l'ecran;
  var windowHeight = getWindowHeight();
  var windowWidth = getWindowWidth();

	if (windowHeight <= 700) windowHeight = 600;
	else if ((windowHeight >= 700) && (windowHeight < 800)) windowHeight = 700;	
	else if ((windowHeight >= 800) && (windowHeight < 1000)) windowHeight = 800;	
	else if ((windowHeight >= 1000)) windowHeight = 1000;	

  $('wrapper').setStyle({'width': windowWidth + 'px', 'height': windowHeight + 'px', 'marginTop': -(windowHeight/2) + 'px' });

	if (!firstRun)
	{
		$('header').setStyle({'left': 130 + 'px'});
		$('socialBookmarking').setStyle({'left': (windowWidth-260) + 'px'});
//		$('thumbsListBox').setStyle({'width': (windowWidth-130) + 'px'});
	}

	var location = String(document.location);

  if (location.indexOf('album', 1) >= 0)
  {
			  //$('contentWrapper').setStyle({'width': windowWidth + 'px'});
			
			  if (!firstRun && ($$('#photoBox ul') != '' ))
			  {
				  $$('#photoBox ul').first().setStyle({'bottom': ((windowHeight/2)-70) + 'px' });
				  var photosWidth = 0;
				  
				  $$('#photosList img').each(function (img) {
				    img.setStyle({'height': (windowHeight - 102) + 'px'});
				    photosWidth += img.getWidth() + 25;
				  });

				  $('photosList').setStyle({'width': photosWidth+25 + 'px'});
				  
				  $$('#thumbsListBox ul li').each(function (li) {
				  	Event.observe(li, 'click', function (event) {
				  	  Event.stop(event);
							window.currentPhotoIndex = Number(li.getAttribute('position'));
				  	  movePhotoList(0);
				  	});
				  });
				  
				  movePhotoList(0);
			  }
			  
	  		var thumbsList = $$('#thumbsListBox ul').first();

	  		if (thumbsList.getWidth() > windowWidth) {
				  Event.observe($('thumbsListBox'), 'mouseover', function (event) {
				    var bottomBoundary = 2*windowWidth/10;
				    var topBoundary = 8*windowWidth/10;
				    
				    var left = thumbsList.getStyle('left');
				    var x = Number(left.substring(0, left.indexOf('px')));
				    
				    if (Event.pointerX(event) < bottomBoundary) {
				      window.scrollInterval = new PeriodicalExecuter(function (pe) {
					      if (x < 130) {
					        x += 7;
					        thumbsList.setStyle({'left': x + 'px'})
					      }
					      else {
						      thumbsList.setStyle({'left': 130 + 'px'})
					        pe.stop();
					      }
				      });
				    }
				    else if (Event.pointerX(event) > topBoundary) {

				      window.scrollInterval = new PeriodicalExecuter(function (pe) {
					      if (x > -(thumbsList.getWidth()-windowWidth+15)) {
					        x -= 7;
					        thumbsList.setStyle({'left': x + 'px'})
					      }
					      else {
						      thumbsList.setStyle({'left': -(thumbsList.getWidth()-windowWidth+15) + 'px'})
					        pe.stop();
					      }
				      });
				    }
				    else {
					    window.scrollInterval.stop();
				    }
				  });

				  Event.observe($('thumbsListBox'), 'mouseout', function (event) {
				    window.scrollInterval.stop();
				  });

	  		}
	  		else
	  		{
	  			$$('#thumbsListBox ul').first().setStyle({'left': 130 + 'px'})
	  		}
	}
	else
	{
      resizeBackground(false);
	}
}


function movePhotoList(direction) {

	window.currentPhotoIndex += direction;
	
	var albumCount = $$('#photosList div.photoBox').length;

	if (window.currentPhotoIndex < 1) {
	  window.currentPhotoIndex = 0;
		$('left').setStyle({display: 'none'})
		$('right').setStyle({display: 'block'})
	
	} else if (window.currentPhotoIndex >= albumCount-1) {
		window.currentPhotoIndex = albumCount-1;
		$('left').setStyle({display: 'block'})
		$('right').setStyle({display: 'none'})
	} else {
		$('left').setStyle({display: 'block'})
		$('right').setStyle({display: 'block'})
	}
	
	var xPos = 0;
	var index = 0;
				
	if (window.currentPhotoIndex > 0) {
		$$('#photosList div.photoBox').each(function (img) {
			xPos += img.getWidth() + 10;
			index++;
			if (index >= window.currentPhotoIndex) 
				throw $break;
		});
	}

	if (window.scrollMove) {
		window.scrollMove.cancel();
	}
  window.scrollMove = new Effect.Move($('photosList'), { x: -xPos, y: 0, mode: 'absolute', duration: 0.5, transition: Effect.Transitions.Cubic.easeOut });
}


function hidePhotoNav() {
  $$('#photoBox ul').first().fade({duration: 0.2});
} 


function hidePhotoDescription(descBox) {
  descBox.fade({duration: 0.2});
} 



function resizeBackground(resizeOnly, description) {
    
  // Trouve la largeur et la hauteur de l'ecran;
  var windowHeight = getWindowHeight();
  var windowWidth = getWindowWidth();
  
  // Trouve la hauteur, la largeur et le ratio de l'image originalement
  var image = $$('#homeImage img')[0];

	if (image)
	{
	  var imageHeight = image.height;
	  var imageWidth  = image.width;
	  var imageRatio = imageHeight/imageWidth;
	  
	  // Resize et repositionne l'image en conséquence
	  
	  var imageBox = $('homeImage');
	
	  if ( windowWidth > windowHeight ) {
	    imageHeight = windowWidth*imageRatio;
	    imageWidth = windowWidth;
	    if (imageHeight < windowHeight) {
	      imageHeight = windowHeight;
	      imageWidth = windowHeight * (1/imageRatio);
	    }
	  
	  } else {
	    imageWidth = windowHeight * (1/imageRatio);
	    imageHeight = windowHeight;
	    if (imageWidth < windowWidth) {
	      imageWidth = windowWidth;
	      imageHeight = windowWidth * imageRatio;
	    }
	  }
	    
	  image.setStyle({width: imageWidth+'px', height: imageHeight+'px'});
	  image.parentNode.setStyle({width: imageWidth+'px', height: imageHeight+'px'});
	  
	  imageBox.setStyle({top: '33%', left: '50%',
	                     marginLeft: -(imageWidth/2)+'px',
	                     marginTop: -(imageHeight/3)+'px',
	                     visibility: 'visible'});
	
	  if ( !resizeOnly ) {
	    Event.observe(image, 'load', function () { 
	    	imageBox.appear({ duration: 0.4 });
	    })
	  }
	}
}



/** *** TOOLS ***** */

function getWindowHeight() {

    var windowHeight = 0;

    if (typeof(window.innerHeight)=='number') {
        windowHeight = window.innerHeight;
    } else {
        if (document.documentElement && document.documentElement.clientHeight) {
            windowHeight = document.documentElement.clientHeight;
        } else {
            if (document.body && document.body.clientHeight) {
                windowHeight = document.body.clientHeight;
            }
        }
    }
    
    return windowHeight;
}

function getWindowWidth() {

    var windowWidth = 0;

    if (typeof(window.innerWidth)=='number') {
        windowWidth = window.innerWidth;
    } else {
        if (document.documentElement && document.documentElement.clientWidth) {
            windowWidth = document.documentElement.clientWidth;
        } else {
            if (document.body && document.body.clientWidth) {
                windowWidth = document.body.clientWidth;
            }
        }
    }
    
    return windowWidth;
}

Event.observe(window, 'load', function () { init() });
Event.observe(window, 'resize', function () { adjustPageDimensions(false) });

