function init() {
	window.timeoutOn = true;
  adjustPageDimensions()
  $('photosList').appear({duration: 0.5});
	$('left').setStyle({display: 'none'})

	var albumCount = $$('#photosList div.photoBox').length;

  window.currentPhotoIndex = 0;
  
  $$('#photoCounter #current').first().innerHTML = 1;
  $$('#photoCounter #total').first().innerHTML = albumCount;
 
  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})
  $('photoContainer').setStyle({'background': 'none'})
}


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

	if (windowHeight > 700) windowHeight = 700;	

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

  $$('#photoBox ul').first().setStyle({'bottom': ((windowHeight/2)-20) + 'px' });
  
  var photosWidth = 0;
  $$('#photosList img').each(function (img) {
    img.setStyle({'height': (windowHeight - 50) + 'px'});
    photosWidth += img.getWidth() + 25;
  });

  $('photosList').setStyle({'width': photosWidth+25 + 'px'});
  
  movePhotoList(0);
}


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() + 25;
			index++;
			if (index >= window.currentPhotoIndex) 
				throw $break;
		});
	}

  $$('#photoCounter #current').first().innerHTML = window.currentPhotoIndex+1;
 
  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});
} 


/** *** 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() });
