// ===================================================================
// Project:		Tvacances
// Version:		1.0
// Last change:	26/05/08
// Author:	 	Xuan Thanh PHAM, Emmanuel Sammut
// Primary use:	Diaporama
// ===================================================================

var imageBegin = 1;
var imageList = new Array();
var consWidth = 227;
var consHeight = 168;

// navigation of image
function displayDiapo(direction, numberImgToDisplay, imageList){
  // defined counter
    var countMaxImage = imageList.length - 1;
    imageBegin += direction;
  // test position
  if (imageBegin > countMaxImage) {
        imageBegin = 1;
    } else if (imageBegin < 1) {
    imageBegin = countMaxImage;
    }
    // call function
    changeImage(imageList[imageBegin]);
  // redefined counter
    countMaxImage = Math.min(countMaxImage, imageBegin + numberImgToDisplay);
    var nameVignettes = "";
  // display thumbunails
    for (var i = imageBegin; i < imageBegin + numberImgToDisplay; i++) {
        var thumbunailsIndex = i - imageBegin + 1;
        var thumbunailsName = "diaporamaThumbunails-" + thumbunailsIndex;
        var imageIndex = i;
    // test position
    if (i > countMaxImage) {
      imageIndex = i - countMaxImage;
    }
    // set thumbunails
        if(document.getElementById(thumbunailsName)!=null){
    document.getElementById(thumbunailsName).src = imageList[imageIndex];
    document.getElementById(thumbunailsName).style.display = 'inline';
     document.getElementById(thumbunailsName).onclick = new Function('changeImage(\'' + imageList[imageIndex] + '\');');
        }
    }
}

// change image grand format
function changeImage(filename) {
  // init varibles
  var mainImage = document.getElementById("bigImage");
  mainImage.src = filename;
  var imageSource = document.createElement('img');
  imageSource.src = filename;
  // create ratio
  var width = imageSource.width;
  var height = imageSource.height;

    //add minmin
    if(width==0)
    width=227;
    if(height==0)
    height=168;
    var ratio = width / height ;
    var constraintsRatio = consWidth / consHeight;
  // test format image
    if (ratio > constraintsRatio) {
        // width
        if (width > consWidth) {
          width = consWidth;
      height = consWidth / ratio;
        }
    } else {
        // height
        if (height > consHeight) {
            height = consHeight;
             width = consHeight * ratio;
        }
    }
    // Redefined image

  mainImage.width = width;
  mainImage.height = height;

  // call function
  imagePosition(mainImage.width, mainImage.height);
}

// position of image
function imagePosition(width, height) {

  // constant offset image
  var offsetX = 1;
  var offsetY = 1;
  // absolute position
    if(width==0)
    width=227;
    if(height==0)
    height=168;
  var x = (consWidth - width) / 2;
  var y = (consHeight - height) / 2;
  // set CSS properties
  document.getElementById("bigImage").style.position = 'absolute'; // create a wrap with CSS property 'position:relative;'
  document.getElementById("bigImage").style.left = x + offsetX + 'px';
  document.getElementById("bigImage").style.top = y + offsetY + 'px';
}
